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