Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2012, 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 // This file contains classes for listening on changes on MediaStreams and
     29 // MediaTracks that are connected to a certain PeerConnection.
     30 // Example: If a user sets a rendererer on a remote video track the renderer is
     31 // connected to the appropriate remote video stream.
     32 
     33 #ifndef TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
     34 #define TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
     35 
     36 #include <list>
     37 #include <vector>
     38 
     39 #include "talk/app/webrtc/mediastreaminterface.h"
     40 #include "talk/app/webrtc/mediastreamprovider.h"
     41 #include "talk/app/webrtc/peerconnectioninterface.h"
     42 #include "talk/base/thread.h"
     43 
     44 namespace webrtc {
     45 
     46 // TrackHandler listen to events on a MediaStreamTrackInterface that is
     47 // connected to a certain PeerConnection.
     48 class TrackHandler : public ObserverInterface {
     49  public:
     50   TrackHandler(MediaStreamTrackInterface* track, uint32 ssrc);
     51   virtual ~TrackHandler();
     52   virtual void OnChanged();
     53   // Stop using |track_| on this PeerConnection.
     54   virtual void Stop() = 0;
     55 
     56   MediaStreamTrackInterface*  track() { return track_; }
     57   uint32 ssrc() const { return ssrc_; }
     58 
     59  protected:
     60   virtual void OnStateChanged() = 0;
     61   virtual void OnEnabledChanged() = 0;
     62 
     63  private:
     64   talk_base::scoped_refptr<MediaStreamTrackInterface> track_;
     65   uint32 ssrc_;
     66   MediaStreamTrackInterface::TrackState state_;
     67   bool enabled_;
     68 };
     69 
     70 // LocalAudioTrackHandler listen to events on a local AudioTrack instance
     71 // connected to a PeerConnection and orders the |provider| to executes the
     72 // requested change.
     73 class LocalAudioTrackHandler : public TrackHandler {
     74  public:
     75   LocalAudioTrackHandler(AudioTrackInterface* track,
     76                          uint32 ssrc,
     77                          AudioProviderInterface* provider);
     78   virtual ~LocalAudioTrackHandler();
     79 
     80   virtual void Stop() OVERRIDE;
     81 
     82  protected:
     83   virtual void OnStateChanged() OVERRIDE;
     84   virtual void OnEnabledChanged() OVERRIDE;
     85 
     86  private:
     87   AudioTrackInterface* audio_track_;
     88   AudioProviderInterface* provider_;
     89 };
     90 
     91 // RemoteAudioTrackHandler listen to events on a remote AudioTrack instance
     92 // connected to a PeerConnection and orders the |provider| to executes the
     93 // requested change.
     94 class RemoteAudioTrackHandler : public TrackHandler {
     95  public:
     96   RemoteAudioTrackHandler(AudioTrackInterface* track,
     97                           uint32 ssrc,
     98                           AudioProviderInterface* provider);
     99   virtual ~RemoteAudioTrackHandler();
    100   virtual void Stop() OVERRIDE;
    101 
    102  protected:
    103   virtual void OnStateChanged() OVERRIDE;
    104   virtual void OnEnabledChanged() OVERRIDE;
    105 
    106  private:
    107   AudioTrackInterface* audio_track_;
    108   AudioProviderInterface* provider_;
    109 };
    110 
    111 // LocalVideoTrackHandler listen to events on a local VideoTrack instance
    112 // connected to a PeerConnection and orders the |provider| to executes the
    113 // requested change.
    114 class LocalVideoTrackHandler : public TrackHandler {
    115  public:
    116   LocalVideoTrackHandler(VideoTrackInterface* track,
    117                          uint32 ssrc,
    118                          VideoProviderInterface* provider);
    119   virtual ~LocalVideoTrackHandler();
    120   virtual void Stop() OVERRIDE;
    121 
    122  protected:
    123   virtual void OnStateChanged() OVERRIDE;
    124   virtual void OnEnabledChanged() OVERRIDE;
    125 
    126  private:
    127   VideoTrackInterface* local_video_track_;
    128   VideoProviderInterface* provider_;
    129 };
    130 
    131 // RemoteVideoTrackHandler listen to events on a remote VideoTrack instance
    132 // connected to a PeerConnection and orders the |provider| to execute
    133 // requested changes.
    134 class RemoteVideoTrackHandler : public TrackHandler {
    135  public:
    136   RemoteVideoTrackHandler(VideoTrackInterface* track,
    137                           uint32 ssrc,
    138                           VideoProviderInterface* provider);
    139   virtual ~RemoteVideoTrackHandler();
    140   virtual void Stop() OVERRIDE;
    141 
    142  protected:
    143   virtual void OnStateChanged() OVERRIDE;
    144   virtual void OnEnabledChanged() OVERRIDE;
    145 
    146  private:
    147   VideoTrackInterface* remote_video_track_;
    148   VideoProviderInterface* provider_;
    149 };
    150 
    151 class MediaStreamHandler : public ObserverInterface {
    152  public:
    153   MediaStreamHandler(MediaStreamInterface* stream,
    154                      AudioProviderInterface* audio_provider,
    155                      VideoProviderInterface* video_provider);
    156   ~MediaStreamHandler();
    157   MediaStreamInterface* stream();
    158   void Stop();
    159 
    160   virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0;
    161   virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0;
    162 
    163   virtual void RemoveTrack(MediaStreamTrackInterface* track);
    164   virtual void OnChanged() OVERRIDE;
    165 
    166  protected:
    167   TrackHandler* FindTrackHandler(MediaStreamTrackInterface* track);
    168   talk_base::scoped_refptr<MediaStreamInterface> stream_;
    169   AudioProviderInterface* audio_provider_;
    170   VideoProviderInterface* video_provider_;
    171   typedef std::vector<TrackHandler*> TrackHandlers;
    172   TrackHandlers track_handlers_;
    173 };
    174 
    175 class LocalMediaStreamHandler : public MediaStreamHandler {
    176  public:
    177   LocalMediaStreamHandler(MediaStreamInterface* stream,
    178                           AudioProviderInterface* audio_provider,
    179                           VideoProviderInterface* video_provider);
    180   ~LocalMediaStreamHandler();
    181 
    182   virtual void AddAudioTrack(AudioTrackInterface* audio_track,
    183                              uint32 ssrc) OVERRIDE;
    184   virtual void AddVideoTrack(VideoTrackInterface* video_track,
    185                              uint32 ssrc) OVERRIDE;
    186 };
    187 
    188 class RemoteMediaStreamHandler : public MediaStreamHandler {
    189  public:
    190   RemoteMediaStreamHandler(MediaStreamInterface* stream,
    191                            AudioProviderInterface* audio_provider,
    192                            VideoProviderInterface* video_provider);
    193   ~RemoteMediaStreamHandler();
    194   virtual void AddAudioTrack(AudioTrackInterface* audio_track,
    195                              uint32 ssrc) OVERRIDE;
    196   virtual void AddVideoTrack(VideoTrackInterface* video_track,
    197                              uint32 ssrc) OVERRIDE;
    198 };
    199 
    200 // Container for MediaStreamHandlers of currently known local and remote
    201 // MediaStreams.
    202 class MediaStreamHandlerContainer {
    203  public:
    204   MediaStreamHandlerContainer(AudioProviderInterface* audio_provider,
    205                               VideoProviderInterface* video_provider);
    206   ~MediaStreamHandlerContainer();
    207 
    208   // Notify all referenced objects that MediaStreamHandlerContainer will be
    209   // destroyed. This method must be called prior to the dtor and prior to the
    210   // |audio_provider| and |video_provider| is destroyed.
    211   void TearDown();
    212 
    213   // Remove all TrackHandlers for tracks in |stream| and make sure
    214   // the audio_provider and video_provider is notified that the tracks has been
    215   // removed.
    216   void RemoveRemoteStream(MediaStreamInterface* stream);
    217 
    218   // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|.
    219   void AddRemoteAudioTrack(MediaStreamInterface* stream,
    220                            AudioTrackInterface* audio_track,
    221                            uint32 ssrc);
    222   // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|.
    223   void AddRemoteVideoTrack(MediaStreamInterface* stream,
    224                            VideoTrackInterface* video_track,
    225                            uint32 ssrc);
    226   // Remove the TrackHandler for |track|.
    227   void RemoveRemoteTrack(MediaStreamInterface* stream,
    228                          MediaStreamTrackInterface* track);
    229 
    230   // Remove all TrackHandlers for tracks in |stream| and make sure
    231   // the audio_provider and video_provider is notified that the tracks has been
    232   // removed.
    233   void RemoveLocalStream(MediaStreamInterface* stream);
    234 
    235   // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|.
    236   void AddLocalAudioTrack(MediaStreamInterface* stream,
    237                           AudioTrackInterface* audio_track,
    238                           uint32 ssrc);
    239   // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|.
    240   void AddLocalVideoTrack(MediaStreamInterface* stream,
    241                           VideoTrackInterface* video_track,
    242                           uint32 ssrc);
    243   // Remove the TrackHandler for |track|.
    244   void RemoveLocalTrack(MediaStreamInterface* stream,
    245                         MediaStreamTrackInterface* track);
    246 
    247  private:
    248   typedef std::list<MediaStreamHandler*> StreamHandlerList;
    249   MediaStreamHandler* FindStreamHandler(const StreamHandlerList& handlers,
    250                                         MediaStreamInterface* stream);
    251   MediaStreamHandler* CreateRemoteStreamHandler(MediaStreamInterface* stream);
    252   MediaStreamHandler* CreateLocalStreamHandler(MediaStreamInterface* stream);
    253   void DeleteStreamHandler(StreamHandlerList* streamhandlers,
    254                            MediaStreamInterface* stream);
    255 
    256   StreamHandlerList local_streams_handlers_;
    257   StreamHandlerList remote_streams_handlers_;
    258   AudioProviderInterface* audio_provider_;
    259   VideoProviderInterface* video_provider_;
    260 };
    261 
    262 }  // namespace webrtc
    263 
    264 #endif  // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
    265