Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2013, 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_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
     29 #define TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
     30 
     31 #include "talk/app/webrtc/peerconnectioninterface.h"
     32 #include "talk/base/messagehandler.h"
     33 #include "talk/p2p/base/transportdescriptionfactory.h"
     34 #include "talk/session/media/mediasession.h"
     35 
     36 namespace cricket {
     37 class ChannelManager;
     38 class TransportDescriptionFactory;
     39 }  // namespace cricket
     40 
     41 namespace webrtc {
     42 class CreateSessionDescriptionObserver;
     43 class MediaConstraintsInterface;
     44 class MediaStreamSignaling;
     45 class SessionDescriptionInterface;
     46 class WebRtcSession;
     47 
     48 // DTLS identity request callback class.
     49 class WebRtcIdentityRequestObserver : public DTLSIdentityRequestObserver,
     50                                       public sigslot::has_slots<> {
     51  public:
     52   // DTLSIdentityRequestObserver overrides.
     53   virtual void OnFailure(int error) {
     54     SignalRequestFailed(error);
     55   }
     56   virtual void OnSuccess(const std::string& der_cert,
     57                          const std::string& der_private_key) {
     58     SignalIdentityReady(der_cert, der_private_key);
     59   }
     60 
     61   sigslot::signal1<int> SignalRequestFailed;
     62   sigslot::signal2<const std::string&, const std::string&> SignalIdentityReady;
     63 };
     64 
     65 struct CreateSessionDescriptionRequest {
     66   enum Type {
     67     kOffer,
     68     kAnswer,
     69   };
     70 
     71   CreateSessionDescriptionRequest(
     72       Type type,
     73       CreateSessionDescriptionObserver* observer,
     74       const cricket::MediaSessionOptions& options)
     75       : type(type),
     76         observer(observer),
     77         options(options) {}
     78 
     79   Type type;
     80   talk_base::scoped_refptr<CreateSessionDescriptionObserver> observer;
     81   cricket::MediaSessionOptions options;
     82 };
     83 
     84 // This class is used to create offer/answer session description with regards to
     85 // the async DTLS identity generation for WebRtcSession.
     86 // It queues the create offer/answer request until the DTLS identity
     87 // request has completed, i.e. when OnIdentityRequestFailed or OnIdentityReady
     88 // is called.
     89 class WebRtcSessionDescriptionFactory : public talk_base::MessageHandler,
     90                                         public sigslot::has_slots<>  {
     91  public:
     92   WebRtcSessionDescriptionFactory(
     93       talk_base::Thread* signaling_thread,
     94       cricket::ChannelManager* channel_manager,
     95       MediaStreamSignaling* mediastream_signaling,
     96       DTLSIdentityServiceInterface* dtls_identity_service,
     97       // TODO(jiayl): remove the dependency on session once b/10226852 is fixed.
     98       WebRtcSession* session,
     99       const std::string& session_id,
    100       cricket::DataChannelType dct,
    101       bool dtls_enabled);
    102   virtual ~WebRtcSessionDescriptionFactory();
    103 
    104   static void CopyCandidatesFromSessionDescription(
    105     const SessionDescriptionInterface* source_desc,
    106     SessionDescriptionInterface* dest_desc);
    107 
    108   void CreateOffer(
    109       CreateSessionDescriptionObserver* observer,
    110       const MediaConstraintsInterface* constraints);
    111   void CreateAnswer(
    112       CreateSessionDescriptionObserver* observer,
    113       const MediaConstraintsInterface* constraints);
    114 
    115   void SetSecure(cricket::SecureMediaPolicy secure_policy);
    116   cricket::SecureMediaPolicy Secure() const;
    117 
    118   sigslot::signal1<talk_base::SSLIdentity*> SignalIdentityReady;
    119 
    120   // For testing.
    121   bool waiting_for_identity() const {
    122     return identity_request_state_ == IDENTITY_WAITING;
    123   }
    124 
    125  private:
    126   enum IdentityRequestState {
    127     IDENTITY_NOT_NEEDED,
    128     IDENTITY_WAITING,
    129     IDENTITY_SUCCEEDED,
    130     IDENTITY_FAILED,
    131   };
    132 
    133   // MessageHandler implementation.
    134   virtual void OnMessage(talk_base::Message* msg);
    135 
    136   void InternalCreateOffer(CreateSessionDescriptionRequest request);
    137   void InternalCreateAnswer(CreateSessionDescriptionRequest request);
    138   void PostCreateSessionDescriptionFailed(
    139       CreateSessionDescriptionObserver* observer,
    140       const std::string& error);
    141   void PostCreateSessionDescriptionSucceeded(
    142       CreateSessionDescriptionObserver* observer,
    143       SessionDescriptionInterface* description);
    144 
    145   void OnIdentityRequestFailed(int error);
    146   void OnIdentityReady(const std::string& der_cert,
    147                        const std::string& der_private_key);
    148   void SetIdentity(talk_base::SSLIdentity* identity);
    149 
    150   std::queue<CreateSessionDescriptionRequest>
    151       create_session_description_requests_;
    152   talk_base::Thread* signaling_thread_;
    153   MediaStreamSignaling* mediastream_signaling_;
    154   cricket::TransportDescriptionFactory transport_desc_factory_;
    155   cricket::MediaSessionDescriptionFactory session_desc_factory_;
    156   uint64 session_version_;
    157   talk_base::scoped_ptr<DTLSIdentityServiceInterface> identity_service_;
    158   talk_base::scoped_refptr<WebRtcIdentityRequestObserver>
    159       identity_request_observer_;
    160   WebRtcSession* session_;
    161   std::string session_id_;
    162   cricket::DataChannelType data_channel_type_;
    163   IdentityRequestState identity_request_state_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
    166 };
    167 }  // namespace webrtc
    168 
    169 #endif  // TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
    170