1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef _MTRANSPORTHANDLER_H__
6 #define _MTRANSPORTHANDLER_H__
7 
8 #include "mozilla/RefPtr.h"
9 #include "nsISupportsImpl.h"
10 #include "transport/sigslot.h"
11 #include "transport/transportlayer.h"  // Need the State enum
12 #include "transport/dtlsidentity.h"    // For DtlsDigest
13 #include "mozilla/dom/RTCPeerConnectionBinding.h"
14 #include "mozilla/dom/RTCConfigurationBinding.h"
15 #include "transport/nricectx.h"  // Need some enums
16 #include "common/CandidateInfo.h"
17 #include "transport/nr_socket_proxy_config.h"
18 #include "RTCStatsReport.h"
19 
20 #include "nsString.h"
21 
22 #include <string>
23 #include <set>
24 #include <vector>
25 
26 namespace mozilla {
27 class DtlsIdentity;
28 class NrIceCtx;
29 class NrIceMediaStream;
30 class NrIceResolver;
31 class TransportFlow;
32 class RTCStatsQuery;
33 
34 namespace dom {
35 struct RTCStatsReportInternal;
36 }
37 
38 class MediaTransportHandler {
39  public:
40   // Creates either a MediaTransportHandlerSTS or a MediaTransportHandlerIPC,
41   // as appropriate. If you want signals to fire on a specific thread, pass
42   // the event target here, otherwise they will fire on whatever is convenient.
43   // Note: This also determines what thread the state cache is updated on!
44   // Don't call GetState on any other thread!
45   static already_AddRefed<MediaTransportHandler> Create(
46       nsISerialEventTarget* aCallbackThread);
47 
MediaTransportHandler(nsISerialEventTarget * aCallbackThread)48   explicit MediaTransportHandler(nsISerialEventTarget* aCallbackThread)
49       : mCallbackThread(aCallbackThread) {}
50 
51   // Exposed so we can synchronously validate ICE servers from PeerConnection
52   static nsresult ConvertIceServers(
53       const nsTArray<dom::RTCIceServer>& aIceServers,
54       std::vector<NrIceStunServer>* aStunServers,
55       std::vector<NrIceTurnServer>* aTurnServers);
56 
57   typedef MozPromise<dom::Sequence<nsString>, nsresult, true> IceLogPromise;
58 
59   // There's a wrinkle here; the ICE logging is not separated out by
60   // MediaTransportHandler. These are a little more like static methods, but
61   // to avoid needing yet another IPC interface, we bolt them on here.
62   virtual RefPtr<IceLogPromise> GetIceLog(const nsCString& aPattern) = 0;
63   virtual void ClearIceLog() = 0;
64   virtual void EnterPrivateMode() = 0;
65   virtual void ExitPrivateMode() = 0;
66 
67   virtual void CreateIceCtx(const std::string& aName) = 0;
68 
69   virtual nsresult SetIceConfig(const nsTArray<dom::RTCIceServer>& aIceServers,
70                                 dom::RTCIceTransportPolicy aIcePolicy) = 0;
71 
72   // We will probably be able to move the proxy lookup stuff into
73   // this class once we move mtransport to its own process.
74   virtual void SetProxyConfig(NrSocketProxyConfig&& aProxyConfig) = 0;
75 
76   virtual void EnsureProvisionalTransport(const std::string& aTransportId,
77                                           const std::string& aLocalUfrag,
78                                           const std::string& aLocalPwd,
79                                           int aComponentCount) = 0;
80 
81   virtual void SetTargetForDefaultLocalAddressLookup(
82       const std::string& aTargetIp, uint16_t aTargetPort) = 0;
83 
84   // We set default-route-only as late as possible because it depends on what
85   // capture permissions have been granted on the window, which could easily
86   // change between Init (ie; when the PC is created) and StartIceGathering
87   // (ie; when we set the local description).
88   virtual void StartIceGathering(bool aDefaultRouteOnly,
89                                  bool aObfuscateHostAddresses,
90                                  // TODO: It probably makes sense to look
91                                  // this up internally
92                                  const nsTArray<NrIceStunAddr>& aStunAddrs) = 0;
93 
94   virtual void ActivateTransport(
95       const std::string& aTransportId, const std::string& aLocalUfrag,
96       const std::string& aLocalPwd, size_t aComponentCount,
97       const std::string& aUfrag, const std::string& aPassword,
98       const nsTArray<uint8_t>& aKeyDer, const nsTArray<uint8_t>& aCertDer,
99       SSLKEAType aAuthType, bool aDtlsClient, const DtlsDigestList& aDigests,
100       bool aPrivacyRequested) = 0;
101 
102   virtual void RemoveTransportsExcept(
103       const std::set<std::string>& aTransportIds) = 0;
104 
105   virtual void StartIceChecks(bool aIsControlling,
106                               const std::vector<std::string>& aIceOptions) = 0;
107 
108   virtual void SendPacket(const std::string& aTransportId,
109                           MediaPacket&& aPacket) = 0;
110 
111   virtual void AddIceCandidate(const std::string& aTransportId,
112                                const std::string& aCandidate,
113                                const std::string& aUFrag,
114                                const std::string& aObfuscatedAddress) = 0;
115 
116   virtual void UpdateNetworkState(bool aOnline) = 0;
117 
118   virtual RefPtr<dom::RTCStatsPromise> GetIceStats(
119       const std::string& aTransportId, DOMHighResTimeStamp aNow) = 0;
120 
121   sigslot::signal2<const std::string&, const CandidateInfo&> SignalCandidate;
122   sigslot::signal2<const std::string&, bool> SignalAlpnNegotiated;
123   sigslot::signal1<dom::RTCIceGatheringState> SignalGatheringStateChange;
124   sigslot::signal1<dom::RTCIceConnectionState> SignalConnectionStateChange;
125 
126   sigslot::signal2<const std::string&, const MediaPacket&> SignalPacketReceived;
127   sigslot::signal2<const std::string&, const MediaPacket&>
128       SignalEncryptedSending;
129   sigslot::signal2<const std::string&, TransportLayer::State> SignalStateChange;
130   sigslot::signal2<const std::string&, TransportLayer::State>
131       SignalRtcpStateChange;
132 
133   NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(MediaTransportHandler,
134                                                      Destroy())
135 
136   TransportLayer::State GetState(const std::string& aTransportId,
137                                  bool aRtcp) const;
138 
139  protected:
140   void OnCandidate(const std::string& aTransportId,
141                    const CandidateInfo& aCandidateInfo);
142   void OnAlpnNegotiated(const std::string& aAlpn);
143   void OnGatheringStateChange(dom::RTCIceGatheringState aState);
144   void OnConnectionStateChange(dom::RTCIceConnectionState aState);
145   void OnPacketReceived(const std::string& aTransportId,
146                         const MediaPacket& aPacket);
147   void OnEncryptedSending(const std::string& aTransportId,
148                           const MediaPacket& aPacket);
149   void OnStateChange(const std::string& aTransportId,
150                      TransportLayer::State aState);
151   void OnRtcpStateChange(const std::string& aTransportId,
152                          TransportLayer::State aState);
153   virtual void Destroy() = 0;
154   virtual ~MediaTransportHandler() = default;
155   std::map<std::string, TransportLayer::State> mStateCache;
156   std::map<std::string, TransportLayer::State> mRtcpStateCache;
157   RefPtr<nsISerialEventTarget> mCallbackThread;
158 };
159 
160 void TokenizeCandidate(const std::string& aCandidate,
161                        std::vector<std::string>& aTokens);
162 
163 }  // namespace mozilla
164 
165 #endif  //_MTRANSPORTHANDLER_H__
166