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 #ifndef TALK_SESSION_MEDIA_CHANNEL_H_
     29 #define TALK_SESSION_MEDIA_CHANNEL_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #include "talk/base/asyncudpsocket.h"
     35 #include "talk/base/criticalsection.h"
     36 #include "talk/base/network.h"
     37 #include "talk/base/sigslot.h"
     38 #include "talk/base/window.h"
     39 #include "talk/media/base/mediachannel.h"
     40 #include "talk/media/base/mediaengine.h"
     41 #include "talk/media/base/screencastid.h"
     42 #include "talk/media/base/streamparams.h"
     43 #include "talk/media/base/videocapturer.h"
     44 #include "talk/p2p/base/session.h"
     45 #include "talk/p2p/client/socketmonitor.h"
     46 #include "talk/session/media/audiomonitor.h"
     47 #include "talk/session/media/bundlefilter.h"
     48 #include "talk/session/media/mediamonitor.h"
     49 #include "talk/session/media/mediasession.h"
     50 #include "talk/session/media/rtcpmuxfilter.h"
     51 #include "talk/session/media/srtpfilter.h"
     52 
     53 namespace cricket {
     54 
     55 struct CryptoParams;
     56 class MediaContentDescription;
     57 struct TypingMonitorOptions;
     58 class TypingMonitor;
     59 struct ViewRequest;
     60 
     61 enum SinkType {
     62   SINK_PRE_CRYPTO,  // Sink packets before encryption or after decryption.
     63   SINK_POST_CRYPTO  // Sink packets after encryption or before decryption.
     64 };
     65 
     66 // BaseChannel contains logic common to voice and video, including
     67 // enable/mute, marshaling calls to a worker thread, and
     68 // connection and media monitors.
     69 //
     70 // WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
     71 // This is required to avoid a data race between the destructor modifying the
     72 // vtable, and the media channel's thread using BaseChannel as the
     73 // NetworkInterface.
     74 
     75 class BaseChannel
     76     : public talk_base::MessageHandler, public sigslot::has_slots<>,
     77       public MediaChannel::NetworkInterface {
     78  public:
     79   BaseChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
     80               MediaChannel* channel, BaseSession* session,
     81               const std::string& content_name, bool rtcp);
     82   virtual ~BaseChannel();
     83   bool Init(TransportChannel* transport_channel,
     84             TransportChannel* rtcp_transport_channel);
     85   // Deinit may be called multiple times and is simply ignored if it's alreay
     86   // done.
     87   void Deinit();
     88 
     89   talk_base::Thread* worker_thread() const { return worker_thread_; }
     90   BaseSession* session() const { return session_; }
     91   const std::string& content_name() { return content_name_; }
     92   TransportChannel* transport_channel() const {
     93     return transport_channel_;
     94   }
     95   TransportChannel* rtcp_transport_channel() const {
     96     return rtcp_transport_channel_;
     97   }
     98   bool enabled() const { return enabled_; }
     99 
    100   // This function returns true if we are using SRTP.
    101   bool secure() const { return srtp_filter_.IsActive(); }
    102   // The following function returns true if we are using
    103   // DTLS-based keying. If you turned off SRTP later, however
    104   // you could have secure() == false and dtls_secure() == true.
    105   bool secure_dtls() const { return dtls_keyed_; }
    106   // This function returns true if we require secure channel for call setup.
    107   bool secure_required() const { return secure_required_; }
    108 
    109   bool writable() const { return writable_; }
    110   bool IsStreamMuted(uint32 ssrc);
    111 
    112   // Channel control
    113   bool SetLocalContent(const MediaContentDescription* content,
    114                        ContentAction action,
    115                        std::string* error_desc);
    116   bool SetRemoteContent(const MediaContentDescription* content,
    117                         ContentAction action,
    118                         std::string* error_desc);
    119 
    120   bool Enable(bool enable);
    121   // Mute sending media on the stream with SSRC |ssrc|
    122   // If there is only one sending stream SSRC 0 can be used.
    123   bool MuteStream(uint32 ssrc, bool mute);
    124 
    125   // Multiplexing
    126   bool AddRecvStream(const StreamParams& sp);
    127   bool RemoveRecvStream(uint32 ssrc);
    128   bool AddSendStream(const StreamParams& sp);
    129   bool RemoveSendStream(uint32 ssrc);
    130 
    131   // Monitoring
    132   void StartConnectionMonitor(int cms);
    133   void StopConnectionMonitor();
    134 
    135   void set_srtp_signal_silent_time(uint32 silent_time) {
    136     srtp_filter_.set_signal_silent_time(silent_time);
    137   }
    138 
    139   void set_content_name(const std::string& content_name) {
    140     ASSERT(signaling_thread()->IsCurrent());
    141     ASSERT(!writable_);
    142     if (session_->state() != BaseSession::STATE_INIT) {
    143       LOG(LS_ERROR) << "Content name for a channel can be changed only "
    144                     << "when BaseSession is in STATE_INIT state.";
    145       return;
    146     }
    147     content_name_ = content_name;
    148   }
    149 
    150   template <class T>
    151   void RegisterSendSink(T* sink,
    152                         void (T::*OnPacket)(const void*, size_t, bool),
    153                         SinkType type) {
    154     talk_base::CritScope cs(&signal_send_packet_cs_);
    155     if (SINK_POST_CRYPTO == type) {
    156       SignalSendPacketPostCrypto.disconnect(sink);
    157       SignalSendPacketPostCrypto.connect(sink, OnPacket);
    158     } else {
    159       SignalSendPacketPreCrypto.disconnect(sink);
    160       SignalSendPacketPreCrypto.connect(sink, OnPacket);
    161     }
    162   }
    163 
    164   void UnregisterSendSink(sigslot::has_slots<>* sink,
    165                           SinkType type) {
    166     talk_base::CritScope cs(&signal_send_packet_cs_);
    167     if (SINK_POST_CRYPTO == type) {
    168       SignalSendPacketPostCrypto.disconnect(sink);
    169     } else {
    170       SignalSendPacketPreCrypto.disconnect(sink);
    171     }
    172   }
    173 
    174   bool HasSendSinks(SinkType type) {
    175     talk_base::CritScope cs(&signal_send_packet_cs_);
    176     if (SINK_POST_CRYPTO == type) {
    177       return !SignalSendPacketPostCrypto.is_empty();
    178     } else {
    179       return !SignalSendPacketPreCrypto.is_empty();
    180     }
    181   }
    182 
    183   template <class T>
    184   void RegisterRecvSink(T* sink,
    185                         void (T::*OnPacket)(const void*, size_t, bool),
    186                         SinkType type) {
    187     talk_base::CritScope cs(&signal_recv_packet_cs_);
    188     if (SINK_POST_CRYPTO == type) {
    189       SignalRecvPacketPostCrypto.disconnect(sink);
    190       SignalRecvPacketPostCrypto.connect(sink, OnPacket);
    191     } else {
    192       SignalRecvPacketPreCrypto.disconnect(sink);
    193       SignalRecvPacketPreCrypto.connect(sink, OnPacket);
    194     }
    195   }
    196 
    197   void UnregisterRecvSink(sigslot::has_slots<>* sink,
    198                           SinkType type) {
    199     talk_base::CritScope cs(&signal_recv_packet_cs_);
    200     if (SINK_POST_CRYPTO == type) {
    201       SignalRecvPacketPostCrypto.disconnect(sink);
    202     } else {
    203       SignalRecvPacketPreCrypto.disconnect(sink);
    204     }
    205   }
    206 
    207   bool HasRecvSinks(SinkType type) {
    208     talk_base::CritScope cs(&signal_recv_packet_cs_);
    209     if (SINK_POST_CRYPTO == type) {
    210       return !SignalRecvPacketPostCrypto.is_empty();
    211     } else {
    212       return !SignalRecvPacketPreCrypto.is_empty();
    213     }
    214   }
    215 
    216   BundleFilter* bundle_filter() { return &bundle_filter_; }
    217 
    218   const std::vector<StreamParams>& local_streams() const {
    219     return local_streams_;
    220   }
    221   const std::vector<StreamParams>& remote_streams() const {
    222     return remote_streams_;
    223   }
    224 
    225   // Used for latency measurements.
    226   sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
    227 
    228   // Used to alert UI when the muted status changes, perhaps autonomously.
    229   sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
    230 
    231   // Made public for easier testing.
    232   void SetReadyToSend(TransportChannel* channel, bool ready);
    233 
    234  protected:
    235   MediaEngineInterface* media_engine() const { return media_engine_; }
    236   virtual MediaChannel* media_channel() const { return media_channel_; }
    237   void set_rtcp_transport_channel(TransportChannel* transport);
    238   bool was_ever_writable() const { return was_ever_writable_; }
    239   void set_local_content_direction(MediaContentDirection direction) {
    240     local_content_direction_ = direction;
    241   }
    242   void set_remote_content_direction(MediaContentDirection direction) {
    243     remote_content_direction_ = direction;
    244   }
    245   bool IsReadyToReceive() const;
    246   bool IsReadyToSend() const;
    247   talk_base::Thread* signaling_thread() { return session_->signaling_thread(); }
    248   SrtpFilter* srtp_filter() { return &srtp_filter_; }
    249   bool rtcp() const { return rtcp_; }
    250 
    251   void FlushRtcpMessages();
    252 
    253   // NetworkInterface implementation, called by MediaEngine
    254   virtual bool SendPacket(talk_base::Buffer* packet,
    255                           talk_base::DiffServCodePoint dscp);
    256   virtual bool SendRtcp(talk_base::Buffer* packet,
    257                         talk_base::DiffServCodePoint dscp);
    258   virtual int SetOption(SocketType type, talk_base::Socket::Option o, int val);
    259 
    260   // From TransportChannel
    261   void OnWritableState(TransportChannel* channel);
    262   virtual void OnChannelRead(TransportChannel* channel,
    263                              const char* data,
    264                              size_t len,
    265                              const talk_base::PacketTime& packet_time,
    266                              int flags);
    267   void OnReadyToSend(TransportChannel* channel);
    268 
    269   bool PacketIsRtcp(const TransportChannel* channel, const char* data,
    270                     size_t len);
    271   bool SendPacket(bool rtcp, talk_base::Buffer* packet,
    272                   talk_base::DiffServCodePoint dscp);
    273   virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
    274   void HandlePacket(bool rtcp, talk_base::Buffer* packet,
    275                     const talk_base::PacketTime& packet_time);
    276 
    277   // Apply the new local/remote session description.
    278   void OnNewLocalDescription(BaseSession* session, ContentAction action);
    279   void OnNewRemoteDescription(BaseSession* session, ContentAction action);
    280 
    281   void EnableMedia_w();
    282   void DisableMedia_w();
    283   virtual bool MuteStream_w(uint32 ssrc, bool mute);
    284   bool IsStreamMuted_w(uint32 ssrc);
    285   void ChannelWritable_w();
    286   void ChannelNotWritable_w();
    287   bool AddRecvStream_w(const StreamParams& sp);
    288   bool RemoveRecvStream_w(uint32 ssrc);
    289   bool AddSendStream_w(const StreamParams& sp);
    290   bool RemoveSendStream_w(uint32 ssrc);
    291   virtual bool ShouldSetupDtlsSrtp() const;
    292   // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
    293   // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
    294   bool SetupDtlsSrtp(bool rtcp_channel);
    295   // Set the DTLS-SRTP cipher policy on this channel as appropriate.
    296   bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
    297 
    298   virtual void ChangeState() = 0;
    299 
    300   // Gets the content info appropriate to the channel (audio or video).
    301   virtual const ContentInfo* GetFirstContent(
    302       const SessionDescription* sdesc) = 0;
    303   bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
    304                             ContentAction action,
    305                             std::string* error_desc);
    306   bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
    307                              ContentAction action,
    308                              std::string* error_desc);
    309   bool SetBaseLocalContent_w(const MediaContentDescription* content,
    310                              ContentAction action,
    311                              std::string* error_desc);
    312   virtual bool SetLocalContent_w(const MediaContentDescription* content,
    313                                  ContentAction action,
    314                                  std::string* error_desc) = 0;
    315   bool SetBaseRemoteContent_w(const MediaContentDescription* content,
    316                               ContentAction action,
    317                               std::string* error_desc);
    318   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
    319                                   ContentAction action,
    320                                   std::string* error_desc) = 0;
    321 
    322   // Helper method to get RTP Absoulute SendTime extension header id if
    323   // present in remote supported extensions list.
    324   void MaybeCacheRtpAbsSendTimeHeaderExtension(
    325     const std::vector<RtpHeaderExtension>& extensions);
    326 
    327   bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
    328                        bool* dtls,
    329                        std::string* error_desc);
    330   bool SetSrtp_w(const std::vector<CryptoParams>& params,
    331                  ContentAction action,
    332                  ContentSource src,
    333                  std::string* error_desc);
    334   bool SetRtcpMux_w(bool enable,
    335                     ContentAction action,
    336                     ContentSource src,
    337                     std::string* error_desc);
    338 
    339   // From MessageHandler
    340   virtual void OnMessage(talk_base::Message* pmsg);
    341 
    342   // Handled in derived classes
    343   // Get the SRTP ciphers to use for RTP media
    344   virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
    345   virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
    346       const std::vector<ConnectionInfo>& infos) = 0;
    347 
    348   // Helper function for invoking bool-returning methods on the worker thread.
    349   template <class FunctorT>
    350   bool InvokeOnWorker(const FunctorT& functor) {
    351     return worker_thread_->Invoke<bool>(functor);
    352   }
    353 
    354  private:
    355   sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
    356   sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
    357   sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
    358   sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
    359   talk_base::CriticalSection signal_send_packet_cs_;
    360   talk_base::CriticalSection signal_recv_packet_cs_;
    361 
    362   talk_base::Thread* worker_thread_;
    363   MediaEngineInterface* media_engine_;
    364   BaseSession* session_;
    365   MediaChannel* media_channel_;
    366   std::vector<StreamParams> local_streams_;
    367   std::vector<StreamParams> remote_streams_;
    368 
    369   std::string content_name_;
    370   bool rtcp_;
    371   TransportChannel* transport_channel_;
    372   TransportChannel* rtcp_transport_channel_;
    373   SrtpFilter srtp_filter_;
    374   RtcpMuxFilter rtcp_mux_filter_;
    375   BundleFilter bundle_filter_;
    376   talk_base::scoped_ptr<SocketMonitor> socket_monitor_;
    377   bool enabled_;
    378   bool writable_;
    379   bool rtp_ready_to_send_;
    380   bool rtcp_ready_to_send_;
    381   bool was_ever_writable_;
    382   MediaContentDirection local_content_direction_;
    383   MediaContentDirection remote_content_direction_;
    384   std::set<uint32> muted_streams_;
    385   bool has_received_packet_;
    386   bool dtls_keyed_;
    387   bool secure_required_;
    388   int rtp_abs_sendtime_extn_id_;
    389 };
    390 
    391 // VoiceChannel is a specialization that adds support for early media, DTMF,
    392 // and input/output level monitoring.
    393 class VoiceChannel : public BaseChannel {
    394  public:
    395   VoiceChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
    396                VoiceMediaChannel* channel, BaseSession* session,
    397                const std::string& content_name, bool rtcp);
    398   ~VoiceChannel();
    399   bool Init();
    400   bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
    401   bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
    402 
    403   // downcasts a MediaChannel
    404   virtual VoiceMediaChannel* media_channel() const {
    405     return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
    406   }
    407 
    408   bool SetRingbackTone(const void* buf, int len);
    409   void SetEarlyMedia(bool enable);
    410   // This signal is emitted when we have gone a period of time without
    411   // receiving early media. When received, a UI should start playing its
    412   // own ringing sound
    413   sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
    414 
    415   bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
    416   // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
    417   bool PressDTMF(int digit, bool playout);
    418   // Returns if the telephone-event has been negotiated.
    419   bool CanInsertDtmf();
    420   // Send and/or play a DTMF |event| according to the |flags|.
    421   // The DTMF out-of-band signal will be used on sending.
    422   // The |ssrc| should be either 0 or a valid send stream ssrc.
    423   // The valid value for the |event| are 0 which corresponding to DTMF
    424   // event 0-9, *, #, A-D.
    425   bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
    426   bool SetOutputScaling(uint32 ssrc, double left, double right);
    427   // Get statistics about the current media session.
    428   bool GetStats(VoiceMediaInfo* stats);
    429 
    430   // Monitoring functions
    431   sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
    432       SignalConnectionMonitor;
    433 
    434   void StartMediaMonitor(int cms);
    435   void StopMediaMonitor();
    436   sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
    437 
    438   void StartAudioMonitor(int cms);
    439   void StopAudioMonitor();
    440   bool IsAudioMonitorRunning() const;
    441   sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
    442 
    443   void StartTypingMonitor(const TypingMonitorOptions& settings);
    444   void StopTypingMonitor();
    445   bool IsTypingMonitorRunning() const;
    446 
    447   // Overrides BaseChannel::MuteStream_w.
    448   virtual bool MuteStream_w(uint32 ssrc, bool mute);
    449 
    450   int GetInputLevel_w();
    451   int GetOutputLevel_w();
    452   void GetActiveStreams_w(AudioInfo::StreamList* actives);
    453 
    454   // Signal errors from VoiceMediaChannel.  Arguments are:
    455   //     ssrc(uint32), and error(VoiceMediaChannel::Error).
    456   sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
    457       SignalMediaError;
    458 
    459   // Configuration and setting.
    460   bool SetChannelOptions(const AudioOptions& options);
    461 
    462  private:
    463   // overrides from BaseChannel
    464   virtual void OnChannelRead(TransportChannel* channel,
    465                              const char* data, size_t len,
    466                              const talk_base::PacketTime& packet_time,
    467                              int flags);
    468   virtual void ChangeState();
    469   virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
    470   virtual bool SetLocalContent_w(const MediaContentDescription* content,
    471                                  ContentAction action,
    472                                  std::string* error_desc);
    473   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
    474                                   ContentAction action,
    475                                   std::string* error_desc);
    476   bool SetRingbackTone_w(const void* buf, int len);
    477   bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
    478   void HandleEarlyMediaTimeout();
    479   bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
    480   bool SetOutputScaling_w(uint32 ssrc, double left, double right);
    481   bool GetStats_w(VoiceMediaInfo* stats);
    482 
    483   virtual void OnMessage(talk_base::Message* pmsg);
    484   virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
    485   virtual void OnConnectionMonitorUpdate(
    486       SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
    487   virtual void OnMediaMonitorUpdate(
    488       VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
    489   void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
    490   void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
    491   void SendLastMediaError();
    492   void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
    493 
    494   static const int kEarlyMediaTimeout = 1000;
    495   bool received_media_;
    496   talk_base::scoped_ptr<VoiceMediaMonitor> media_monitor_;
    497   talk_base::scoped_ptr<AudioMonitor> audio_monitor_;
    498   talk_base::scoped_ptr<TypingMonitor> typing_monitor_;
    499 };
    500 
    501 // VideoChannel is a specialization for video.
    502 class VideoChannel : public BaseChannel {
    503  public:
    504   // Make screen capturer virtual so that it can be overriden in testing.
    505   // E.g. used to test that window events are triggered correctly.
    506   class ScreenCapturerFactory {
    507    public:
    508     virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
    509     virtual ~ScreenCapturerFactory() {}
    510   };
    511 
    512   VideoChannel(talk_base::Thread* thread, MediaEngineInterface* media_engine,
    513                VideoMediaChannel* channel, BaseSession* session,
    514                const std::string& content_name, bool rtcp,
    515                VoiceChannel* voice_channel);
    516   ~VideoChannel();
    517   bool Init();
    518 
    519   bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
    520   bool ApplyViewRequest(const ViewRequest& request);
    521 
    522   // TODO(pthatcher): Refactor to use a "capture id" instead of an
    523   // ssrc here as the "key".
    524   VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
    525   bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
    526   bool RemoveScreencast(uint32 ssrc);
    527   // True if we've added a screencast.  Doesn't matter if the capturer
    528   // has been started or not.
    529   bool IsScreencasting();
    530   int GetScreencastFps(uint32 ssrc);
    531   int GetScreencastMaxPixels(uint32 ssrc);
    532   // Get statistics about the current media session.
    533   bool GetStats(const StatsOptions& options, VideoMediaInfo* stats);
    534 
    535   sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
    536       SignalConnectionMonitor;
    537 
    538   void StartMediaMonitor(int cms);
    539   void StopMediaMonitor();
    540   sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
    541   sigslot::signal2<uint32, talk_base::WindowEvent> SignalScreencastWindowEvent;
    542 
    543   bool SendIntraFrame();
    544   bool RequestIntraFrame();
    545   sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
    546       SignalMediaError;
    547 
    548   void SetScreenCaptureFactory(
    549       ScreenCapturerFactory* screencapture_factory);
    550 
    551   // Configuration and setting.
    552   bool SetChannelOptions(const VideoOptions& options);
    553 
    554  protected:
    555   // downcasts a MediaChannel
    556   virtual VideoMediaChannel* media_channel() const {
    557     return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
    558   }
    559 
    560  private:
    561   typedef std::map<uint32, VideoCapturer*> ScreencastMap;
    562   struct ScreencastDetailsData;
    563 
    564   // overrides from BaseChannel
    565   virtual void ChangeState();
    566   virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
    567   virtual bool SetLocalContent_w(const MediaContentDescription* content,
    568                                  ContentAction action,
    569                                  std::string* error_desc);
    570   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
    571                                   ContentAction action,
    572                                   std::string* error_desc);
    573   bool ApplyViewRequest_w(const ViewRequest& request);
    574 
    575   VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
    576   bool RemoveScreencast_w(uint32 ssrc);
    577   void OnScreencastWindowEvent_s(uint32 ssrc, talk_base::WindowEvent we);
    578   bool IsScreencasting_w() const;
    579   void GetScreencastDetails_w(ScreencastDetailsData* d) const;
    580   void SetScreenCaptureFactory_w(
    581       ScreenCapturerFactory* screencapture_factory);
    582   bool GetStats_w(VideoMediaInfo* stats);
    583 
    584   virtual void OnMessage(talk_base::Message* pmsg);
    585   virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
    586   virtual void OnConnectionMonitorUpdate(
    587       SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
    588   virtual void OnMediaMonitorUpdate(
    589       VideoMediaChannel* media_channel, const VideoMediaInfo& info);
    590   virtual void OnScreencastWindowEvent(uint32 ssrc,
    591                                        talk_base::WindowEvent event);
    592   virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
    593   bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
    594 
    595   void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
    596   void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
    597 
    598   VoiceChannel* voice_channel_;
    599   VideoRenderer* renderer_;
    600   talk_base::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
    601   ScreencastMap screencast_capturers_;
    602   talk_base::scoped_ptr<VideoMediaMonitor> media_monitor_;
    603 
    604   talk_base::WindowEvent previous_we_;
    605 };
    606 
    607 // DataChannel is a specialization for data.
    608 class DataChannel : public BaseChannel {
    609  public:
    610   DataChannel(talk_base::Thread* thread,
    611               DataMediaChannel* media_channel,
    612               BaseSession* session,
    613               const std::string& content_name,
    614               bool rtcp);
    615   ~DataChannel();
    616   bool Init();
    617 
    618   virtual bool SendData(const SendDataParams& params,
    619                         const talk_base::Buffer& payload,
    620                         SendDataResult* result);
    621 
    622   void StartMediaMonitor(int cms);
    623   void StopMediaMonitor();
    624 
    625   // Should be called on the signaling thread only.
    626   bool ready_to_send_data() const {
    627     return ready_to_send_data_;
    628   }
    629 
    630   sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
    631   sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
    632       SignalConnectionMonitor;
    633   sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
    634       SignalMediaError;
    635   sigslot::signal3<DataChannel*,
    636                    const ReceiveDataParams&,
    637                    const talk_base::Buffer&>
    638       SignalDataReceived;
    639   // Signal for notifying when the channel becomes ready to send data.
    640   // That occurs when the channel is enabled, the transport is writable,
    641   // both local and remote descriptions are set, and the channel is unblocked.
    642   sigslot::signal1<bool> SignalReadyToSendData;
    643   // Signal for notifying that the remote side has closed the DataChannel.
    644   sigslot::signal1<uint32> SignalStreamClosedRemotely;
    645 
    646  protected:
    647   // downcasts a MediaChannel.
    648   virtual DataMediaChannel* media_channel() const {
    649     return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
    650   }
    651 
    652  private:
    653   struct SendDataMessageData : public talk_base::MessageData {
    654     SendDataMessageData(const SendDataParams& params,
    655                         const talk_base::Buffer* payload,
    656                         SendDataResult* result)
    657         : params(params),
    658           payload(payload),
    659           result(result),
    660           succeeded(false) {
    661     }
    662 
    663     const SendDataParams& params;
    664     const talk_base::Buffer* payload;
    665     SendDataResult* result;
    666     bool succeeded;
    667   };
    668 
    669   struct DataReceivedMessageData : public talk_base::MessageData {
    670     // We copy the data because the data will become invalid after we
    671     // handle DataMediaChannel::SignalDataReceived but before we fire
    672     // SignalDataReceived.
    673     DataReceivedMessageData(
    674         const ReceiveDataParams& params, const char* data, size_t len)
    675         : params(params),
    676           payload(data, len) {
    677     }
    678     const ReceiveDataParams params;
    679     const talk_base::Buffer payload;
    680   };
    681 
    682   typedef talk_base::TypedMessageData<bool> DataChannelReadyToSendMessageData;
    683 
    684   // overrides from BaseChannel
    685   virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
    686   // If data_channel_type_ is DCT_NONE, set it.  Otherwise, check that
    687   // it's the same as what was set previously.  Returns false if it's
    688   // set to one type one type and changed to another type later.
    689   bool SetDataChannelType(DataChannelType new_data_channel_type,
    690                           std::string* error_desc);
    691   // Same as SetDataChannelType, but extracts the type from the
    692   // DataContentDescription.
    693   bool SetDataChannelTypeFromContent(const DataContentDescription* content,
    694                                      std::string* error_desc);
    695   virtual bool SetLocalContent_w(const MediaContentDescription* content,
    696                                  ContentAction action,
    697                                  std::string* error_desc);
    698   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
    699                                   ContentAction action,
    700                                   std::string* error_desc);
    701   virtual void ChangeState();
    702   virtual bool WantsPacket(bool rtcp, talk_base::Buffer* packet);
    703 
    704   virtual void OnMessage(talk_base::Message* pmsg);
    705   virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
    706   virtual void OnConnectionMonitorUpdate(
    707       SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
    708   virtual void OnMediaMonitorUpdate(
    709       DataMediaChannel* media_channel, const DataMediaInfo& info);
    710   virtual bool ShouldSetupDtlsSrtp() const;
    711   void OnDataReceived(
    712       const ReceiveDataParams& params, const char* data, size_t len);
    713   void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
    714   void OnDataChannelReadyToSend(bool writable);
    715   void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
    716   void OnStreamClosedRemotely(uint32 sid);
    717 
    718   talk_base::scoped_ptr<DataMediaMonitor> media_monitor_;
    719   // TODO(pthatcher): Make a separate SctpDataChannel and
    720   // RtpDataChannel instead of using this.
    721   DataChannelType data_channel_type_;
    722   bool ready_to_send_data_;
    723 };
    724 
    725 }  // namespace cricket
    726 
    727 #endif  // TALK_SESSION_MEDIA_CHANNEL_H_
    728