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 interfaces for MediaStream, MediaTrack and MediaSource.
     29 // These interfaces are used for implementing MediaStream and MediaTrack as
     30 // defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
     31 // interfaces must be used only with PeerConnection. PeerConnectionManager
     32 // interface provides the factory methods to create MediaStream and MediaTracks.
     33 
     34 #ifndef TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
     35 #define TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
     36 
     37 #include <string>
     38 #include <vector>
     39 
     40 #include "talk/base/basictypes.h"
     41 #include "talk/base/refcount.h"
     42 #include "talk/base/scoped_ref_ptr.h"
     43 
     44 namespace cricket {
     45 
     46 class AudioRenderer;
     47 class VideoCapturer;
     48 class VideoRenderer;
     49 class VideoFrame;
     50 
     51 }  // namespace cricket
     52 
     53 namespace webrtc {
     54 
     55 // Generic observer interface.
     56 class ObserverInterface {
     57  public:
     58   virtual void OnChanged() = 0;
     59 
     60  protected:
     61   virtual ~ObserverInterface() {}
     62 };
     63 
     64 class NotifierInterface {
     65  public:
     66   virtual void RegisterObserver(ObserverInterface* observer) = 0;
     67   virtual void UnregisterObserver(ObserverInterface* observer) = 0;
     68 
     69   virtual ~NotifierInterface() {}
     70 };
     71 
     72 // Base class for sources. A MediaStreamTrack have an underlying source that
     73 // provide media. A source can be shared with multiple tracks.
     74 // TODO(perkj): Implement sources for local and remote audio tracks and
     75 // remote video tracks.
     76 class MediaSourceInterface : public talk_base::RefCountInterface,
     77                              public NotifierInterface {
     78  public:
     79   enum SourceState {
     80     kInitializing,
     81     kLive,
     82     kEnded,
     83     kMuted
     84   };
     85 
     86   virtual SourceState state() const = 0;
     87 
     88  protected:
     89   virtual ~MediaSourceInterface() {}
     90 };
     91 
     92 // Information about a track.
     93 class MediaStreamTrackInterface : public talk_base::RefCountInterface,
     94                                   public NotifierInterface {
     95  public:
     96   enum TrackState {
     97     kInitializing,  // Track is beeing negotiated.
     98     kLive = 1,  // Track alive
     99     kEnded = 2,  // Track have ended
    100     kFailed = 3,  // Track negotiation failed.
    101   };
    102 
    103   virtual std::string kind() const = 0;
    104   virtual std::string id() const = 0;
    105   virtual bool enabled() const = 0;
    106   virtual TrackState state() const = 0;
    107   virtual bool set_enabled(bool enable) = 0;
    108   // These methods should be called by implementation only.
    109   virtual bool set_state(TrackState new_state) = 0;
    110 };
    111 
    112 // Interface for rendering VideoFrames from a VideoTrack
    113 class VideoRendererInterface {
    114  public:
    115   virtual void SetSize(int width, int height) = 0;
    116   virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
    117 
    118  protected:
    119   // The destructor is protected to prevent deletion via the interface.
    120   // This is so that we allow reference counted classes, where the destructor
    121   // should never be public, to implement the interface.
    122   virtual ~VideoRendererInterface() {}
    123 };
    124 
    125 class VideoSourceInterface;
    126 
    127 class VideoTrackInterface : public MediaStreamTrackInterface {
    128  public:
    129   // Register a renderer that will render all frames received on this track.
    130   virtual void AddRenderer(VideoRendererInterface* renderer) = 0;
    131   // Deregister a renderer.
    132   virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0;
    133 
    134   // Gets a pointer to the frame input of this VideoTrack.
    135   // The pointer is valid for the lifetime of this VideoTrack.
    136   // VideoFrames rendered to the cricket::VideoRenderer will be rendered on all
    137   // registered renderers.
    138   virtual cricket::VideoRenderer* FrameInput() = 0;
    139 
    140   virtual VideoSourceInterface* GetSource() const = 0;
    141 
    142  protected:
    143   virtual ~VideoTrackInterface() {}
    144 };
    145 
    146 // AudioSourceInterface is a reference counted source used for AudioTracks.
    147 // The same source can be used in multiple AudioTracks.
    148 // TODO(perkj): Extend this class with necessary methods to allow separate
    149 // sources for each audio track.
    150 class AudioSourceInterface : public MediaSourceInterface {
    151 };
    152 
    153 class AudioTrackInterface : public MediaStreamTrackInterface {
    154  public:
    155   // TODO(xians): Figure out if the following interface should be const or not.
    156   virtual AudioSourceInterface* GetSource() const =  0;
    157 
    158   // Gets a pointer to the audio renderer of this AudioTrack.
    159   // The pointer is valid for the lifetime of this AudioTrack.
    160   // TODO(xians): Make the following interface pure virtual once Chrome has its
    161   // implementation.
    162   virtual cricket::AudioRenderer* GetRenderer() { return NULL; }
    163 
    164  protected:
    165   virtual ~AudioTrackInterface() {}
    166 };
    167 
    168 typedef std::vector<talk_base::scoped_refptr<AudioTrackInterface> >
    169     AudioTrackVector;
    170 typedef std::vector<talk_base::scoped_refptr<VideoTrackInterface> >
    171     VideoTrackVector;
    172 
    173 class MediaStreamInterface : public talk_base::RefCountInterface,
    174                              public NotifierInterface {
    175  public:
    176   virtual std::string label() const = 0;
    177 
    178   virtual AudioTrackVector GetAudioTracks() = 0;
    179   virtual VideoTrackVector GetVideoTracks() = 0;
    180   virtual talk_base::scoped_refptr<AudioTrackInterface>
    181       FindAudioTrack(const std::string& track_id) = 0;
    182   virtual talk_base::scoped_refptr<VideoTrackInterface>
    183       FindVideoTrack(const std::string& track_id) = 0;
    184 
    185   virtual bool AddTrack(AudioTrackInterface* track) = 0;
    186   virtual bool AddTrack(VideoTrackInterface* track) = 0;
    187   virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
    188   virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
    189 
    190  protected:
    191   virtual ~MediaStreamInterface() {}
    192 };
    193 
    194 }  // namespace webrtc
    195 
    196 #endif  // TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
    197