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