1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef PC_DTLS_TRANSPORT_H_
12 #define PC_DTLS_TRANSPORT_H_
13 
14 #include <memory>
15 
16 #include "api/dtls_transport_interface.h"
17 #include "api/ice_transport_interface.h"
18 #include "api/scoped_refptr.h"
19 #include "p2p/base/dtls_transport.h"
20 #include "p2p/base/dtls_transport_internal.h"
21 #include "pc/ice_transport.h"
22 #include "rtc_base/synchronization/mutex.h"
23 #include "rtc_base/thread.h"
24 #include "rtc_base/thread_annotations.h"
25 
26 namespace webrtc {
27 
28 class IceTransportWithPointer;
29 
30 // This implementation wraps a cricket::DtlsTransport, and takes
31 // ownership of it.
32 class DtlsTransport : public DtlsTransportInterface {
33  public:
34   // This object must be constructed and updated on a consistent thread,
35   // the same thread as the one the cricket::DtlsTransportInternal object
36   // lives on.
37   // The Information() function can be called from a different thread,
38   // such as the signalling thread.
39   explicit DtlsTransport(
40       std::unique_ptr<cricket::DtlsTransportInternal> internal);
41 
42   rtc::scoped_refptr<IceTransportInterface> ice_transport() override;
43   DtlsTransportInformation Information() override;
44   void RegisterObserver(DtlsTransportObserverInterface* observer) override;
45   void UnregisterObserver() override;
46   void Clear();
47 
internal()48   cricket::DtlsTransportInternal* internal() {
49     MutexLock lock(&lock_);
50     return internal_dtls_transport_.get();
51   }
52 
internal()53   const cricket::DtlsTransportInternal* internal() const {
54     MutexLock lock(&lock_);
55     return internal_dtls_transport_.get();
56   }
57 
58  protected:
59   ~DtlsTransport();
60 
61  private:
62   void OnInternalDtlsState(cricket::DtlsTransportInternal* transport,
63                            cricket::DtlsTransportState state);
64   void UpdateInformation();
65 
66   DtlsTransportObserverInterface* observer_ = nullptr;
67   rtc::Thread* owner_thread_;
68   mutable Mutex lock_;
69   DtlsTransportInformation info_ RTC_GUARDED_BY(lock_);
70   std::unique_ptr<cricket::DtlsTransportInternal> internal_dtls_transport_
71       RTC_GUARDED_BY(lock_);
72   const rtc::scoped_refptr<IceTransportWithPointer> ice_transport_;
73 };
74 
75 }  // namespace webrtc
76 #endif  // PC_DTLS_TRANSPORT_H_
77