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