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 the PeerConnection interface as defined in
     29 // http://dev.w3.org/2011/webrtc/editor/webrtc.html#peer-to-peer-connections.
     30 // Applications must use this interface to implement peerconnection.
     31 // PeerConnectionFactory class provides factory methods to create
     32 // peerconnection, mediastream and media tracks objects.
     33 //
     34 // The Following steps are needed to setup a typical call using Jsep.
     35 // 1. Create a PeerConnectionFactoryInterface. Check constructors for more
     36 // information about input parameters.
     37 // 2. Create a PeerConnection object. Provide a configuration string which
     38 // points either to stun or turn server to generate ICE candidates and provide
     39 // an object that implements the PeerConnectionObserver interface.
     40 // 3. Create local MediaStream and MediaTracks using the PeerConnectionFactory
     41 // and add it to PeerConnection by calling AddStream.
     42 // 4. Create an offer and serialize it and send it to the remote peer.
     43 // 5. Once an ice candidate have been found PeerConnection will call the
     44 // observer function OnIceCandidate. The candidates must also be serialized and
     45 // sent to the remote peer.
     46 // 6. Once an answer is received from the remote peer, call
     47 // SetLocalSessionDescription with the offer and SetRemoteSessionDescription
     48 // with the remote answer.
     49 // 7. Once a remote candidate is received from the remote peer, provide it to
     50 // the peerconnection by calling AddIceCandidate.
     51 
     52 
     53 // The Receiver of a call can decide to accept or reject the call.
     54 // This decision will be taken by the application not peerconnection.
     55 // If application decides to accept the call
     56 // 1. Create PeerConnectionFactoryInterface if it doesn't exist.
     57 // 2. Create a new PeerConnection.
     58 // 3. Provide the remote offer to the new PeerConnection object by calling
     59 // SetRemoteSessionDescription.
     60 // 4. Generate an answer to the remote offer by calling CreateAnswer and send it
     61 // back to the remote peer.
     62 // 5. Provide the local answer to the new PeerConnection by calling
     63 // SetLocalSessionDescription with the answer.
     64 // 6. Provide the remote ice candidates by calling AddIceCandidate.
     65 // 7. Once a candidate have been found PeerConnection will call the observer
     66 // function OnIceCandidate. Send these candidates to the remote peer.
     67 
     68 #ifndef TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
     69 #define TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
     70 
     71 #include <string>
     72 #include <vector>
     73 
     74 #include "talk/app/webrtc/datachannelinterface.h"
     75 #include "talk/app/webrtc/dtmfsenderinterface.h"
     76 #include "talk/app/webrtc/jsep.h"
     77 #include "talk/app/webrtc/mediastreaminterface.h"
     78 #include "talk/app/webrtc/statstypes.h"
     79 #include "talk/base/socketaddress.h"
     80 
     81 namespace talk_base {
     82 class Thread;
     83 }
     84 
     85 namespace cricket {
     86 class PortAllocator;
     87 class WebRtcVideoDecoderFactory;
     88 class WebRtcVideoEncoderFactory;
     89 }
     90 
     91 namespace webrtc {
     92 class AudioDeviceModule;
     93 class MediaConstraintsInterface;
     94 
     95 // MediaStream container interface.
     96 class StreamCollectionInterface : public talk_base::RefCountInterface {
     97  public:
     98   // TODO(ronghuawu): Update the function names to c++ style, e.g. find -> Find.
     99   virtual size_t count() = 0;
    100   virtual MediaStreamInterface* at(size_t index) = 0;
    101   virtual MediaStreamInterface* find(const std::string& label) = 0;
    102   virtual MediaStreamTrackInterface* FindAudioTrack(
    103       const std::string& id) = 0;
    104   virtual MediaStreamTrackInterface* FindVideoTrack(
    105       const std::string& id) = 0;
    106 
    107  protected:
    108   // Dtor protected as objects shouldn't be deleted via this interface.
    109   ~StreamCollectionInterface() {}
    110 };
    111 
    112 class StatsObserver : public talk_base::RefCountInterface {
    113  public:
    114   virtual void OnComplete(const std::vector<StatsReport>& reports) = 0;
    115 
    116  protected:
    117   virtual ~StatsObserver() {}
    118 };
    119 
    120 class PeerConnectionInterface : public talk_base::RefCountInterface {
    121  public:
    122   // See http://dev.w3.org/2011/webrtc/editor/webrtc.html#state-definitions .
    123   enum SignalingState {
    124     kStable,
    125     kHaveLocalOffer,
    126     kHaveLocalPrAnswer,
    127     kHaveRemoteOffer,
    128     kHaveRemotePrAnswer,
    129     kClosed,
    130   };
    131 
    132   // TODO(bemasc): Remove IceState when callers are changed to
    133   // IceConnection/GatheringState.
    134   enum IceState {
    135     kIceNew,
    136     kIceGathering,
    137     kIceWaiting,
    138     kIceChecking,
    139     kIceConnected,
    140     kIceCompleted,
    141     kIceFailed,
    142     kIceClosed,
    143   };
    144 
    145   enum IceGatheringState {
    146     kIceGatheringNew,
    147     kIceGatheringGathering,
    148     kIceGatheringComplete
    149   };
    150 
    151   enum IceConnectionState {
    152     kIceConnectionNew,
    153     kIceConnectionChecking,
    154     kIceConnectionConnected,
    155     kIceConnectionCompleted,
    156     kIceConnectionFailed,
    157     kIceConnectionDisconnected,
    158     kIceConnectionClosed,
    159   };
    160 
    161   struct IceServer {
    162     std::string uri;
    163     std::string username;
    164     std::string password;
    165   };
    166   typedef std::vector<IceServer> IceServers;
    167 
    168   // Accessor methods to active local streams.
    169   virtual talk_base::scoped_refptr<StreamCollectionInterface>
    170       local_streams() = 0;
    171 
    172   // Accessor methods to remote streams.
    173   virtual talk_base::scoped_refptr<StreamCollectionInterface>
    174       remote_streams() = 0;
    175 
    176   // Add a new MediaStream to be sent on this PeerConnection.
    177   // Note that a SessionDescription negotiation is needed before the
    178   // remote peer can receive the stream.
    179   virtual bool AddStream(MediaStreamInterface* stream,
    180                          const MediaConstraintsInterface* constraints) = 0;
    181 
    182   // Remove a MediaStream from this PeerConnection.
    183   // Note that a SessionDescription negotiation is need before the
    184   // remote peer is notified.
    185   virtual void RemoveStream(MediaStreamInterface* stream) = 0;
    186 
    187   // Returns pointer to the created DtmfSender on success.
    188   // Otherwise returns NULL.
    189   virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
    190       AudioTrackInterface* track) = 0;
    191 
    192   virtual bool GetStats(StatsObserver* observer,
    193                         MediaStreamTrackInterface* track) = 0;
    194 
    195   virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
    196       const std::string& label,
    197       const DataChannelInit* config) = 0;
    198 
    199   virtual const SessionDescriptionInterface* local_description() const = 0;
    200   virtual const SessionDescriptionInterface* remote_description() const = 0;
    201 
    202   // Create a new offer.
    203   // The CreateSessionDescriptionObserver callback will be called when done.
    204   virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
    205                            const MediaConstraintsInterface* constraints) = 0;
    206   // Create an answer to an offer.
    207   // The CreateSessionDescriptionObserver callback will be called when done.
    208   virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
    209                             const MediaConstraintsInterface* constraints) = 0;
    210   // Sets the local session description.
    211   // JsepInterface takes the ownership of |desc| even if it fails.
    212   // The |observer| callback will be called when done.
    213   virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
    214                                    SessionDescriptionInterface* desc) = 0;
    215   // Sets the remote session description.
    216   // JsepInterface takes the ownership of |desc| even if it fails.
    217   // The |observer| callback will be called when done.
    218   virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
    219                                     SessionDescriptionInterface* desc) = 0;
    220   // Restarts or updates the ICE Agent process of gathering local candidates
    221   // and pinging remote candidates.
    222   virtual bool UpdateIce(const IceServers& configuration,
    223                          const MediaConstraintsInterface* constraints) = 0;
    224   // Provides a remote candidate to the ICE Agent.
    225   // A copy of the |candidate| will be created and added to the remote
    226   // description. So the caller of this method still has the ownership of the
    227   // |candidate|.
    228   // TODO(ronghuawu): Consider to change this so that the AddIceCandidate will
    229   // take the ownership of the |candidate|.
    230   virtual bool AddIceCandidate(const IceCandidateInterface* candidate) = 0;
    231 
    232   // Returns the current SignalingState.
    233   virtual SignalingState signaling_state() = 0;
    234 
    235   // TODO(bemasc): Remove ice_state when callers are changed to
    236   // IceConnection/GatheringState.
    237   // Returns the current IceState.
    238   virtual IceState ice_state() = 0;
    239   virtual IceConnectionState ice_connection_state() = 0;
    240   virtual IceGatheringState ice_gathering_state() = 0;
    241 
    242   // Terminates all media and closes the transport.
    243   virtual void Close() = 0;
    244 
    245  protected:
    246   // Dtor protected as objects shouldn't be deleted via this interface.
    247   ~PeerConnectionInterface() {}
    248 };
    249 
    250 // PeerConnection callback interface. Application should implement these
    251 // methods.
    252 class PeerConnectionObserver {
    253  public:
    254   enum StateType {
    255     kSignalingState,
    256     kIceState,
    257   };
    258 
    259   virtual void OnError() = 0;
    260 
    261   // Triggered when the SignalingState changed.
    262   virtual void OnSignalingChange(
    263      PeerConnectionInterface::SignalingState new_state) {}
    264 
    265   // Triggered when SignalingState or IceState have changed.
    266   // TODO(bemasc): Remove once callers transition to OnSignalingChange.
    267   virtual void OnStateChange(StateType state_changed) {}
    268 
    269   // Triggered when media is received on a new stream from remote peer.
    270   virtual void OnAddStream(MediaStreamInterface* stream) = 0;
    271 
    272   // Triggered when a remote peer close a stream.
    273   virtual void OnRemoveStream(MediaStreamInterface* stream) = 0;
    274 
    275   // Triggered when a remote peer open a data channel.
    276   // TODO(perkj): Make pure virtual.
    277   virtual void OnDataChannel(DataChannelInterface* data_channel) {}
    278 
    279   // Triggered when renegotation is needed, for example the ICE has restarted.
    280   virtual void OnRenegotiationNeeded() {}
    281 
    282   // Called any time the IceConnectionState changes
    283   virtual void OnIceConnectionChange(
    284       PeerConnectionInterface::IceConnectionState new_state) {}
    285 
    286   // Called any time the IceGatheringState changes
    287   virtual void OnIceGatheringChange(
    288       PeerConnectionInterface::IceGatheringState new_state) {}
    289 
    290   // New Ice candidate have been found.
    291   virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
    292 
    293   // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
    294   // All Ice candidates have been found.
    295   virtual void OnIceComplete() {}
    296 
    297  protected:
    298   // Dtor protected as objects shouldn't be deleted via this interface.
    299   ~PeerConnectionObserver() {}
    300 };
    301 
    302 // Factory class used for creating cricket::PortAllocator that is used
    303 // for ICE negotiation.
    304 class PortAllocatorFactoryInterface : public talk_base::RefCountInterface {
    305  public:
    306   struct StunConfiguration {
    307     StunConfiguration(const std::string& address, int port)
    308         : server(address, port) {}
    309     // STUN server address and port.
    310     talk_base::SocketAddress server;
    311   };
    312 
    313   struct TurnConfiguration {
    314     TurnConfiguration(const std::string& address,
    315                       int port,
    316                       const std::string& username,
    317                       const std::string& password,
    318                       const std::string& transport_type,
    319                       bool secure)
    320         : server(address, port),
    321           username(username),
    322           password(password),
    323           transport_type(transport_type),
    324           secure(secure) {}
    325     talk_base::SocketAddress server;
    326     std::string username;
    327     std::string password;
    328     std::string transport_type;
    329     bool secure;
    330   };
    331 
    332   virtual cricket::PortAllocator* CreatePortAllocator(
    333       const std::vector<StunConfiguration>& stun_servers,
    334       const std::vector<TurnConfiguration>& turn_configurations) = 0;
    335 
    336  protected:
    337   PortAllocatorFactoryInterface() {}
    338   ~PortAllocatorFactoryInterface() {}
    339 };
    340 
    341 // Used to receive callbacks of DTLS identity requests.
    342 class DTLSIdentityRequestObserver : public talk_base::RefCountInterface {
    343  public:
    344   virtual void OnFailure(int error) = 0;
    345   virtual void OnSuccess(const std::string& der_cert,
    346                          const std::string& der_private_key) = 0;
    347  protected:
    348   virtual ~DTLSIdentityRequestObserver() {}
    349 };
    350 
    351 class DTLSIdentityServiceInterface {
    352  public:
    353   // Asynchronously request a DTLS identity, including a self-signed certificate
    354   // and the private key used to sign the certificate, from the identity store
    355   // for the given identity name.
    356   // DTLSIdentityRequestObserver::OnSuccess will be called with the identity if
    357   // the request succeeded; DTLSIdentityRequestObserver::OnFailure will be
    358   // called with an error code if the request failed.
    359   //
    360   // Only one request can be made at a time. If a second request is called
    361   // before the first one completes, RequestIdentity will abort and return
    362   // false.
    363   //
    364   // |identity_name| is an internal name selected by the client to identify an
    365   // identity within an origin. E.g. an web site may cache the certificates used
    366   // to communicate with differnent peers under different identity names.
    367   //
    368   // |common_name| is the common name used to generate the certificate. If the
    369   // certificate already exists in the store, |common_name| is ignored.
    370   //
    371   // |observer| is the object to receive success or failure callbacks.
    372   //
    373   // Returns true if either OnFailure or OnSuccess will be called.
    374   virtual bool RequestIdentity(
    375       const std::string& identity_name,
    376       const std::string& common_name,
    377       DTLSIdentityRequestObserver* observer) = 0;
    378 
    379   virtual ~DTLSIdentityServiceInterface() {}
    380 };
    381 
    382 // PeerConnectionFactoryInterface is the factory interface use for creating
    383 // PeerConnection, MediaStream and media tracks.
    384 // PeerConnectionFactoryInterface will create required libjingle threads,
    385 // socket and network manager factory classes for networking.
    386 // If an application decides to provide its own threads and network
    387 // implementation of these classes it should use the alternate
    388 // CreatePeerConnectionFactory method which accepts threads as input and use the
    389 // CreatePeerConnection version that takes a PortAllocatorFactoryInterface as
    390 // argument.
    391 class PeerConnectionFactoryInterface : public talk_base::RefCountInterface {
    392  public:
    393   virtual talk_base::scoped_refptr<PeerConnectionInterface>
    394      CreatePeerConnection(
    395          const PeerConnectionInterface::IceServers& configuration,
    396          const MediaConstraintsInterface* constraints,
    397          DTLSIdentityServiceInterface* dtls_identity_service,
    398          PeerConnectionObserver* observer) = 0;
    399   virtual talk_base::scoped_refptr<PeerConnectionInterface>
    400       CreatePeerConnection(
    401           const PeerConnectionInterface::IceServers& configuration,
    402           const MediaConstraintsInterface* constraints,
    403           PortAllocatorFactoryInterface* allocator_factory,
    404           DTLSIdentityServiceInterface* dtls_identity_service,
    405           PeerConnectionObserver* observer) = 0;
    406   virtual talk_base::scoped_refptr<MediaStreamInterface>
    407       CreateLocalMediaStream(const std::string& label) = 0;
    408 
    409   // Creates a AudioSourceInterface.
    410   // |constraints| decides audio processing settings but can be NULL.
    411   virtual talk_base::scoped_refptr<AudioSourceInterface> CreateAudioSource(
    412       const MediaConstraintsInterface* constraints) = 0;
    413 
    414   // Creates a VideoSourceInterface. The new source take ownership of
    415   // |capturer|. |constraints| decides video resolution and frame rate but can
    416   // be NULL.
    417   virtual talk_base::scoped_refptr<VideoSourceInterface> CreateVideoSource(
    418       cricket::VideoCapturer* capturer,
    419       const MediaConstraintsInterface* constraints) = 0;
    420 
    421   // Creates a new local VideoTrack. The same |source| can be used in several
    422   // tracks.
    423   virtual talk_base::scoped_refptr<VideoTrackInterface>
    424       CreateVideoTrack(const std::string& label,
    425                        VideoSourceInterface* source) = 0;
    426 
    427   // Creates an new AudioTrack. At the moment |source| can be NULL.
    428   virtual talk_base::scoped_refptr<AudioTrackInterface>
    429       CreateAudioTrack(const std::string& label,
    430                        AudioSourceInterface* source) = 0;
    431 
    432  protected:
    433   // Dtor and ctor protected as objects shouldn't be created or deleted via
    434   // this interface.
    435   PeerConnectionFactoryInterface() {}
    436   ~PeerConnectionFactoryInterface() {} // NOLINT
    437 };
    438 
    439 // Create a new instance of PeerConnectionFactoryInterface.
    440 talk_base::scoped_refptr<PeerConnectionFactoryInterface>
    441 CreatePeerConnectionFactory();
    442 
    443 // Create a new instance of PeerConnectionFactoryInterface.
    444 // Ownership of |factory|, |default_adm|, and optionally |encoder_factory| and
    445 // |decoder_factory| transferred to the returned factory.
    446 talk_base::scoped_refptr<PeerConnectionFactoryInterface>
    447 CreatePeerConnectionFactory(
    448     talk_base::Thread* worker_thread,
    449     talk_base::Thread* signaling_thread,
    450     AudioDeviceModule* default_adm,
    451     cricket::WebRtcVideoEncoderFactory* encoder_factory,
    452     cricket::WebRtcVideoDecoderFactory* decoder_factory);
    453 
    454 }  // namespace webrtc
    455 
    456 #endif  // TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
    457