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