Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / session / media / channel.cc
1 /*
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
30 #include "talk/base/bind.h"
31 #include "talk/base/buffer.h"
32 #include "talk/base/byteorder.h"
33 #include "talk/base/common.h"
34 #include "talk/base/dscp.h"
35 #include "talk/base/logging.h"
36 #include "talk/media/base/constants.h"
37 #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/typingmonitor.h"
42
43
44 namespace cricket {
45
46 using talk_base::Bind;
47
48 enum {
49   MSG_EARLYMEDIATIMEOUT = 1,
50   MSG_SCREENCASTWINDOWEVENT,
51   MSG_RTPPACKET,
52   MSG_RTCPPACKET,
53   MSG_CHANNEL_ERROR,
54   MSG_READYTOSENDDATA,
55   MSG_DATARECEIVED,
56   MSG_FIRSTPACKETRECEIVED,
57 };
58
59 // Value specified in RFC 5764.
60 static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
61
62 static const int kAgcMinus10db = -10;
63
64 static void SetSessionError(BaseSession* session, BaseSession::Error error,
65                             const std::string& error_desc) {
66   session->SetError(error, error_desc);
67 }
68
69 static void SafeSetError(const std::string& message, std::string* error_desc) {
70   if (error_desc) {
71     *error_desc = message;
72   }
73 }
74
75 // TODO(hellner): use the device manager for creation of screen capturers when
76 // the cl enabling it has landed.
77 class NullScreenCapturerFactory : public VideoChannel::ScreenCapturerFactory {
78  public:
79   VideoCapturer* CreateScreenCapturer(const ScreencastId& window) {
80     return NULL;
81   }
82 };
83
84
85 VideoChannel::ScreenCapturerFactory* CreateScreenCapturerFactory() {
86   return new NullScreenCapturerFactory();
87 }
88
89 struct PacketMessageData : public talk_base::MessageData {
90   talk_base::Buffer packet;
91   talk_base::DiffServCodePoint dscp;
92 };
93
94 struct ScreencastEventMessageData : public talk_base::MessageData {
95   ScreencastEventMessageData(uint32 s, talk_base::WindowEvent we)
96       : ssrc(s),
97         event(we) {
98   }
99   uint32 ssrc;
100   talk_base::WindowEvent event;
101 };
102
103 struct VoiceChannelErrorMessageData : public talk_base::MessageData {
104   VoiceChannelErrorMessageData(uint32 in_ssrc,
105                                VoiceMediaChannel::Error in_error)
106       : ssrc(in_ssrc),
107         error(in_error) {
108   }
109   uint32 ssrc;
110   VoiceMediaChannel::Error error;
111 };
112
113 struct VideoChannelErrorMessageData : public talk_base::MessageData {
114   VideoChannelErrorMessageData(uint32 in_ssrc,
115                                VideoMediaChannel::Error in_error)
116       : ssrc(in_ssrc),
117         error(in_error) {
118   }
119   uint32 ssrc;
120   VideoMediaChannel::Error error;
121 };
122
123 struct DataChannelErrorMessageData : public talk_base::MessageData {
124   DataChannelErrorMessageData(uint32 in_ssrc,
125                               DataMediaChannel::Error in_error)
126       : ssrc(in_ssrc),
127         error(in_error) {}
128   uint32 ssrc;
129   DataMediaChannel::Error error;
130 };
131
132
133 struct VideoChannel::ScreencastDetailsData {
134   explicit ScreencastDetailsData(uint32 s)
135       : ssrc(s), fps(0), screencast_max_pixels(0) {
136   }
137   uint32 ssrc;
138   int fps;
139   int screencast_max_pixels;
140 };
141
142 static const char* PacketType(bool rtcp) {
143   return (!rtcp) ? "RTP" : "RTCP";
144 }
145
146 static bool ValidPacket(bool rtcp, const talk_base::Buffer* packet) {
147   // Check the packet size. We could check the header too if needed.
148   return (packet &&
149       packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
150       packet->length() <= kMaxRtpPacketLen);
151 }
152
153 static bool IsReceiveContentDirection(MediaContentDirection direction) {
154   return direction == MD_SENDRECV || direction == MD_RECVONLY;
155 }
156
157 static bool IsSendContentDirection(MediaContentDirection direction) {
158   return direction == MD_SENDRECV || direction == MD_SENDONLY;
159 }
160
161 static const MediaContentDescription* GetContentDescription(
162     const ContentInfo* cinfo) {
163   if (cinfo == NULL)
164     return NULL;
165   return static_cast<const MediaContentDescription*>(cinfo->description);
166 }
167
168 BaseChannel::BaseChannel(talk_base::Thread* thread,
169                          MediaEngineInterface* media_engine,
170                          MediaChannel* media_channel, BaseSession* session,
171                          const std::string& content_name, bool rtcp)
172     : worker_thread_(thread),
173       media_engine_(media_engine),
174       session_(session),
175       media_channel_(media_channel),
176       content_name_(content_name),
177       rtcp_(rtcp),
178       transport_channel_(NULL),
179       rtcp_transport_channel_(NULL),
180       enabled_(false),
181       writable_(false),
182       rtp_ready_to_send_(false),
183       rtcp_ready_to_send_(false),
184       was_ever_writable_(false),
185       local_content_direction_(MD_INACTIVE),
186       remote_content_direction_(MD_INACTIVE),
187       has_received_packet_(false),
188       dtls_keyed_(false),
189       secure_required_(false),
190       rtp_abs_sendtime_extn_id_(-1) {
191   ASSERT(worker_thread_ == talk_base::Thread::Current());
192   LOG(LS_INFO) << "Created channel for " << content_name;
193 }
194
195 BaseChannel::~BaseChannel() {
196   ASSERT(worker_thread_ == talk_base::Thread::Current());
197   Deinit();
198   StopConnectionMonitor();
199   FlushRtcpMessages();  // Send any outstanding RTCP packets.
200   worker_thread_->Clear(this);  // eats any outstanding messages or packets
201   // We must destroy the media channel before the transport channel, otherwise
202   // the media channel may try to send on the dead transport channel. NULLing
203   // is not an effective strategy since the sends will come on another thread.
204   delete media_channel_;
205   set_rtcp_transport_channel(NULL);
206   if (transport_channel_ != NULL)
207     session_->DestroyChannel(content_name_, transport_channel_->component());
208   LOG(LS_INFO) << "Destroyed channel";
209 }
210
211 bool BaseChannel::Init(TransportChannel* transport_channel,
212                        TransportChannel* rtcp_transport_channel) {
213   if (transport_channel == NULL) {
214     return false;
215   }
216   if (rtcp() && rtcp_transport_channel == NULL) {
217     return false;
218   }
219   transport_channel_ = transport_channel;
220
221   if (!SetDtlsSrtpCiphers(transport_channel_, false)) {
222     return false;
223   }
224
225   transport_channel_->SignalWritableState.connect(
226       this, &BaseChannel::OnWritableState);
227   transport_channel_->SignalReadPacket.connect(
228       this, &BaseChannel::OnChannelRead);
229   transport_channel_->SignalReadyToSend.connect(
230       this, &BaseChannel::OnReadyToSend);
231
232   session_->SignalNewLocalDescription.connect(
233       this, &BaseChannel::OnNewLocalDescription);
234   session_->SignalNewRemoteDescription.connect(
235       this, &BaseChannel::OnNewRemoteDescription);
236
237   set_rtcp_transport_channel(rtcp_transport_channel);
238   // Both RTP and RTCP channels are set, we can call SetInterface on
239   // media channel and it can set network options.
240   media_channel_->SetInterface(this);
241   return true;
242 }
243
244 void BaseChannel::Deinit() {
245   media_channel_->SetInterface(NULL);
246 }
247
248 bool BaseChannel::Enable(bool enable) {
249   worker_thread_->Invoke<void>(Bind(
250       enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
251       this));
252   return true;
253 }
254
255 bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
256   return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
257 }
258
259 bool BaseChannel::IsStreamMuted(uint32 ssrc) {
260   return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
261 }
262
263 bool BaseChannel::AddRecvStream(const StreamParams& sp) {
264   return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
265 }
266
267 bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
268   return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
269 }
270
271 bool BaseChannel::AddSendStream(const StreamParams& sp) {
272   return InvokeOnWorker(
273       Bind(&MediaChannel::AddSendStream, media_channel(), sp));
274 }
275
276 bool BaseChannel::RemoveSendStream(uint32 ssrc) {
277   return InvokeOnWorker(
278       Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
279 }
280
281 bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
282                                   ContentAction action,
283                                   std::string* error_desc) {
284   return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
285                              this, content, action, error_desc));
286 }
287
288 bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
289                                    ContentAction action,
290                                    std::string* error_desc) {
291   return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
292                              this, content, action, error_desc));
293 }
294
295 void BaseChannel::StartConnectionMonitor(int cms) {
296   socket_monitor_.reset(new SocketMonitor(transport_channel_,
297                                           worker_thread(),
298                                           talk_base::Thread::Current()));
299   socket_monitor_->SignalUpdate.connect(
300       this, &BaseChannel::OnConnectionMonitorUpdate);
301   socket_monitor_->Start(cms);
302 }
303
304 void BaseChannel::StopConnectionMonitor() {
305   if (socket_monitor_) {
306     socket_monitor_->Stop();
307     socket_monitor_.reset();
308   }
309 }
310
311 void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
312   if (rtcp_transport_channel_ != channel) {
313     if (rtcp_transport_channel_) {
314       session_->DestroyChannel(
315           content_name_, rtcp_transport_channel_->component());
316     }
317     rtcp_transport_channel_ = channel;
318     if (rtcp_transport_channel_) {
319       // TODO(juberti): Propagate this error code
320       VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
321       rtcp_transport_channel_->SignalWritableState.connect(
322           this, &BaseChannel::OnWritableState);
323       rtcp_transport_channel_->SignalReadPacket.connect(
324           this, &BaseChannel::OnChannelRead);
325       rtcp_transport_channel_->SignalReadyToSend.connect(
326           this, &BaseChannel::OnReadyToSend);
327     }
328   }
329 }
330
331 bool BaseChannel::IsReadyToReceive() const {
332   // Receive data if we are enabled and have local content,
333   return enabled() && IsReceiveContentDirection(local_content_direction_);
334 }
335
336 bool BaseChannel::IsReadyToSend() const {
337   // Send outgoing data if we are enabled, have local and remote content,
338   // and we have had some form of connectivity.
339   return enabled() &&
340          IsReceiveContentDirection(remote_content_direction_) &&
341          IsSendContentDirection(local_content_direction_) &&
342          was_ever_writable();
343 }
344
345 bool BaseChannel::SendPacket(talk_base::Buffer* packet,
346                              talk_base::DiffServCodePoint dscp) {
347   return SendPacket(false, packet, dscp);
348 }
349
350 bool BaseChannel::SendRtcp(talk_base::Buffer* packet,
351                            talk_base::DiffServCodePoint dscp) {
352   return SendPacket(true, packet, dscp);
353 }
354
355 int BaseChannel::SetOption(SocketType type, talk_base::Socket::Option opt,
356                            int value) {
357   TransportChannel* channel = NULL;
358   switch (type) {
359     case ST_RTP:
360       channel = transport_channel_;
361       break;
362     case ST_RTCP:
363       channel = rtcp_transport_channel_;
364       break;
365   }
366   return channel ? channel->SetOption(opt, value) : -1;
367 }
368
369 void BaseChannel::OnWritableState(TransportChannel* channel) {
370   ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
371   if (transport_channel_->writable()
372       && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
373     ChannelWritable_w();
374   } else {
375     ChannelNotWritable_w();
376   }
377 }
378
379 void BaseChannel::OnChannelRead(TransportChannel* channel,
380                                 const char* data, size_t len,
381                                 const talk_base::PacketTime& packet_time,
382                                 int flags) {
383   // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
384   ASSERT(worker_thread_ == talk_base::Thread::Current());
385
386   // When using RTCP multiplexing we might get RTCP packets on the RTP
387   // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
388   bool rtcp = PacketIsRtcp(channel, data, len);
389   talk_base::Buffer packet(data, len);
390   HandlePacket(rtcp, &packet, packet_time);
391 }
392
393 void BaseChannel::OnReadyToSend(TransportChannel* channel) {
394   SetReadyToSend(channel, true);
395 }
396
397 void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
398   ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
399   if (channel == transport_channel_) {
400     rtp_ready_to_send_ = ready;
401   }
402   if (channel == rtcp_transport_channel_) {
403     rtcp_ready_to_send_ = ready;
404   }
405
406   if (!ready) {
407     // Notify the MediaChannel when either rtp or rtcp channel can't send.
408     media_channel_->OnReadyToSend(false);
409   } else if (rtp_ready_to_send_ &&
410              // In the case of rtcp mux |rtcp_transport_channel_| will be null.
411              (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
412     // Notify the MediaChannel when both rtp and rtcp channel can send.
413     media_channel_->OnReadyToSend(true);
414   }
415 }
416
417 bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
418                                const char* data, size_t len) {
419   return (channel == rtcp_transport_channel_ ||
420           rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
421 }
422
423 bool BaseChannel::SendPacket(bool rtcp, talk_base::Buffer* packet,
424                              talk_base::DiffServCodePoint dscp) {
425   // SendPacket gets called from MediaEngine, typically on an encoder thread.
426   // If the thread is not our worker thread, we will post to our worker
427   // so that the real work happens on our worker. This avoids us having to
428   // synchronize access to all the pieces of the send path, including
429   // SRTP and the inner workings of the transport channels.
430   // The only downside is that we can't return a proper failure code if
431   // needed. Since UDP is unreliable anyway, this should be a non-issue.
432   if (talk_base::Thread::Current() != worker_thread_) {
433     // Avoid a copy by transferring the ownership of the packet data.
434     int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
435     PacketMessageData* data = new PacketMessageData;
436     packet->TransferTo(&data->packet);
437     data->dscp = dscp;
438     worker_thread_->Post(this, message_id, data);
439     return true;
440   }
441
442   // Now that we are on the correct thread, ensure we have a place to send this
443   // packet before doing anything. (We might get RTCP packets that we don't
444   // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
445   // transport.
446   TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
447       transport_channel_ : rtcp_transport_channel_;
448   if (!channel || !channel->writable()) {
449     return false;
450   }
451
452   // Protect ourselves against crazy data.
453   if (!ValidPacket(rtcp, packet)) {
454     LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
455                   << PacketType(rtcp) << " packet: wrong size="
456                   << packet->length();
457     return false;
458   }
459
460   // Signal to the media sink before protecting the packet.
461   {
462     talk_base::CritScope cs(&signal_send_packet_cs_);
463     SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
464   }
465
466   talk_base::PacketOptions options(dscp);
467   // Protect if needed.
468   if (srtp_filter_.IsActive()) {
469     bool res;
470     char* data = packet->data();
471     int len = static_cast<int>(packet->length());
472     if (!rtcp) {
473     // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
474     // inside libsrtp for a RTP packet. A external HMAC module will be writing
475     // a fake HMAC value. This is ONLY done for a RTP packet.
476     // Socket layer will update rtp sendtime extension header if present in
477     // packet with current time before updating the HMAC.
478 #if !defined(ENABLE_EXTERNAL_AUTH)
479       res = srtp_filter_.ProtectRtp(
480           data, len, static_cast<int>(packet->capacity()), &len);
481 #else
482       options.packet_time_params.rtp_sendtime_extension_id =
483           rtp_abs_sendtime_extn_id_;
484       res = srtp_filter_.ProtectRtp(
485           data, len, static_cast<int>(packet->capacity()), &len,
486           &options.packet_time_params.srtp_packet_index);
487       // If protection succeeds, let's get auth params from srtp.
488       if (res) {
489         uint8* auth_key = NULL;
490         int key_len;
491         res = srtp_filter_.GetRtpAuthParams(
492             &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
493         if (res) {
494           options.packet_time_params.srtp_auth_key.resize(key_len);
495           options.packet_time_params.srtp_auth_key.assign(auth_key,
496                                                           auth_key + key_len);
497         }
498       }
499 #endif
500       if (!res) {
501         int seq_num = -1;
502         uint32 ssrc = 0;
503         GetRtpSeqNum(data, len, &seq_num);
504         GetRtpSsrc(data, len, &ssrc);
505         LOG(LS_ERROR) << "Failed to protect " << content_name_
506                       << " RTP packet: size=" << len
507                       << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
508         return false;
509       }
510     } else {
511       res = srtp_filter_.ProtectRtcp(data, len,
512                                      static_cast<int>(packet->capacity()),
513                                      &len);
514       if (!res) {
515         int type = -1;
516         GetRtcpType(data, len, &type);
517         LOG(LS_ERROR) << "Failed to protect " << content_name_
518                       << " RTCP packet: size=" << len << ", type=" << type;
519         return false;
520       }
521     }
522
523     // Update the length of the packet now that we've added the auth tag.
524     packet->SetLength(len);
525   } else if (secure_required_) {
526     // This is a double check for something that supposedly can't happen.
527     LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
528                   << " packet when SRTP is inactive and crypto is required";
529
530     ASSERT(false);
531     return false;
532   }
533
534   // Signal to the media sink after protecting the packet.
535   {
536     talk_base::CritScope cs(&signal_send_packet_cs_);
537     SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
538   }
539
540   // Bon voyage.
541   int ret = channel->SendPacket(packet->data(), packet->length(), options,
542       (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
543   if (ret != static_cast<int>(packet->length())) {
544     if (channel->GetError() == EWOULDBLOCK) {
545       LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
546       SetReadyToSend(channel, false);
547     }
548     return false;
549   }
550   return true;
551 }
552
553 bool BaseChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
554   // Protect ourselves against crazy data.
555   if (!ValidPacket(rtcp, packet)) {
556     LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
557                   << PacketType(rtcp) << " packet: wrong size="
558                   << packet->length();
559     return false;
560   }
561
562   // Bundle filter handles both rtp and rtcp packets.
563   return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
564 }
565
566 void BaseChannel::HandlePacket(bool rtcp, talk_base::Buffer* packet,
567                                const talk_base::PacketTime& packet_time) {
568   if (!WantsPacket(rtcp, packet)) {
569     return;
570   }
571
572   if (!has_received_packet_) {
573     has_received_packet_ = true;
574     signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
575   }
576
577   // Signal to the media sink before unprotecting the packet.
578   {
579     talk_base::CritScope cs(&signal_recv_packet_cs_);
580     SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
581   }
582
583   // Unprotect the packet, if needed.
584   if (srtp_filter_.IsActive()) {
585     char* data = packet->data();
586     int len = static_cast<int>(packet->length());
587     bool res;
588     if (!rtcp) {
589       res = srtp_filter_.UnprotectRtp(data, len, &len);
590       if (!res) {
591         int seq_num = -1;
592         uint32 ssrc = 0;
593         GetRtpSeqNum(data, len, &seq_num);
594         GetRtpSsrc(data, len, &ssrc);
595         LOG(LS_ERROR) << "Failed to unprotect " << content_name_
596                       << " RTP packet: size=" << len
597                       << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
598         return;
599       }
600     } else {
601       res = srtp_filter_.UnprotectRtcp(data, len, &len);
602       if (!res) {
603         int type = -1;
604         GetRtcpType(data, len, &type);
605         LOG(LS_ERROR) << "Failed to unprotect " << content_name_
606                       << " RTCP packet: size=" << len << ", type=" << type;
607         return;
608       }
609     }
610
611     packet->SetLength(len);
612   } else if (secure_required_) {
613     // Our session description indicates that SRTP is required, but we got a
614     // packet before our SRTP filter is active. This means either that
615     // a) we got SRTP packets before we received the SDES keys, in which case
616     //    we can't decrypt it anyway, or
617     // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
618     //    channels, so we haven't yet extracted keys, even if DTLS did complete
619     //    on the channel that the packets are being sent on. It's really good
620     //    practice to wait for both RTP and RTCP to be good to go before sending
621     //    media, to prevent weird failure modes, so it's fine for us to just eat
622     //    packets here. This is all sidestepped if RTCP mux is used anyway.
623     LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
624                     << " packet when SRTP is inactive and crypto is required";
625     return;
626   }
627
628   // Signal to the media sink after unprotecting the packet.
629   {
630     talk_base::CritScope cs(&signal_recv_packet_cs_);
631     SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
632   }
633
634   // Push it down to the media channel.
635   if (!rtcp) {
636     media_channel_->OnPacketReceived(packet, packet_time);
637   } else {
638     media_channel_->OnRtcpReceived(packet, packet_time);
639   }
640 }
641
642 void BaseChannel::OnNewLocalDescription(
643     BaseSession* session, ContentAction action) {
644   const ContentInfo* content_info =
645       GetFirstContent(session->local_description());
646   const MediaContentDescription* content_desc =
647       GetContentDescription(content_info);
648   std::string error_desc;
649   if (content_desc && content_info && !content_info->rejected &&
650       !SetLocalContent(content_desc, action, &error_desc)) {
651     SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
652     LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
653   }
654 }
655
656 void BaseChannel::OnNewRemoteDescription(
657     BaseSession* session, ContentAction action) {
658   const ContentInfo* content_info =
659       GetFirstContent(session->remote_description());
660   const MediaContentDescription* content_desc =
661       GetContentDescription(content_info);
662   std::string error_desc;
663   if (content_desc && content_info && !content_info->rejected &&
664       !SetRemoteContent(content_desc, action, &error_desc)) {
665     SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
666     LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
667   }
668 }
669
670 void BaseChannel::EnableMedia_w() {
671   ASSERT(worker_thread_ == talk_base::Thread::Current());
672   if (enabled_)
673     return;
674
675   LOG(LS_INFO) << "Channel enabled";
676   enabled_ = true;
677   ChangeState();
678 }
679
680 void BaseChannel::DisableMedia_w() {
681   ASSERT(worker_thread_ == talk_base::Thread::Current());
682   if (!enabled_)
683     return;
684
685   LOG(LS_INFO) << "Channel disabled";
686   enabled_ = false;
687   ChangeState();
688 }
689
690 bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
691   ASSERT(worker_thread_ == talk_base::Thread::Current());
692   bool ret = media_channel()->MuteStream(ssrc, mute);
693   if (ret) {
694     if (mute)
695       muted_streams_.insert(ssrc);
696     else
697       muted_streams_.erase(ssrc);
698   }
699   return ret;
700 }
701
702 bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
703   ASSERT(worker_thread_ == talk_base::Thread::Current());
704   return muted_streams_.find(ssrc) != muted_streams_.end();
705 }
706
707 void BaseChannel::ChannelWritable_w() {
708   ASSERT(worker_thread_ == talk_base::Thread::Current());
709   if (writable_)
710     return;
711
712   LOG(LS_INFO) << "Channel socket writable ("
713                << transport_channel_->content_name() << ", "
714                << transport_channel_->component() << ")"
715                << (was_ever_writable_ ? "" : " for the first time");
716
717   std::vector<ConnectionInfo> infos;
718   transport_channel_->GetStats(&infos);
719   for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
720        it != infos.end(); ++it) {
721     if (it->best_connection) {
722       LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
723                    << "->" << it->remote_candidate.ToSensitiveString();
724       break;
725     }
726   }
727
728   // If we're doing DTLS-SRTP, now is the time.
729   if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
730     if (!SetupDtlsSrtp(false)) {
731       const std::string error_desc =
732           "Couldn't set up DTLS-SRTP on RTP channel.";
733       // Sent synchronously.
734       signaling_thread()->Invoke<void>(Bind(
735           &SetSessionError,
736           session_,
737           BaseSession::ERROR_TRANSPORT,
738           error_desc));
739       return;
740     }
741
742     if (rtcp_transport_channel_) {
743       if (!SetupDtlsSrtp(true)) {
744         const std::string error_desc =
745             "Couldn't set up DTLS-SRTP on RTCP channel";
746         // Sent synchronously.
747         signaling_thread()->Invoke<void>(Bind(
748             &SetSessionError,
749             session_,
750             BaseSession::ERROR_TRANSPORT,
751             error_desc));
752         return;
753       }
754     }
755   }
756
757   was_ever_writable_ = true;
758   writable_ = true;
759   ChangeState();
760 }
761
762 bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
763   std::vector<std::string> ciphers;
764   // We always use the default SRTP ciphers for RTCP, but we may use different
765   // ciphers for RTP depending on the media type.
766   if (!rtcp) {
767     GetSrtpCiphers(&ciphers);
768   } else {
769     GetSupportedDefaultCryptoSuites(&ciphers);
770   }
771   return tc->SetSrtpCiphers(ciphers);
772 }
773
774 bool BaseChannel::ShouldSetupDtlsSrtp() const {
775   return true;
776 }
777
778 // This function returns true if either DTLS-SRTP is not in use
779 // *or* DTLS-SRTP is successfully set up.
780 bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
781   bool ret = false;
782
783   TransportChannel *channel = rtcp_channel ?
784       rtcp_transport_channel_ : transport_channel_;
785
786   // No DTLS
787   if (!channel->IsDtlsActive())
788     return true;
789
790   std::string selected_cipher;
791
792   if (!channel->GetSrtpCipher(&selected_cipher)) {
793     LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
794     return false;
795   }
796
797   LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
798                << content_name() << " "
799                << PacketType(rtcp_channel);
800
801   // OK, we're now doing DTLS (RFC 5764)
802   std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
803                                          SRTP_MASTER_KEY_SALT_LEN * 2);
804
805   // RFC 5705 exporter using the RFC 5764 parameters
806   if (!channel->ExportKeyingMaterial(
807           kDtlsSrtpExporterLabel,
808           NULL, 0, false,
809           &dtls_buffer[0], dtls_buffer.size())) {
810     LOG(LS_WARNING) << "DTLS-SRTP key export failed";
811     ASSERT(false);  // This should never happen
812     return false;
813   }
814
815   // Sync up the keys with the DTLS-SRTP interface
816   std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
817     SRTP_MASTER_KEY_SALT_LEN);
818   std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
819     SRTP_MASTER_KEY_SALT_LEN);
820   size_t offset = 0;
821   memcpy(&client_write_key[0], &dtls_buffer[offset],
822     SRTP_MASTER_KEY_KEY_LEN);
823   offset += SRTP_MASTER_KEY_KEY_LEN;
824   memcpy(&server_write_key[0], &dtls_buffer[offset],
825     SRTP_MASTER_KEY_KEY_LEN);
826   offset += SRTP_MASTER_KEY_KEY_LEN;
827   memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
828     &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
829   offset += SRTP_MASTER_KEY_SALT_LEN;
830   memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
831     &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
832
833   std::vector<unsigned char> *send_key, *recv_key;
834   talk_base::SSLRole role;
835   if (!channel->GetSslRole(&role)) {
836     LOG(LS_WARNING) << "GetSslRole failed";
837     return false;
838   }
839
840   if (role == talk_base::SSL_SERVER) {
841     send_key = &server_write_key;
842     recv_key = &client_write_key;
843   } else {
844     send_key = &client_write_key;
845     recv_key = &server_write_key;
846   }
847
848   if (rtcp_channel) {
849     ret = srtp_filter_.SetRtcpParams(
850         selected_cipher,
851         &(*send_key)[0],
852         static_cast<int>(send_key->size()),
853         selected_cipher,
854         &(*recv_key)[0],
855         static_cast<int>(recv_key->size()));
856   } else {
857     ret = srtp_filter_.SetRtpParams(
858         selected_cipher,
859         &(*send_key)[0],
860         static_cast<int>(send_key->size()),
861         selected_cipher,
862         &(*recv_key)[0],
863         static_cast<int>(recv_key->size()));
864   }
865
866   if (!ret)
867     LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
868   else
869     dtls_keyed_ = true;
870
871   return ret;
872 }
873
874 void BaseChannel::ChannelNotWritable_w() {
875   ASSERT(worker_thread_ == talk_base::Thread::Current());
876   if (!writable_)
877     return;
878
879   LOG(LS_INFO) << "Channel socket not writable ("
880                << transport_channel_->content_name() << ", "
881                << transport_channel_->component() << ")";
882   writable_ = false;
883   ChangeState();
884 }
885
886 // |dtls| will be set to true if DTLS is active for transport channel and
887 // crypto is empty.
888 bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
889                                   bool* dtls,
890                                   std::string* error_desc) {
891   *dtls = transport_channel_->IsDtlsActive();
892   if (*dtls && !cryptos.empty()) {
893     SafeSetError("Cryptos must be empty when DTLS is active.",
894                  error_desc);
895     return false;
896   }
897   return true;
898 }
899
900 bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
901                             ContentAction action,
902                             ContentSource src,
903                             std::string* error_desc) {
904   if (action == CA_UPDATE) {
905     // no crypto params.
906     return true;
907   }
908   bool ret = false;
909   bool dtls = false;
910   ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
911   if (!ret) {
912     return false;
913   }
914   switch (action) {
915     case CA_OFFER:
916       // If DTLS is already active on the channel, we could be renegotiating
917       // here. We don't update the srtp filter.
918       if (!dtls) {
919         ret = srtp_filter_.SetOffer(cryptos, src);
920       }
921       break;
922     case CA_PRANSWER:
923       // If we're doing DTLS-SRTP, we don't want to update the filter
924       // with an answer, because we already have SRTP parameters.
925       if (!dtls) {
926         ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
927       }
928       break;
929     case CA_ANSWER:
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.
932       if (!dtls) {
933         ret = srtp_filter_.SetAnswer(cryptos, src);
934       }
935       break;
936     default:
937       break;
938   }
939   if (!ret) {
940     SafeSetError("Failed to setup SRTP filter.", error_desc);
941     return false;
942   }
943   return true;
944 }
945
946 bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
947                                ContentSource src,
948                                std::string* error_desc) {
949   bool ret = false;
950   switch (action) {
951     case CA_OFFER:
952       ret = rtcp_mux_filter_.SetOffer(enable, src);
953       break;
954     case CA_PRANSWER:
955       ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
956       break;
957     case CA_ANSWER:
958       ret = rtcp_mux_filter_.SetAnswer(enable, src);
959       if (ret && rtcp_mux_filter_.IsActive()) {
960         // We activated RTCP mux, close down the RTCP transport.
961         set_rtcp_transport_channel(NULL);
962       }
963       break;
964     case CA_UPDATE:
965       // No RTCP mux info.
966       ret = true;
967     default:
968       break;
969   }
970   if (!ret) {
971     SafeSetError("Failed to setup RTCP mux filter.", error_desc);
972     return false;
973   }
974   // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
975   // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
976   // received a final answer.
977   if (rtcp_mux_filter_.IsActive()) {
978     // If the RTP transport is already writable, then so are we.
979     if (transport_channel_->writable()) {
980       ChannelWritable_w();
981     }
982   }
983
984   return true;
985 }
986
987 bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
988   ASSERT(worker_thread() == talk_base::Thread::Current());
989   if (!media_channel()->AddRecvStream(sp))
990     return false;
991
992   return bundle_filter_.AddStream(sp);
993 }
994
995 bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
996   ASSERT(worker_thread() == talk_base::Thread::Current());
997   bundle_filter_.RemoveStream(ssrc);
998   return media_channel()->RemoveRecvStream(ssrc);
999 }
1000
1001 bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
1002                                        ContentAction action,
1003                                        std::string* error_desc) {
1004   if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1005               action == CA_PRANSWER || action == CA_UPDATE))
1006     return false;
1007
1008   // If this is an update, streams only contain streams that have changed.
1009   if (action == CA_UPDATE) {
1010     for (StreamParamsVec::const_iterator it = streams.begin();
1011          it != streams.end(); ++it) {
1012       StreamParams existing_stream;
1013       bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
1014                                          it->id, &existing_stream);
1015       if (!stream_exist && it->has_ssrcs()) {
1016         if (media_channel()->AddSendStream(*it)) {
1017           local_streams_.push_back(*it);
1018           LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1019         } else {
1020           std::ostringstream desc;
1021           desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1022           SafeSetError(desc.str(), error_desc);
1023           return false;
1024         }
1025       } else if (stream_exist && !it->has_ssrcs()) {
1026         if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
1027           std::ostringstream desc;
1028           desc << "Failed to remove send stream with ssrc "
1029                << it->first_ssrc() << ".";
1030           SafeSetError(desc.str(), error_desc);
1031           return false;
1032         }
1033         RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1034       } else {
1035         LOG(LS_WARNING) << "Ignore unsupported stream update";
1036       }
1037     }
1038     return true;
1039   }
1040   // Else streams are all the streams we want to send.
1041
1042   // Check for streams that have been removed.
1043   bool ret = true;
1044   for (StreamParamsVec::const_iterator it = local_streams_.begin();
1045        it != local_streams_.end(); ++it) {
1046     if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1047       if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
1048         std::ostringstream desc;
1049         desc << "Failed to remove send stream with ssrc "
1050              << it->first_ssrc() << ".";
1051         SafeSetError(desc.str(), error_desc);
1052         ret = false;
1053       }
1054     }
1055   }
1056   // Check for new streams.
1057   for (StreamParamsVec::const_iterator it = streams.begin();
1058        it != streams.end(); ++it) {
1059     if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1060       if (media_channel()->AddSendStream(*it)) {
1061         LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1062       } else {
1063         std::ostringstream desc;
1064         desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1065         SafeSetError(desc.str(), error_desc);
1066         ret = false;
1067       }
1068     }
1069   }
1070   local_streams_ = streams;
1071   return ret;
1072 }
1073
1074 bool BaseChannel::UpdateRemoteStreams_w(
1075     const std::vector<StreamParams>& streams,
1076     ContentAction action,
1077     std::string* error_desc) {
1078   if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1079               action == CA_PRANSWER || action == CA_UPDATE))
1080     return false;
1081
1082   // If this is an update, streams only contain streams that have changed.
1083   if (action == CA_UPDATE) {
1084     for (StreamParamsVec::const_iterator it = streams.begin();
1085          it != streams.end(); ++it) {
1086       StreamParams existing_stream;
1087       bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1088                                           it->id, &existing_stream);
1089       if (!stream_exists && it->has_ssrcs()) {
1090         if (AddRecvStream_w(*it)) {
1091           remote_streams_.push_back(*it);
1092           LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1093         } else {
1094           std::ostringstream desc;
1095           desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1096           SafeSetError(desc.str(), error_desc);
1097           return false;
1098         }
1099       } else if (stream_exists && !it->has_ssrcs()) {
1100         if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
1101           std::ostringstream desc;
1102           desc << "Failed to remove remote stream with ssrc "
1103                << it->first_ssrc() << ".";
1104           SafeSetError(desc.str(), error_desc);
1105           return false;
1106         }
1107         RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1108       } else {
1109         LOG(LS_WARNING) << "Ignore unsupported stream update."
1110                         << " Stream exists? " << stream_exists
1111                         << " existing stream = " << existing_stream.ToString()
1112                         << " new stream = " << it->ToString();
1113       }
1114     }
1115     return true;
1116   }
1117   // Else streams are all the streams we want to receive.
1118
1119   // Check for streams that have been removed.
1120   bool ret = true;
1121   for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1122        it != remote_streams_.end(); ++it) {
1123     if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1124       if (!RemoveRecvStream_w(it->first_ssrc())) {
1125         std::ostringstream desc;
1126         desc << "Failed to remove remote stream with ssrc "
1127              << it->first_ssrc() << ".";
1128         SafeSetError(desc.str(), error_desc);
1129         ret = false;
1130       }
1131     }
1132   }
1133   // Check for new streams.
1134   for (StreamParamsVec::const_iterator it = streams.begin();
1135       it != streams.end(); ++it) {
1136     if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1137       if (AddRecvStream_w(*it)) {
1138         LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1139       } else {
1140         std::ostringstream desc;
1141         desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1142         SafeSetError(desc.str(), error_desc);
1143         ret = false;
1144       }
1145     }
1146   }
1147   remote_streams_ = streams;
1148   return ret;
1149 }
1150
1151 bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
1152                                         ContentAction action,
1153                                         std::string* error_desc) {
1154   // Cache secure_required_ for belt and suspenders check on SendPacket
1155   secure_required_ = content->crypto_required() != CT_NONE;
1156   bool ret = UpdateLocalStreams_w(content->streams(), action, error_desc);
1157   // Set local SRTP parameters (what we will encrypt with).
1158   ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
1159   // Set local RTCP mux parameters.
1160   ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
1161   // Set local RTP header extensions.
1162   if (content->rtp_header_extensions_set()) {
1163     if (!media_channel()->SetRecvRtpHeaderExtensions(
1164             content->rtp_header_extensions())) {
1165       std::ostringstream desc;
1166       desc << "Failed to set receive rtp header extensions for "
1167            << MediaTypeToString(content->type()) << " content.";
1168       SafeSetError(desc.str(), error_desc);
1169       ret = false;
1170     }
1171   }
1172   set_local_content_direction(content->direction());
1173   return ret;
1174 }
1175
1176 bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
1177                                          ContentAction action,
1178                                          std::string* error_desc) {
1179   bool ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
1180   // Set remote SRTP parameters (what the other side will encrypt with).
1181   ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
1182   // Set remote RTCP mux parameters.
1183   ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
1184   // Set remote RTP header extensions.
1185   if (content->rtp_header_extensions_set()) {
1186     if (!media_channel()->SetSendRtpHeaderExtensions(
1187             content->rtp_header_extensions())) {
1188       std::ostringstream desc;
1189       desc << "Failed to set send rtp header extensions for "
1190            << MediaTypeToString(content->type()) << " content.";
1191       SafeSetError(desc.str(), error_desc);
1192       ret = false;
1193     } else {
1194       MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
1195     }
1196   }
1197
1198   if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1199     std::ostringstream desc;
1200     desc << "Failed to set max send bandwidth for "
1201          << MediaTypeToString(content->type()) << " content.";
1202     SafeSetError(desc.str(), error_desc);
1203     ret = false;
1204   }
1205   set_remote_content_direction(content->direction());
1206   return ret;
1207 }
1208
1209 void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1210     const std::vector<RtpHeaderExtension>& extensions) {
1211   const RtpHeaderExtension* send_time_extension =
1212       FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
1213   rtp_abs_sendtime_extn_id_ =
1214       send_time_extension ? send_time_extension->id : -1;
1215 }
1216
1217 void BaseChannel::OnMessage(talk_base::Message *pmsg) {
1218   switch (pmsg->message_id) {
1219     case MSG_RTPPACKET:
1220     case MSG_RTCPPACKET: {
1221       PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
1222       SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
1223       delete data;  // because it is Posted
1224       break;
1225     }
1226     case MSG_FIRSTPACKETRECEIVED: {
1227       SignalFirstPacketReceived(this);
1228       break;
1229     }
1230   }
1231 }
1232
1233 void BaseChannel::FlushRtcpMessages() {
1234   // Flush all remaining RTCP messages. This should only be called in
1235   // destructor.
1236   ASSERT(talk_base::Thread::Current() == worker_thread_);
1237   talk_base::MessageList rtcp_messages;
1238   worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
1239   for (talk_base::MessageList::iterator it = rtcp_messages.begin();
1240        it != rtcp_messages.end(); ++it) {
1241     worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
1242   }
1243 }
1244
1245 VoiceChannel::VoiceChannel(talk_base::Thread* thread,
1246                            MediaEngineInterface* media_engine,
1247                            VoiceMediaChannel* media_channel,
1248                            BaseSession* session,
1249                            const std::string& content_name,
1250                            bool rtcp)
1251     : BaseChannel(thread, media_engine, media_channel, session, content_name,
1252                   rtcp),
1253       received_media_(false) {
1254 }
1255
1256 VoiceChannel::~VoiceChannel() {
1257   StopAudioMonitor();
1258   StopMediaMonitor();
1259   // this can't be done in the base class, since it calls a virtual
1260   DisableMedia_w();
1261   Deinit();
1262 }
1263
1264 bool VoiceChannel::Init() {
1265   TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1266       content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1267   if (!BaseChannel::Init(session()->CreateChannel(
1268           content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1269           rtcp_channel)) {
1270     return false;
1271   }
1272   media_channel()->SignalMediaError.connect(
1273       this, &VoiceChannel::OnVoiceChannelError);
1274   srtp_filter()->SignalSrtpError.connect(
1275       this, &VoiceChannel::OnSrtpError);
1276   return true;
1277 }
1278
1279 bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
1280   return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1281                              media_channel(), ssrc, renderer));
1282 }
1283
1284 bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
1285   return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1286                              media_channel(), ssrc, renderer));
1287 }
1288
1289 bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
1290   return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
1291 }
1292
1293 // TODO(juberti): Handle early media the right way. We should get an explicit
1294 // ringing message telling us to start playing local ringback, which we cancel
1295 // if any early media actually arrives. For now, we do the opposite, which is
1296 // to wait 1 second for early media, and start playing local ringback if none
1297 // arrives.
1298 void VoiceChannel::SetEarlyMedia(bool enable) {
1299   if (enable) {
1300     // Start the early media timeout
1301     worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1302                                 MSG_EARLYMEDIATIMEOUT);
1303   } else {
1304     // Stop the timeout if currently going.
1305     worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
1306   }
1307 }
1308
1309 bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
1310   return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1311                              this, ssrc, play, loop));
1312 }
1313
1314 bool VoiceChannel::PressDTMF(int digit, bool playout) {
1315   int flags = DF_SEND;
1316   if (playout) {
1317     flags |= DF_PLAY;
1318   }
1319   int duration_ms = 160;
1320   return InsertDtmf(0, digit, duration_ms, flags);
1321 }
1322
1323 bool VoiceChannel::CanInsertDtmf() {
1324   return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1325                              media_channel()));
1326 }
1327
1328 bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1329                               int flags) {
1330   return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1331                              ssrc, event_code, duration, flags));
1332 }
1333
1334 bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
1335   return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1336                              media_channel(), ssrc, left, right));
1337 }
1338
1339 bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
1340   return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1341                              media_channel(), stats));
1342 }
1343
1344 void VoiceChannel::StartMediaMonitor(int cms) {
1345   media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
1346       talk_base::Thread::Current()));
1347   media_monitor_->SignalUpdate.connect(
1348       this, &VoiceChannel::OnMediaMonitorUpdate);
1349   media_monitor_->Start(cms);
1350 }
1351
1352 void VoiceChannel::StopMediaMonitor() {
1353   if (media_monitor_) {
1354     media_monitor_->Stop();
1355     media_monitor_->SignalUpdate.disconnect(this);
1356     media_monitor_.reset();
1357   }
1358 }
1359
1360 void VoiceChannel::StartAudioMonitor(int cms) {
1361   audio_monitor_.reset(new AudioMonitor(this, talk_base::Thread::Current()));
1362   audio_monitor_
1363     ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1364   audio_monitor_->Start(cms);
1365 }
1366
1367 void VoiceChannel::StopAudioMonitor() {
1368   if (audio_monitor_) {
1369     audio_monitor_->Stop();
1370     audio_monitor_.reset();
1371   }
1372 }
1373
1374 bool VoiceChannel::IsAudioMonitorRunning() const {
1375   return (audio_monitor_.get() != NULL);
1376 }
1377
1378 void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1379   typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1380   SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1381 }
1382
1383 void VoiceChannel::StopTypingMonitor() {
1384   typing_monitor_.reset();
1385 }
1386
1387 bool VoiceChannel::IsTypingMonitorRunning() const {
1388   return typing_monitor_;
1389 }
1390
1391 bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1392   bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1393   if (typing_monitor_ && mute)
1394     typing_monitor_->OnChannelMuted();
1395   return ret;
1396 }
1397
1398 int VoiceChannel::GetInputLevel_w() {
1399   return media_engine()->GetInputLevel();
1400 }
1401
1402 int VoiceChannel::GetOutputLevel_w() {
1403   return media_channel()->GetOutputLevel();
1404 }
1405
1406 void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1407   media_channel()->GetActiveStreams(actives);
1408 }
1409
1410 void VoiceChannel::OnChannelRead(TransportChannel* channel,
1411                                  const char* data, size_t len,
1412                                  const talk_base::PacketTime& packet_time,
1413                                 int flags) {
1414   BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
1415
1416   // Set a flag when we've received an RTP packet. If we're waiting for early
1417   // media, this will disable the timeout.
1418   if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1419     received_media_ = true;
1420   }
1421 }
1422
1423 void VoiceChannel::ChangeState() {
1424   // Render incoming data if we're the active call, and we have the local
1425   // content. We receive data on the default channel and multiplexed streams.
1426   bool recv = IsReadyToReceive();
1427   if (!media_channel()->SetPlayout(recv)) {
1428     SendLastMediaError();
1429   }
1430
1431   // Send outgoing data if we're the active call, we have the remote content,
1432   // and we have had some form of connectivity.
1433   bool send = IsReadyToSend();
1434   SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1435   if (!media_channel()->SetSend(send_flag)) {
1436     LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1437     SendLastMediaError();
1438   }
1439
1440   LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1441 }
1442
1443 const ContentInfo* VoiceChannel::GetFirstContent(
1444     const SessionDescription* sdesc) {
1445   return GetFirstAudioContent(sdesc);
1446 }
1447
1448 bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
1449                                      ContentAction action,
1450                                      std::string* error_desc) {
1451   ASSERT(worker_thread() == talk_base::Thread::Current());
1452   LOG(LS_INFO) << "Setting local voice description";
1453
1454   const AudioContentDescription* audio =
1455       static_cast<const AudioContentDescription*>(content);
1456   ASSERT(audio != NULL);
1457   if (!audio) {
1458     SafeSetError("Can't find audio content in local description.", error_desc);
1459     return false;
1460   }
1461
1462   bool ret = SetBaseLocalContent_w(content, action, error_desc);
1463   // Set local audio codecs (what we want to receive).
1464   // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1465   // is set properly.
1466   if (action != CA_UPDATE || audio->has_codecs()) {
1467     if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1468       SafeSetError("Failed to set audio receive codecs.", error_desc);
1469       ret = false;
1470     }
1471   }
1472
1473   // If everything worked, see if we can start receiving.
1474   if (ret) {
1475     std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1476     for (; it != audio->codecs().end(); ++it) {
1477       bundle_filter()->AddPayloadType(it->id);
1478     }
1479     ChangeState();
1480   } else {
1481     LOG(LS_WARNING) << "Failed to set local voice description";
1482   }
1483   return ret;
1484 }
1485
1486 bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
1487                                       ContentAction action,
1488                                       std::string* error_desc) {
1489   ASSERT(worker_thread() == talk_base::Thread::Current());
1490   LOG(LS_INFO) << "Setting remote voice description";
1491
1492   const AudioContentDescription* audio =
1493       static_cast<const AudioContentDescription*>(content);
1494   ASSERT(audio != NULL);
1495   if (!audio) {
1496     SafeSetError("Can't find audio content in remote description.", error_desc);
1497     return false;
1498   }
1499
1500   bool ret = true;
1501   // Set remote video codecs (what the other side wants to receive).
1502   if (action != CA_UPDATE || audio->has_codecs()) {
1503     if (!media_channel()->SetSendCodecs(audio->codecs())) {
1504       SafeSetError("Failed to set audio send codecs.", error_desc);
1505       ret = false;
1506     }
1507   }
1508
1509   ret &= SetBaseRemoteContent_w(content, action, error_desc);
1510
1511   if (action != CA_UPDATE) {
1512     // Tweak our audio processing settings, if needed.
1513     AudioOptions audio_options;
1514     if (!media_channel()->GetOptions(&audio_options)) {
1515       LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1516     } else {
1517       if (audio->conference_mode()) {
1518         audio_options.conference_mode.Set(true);
1519       }
1520       if (audio->agc_minus_10db()) {
1521         audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1522       }
1523       if (!media_channel()->SetOptions(audio_options)) {
1524         // Log an error on failure, but don't abort the call.
1525         LOG(LS_ERROR) << "Failed to set voice channel options";
1526       }
1527     }
1528   }
1529
1530   // If everything worked, see if we can start sending.
1531   if (ret) {
1532     ChangeState();
1533   } else {
1534     LOG(LS_WARNING) << "Failed to set remote voice description";
1535   }
1536   return ret;
1537 }
1538
1539 bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
1540   ASSERT(worker_thread() == talk_base::Thread::Current());
1541   return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1542 }
1543
1544 bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
1545   ASSERT(worker_thread() == talk_base::Thread::Current());
1546   if (play) {
1547     LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1548   } else {
1549     LOG(LS_INFO) << "Stopping ringback tone";
1550   }
1551   return media_channel()->PlayRingbackTone(ssrc, play, loop);
1552 }
1553
1554 void VoiceChannel::HandleEarlyMediaTimeout() {
1555   // This occurs on the main thread, not the worker thread.
1556   if (!received_media_) {
1557     LOG(LS_INFO) << "No early media received before timeout";
1558     SignalEarlyMediaTimeout(this);
1559   }
1560 }
1561
1562 bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1563                                 int flags) {
1564   if (!enabled()) {
1565     return false;
1566   }
1567
1568   return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1569 }
1570
1571 bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
1572   return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1573                              media_channel(), options));
1574 }
1575
1576 void VoiceChannel::OnMessage(talk_base::Message *pmsg) {
1577   switch (pmsg->message_id) {
1578     case MSG_EARLYMEDIATIMEOUT:
1579       HandleEarlyMediaTimeout();
1580       break;
1581     case MSG_CHANNEL_ERROR: {
1582       VoiceChannelErrorMessageData* data =
1583           static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1584       SignalMediaError(this, data->ssrc, data->error);
1585       delete data;
1586       break;
1587     }
1588     default:
1589       BaseChannel::OnMessage(pmsg);
1590       break;
1591   }
1592 }
1593
1594 void VoiceChannel::OnConnectionMonitorUpdate(
1595     SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1596   SignalConnectionMonitor(this, infos);
1597 }
1598
1599 void VoiceChannel::OnMediaMonitorUpdate(
1600     VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1601   ASSERT(media_channel == this->media_channel());
1602   SignalMediaMonitor(this, info);
1603 }
1604
1605 void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1606                                         const AudioInfo& info) {
1607   SignalAudioMonitor(this, info);
1608 }
1609
1610 void VoiceChannel::OnVoiceChannelError(
1611     uint32 ssrc, VoiceMediaChannel::Error err) {
1612   VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1613       ssrc, err);
1614   signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1615 }
1616
1617 void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1618                                SrtpFilter::Error error) {
1619   switch (error) {
1620     case SrtpFilter::ERROR_FAIL:
1621       OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1622                           VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1623                           VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1624       break;
1625     case SrtpFilter::ERROR_AUTH:
1626       OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1627                           VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1628                           VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1629       break;
1630     case SrtpFilter::ERROR_REPLAY:
1631       // Only receving channel should have this error.
1632       ASSERT(mode == SrtpFilter::UNPROTECT);
1633       OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1634       break;
1635     default:
1636       break;
1637   }
1638 }
1639
1640 void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1641   GetSupportedAudioCryptoSuites(ciphers);
1642 }
1643
1644 VideoChannel::VideoChannel(talk_base::Thread* thread,
1645                            MediaEngineInterface* media_engine,
1646                            VideoMediaChannel* media_channel,
1647                            BaseSession* session,
1648                            const std::string& content_name,
1649                            bool rtcp,
1650                            VoiceChannel* voice_channel)
1651     : BaseChannel(thread, media_engine, media_channel, session, content_name,
1652                   rtcp),
1653       voice_channel_(voice_channel),
1654       renderer_(NULL),
1655       screencapture_factory_(CreateScreenCapturerFactory()),
1656       previous_we_(talk_base::WE_CLOSE) {
1657 }
1658
1659 bool VideoChannel::Init() {
1660   TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1661       content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1662   if (!BaseChannel::Init(session()->CreateChannel(
1663           content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1664           rtcp_channel)) {
1665     return false;
1666   }
1667   media_channel()->SignalMediaError.connect(
1668       this, &VideoChannel::OnVideoChannelError);
1669   srtp_filter()->SignalSrtpError.connect(
1670       this, &VideoChannel::OnSrtpError);
1671   return true;
1672 }
1673
1674 void VoiceChannel::SendLastMediaError() {
1675   uint32 ssrc;
1676   VoiceMediaChannel::Error error;
1677   media_channel()->GetLastMediaError(&ssrc, &error);
1678   SignalMediaError(this, ssrc, error);
1679 }
1680
1681 VideoChannel::~VideoChannel() {
1682   std::vector<uint32> screencast_ssrcs;
1683   ScreencastMap::iterator iter;
1684   while (!screencast_capturers_.empty()) {
1685     if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1686       LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1687                     << screencast_capturers_.begin()->first;
1688       ASSERT(false);
1689       break;
1690     }
1691   }
1692
1693   StopMediaMonitor();
1694   // this can't be done in the base class, since it calls a virtual
1695   DisableMedia_w();
1696
1697   Deinit();
1698 }
1699
1700 bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
1701   worker_thread()->Invoke<void>(Bind(
1702       &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
1703   return true;
1704 }
1705
1706 bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
1707   return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
1708 }
1709
1710 VideoCapturer* VideoChannel::AddScreencast(
1711     uint32 ssrc, const ScreencastId& id) {
1712   return worker_thread()->Invoke<VideoCapturer*>(Bind(
1713       &VideoChannel::AddScreencast_w, this, ssrc, id));
1714 }
1715
1716 bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1717   return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1718                              media_channel(), ssrc, capturer));
1719 }
1720
1721 bool VideoChannel::RemoveScreencast(uint32 ssrc) {
1722   return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
1723 }
1724
1725 bool VideoChannel::IsScreencasting() {
1726   return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
1727 }
1728
1729 int VideoChannel::GetScreencastFps(uint32 ssrc) {
1730   ScreencastDetailsData data(ssrc);
1731   worker_thread()->Invoke<void>(Bind(
1732       &VideoChannel::GetScreencastDetails_w, this, &data));
1733   return data.fps;
1734 }
1735
1736 int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
1737   ScreencastDetailsData data(ssrc);
1738   worker_thread()->Invoke<void>(Bind(
1739       &VideoChannel::GetScreencastDetails_w, this, &data));
1740   return data.screencast_max_pixels;
1741 }
1742
1743 bool VideoChannel::SendIntraFrame() {
1744   worker_thread()->Invoke<void>(Bind(
1745       &VideoMediaChannel::SendIntraFrame, media_channel()));
1746   return true;
1747 }
1748
1749 bool VideoChannel::RequestIntraFrame() {
1750   worker_thread()->Invoke<void>(Bind(
1751       &VideoMediaChannel::RequestIntraFrame, media_channel()));
1752   return true;
1753 }
1754
1755 void VideoChannel::SetScreenCaptureFactory(
1756     ScreenCapturerFactory* screencapture_factory) {
1757   worker_thread()->Invoke<void>(Bind(
1758       &VideoChannel::SetScreenCaptureFactory_w,
1759       this, screencapture_factory));
1760 }
1761
1762 void VideoChannel::ChangeState() {
1763   // Render incoming data if we're the active call, and we have the local
1764   // content. We receive data on the default channel and multiplexed streams.
1765   bool recv = IsReadyToReceive();
1766   if (!media_channel()->SetRender(recv)) {
1767     LOG(LS_ERROR) << "Failed to SetRender on video channel";
1768     // TODO(gangji): Report error back to server.
1769   }
1770
1771   // Send outgoing data if we're the active call, we have the remote content,
1772   // and we have had some form of connectivity.
1773   bool send = IsReadyToSend();
1774   if (!media_channel()->SetSend(send)) {
1775     LOG(LS_ERROR) << "Failed to SetSend on video channel";
1776     // TODO(gangji): Report error back to server.
1777   }
1778
1779   LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1780 }
1781
1782 bool VideoChannel::GetStats(
1783     const StatsOptions& options, VideoMediaInfo* stats) {
1784   return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
1785                              media_channel(), options, stats));
1786 }
1787
1788 void VideoChannel::StartMediaMonitor(int cms) {
1789   media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
1790       talk_base::Thread::Current()));
1791   media_monitor_->SignalUpdate.connect(
1792       this, &VideoChannel::OnMediaMonitorUpdate);
1793   media_monitor_->Start(cms);
1794 }
1795
1796 void VideoChannel::StopMediaMonitor() {
1797   if (media_monitor_) {
1798     media_monitor_->Stop();
1799     media_monitor_.reset();
1800   }
1801 }
1802
1803 const ContentInfo* VideoChannel::GetFirstContent(
1804     const SessionDescription* sdesc) {
1805   return GetFirstVideoContent(sdesc);
1806 }
1807
1808 bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
1809                                      ContentAction action,
1810                                      std::string* error_desc) {
1811   ASSERT(worker_thread() == talk_base::Thread::Current());
1812   LOG(LS_INFO) << "Setting local video description";
1813
1814   const VideoContentDescription* video =
1815       static_cast<const VideoContentDescription*>(content);
1816   ASSERT(video != NULL);
1817   if (!video) {
1818     SafeSetError("Can't find video content in local description.", error_desc);
1819     return false;
1820   }
1821
1822   bool ret = SetBaseLocalContent_w(content, action, error_desc);
1823   // Set local video codecs (what we want to receive).
1824   if (action != CA_UPDATE || video->has_codecs()) {
1825     if (!media_channel()->SetRecvCodecs(video->codecs())) {
1826       SafeSetError("Failed to set video receive codecs.", error_desc);
1827       ret = false;
1828     }
1829   }
1830
1831   if (action != CA_UPDATE) {
1832     VideoOptions video_options;
1833     media_channel()->GetOptions(&video_options);
1834     video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1835
1836     if (!media_channel()->SetOptions(video_options)) {
1837       // Log an error on failure, but don't abort the call.
1838       LOG(LS_ERROR) << "Failed to set video channel options";
1839     }
1840   }
1841
1842   // If everything worked, see if we can start receiving.
1843   if (ret) {
1844     std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1845     for (; it != video->codecs().end(); ++it) {
1846       bundle_filter()->AddPayloadType(it->id);
1847     }
1848     ChangeState();
1849   } else {
1850     LOG(LS_WARNING) << "Failed to set local video description";
1851   }
1852   return ret;
1853 }
1854
1855 bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
1856                                       ContentAction action,
1857                                       std::string* error_desc) {
1858   ASSERT(worker_thread() == talk_base::Thread::Current());
1859   LOG(LS_INFO) << "Setting remote video description";
1860
1861   const VideoContentDescription* video =
1862       static_cast<const VideoContentDescription*>(content);
1863   ASSERT(video != NULL);
1864   if (!video) {
1865     SafeSetError("Can't find video content in remote description.", error_desc);
1866     return false;
1867   }
1868
1869   bool ret = true;
1870   // Set remote video codecs (what the other side wants to receive).
1871   if (action != CA_UPDATE || video->has_codecs()) {
1872     if (!media_channel()->SetSendCodecs(video->codecs())) {
1873       SafeSetError("Failed to set video send codecs.", error_desc);
1874       ret = false;
1875     }
1876   }
1877
1878   ret &= SetBaseRemoteContent_w(content, action, error_desc);
1879
1880   if (action != CA_UPDATE) {
1881     // Tweak our video processing settings, if needed.
1882     VideoOptions video_options;
1883     media_channel()->GetOptions(&video_options);
1884     if (video->conference_mode()) {
1885       video_options.conference_mode.Set(true);
1886     }
1887     video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1888
1889     if (!media_channel()->SetOptions(video_options)) {
1890       // Log an error on failure, but don't abort the call.
1891       LOG(LS_ERROR) << "Failed to set video channel options";
1892     }
1893   }
1894
1895   // If everything worked, see if we can start sending.
1896   if (ret) {
1897     ChangeState();
1898   } else {
1899     LOG(LS_WARNING) << "Failed to set remote video description";
1900   }
1901   return ret;
1902 }
1903
1904 bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1905   bool ret = true;
1906   // Set the send format for each of the local streams. If the view request
1907   // does not contain a local stream, set its send format to 0x0, which will
1908   // drop all frames.
1909   for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1910       it != local_streams().end(); ++it) {
1911     VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1912     StaticVideoViews::const_iterator view;
1913     for (view = request.static_video_views.begin();
1914          view != request.static_video_views.end(); ++view) {
1915       if (view->selector.Matches(*it)) {
1916         format.width = view->width;
1917         format.height = view->height;
1918         format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1919         break;
1920       }
1921     }
1922
1923     ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1924   }
1925
1926   // Check if the view request has invalid streams.
1927   for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1928       it != request.static_video_views.end(); ++it) {
1929     if (!GetStream(local_streams(), it->selector, NULL)) {
1930       LOG(LS_WARNING) << "View request for ("
1931                       << it->selector.ssrc << ", '"
1932                       << it->selector.groupid << "', '"
1933                       << it->selector.streamid << "'"
1934                       << ") is not in the local streams.";
1935     }
1936   }
1937
1938   return ret;
1939 }
1940
1941 VideoCapturer* VideoChannel::AddScreencast_w(
1942     uint32 ssrc, const ScreencastId& id) {
1943   if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
1944     return NULL;
1945   }
1946   VideoCapturer* screen_capturer =
1947       screencapture_factory_->CreateScreenCapturer(id);
1948   if (!screen_capturer) {
1949     return NULL;
1950   }
1951   screen_capturer->SignalStateChange.connect(this,
1952                                              &VideoChannel::OnStateChange);
1953   screencast_capturers_[ssrc] = screen_capturer;
1954   return screen_capturer;
1955 }
1956
1957 bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1958   ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1959   if (iter  == screencast_capturers_.end()) {
1960     return false;
1961   }
1962   // Clean up VideoCapturer.
1963   delete iter->second;
1964   screencast_capturers_.erase(iter);
1965   return true;
1966 }
1967
1968 bool VideoChannel::IsScreencasting_w() const {
1969   return !screencast_capturers_.empty();
1970 }
1971
1972 void VideoChannel::GetScreencastDetails_w(
1973     ScreencastDetailsData* data) const {
1974   ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
1975   if (iter == screencast_capturers_.end()) {
1976     return;
1977   }
1978   VideoCapturer* capturer = iter->second;
1979   const VideoFormat* video_format = capturer->GetCaptureFormat();
1980   data->fps = VideoFormat::IntervalToFps(video_format->interval);
1981   data->screencast_max_pixels = capturer->screencast_max_pixels();
1982 }
1983
1984 void VideoChannel::SetScreenCaptureFactory_w(
1985     ScreenCapturerFactory* screencapture_factory) {
1986   if (screencapture_factory == NULL) {
1987     screencapture_factory_.reset(CreateScreenCapturerFactory());
1988   } else {
1989     screencapture_factory_.reset(screencapture_factory);
1990   }
1991 }
1992
1993 void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
1994                                              talk_base::WindowEvent we) {
1995   ASSERT(signaling_thread() == talk_base::Thread::Current());
1996   SignalScreencastWindowEvent(ssrc, we);
1997 }
1998
1999 bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
2000   return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2001                              media_channel(), options));
2002 }
2003
2004 void VideoChannel::OnMessage(talk_base::Message *pmsg) {
2005   switch (pmsg->message_id) {
2006     case MSG_SCREENCASTWINDOWEVENT: {
2007       const ScreencastEventMessageData* data =
2008           static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2009       OnScreencastWindowEvent_s(data->ssrc, data->event);
2010       delete data;
2011       break;
2012     }
2013     case MSG_CHANNEL_ERROR: {
2014       const VideoChannelErrorMessageData* data =
2015           static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2016       SignalMediaError(this, data->ssrc, data->error);
2017       delete data;
2018       break;
2019     }
2020     default:
2021       BaseChannel::OnMessage(pmsg);
2022       break;
2023   }
2024 }
2025
2026 void VideoChannel::OnConnectionMonitorUpdate(
2027     SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2028   SignalConnectionMonitor(this, infos);
2029 }
2030
2031 // TODO(pthatcher): Look into removing duplicate code between
2032 // audio, video, and data, perhaps by using templates.
2033 void VideoChannel::OnMediaMonitorUpdate(
2034     VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2035   ASSERT(media_channel == this->media_channel());
2036   SignalMediaMonitor(this, info);
2037 }
2038
2039 void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
2040                                            talk_base::WindowEvent event) {
2041   ScreencastEventMessageData* pdata =
2042       new ScreencastEventMessageData(ssrc, event);
2043   signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2044 }
2045
2046 void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2047   // Map capturer events to window events. In the future we may want to simply
2048   // pass these events up directly.
2049   talk_base::WindowEvent we;
2050   if (ev == CS_STOPPED) {
2051     we = talk_base::WE_CLOSE;
2052   } else if (ev == CS_PAUSED) {
2053     we = talk_base::WE_MINIMIZE;
2054   } else if (ev == CS_RUNNING && previous_we_ == talk_base::WE_MINIMIZE) {
2055     we = talk_base::WE_RESTORE;
2056   } else {
2057     return;
2058   }
2059   previous_we_ = we;
2060
2061   uint32 ssrc = 0;
2062   if (!GetLocalSsrc(capturer, &ssrc)) {
2063     return;
2064   }
2065
2066   OnScreencastWindowEvent(ssrc, we);
2067 }
2068
2069 bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2070   *ssrc = 0;
2071   for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2072        iter != screencast_capturers_.end(); ++iter) {
2073     if (iter->second == capturer) {
2074       *ssrc = iter->first;
2075       return true;
2076     }
2077   }
2078   return false;
2079 }
2080
2081 void VideoChannel::OnVideoChannelError(uint32 ssrc,
2082                                        VideoMediaChannel::Error error) {
2083   VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2084       ssrc, error);
2085   signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2086 }
2087
2088 void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2089                                SrtpFilter::Error error) {
2090   switch (error) {
2091     case SrtpFilter::ERROR_FAIL:
2092       OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2093                           VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2094                           VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2095       break;
2096     case SrtpFilter::ERROR_AUTH:
2097       OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2098                           VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2099                           VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2100       break;
2101     case SrtpFilter::ERROR_REPLAY:
2102       // Only receving channel should have this error.
2103       ASSERT(mode == SrtpFilter::UNPROTECT);
2104       // TODO(gangji): Turn on the signaling of replay error once we have
2105       // switched to the new mechanism for doing video retransmissions.
2106       // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2107       break;
2108     default:
2109       break;
2110   }
2111 }
2112
2113
2114 void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2115   GetSupportedVideoCryptoSuites(ciphers);
2116 }
2117
2118 DataChannel::DataChannel(talk_base::Thread* thread,
2119                          DataMediaChannel* media_channel,
2120                          BaseSession* session,
2121                          const std::string& content_name,
2122                          bool rtcp)
2123     // MediaEngine is NULL
2124     : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
2125       data_channel_type_(cricket::DCT_NONE),
2126       ready_to_send_data_(false) {
2127 }
2128
2129 DataChannel::~DataChannel() {
2130   StopMediaMonitor();
2131   // this can't be done in the base class, since it calls a virtual
2132   DisableMedia_w();
2133
2134   Deinit();
2135 }
2136
2137 bool DataChannel::Init() {
2138   TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2139       content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2140   if (!BaseChannel::Init(session()->CreateChannel(
2141           content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2142           rtcp_channel)) {
2143     return false;
2144   }
2145   media_channel()->SignalDataReceived.connect(
2146       this, &DataChannel::OnDataReceived);
2147   media_channel()->SignalMediaError.connect(
2148       this, &DataChannel::OnDataChannelError);
2149   media_channel()->SignalReadyToSend.connect(
2150       this, &DataChannel::OnDataChannelReadyToSend);
2151   srtp_filter()->SignalSrtpError.connect(
2152       this, &DataChannel::OnSrtpError);
2153   return true;
2154 }
2155
2156 bool DataChannel::SendData(const SendDataParams& params,
2157                            const talk_base::Buffer& payload,
2158                            SendDataResult* result) {
2159   return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2160                              media_channel(), params, payload, result));
2161 }
2162
2163 const ContentInfo* DataChannel::GetFirstContent(
2164     const SessionDescription* sdesc) {
2165   return GetFirstDataContent(sdesc);
2166 }
2167
2168
2169 static bool IsRtpPacket(const talk_base::Buffer* packet) {
2170   int version;
2171   if (!GetRtpVersion(packet->data(), packet->length(), &version)) {
2172     return false;
2173   }
2174
2175   return version == 2;
2176 }
2177
2178 bool DataChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
2179   if (data_channel_type_ == DCT_SCTP) {
2180     // TODO(pthatcher): Do this in a more robust way by checking for
2181     // SCTP or DTLS.
2182     return !IsRtpPacket(packet);
2183   } else if (data_channel_type_ == DCT_RTP) {
2184     return BaseChannel::WantsPacket(rtcp, packet);
2185   }
2186   return false;
2187 }
2188
2189 bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2190                                      std::string* error_desc) {
2191   // It hasn't been set before, so set it now.
2192   if (data_channel_type_ == DCT_NONE) {
2193     data_channel_type_ = new_data_channel_type;
2194     return true;
2195   }
2196
2197   // It's been set before, but doesn't match.  That's bad.
2198   if (data_channel_type_ != new_data_channel_type) {
2199     std::ostringstream desc;
2200     desc << "Data channel type mismatch."
2201          << " Expected " << data_channel_type_
2202          << " Got " << new_data_channel_type;
2203     SafeSetError(desc.str(), error_desc);
2204     return false;
2205   }
2206
2207   // It's hasn't changed.  Nothing to do.
2208   return true;
2209 }
2210
2211 bool DataChannel::SetDataChannelTypeFromContent(
2212     const DataContentDescription* content,
2213     std::string* error_desc) {
2214   bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2215                   (content->protocol() == kMediaProtocolDtlsSctp));
2216   DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
2217   return SetDataChannelType(data_channel_type, error_desc);
2218 }
2219
2220 bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
2221                                     ContentAction action,
2222                                     std::string* error_desc) {
2223   ASSERT(worker_thread() == talk_base::Thread::Current());
2224   LOG(LS_INFO) << "Setting local data description";
2225
2226   const DataContentDescription* data =
2227       static_cast<const DataContentDescription*>(content);
2228   ASSERT(data != NULL);
2229   if (!data) {
2230     SafeSetError("Can't find data content in local description.", error_desc);
2231     return false;
2232   }
2233
2234   bool ret = false;
2235   if (!SetDataChannelTypeFromContent(data, error_desc)) {
2236     return false;
2237   }
2238
2239   if (data_channel_type_ == DCT_SCTP) {
2240     // SCTP data channels don't need the rest of the stuff.
2241     ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
2242     if (ret) {
2243       set_local_content_direction(content->direction());
2244       // As in SetRemoteContent_w, make sure we set the local SCTP port
2245       // number as specified in our DataContentDescription.
2246       if (!media_channel()->SetRecvCodecs(data->codecs())) {
2247         SafeSetError("Failed to set data receive codecs.", error_desc);
2248         ret = false;
2249       }
2250     }
2251   } else {
2252     ret = SetBaseLocalContent_w(content, action, error_desc);
2253     if (action != CA_UPDATE || data->has_codecs()) {
2254       if (!media_channel()->SetRecvCodecs(data->codecs())) {
2255         SafeSetError("Failed to set data receive codecs.", error_desc);
2256         ret = false;
2257       }
2258     }
2259   }
2260
2261   // If everything worked, see if we can start receiving.
2262   if (ret) {
2263     std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2264     for (; it != data->codecs().end(); ++it) {
2265       bundle_filter()->AddPayloadType(it->id);
2266     }
2267     ChangeState();
2268   } else {
2269     LOG(LS_WARNING) << "Failed to set local data description";
2270   }
2271   return ret;
2272 }
2273
2274 bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2275                                      ContentAction action,
2276                                      std::string* error_desc) {
2277   ASSERT(worker_thread() == talk_base::Thread::Current());
2278
2279   const DataContentDescription* data =
2280       static_cast<const DataContentDescription*>(content);
2281   ASSERT(data != NULL);
2282   if (!data) {
2283     SafeSetError("Can't find data content in remote description.", error_desc);
2284     return false;
2285   }
2286
2287   bool ret = true;
2288   if (!SetDataChannelTypeFromContent(data, error_desc)) {
2289     return false;
2290   }
2291
2292   if (data_channel_type_ == DCT_SCTP) {
2293     LOG(LS_INFO) << "Setting SCTP remote data description";
2294     // SCTP data channels don't need the rest of the stuff.
2295     ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
2296     if (ret) {
2297       set_remote_content_direction(content->direction());
2298       // We send the SCTP port number (not to be confused with the underlying
2299       // UDP port number) as a codec parameter.  Make sure it gets there.
2300       if (!media_channel()->SetSendCodecs(data->codecs())) {
2301         SafeSetError("Failed to set data send codecs.", error_desc);
2302         ret = false;
2303       }
2304     }
2305   } else {
2306     // If the remote data doesn't have codecs and isn't an update, it
2307     // must be empty, so ignore it.
2308     if (action != CA_UPDATE && !data->has_codecs()) {
2309       return true;
2310     }
2311     LOG(LS_INFO) << "Setting remote data description";
2312
2313     // Set remote video codecs (what the other side wants to receive).
2314     if (action != CA_UPDATE || data->has_codecs()) {
2315       if (!media_channel()->SetSendCodecs(data->codecs())) {
2316         SafeSetError("Failed to set data send codecs.", error_desc);
2317         ret = false;
2318       }
2319     }
2320
2321     if (ret) {
2322       ret &= SetBaseRemoteContent_w(content, action, error_desc);
2323     }
2324
2325     if (action != CA_UPDATE) {
2326       int bandwidth_bps = data->bandwidth();
2327       if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2328         std::ostringstream desc;
2329         desc << "Failed to set max send bandwidth for data content.";
2330         SafeSetError(desc.str(), error_desc);
2331         ret = false;
2332       }
2333     }
2334   }
2335
2336   // If everything worked, see if we can start sending.
2337   if (ret) {
2338     ChangeState();
2339   } else {
2340     LOG(LS_WARNING) << "Failed to set remote data description";
2341   }
2342   return ret;
2343 }
2344
2345 void DataChannel::ChangeState() {
2346   // Render incoming data if we're the active call, and we have the local
2347   // content. We receive data on the default channel and multiplexed streams.
2348   bool recv = IsReadyToReceive();
2349   if (!media_channel()->SetReceive(recv)) {
2350     LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2351   }
2352
2353   // Send outgoing data if we're the active call, we have the remote content,
2354   // and we have had some form of connectivity.
2355   bool send = IsReadyToSend();
2356   if (!media_channel()->SetSend(send)) {
2357     LOG(LS_ERROR) << "Failed to SetSend on data channel";
2358   }
2359
2360   // Trigger SignalReadyToSendData asynchronously.
2361   OnDataChannelReadyToSend(send);
2362
2363   LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2364 }
2365
2366 void DataChannel::OnMessage(talk_base::Message *pmsg) {
2367   switch (pmsg->message_id) {
2368     case MSG_READYTOSENDDATA: {
2369       DataChannelReadyToSendMessageData* data =
2370           static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
2371       ready_to_send_data_ = data->data();
2372       SignalReadyToSendData(ready_to_send_data_);
2373       delete data;
2374       break;
2375     }
2376     case MSG_DATARECEIVED: {
2377       DataReceivedMessageData* data =
2378           static_cast<DataReceivedMessageData*>(pmsg->pdata);
2379       SignalDataReceived(this, data->params, data->payload);
2380       delete data;
2381       break;
2382     }
2383     case MSG_CHANNEL_ERROR: {
2384       const DataChannelErrorMessageData* data =
2385           static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2386       SignalMediaError(this, data->ssrc, data->error);
2387       delete data;
2388       break;
2389     }
2390     default:
2391       BaseChannel::OnMessage(pmsg);
2392       break;
2393   }
2394 }
2395
2396 void DataChannel::OnConnectionMonitorUpdate(
2397     SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2398   SignalConnectionMonitor(this, infos);
2399 }
2400
2401 void DataChannel::StartMediaMonitor(int cms) {
2402   media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
2403       talk_base::Thread::Current()));
2404   media_monitor_->SignalUpdate.connect(
2405       this, &DataChannel::OnMediaMonitorUpdate);
2406   media_monitor_->Start(cms);
2407 }
2408
2409 void DataChannel::StopMediaMonitor() {
2410   if (media_monitor_) {
2411     media_monitor_->Stop();
2412     media_monitor_->SignalUpdate.disconnect(this);
2413     media_monitor_.reset();
2414   }
2415 }
2416
2417 void DataChannel::OnMediaMonitorUpdate(
2418     DataMediaChannel* media_channel, const DataMediaInfo& info) {
2419   ASSERT(media_channel == this->media_channel());
2420   SignalMediaMonitor(this, info);
2421 }
2422
2423 void DataChannel::OnDataReceived(
2424     const ReceiveDataParams& params, const char* data, size_t len) {
2425   DataReceivedMessageData* msg = new DataReceivedMessageData(
2426       params, data, len);
2427   signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2428 }
2429
2430 void DataChannel::OnDataChannelError(
2431     uint32 ssrc, DataMediaChannel::Error err) {
2432   DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2433       ssrc, err);
2434   signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2435 }
2436
2437 void DataChannel::OnDataChannelReadyToSend(bool writable) {
2438   // This is usded for congestion control to indicate that the stream is ready
2439   // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2440   // that the transport channel is ready.
2441   signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2442                            new DataChannelReadyToSendMessageData(writable));
2443 }
2444
2445 void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2446                               SrtpFilter::Error error) {
2447   switch (error) {
2448     case SrtpFilter::ERROR_FAIL:
2449       OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2450                          DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2451                          DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2452       break;
2453     case SrtpFilter::ERROR_AUTH:
2454       OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2455                          DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2456                          DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2457       break;
2458     case SrtpFilter::ERROR_REPLAY:
2459       // Only receving channel should have this error.
2460       ASSERT(mode == SrtpFilter::UNPROTECT);
2461       OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2462       break;
2463     default:
2464       break;
2465   }
2466 }
2467
2468 void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2469   GetSupportedDataCryptoSuites(ciphers);
2470 }
2471
2472 bool DataChannel::ShouldSetupDtlsSrtp() const {
2473   return (data_channel_type_ == DCT_RTP);
2474 }
2475
2476 }  // namespace cricket