Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef _PEER_CONNECTION_IMPL_H_ |
michael@0 | 6 | #define _PEER_CONNECTION_IMPL_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <deque> |
michael@0 | 9 | #include <string> |
michael@0 | 10 | #include <vector> |
michael@0 | 11 | #include <map> |
michael@0 | 12 | #include <cmath> |
michael@0 | 13 | |
michael@0 | 14 | #include "prlock.h" |
michael@0 | 15 | #include "mozilla/RefPtr.h" |
michael@0 | 16 | #include "nsWeakPtr.h" |
michael@0 | 17 | #include "nsAutoPtr.h" |
michael@0 | 18 | #include "nsIWeakReferenceUtils.h" // for the definition of nsWeakPtr |
michael@0 | 19 | #include "IPeerConnection.h" |
michael@0 | 20 | #include "sigslot.h" |
michael@0 | 21 | #include "nricectx.h" |
michael@0 | 22 | #include "nricemediastream.h" |
michael@0 | 23 | #include "nsComponentManagerUtils.h" |
michael@0 | 24 | #include "nsPIDOMWindow.h" |
michael@0 | 25 | #include "nsIThread.h" |
michael@0 | 26 | |
michael@0 | 27 | #include "mozilla/ErrorResult.h" |
michael@0 | 28 | #include "mozilla/dom/PeerConnectionImplEnumsBinding.h" |
michael@0 | 29 | #include "StreamBuffer.h" |
michael@0 | 30 | #include "LoadManagerFactory.h" |
michael@0 | 31 | |
michael@0 | 32 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 33 | #include "mozilla/TimeStamp.h" |
michael@0 | 34 | #include "mozilla/net/DataChannel.h" |
michael@0 | 35 | #include "VideoUtils.h" |
michael@0 | 36 | #include "VideoSegment.h" |
michael@0 | 37 | #include "nsNSSShutDown.h" |
michael@0 | 38 | #include "mozilla/dom/RTCStatsReportBinding.h" |
michael@0 | 39 | #endif |
michael@0 | 40 | |
michael@0 | 41 | namespace test { |
michael@0 | 42 | #ifdef USE_FAKE_PCOBSERVER |
michael@0 | 43 | class AFakePCObserver; |
michael@0 | 44 | #endif |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | #ifdef USE_FAKE_MEDIA_STREAMS |
michael@0 | 48 | class Fake_DOMMediaStream; |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | class nsGlobalWindow; |
michael@0 | 52 | class nsIDOMMediaStream; |
michael@0 | 53 | class nsDOMDataChannel; |
michael@0 | 54 | |
michael@0 | 55 | namespace mozilla { |
michael@0 | 56 | class DataChannel; |
michael@0 | 57 | class DtlsIdentity; |
michael@0 | 58 | class NrIceCtx; |
michael@0 | 59 | class NrIceMediaStream; |
michael@0 | 60 | class NrIceStunServer; |
michael@0 | 61 | class NrIceTurnServer; |
michael@0 | 62 | class MediaPipeline; |
michael@0 | 63 | |
michael@0 | 64 | #ifdef USE_FAKE_MEDIA_STREAMS |
michael@0 | 65 | typedef Fake_DOMMediaStream DOMMediaStream; |
michael@0 | 66 | #else |
michael@0 | 67 | class DOMMediaStream; |
michael@0 | 68 | #endif |
michael@0 | 69 | |
michael@0 | 70 | namespace dom { |
michael@0 | 71 | class RTCConfiguration; |
michael@0 | 72 | class MediaConstraintsInternal; |
michael@0 | 73 | class MediaStreamTrack; |
michael@0 | 74 | class RTCStatsReportInternal; |
michael@0 | 75 | |
michael@0 | 76 | #ifdef USE_FAKE_PCOBSERVER |
michael@0 | 77 | typedef test::AFakePCObserver PeerConnectionObserver; |
michael@0 | 78 | typedef const char *PCObserverString; |
michael@0 | 79 | #else |
michael@0 | 80 | class PeerConnectionObserver; |
michael@0 | 81 | typedef NS_ConvertUTF8toUTF16 PCObserverString; |
michael@0 | 82 | #endif |
michael@0 | 83 | } |
michael@0 | 84 | class MediaConstraintsExternal; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | #if defined(__cplusplus) && __cplusplus >= 201103L |
michael@0 | 88 | typedef struct Timecard Timecard; |
michael@0 | 89 | #else |
michael@0 | 90 | #include "timecard.h" |
michael@0 | 91 | #endif |
michael@0 | 92 | |
michael@0 | 93 | // To preserve blame, convert nsresult to ErrorResult with wrappers. These macros |
michael@0 | 94 | // help declare wrappers w/function being wrapped when there are no differences. |
michael@0 | 95 | |
michael@0 | 96 | #define NS_IMETHODIMP_TO_ERRORRESULT(func, rv, ...) \ |
michael@0 | 97 | NS_IMETHODIMP func(__VA_ARGS__); \ |
michael@0 | 98 | void func (__VA_ARGS__, rv) |
michael@0 | 99 | |
michael@0 | 100 | #define NS_IMETHODIMP_TO_ERRORRESULT_RETREF(resulttype, func, rv, ...) \ |
michael@0 | 101 | NS_IMETHODIMP func(__VA_ARGS__, resulttype **result); \ |
michael@0 | 102 | already_AddRefed<resulttype> func (__VA_ARGS__, rv) |
michael@0 | 103 | |
michael@0 | 104 | namespace sipcc { |
michael@0 | 105 | |
michael@0 | 106 | using mozilla::dom::PeerConnectionObserver; |
michael@0 | 107 | using mozilla::dom::RTCConfiguration; |
michael@0 | 108 | using mozilla::dom::MediaConstraintsInternal; |
michael@0 | 109 | using mozilla::MediaConstraintsExternal; |
michael@0 | 110 | using mozilla::DOMMediaStream; |
michael@0 | 111 | using mozilla::NrIceCtx; |
michael@0 | 112 | using mozilla::NrIceMediaStream; |
michael@0 | 113 | using mozilla::DtlsIdentity; |
michael@0 | 114 | using mozilla::ErrorResult; |
michael@0 | 115 | using mozilla::NrIceStunServer; |
michael@0 | 116 | using mozilla::NrIceTurnServer; |
michael@0 | 117 | |
michael@0 | 118 | class PeerConnectionWrapper; |
michael@0 | 119 | class PeerConnectionMedia; |
michael@0 | 120 | class RemoteSourceStreamInfo; |
michael@0 | 121 | class OnCallEventArgs; |
michael@0 | 122 | |
michael@0 | 123 | class IceConfiguration |
michael@0 | 124 | { |
michael@0 | 125 | public: |
michael@0 | 126 | bool addStunServer(const std::string& addr, uint16_t port) |
michael@0 | 127 | { |
michael@0 | 128 | NrIceStunServer* server(NrIceStunServer::Create(addr, port)); |
michael@0 | 129 | if (!server) { |
michael@0 | 130 | return false; |
michael@0 | 131 | } |
michael@0 | 132 | addStunServer(*server); |
michael@0 | 133 | return true; |
michael@0 | 134 | } |
michael@0 | 135 | bool addTurnServer(const std::string& addr, uint16_t port, |
michael@0 | 136 | const std::string& username, |
michael@0 | 137 | const std::string& pwd, |
michael@0 | 138 | const char* transport) |
michael@0 | 139 | { |
michael@0 | 140 | // TODO(ekr@rtfm.com): Need support for SASLprep for |
michael@0 | 141 | // username and password. Bug # ??? |
michael@0 | 142 | std::vector<unsigned char> password(pwd.begin(), pwd.end()); |
michael@0 | 143 | |
michael@0 | 144 | NrIceTurnServer* server(NrIceTurnServer::Create(addr, port, username, password, |
michael@0 | 145 | transport)); |
michael@0 | 146 | if (!server) { |
michael@0 | 147 | return false; |
michael@0 | 148 | } |
michael@0 | 149 | addTurnServer(*server); |
michael@0 | 150 | return true; |
michael@0 | 151 | } |
michael@0 | 152 | void addStunServer(const NrIceStunServer& server) { mStunServers.push_back (server); } |
michael@0 | 153 | void addTurnServer(const NrIceTurnServer& server) { mTurnServers.push_back (server); } |
michael@0 | 154 | const std::vector<NrIceStunServer>& getStunServers() const { return mStunServers; } |
michael@0 | 155 | const std::vector<NrIceTurnServer>& getTurnServers() const { return mTurnServers; } |
michael@0 | 156 | private: |
michael@0 | 157 | std::vector<NrIceStunServer> mStunServers; |
michael@0 | 158 | std::vector<NrIceTurnServer> mTurnServers; |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 162 | // Not an inner class so we can forward declare. |
michael@0 | 163 | class RTCStatsQuery { |
michael@0 | 164 | public: |
michael@0 | 165 | explicit RTCStatsQuery(bool internalStats); |
michael@0 | 166 | ~RTCStatsQuery(); |
michael@0 | 167 | |
michael@0 | 168 | mozilla::dom::RTCStatsReportInternal report; |
michael@0 | 169 | std::string error; |
michael@0 | 170 | |
michael@0 | 171 | private: |
michael@0 | 172 | friend class PeerConnectionImpl; |
michael@0 | 173 | std::string pcName; |
michael@0 | 174 | bool internalStats; |
michael@0 | 175 | nsTArray<mozilla::RefPtr<mozilla::MediaPipeline>> pipelines; |
michael@0 | 176 | mozilla::RefPtr<NrIceCtx> iceCtx; |
michael@0 | 177 | nsTArray<mozilla::RefPtr<NrIceMediaStream>> streams; |
michael@0 | 178 | DOMHighResTimeStamp now; |
michael@0 | 179 | }; |
michael@0 | 180 | #endif // MOZILLA_INTERNAL_API |
michael@0 | 181 | |
michael@0 | 182 | // Enter an API call and check that the state is OK, |
michael@0 | 183 | // the PC isn't closed, etc. |
michael@0 | 184 | #define PC_AUTO_ENTER_API_CALL(assert_ice_ready) \ |
michael@0 | 185 | do { \ |
michael@0 | 186 | /* do/while prevents res from conflicting with locals */ \ |
michael@0 | 187 | nsresult res = CheckApiState(assert_ice_ready); \ |
michael@0 | 188 | if (NS_FAILED(res)) return res; \ |
michael@0 | 189 | } while(0) |
michael@0 | 190 | #define PC_AUTO_ENTER_API_CALL_VOID_RETURN(assert_ice_ready) \ |
michael@0 | 191 | do { \ |
michael@0 | 192 | /* do/while prevents res from conflicting with locals */ \ |
michael@0 | 193 | nsresult res = CheckApiState(assert_ice_ready); \ |
michael@0 | 194 | if (NS_FAILED(res)) return; \ |
michael@0 | 195 | } while(0) |
michael@0 | 196 | #define PC_AUTO_ENTER_API_CALL_NO_CHECK() CheckThread() |
michael@0 | 197 | |
michael@0 | 198 | class PeerConnectionImpl MOZ_FINAL : public nsISupports, |
michael@0 | 199 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 200 | public mozilla::DataChannelConnection::DataConnectionListener, |
michael@0 | 201 | public nsNSSShutDownObject, |
michael@0 | 202 | #endif |
michael@0 | 203 | public sigslot::has_slots<> |
michael@0 | 204 | { |
michael@0 | 205 | class Internal; // Avoid exposing c includes to bindings |
michael@0 | 206 | |
michael@0 | 207 | public: |
michael@0 | 208 | PeerConnectionImpl(const mozilla::dom::GlobalObject* aGlobal = nullptr); |
michael@0 | 209 | virtual ~PeerConnectionImpl(); |
michael@0 | 210 | |
michael@0 | 211 | enum Error { |
michael@0 | 212 | kNoError = 0, |
michael@0 | 213 | kInvalidConstraintsType = 1, |
michael@0 | 214 | kInvalidCandidateType = 2, |
michael@0 | 215 | kInvalidMediastreamTrack = 3, |
michael@0 | 216 | kInvalidState = 4, |
michael@0 | 217 | kInvalidSessionDescription = 5, |
michael@0 | 218 | kIncompatibleSessionDescription = 6, |
michael@0 | 219 | kIncompatibleConstraints = 7, |
michael@0 | 220 | kIncompatibleMediaStreamTrack = 8, |
michael@0 | 221 | kInternalError = 9 |
michael@0 | 222 | }; |
michael@0 | 223 | |
michael@0 | 224 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 225 | |
michael@0 | 226 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 227 | virtual JSObject* WrapObject(JSContext* cx); |
michael@0 | 228 | #endif |
michael@0 | 229 | |
michael@0 | 230 | static already_AddRefed<PeerConnectionImpl> |
michael@0 | 231 | Constructor(const mozilla::dom::GlobalObject& aGlobal, ErrorResult& rv); |
michael@0 | 232 | static PeerConnectionImpl* CreatePeerConnection(); |
michael@0 | 233 | static nsresult ConvertRTCConfiguration(const RTCConfiguration& aSrc, |
michael@0 | 234 | IceConfiguration *aDst); |
michael@0 | 235 | static already_AddRefed<DOMMediaStream> MakeMediaStream(nsPIDOMWindow* aWindow, |
michael@0 | 236 | uint32_t aHint); |
michael@0 | 237 | |
michael@0 | 238 | nsresult CreateRemoteSourceStreamInfo(nsRefPtr<RemoteSourceStreamInfo>* aInfo); |
michael@0 | 239 | |
michael@0 | 240 | // Implementation of the only observer we need |
michael@0 | 241 | void onCallEvent(const OnCallEventArgs &args); |
michael@0 | 242 | |
michael@0 | 243 | // DataConnection observers |
michael@0 | 244 | void NotifyConnection(); |
michael@0 | 245 | void NotifyClosedConnection(); |
michael@0 | 246 | void NotifyDataChannel(already_AddRefed<mozilla::DataChannel> aChannel); |
michael@0 | 247 | |
michael@0 | 248 | // Get the media object |
michael@0 | 249 | const nsRefPtr<PeerConnectionMedia>& media() const { |
michael@0 | 250 | PC_AUTO_ENTER_API_CALL_NO_CHECK(); |
michael@0 | 251 | return mMedia; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | mozilla::LoadManager* load_manager() { |
michael@0 | 255 | return mLoadManager; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | // Handle system to allow weak references to be passed through C code |
michael@0 | 259 | virtual const std::string& GetHandle(); |
michael@0 | 260 | |
michael@0 | 261 | // Name suitable for exposing to content |
michael@0 | 262 | virtual const std::string& GetName(); |
michael@0 | 263 | |
michael@0 | 264 | // ICE events |
michael@0 | 265 | void IceConnectionStateChange(NrIceCtx* ctx, |
michael@0 | 266 | NrIceCtx::ConnectionState state); |
michael@0 | 267 | void IceGatheringStateChange(NrIceCtx* ctx, |
michael@0 | 268 | NrIceCtx::GatheringState state); |
michael@0 | 269 | void IceStreamReady(NrIceMediaStream *aStream); |
michael@0 | 270 | |
michael@0 | 271 | static void ListenThread(void *aData); |
michael@0 | 272 | static void ConnectThread(void *aData); |
michael@0 | 273 | |
michael@0 | 274 | // Get the main thread |
michael@0 | 275 | nsCOMPtr<nsIThread> GetMainThread() { |
michael@0 | 276 | PC_AUTO_ENTER_API_CALL_NO_CHECK(); |
michael@0 | 277 | return mThread; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | // Get the STS thread |
michael@0 | 281 | nsCOMPtr<nsIEventTarget> GetSTSThread() { |
michael@0 | 282 | PC_AUTO_ENTER_API_CALL_NO_CHECK(); |
michael@0 | 283 | return mSTSThread; |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | // Get the DTLS identity |
michael@0 | 287 | mozilla::RefPtr<DtlsIdentity> const GetIdentity() const; |
michael@0 | 288 | std::string GetFingerprint() const; |
michael@0 | 289 | std::string GetFingerprintAlgorithm() const; |
michael@0 | 290 | std::string GetFingerprintHexValue() const; |
michael@0 | 291 | |
michael@0 | 292 | // Create a fake media stream |
michael@0 | 293 | nsresult CreateFakeMediaStream(uint32_t hint, nsIDOMMediaStream** retval); |
michael@0 | 294 | |
michael@0 | 295 | nsPIDOMWindow* GetWindow() const { |
michael@0 | 296 | PC_AUTO_ENTER_API_CALL_NO_CHECK(); |
michael@0 | 297 | return mWindow; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | // Initialize PeerConnection from an IceConfiguration object (unit-tests) |
michael@0 | 301 | nsresult Initialize(PeerConnectionObserver& aObserver, |
michael@0 | 302 | nsGlobalWindow* aWindow, |
michael@0 | 303 | const IceConfiguration& aConfiguration, |
michael@0 | 304 | nsIThread* aThread) { |
michael@0 | 305 | return Initialize(aObserver, aWindow, &aConfiguration, nullptr, aThread); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | // Initialize PeerConnection from an RTCConfiguration object (JS entrypoint) |
michael@0 | 309 | void Initialize(PeerConnectionObserver& aObserver, |
michael@0 | 310 | nsGlobalWindow& aWindow, |
michael@0 | 311 | const RTCConfiguration& aConfiguration, |
michael@0 | 312 | nsISupports* aThread, |
michael@0 | 313 | ErrorResult &rv) |
michael@0 | 314 | { |
michael@0 | 315 | nsresult r = Initialize(aObserver, &aWindow, nullptr, &aConfiguration, aThread); |
michael@0 | 316 | if (NS_FAILED(r)) { |
michael@0 | 317 | rv.Throw(r); |
michael@0 | 318 | } |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | NS_IMETHODIMP_TO_ERRORRESULT(CreateOffer, ErrorResult &rv, |
michael@0 | 322 | const MediaConstraintsInternal& aConstraints) |
michael@0 | 323 | { |
michael@0 | 324 | rv = CreateOffer(aConstraints); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | NS_IMETHODIMP_TO_ERRORRESULT(CreateAnswer, ErrorResult &rv, |
michael@0 | 328 | const MediaConstraintsInternal& aConstraints) |
michael@0 | 329 | { |
michael@0 | 330 | rv = CreateAnswer(aConstraints); |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | NS_IMETHODIMP CreateOffer(const MediaConstraintsExternal& aConstraints); |
michael@0 | 334 | NS_IMETHODIMP CreateAnswer(const MediaConstraintsExternal& aConstraints); |
michael@0 | 335 | |
michael@0 | 336 | NS_IMETHODIMP SetLocalDescription (int32_t aAction, const char* aSDP); |
michael@0 | 337 | |
michael@0 | 338 | void SetLocalDescription (int32_t aAction, const nsAString& aSDP, ErrorResult &rv) |
michael@0 | 339 | { |
michael@0 | 340 | rv = SetLocalDescription(aAction, NS_ConvertUTF16toUTF8(aSDP).get()); |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | NS_IMETHODIMP SetRemoteDescription (int32_t aAction, const char* aSDP); |
michael@0 | 344 | |
michael@0 | 345 | void SetRemoteDescription (int32_t aAction, const nsAString& aSDP, ErrorResult &rv) |
michael@0 | 346 | { |
michael@0 | 347 | rv = SetRemoteDescription(aAction, NS_ConvertUTF16toUTF8(aSDP).get()); |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | NS_IMETHODIMP_TO_ERRORRESULT(GetStats, ErrorResult &rv, |
michael@0 | 351 | mozilla::dom::MediaStreamTrack *aSelector) |
michael@0 | 352 | { |
michael@0 | 353 | rv = GetStats(aSelector); |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | NS_IMETHODIMP AddIceCandidate(const char* aCandidate, const char* aMid, |
michael@0 | 357 | unsigned short aLevel); |
michael@0 | 358 | |
michael@0 | 359 | void AddIceCandidate(const nsAString& aCandidate, const nsAString& aMid, |
michael@0 | 360 | unsigned short aLevel, ErrorResult &rv) |
michael@0 | 361 | { |
michael@0 | 362 | rv = AddIceCandidate(NS_ConvertUTF16toUTF8(aCandidate).get(), |
michael@0 | 363 | NS_ConvertUTF16toUTF8(aMid).get(), aLevel); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | NS_IMETHODIMP CloseStreams(); |
michael@0 | 367 | |
michael@0 | 368 | void CloseStreams(ErrorResult &rv) |
michael@0 | 369 | { |
michael@0 | 370 | rv = CloseStreams(); |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | NS_IMETHODIMP_TO_ERRORRESULT(AddStream, ErrorResult &rv, |
michael@0 | 374 | DOMMediaStream& aMediaStream, |
michael@0 | 375 | const MediaConstraintsInternal& aConstraints) |
michael@0 | 376 | { |
michael@0 | 377 | rv = AddStream(aMediaStream, aConstraints); |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | NS_IMETHODIMP AddStream(DOMMediaStream & aMediaStream, |
michael@0 | 381 | const MediaConstraintsExternal& aConstraints); |
michael@0 | 382 | |
michael@0 | 383 | NS_IMETHODIMP_TO_ERRORRESULT(RemoveStream, ErrorResult &rv, |
michael@0 | 384 | DOMMediaStream& aMediaStream) |
michael@0 | 385 | { |
michael@0 | 386 | rv = RemoveStream(aMediaStream); |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | NS_IMETHODIMP GetFingerprint(char** fingerprint); |
michael@0 | 390 | void GetFingerprint(nsAString& fingerprint) |
michael@0 | 391 | { |
michael@0 | 392 | char *tmp; |
michael@0 | 393 | GetFingerprint(&tmp); |
michael@0 | 394 | fingerprint.AssignASCII(tmp); |
michael@0 | 395 | delete[] tmp; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | NS_IMETHODIMP GetLocalDescription(char** aSDP); |
michael@0 | 399 | |
michael@0 | 400 | void GetLocalDescription(nsAString& aSDP) |
michael@0 | 401 | { |
michael@0 | 402 | char *tmp; |
michael@0 | 403 | GetLocalDescription(&tmp); |
michael@0 | 404 | aSDP.AssignASCII(tmp); |
michael@0 | 405 | delete tmp; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | NS_IMETHODIMP GetRemoteDescription(char** aSDP); |
michael@0 | 409 | |
michael@0 | 410 | void GetRemoteDescription(nsAString& aSDP) |
michael@0 | 411 | { |
michael@0 | 412 | char *tmp; |
michael@0 | 413 | GetRemoteDescription(&tmp); |
michael@0 | 414 | aSDP.AssignASCII(tmp); |
michael@0 | 415 | delete tmp; |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | NS_IMETHODIMP ReadyState(mozilla::dom::PCImplReadyState* aState); |
michael@0 | 419 | |
michael@0 | 420 | mozilla::dom::PCImplReadyState ReadyState() |
michael@0 | 421 | { |
michael@0 | 422 | mozilla::dom::PCImplReadyState state; |
michael@0 | 423 | ReadyState(&state); |
michael@0 | 424 | return state; |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | NS_IMETHODIMP SignalingState(mozilla::dom::PCImplSignalingState* aState); |
michael@0 | 428 | |
michael@0 | 429 | mozilla::dom::PCImplSignalingState SignalingState() |
michael@0 | 430 | { |
michael@0 | 431 | mozilla::dom::PCImplSignalingState state; |
michael@0 | 432 | SignalingState(&state); |
michael@0 | 433 | return state; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | NS_IMETHODIMP SipccState(mozilla::dom::PCImplSipccState* aState); |
michael@0 | 437 | |
michael@0 | 438 | mozilla::dom::PCImplSipccState SipccState() |
michael@0 | 439 | { |
michael@0 | 440 | mozilla::dom::PCImplSipccState state; |
michael@0 | 441 | SipccState(&state); |
michael@0 | 442 | return state; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | NS_IMETHODIMP IceConnectionState( |
michael@0 | 446 | mozilla::dom::PCImplIceConnectionState* aState); |
michael@0 | 447 | |
michael@0 | 448 | mozilla::dom::PCImplIceConnectionState IceConnectionState() |
michael@0 | 449 | { |
michael@0 | 450 | mozilla::dom::PCImplIceConnectionState state; |
michael@0 | 451 | IceConnectionState(&state); |
michael@0 | 452 | return state; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | NS_IMETHODIMP IceGatheringState( |
michael@0 | 456 | mozilla::dom::PCImplIceGatheringState* aState); |
michael@0 | 457 | |
michael@0 | 458 | mozilla::dom::PCImplIceGatheringState IceGatheringState() |
michael@0 | 459 | { |
michael@0 | 460 | mozilla::dom::PCImplIceGatheringState state; |
michael@0 | 461 | IceGatheringState(&state); |
michael@0 | 462 | return state; |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | NS_IMETHODIMP Close(); |
michael@0 | 466 | |
michael@0 | 467 | void Close(ErrorResult &rv) |
michael@0 | 468 | { |
michael@0 | 469 | rv = Close(); |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | nsresult InitializeDataChannel(int track_id, uint16_t aLocalport, |
michael@0 | 473 | uint16_t aRemoteport, uint16_t aNumstreams); |
michael@0 | 474 | |
michael@0 | 475 | NS_IMETHODIMP_TO_ERRORRESULT(ConnectDataConnection, ErrorResult &rv, |
michael@0 | 476 | uint16_t aLocalport, |
michael@0 | 477 | uint16_t aRemoteport, |
michael@0 | 478 | uint16_t aNumstreams) |
michael@0 | 479 | { |
michael@0 | 480 | rv = ConnectDataConnection(aLocalport, aRemoteport, aNumstreams); |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | NS_IMETHODIMP_TO_ERRORRESULT_RETREF(nsDOMDataChannel, |
michael@0 | 484 | CreateDataChannel, ErrorResult &rv, |
michael@0 | 485 | const nsAString& aLabel, |
michael@0 | 486 | const nsAString& aProtocol, |
michael@0 | 487 | uint16_t aType, |
michael@0 | 488 | bool outOfOrderAllowed, |
michael@0 | 489 | uint16_t aMaxTime, |
michael@0 | 490 | uint16_t aMaxNum, |
michael@0 | 491 | bool aExternalNegotiated, |
michael@0 | 492 | uint16_t aStream); |
michael@0 | 493 | |
michael@0 | 494 | NS_IMETHODIMP_TO_ERRORRESULT(GetLocalStreams, ErrorResult &rv, |
michael@0 | 495 | nsTArray<nsRefPtr<DOMMediaStream > >& result) |
michael@0 | 496 | { |
michael@0 | 497 | rv = GetLocalStreams(result); |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | NS_IMETHODIMP_TO_ERRORRESULT(GetRemoteStreams, ErrorResult &rv, |
michael@0 | 501 | nsTArray<nsRefPtr<DOMMediaStream > >& result) |
michael@0 | 502 | { |
michael@0 | 503 | rv = GetRemoteStreams(result); |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | // Called whenever something is unrecognized by the parser |
michael@0 | 507 | // May be called more than once and does not necessarily mean |
michael@0 | 508 | // that parsing was stopped, only that something was unrecognized. |
michael@0 | 509 | void OnSdpParseError(const char* errorMessage); |
michael@0 | 510 | |
michael@0 | 511 | // Called when OnLocal/RemoteDescriptionSuccess/Error |
michael@0 | 512 | // is called to start the list over. |
michael@0 | 513 | void ClearSdpParseErrorMessages(); |
michael@0 | 514 | |
michael@0 | 515 | // Called to retreive the list of parsing errors. |
michael@0 | 516 | const std::vector<std::string> &GetSdpParseErrors(); |
michael@0 | 517 | |
michael@0 | 518 | // Sets the RTC Signaling State |
michael@0 | 519 | void SetSignalingState_m(mozilla::dom::PCImplSignalingState aSignalingState); |
michael@0 | 520 | |
michael@0 | 521 | bool IsClosed() const; |
michael@0 | 522 | |
michael@0 | 523 | bool HasMedia() const; |
michael@0 | 524 | |
michael@0 | 525 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 526 | // initialize telemetry for when calls start |
michael@0 | 527 | void startCallTelem(); |
michael@0 | 528 | |
michael@0 | 529 | nsresult BuildStatsQuery_m( |
michael@0 | 530 | mozilla::dom::MediaStreamTrack *aSelector, |
michael@0 | 531 | RTCStatsQuery *query); |
michael@0 | 532 | |
michael@0 | 533 | static nsresult ExecuteStatsQuery_s(RTCStatsQuery *query); |
michael@0 | 534 | #endif |
michael@0 | 535 | |
michael@0 | 536 | private: |
michael@0 | 537 | PeerConnectionImpl(const PeerConnectionImpl&rhs); |
michael@0 | 538 | PeerConnectionImpl& operator=(PeerConnectionImpl); |
michael@0 | 539 | NS_IMETHODIMP Initialize(PeerConnectionObserver& aObserver, |
michael@0 | 540 | nsGlobalWindow* aWindow, |
michael@0 | 541 | const IceConfiguration* aConfiguration, |
michael@0 | 542 | const RTCConfiguration* aRTCConfiguration, |
michael@0 | 543 | nsISupports* aThread); |
michael@0 | 544 | |
michael@0 | 545 | NS_IMETHODIMP EnsureDataConnection(uint16_t aNumstreams); |
michael@0 | 546 | |
michael@0 | 547 | nsresult CloseInt(); |
michael@0 | 548 | void ChangeReadyState(mozilla::dom::PCImplReadyState aReadyState); |
michael@0 | 549 | nsresult CheckApiState(bool assert_ice_ready) const; |
michael@0 | 550 | void CheckThread() const { |
michael@0 | 551 | NS_ABORT_IF_FALSE(CheckThreadInt(), "Wrong thread"); |
michael@0 | 552 | } |
michael@0 | 553 | bool CheckThreadInt() const { |
michael@0 | 554 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 555 | // Thread assertions are disabled in the C++ unit tests because those |
michael@0 | 556 | // make API calls off the main thread. |
michael@0 | 557 | // TODO(ekr@rtfm.com): Fix the unit tests so they don't do that. |
michael@0 | 558 | bool on; |
michael@0 | 559 | NS_ENSURE_SUCCESS(mThread->IsOnCurrentThread(&on), false); |
michael@0 | 560 | NS_ENSURE_TRUE(on, false); |
michael@0 | 561 | #endif |
michael@0 | 562 | return true; |
michael@0 | 563 | } |
michael@0 | 564 | |
michael@0 | 565 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 566 | void virtualDestroyNSSReference() MOZ_FINAL; |
michael@0 | 567 | void destructorSafeDestroyNSSReference(); |
michael@0 | 568 | nsresult GetTimeSinceEpoch(DOMHighResTimeStamp *result); |
michael@0 | 569 | #endif |
michael@0 | 570 | |
michael@0 | 571 | // Shut down media - called on main thread only |
michael@0 | 572 | void ShutdownMedia(); |
michael@0 | 573 | |
michael@0 | 574 | NS_IMETHOD FingerprintSplitHelper( |
michael@0 | 575 | std::string& fingerprint, size_t& spaceIdx) const; |
michael@0 | 576 | |
michael@0 | 577 | |
michael@0 | 578 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 579 | static void GetStatsForPCObserver_s( |
michael@0 | 580 | const std::string& pcHandle, |
michael@0 | 581 | nsAutoPtr<RTCStatsQuery> query); |
michael@0 | 582 | |
michael@0 | 583 | // Sends an RTCStatsReport to JS. Must run on main thread. |
michael@0 | 584 | static void DeliverStatsReportToPCObserver_m( |
michael@0 | 585 | const std::string& pcHandle, |
michael@0 | 586 | nsresult result, |
michael@0 | 587 | nsAutoPtr<RTCStatsQuery> query); |
michael@0 | 588 | #endif |
michael@0 | 589 | |
michael@0 | 590 | // When ICE completes, we record a bunch of statistics that outlive the |
michael@0 | 591 | // PeerConnection. This is just telemetry right now, but this can also |
michael@0 | 592 | // include things like dumping the RLogRingbuffer somewhere, saving away |
michael@0 | 593 | // an RTCStatsReport somewhere so it can be inspected after the call is over, |
michael@0 | 594 | // or other things. |
michael@0 | 595 | void RecordLongtermICEStatistics(); |
michael@0 | 596 | |
michael@0 | 597 | // Timecard used to measure processing time. This should be the first class |
michael@0 | 598 | // attribute so that we accurately measure the time required to instantiate |
michael@0 | 599 | // any other attributes of this class. |
michael@0 | 600 | Timecard *mTimeCard; |
michael@0 | 601 | |
michael@0 | 602 | // The call |
michael@0 | 603 | mozilla::ScopedDeletePtr<Internal> mInternal; |
michael@0 | 604 | mozilla::dom::PCImplReadyState mReadyState; |
michael@0 | 605 | mozilla::dom::PCImplSignalingState mSignalingState; |
michael@0 | 606 | |
michael@0 | 607 | // ICE State |
michael@0 | 608 | mozilla::dom::PCImplIceConnectionState mIceConnectionState; |
michael@0 | 609 | mozilla::dom::PCImplIceGatheringState mIceGatheringState; |
michael@0 | 610 | |
michael@0 | 611 | nsCOMPtr<nsIThread> mThread; |
michael@0 | 612 | // TODO: Remove if we ever properly wire PeerConnection for cycle-collection. |
michael@0 | 613 | nsWeakPtr mPCObserver; |
michael@0 | 614 | |
michael@0 | 615 | nsCOMPtr<nsPIDOMWindow> mWindow; |
michael@0 | 616 | |
michael@0 | 617 | // The SDP sent in from JS - here for debugging. |
michael@0 | 618 | std::string mLocalRequestedSDP; |
michael@0 | 619 | std::string mRemoteRequestedSDP; |
michael@0 | 620 | // The SDP we are using. |
michael@0 | 621 | std::string mLocalSDP; |
michael@0 | 622 | std::string mRemoteSDP; |
michael@0 | 623 | |
michael@0 | 624 | // DTLS fingerprint |
michael@0 | 625 | std::string mFingerprint; |
michael@0 | 626 | std::string mRemoteFingerprint; |
michael@0 | 627 | |
michael@0 | 628 | // The DTLS identity |
michael@0 | 629 | mozilla::RefPtr<DtlsIdentity> mIdentity; |
michael@0 | 630 | |
michael@0 | 631 | // A handle to refer to this PC with |
michael@0 | 632 | std::string mHandle; |
michael@0 | 633 | |
michael@0 | 634 | // A name for this PC that we are willing to expose to content. |
michael@0 | 635 | std::string mName; |
michael@0 | 636 | |
michael@0 | 637 | // The target to run stuff on |
michael@0 | 638 | nsCOMPtr<nsIEventTarget> mSTSThread; |
michael@0 | 639 | |
michael@0 | 640 | // CPU Load adaptation stuff |
michael@0 | 641 | mozilla::LoadManager* mLoadManager; |
michael@0 | 642 | |
michael@0 | 643 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 644 | // DataConnection that's used to get all the DataChannels |
michael@0 | 645 | nsRefPtr<mozilla::DataChannelConnection> mDataConnection; |
michael@0 | 646 | #endif |
michael@0 | 647 | |
michael@0 | 648 | nsRefPtr<PeerConnectionMedia> mMedia; |
michael@0 | 649 | |
michael@0 | 650 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 651 | // Start time of ICE, used for telemetry |
michael@0 | 652 | mozilla::TimeStamp mIceStartTime; |
michael@0 | 653 | // Start time of call used for Telemetry |
michael@0 | 654 | mozilla::TimeStamp mStartTime; |
michael@0 | 655 | #endif |
michael@0 | 656 | |
michael@0 | 657 | // Temporary: used to prevent multiple audio streams or multiple video streams |
michael@0 | 658 | // in a single PC. This is tied up in the IETF discussion around proper |
michael@0 | 659 | // representation of multiple streams in SDP, and strongly related to |
michael@0 | 660 | // Bug 840728. |
michael@0 | 661 | int mNumAudioStreams; |
michael@0 | 662 | int mNumVideoStreams; |
michael@0 | 663 | |
michael@0 | 664 | bool mHaveDataStream; |
michael@0 | 665 | |
michael@0 | 666 | // Holder for error messages from parsing SDP |
michael@0 | 667 | std::vector<std::string> mSDPParseErrorMessages; |
michael@0 | 668 | |
michael@0 | 669 | bool mTrickle; |
michael@0 | 670 | |
michael@0 | 671 | public: |
michael@0 | 672 | //these are temporary until the DataChannel Listen/Connect API is removed |
michael@0 | 673 | unsigned short listenPort; |
michael@0 | 674 | unsigned short connectPort; |
michael@0 | 675 | char *connectStr; // XXX ownership/free |
michael@0 | 676 | }; |
michael@0 | 677 | |
michael@0 | 678 | // This is what is returned when you acquire on a handle |
michael@0 | 679 | class PeerConnectionWrapper |
michael@0 | 680 | { |
michael@0 | 681 | public: |
michael@0 | 682 | PeerConnectionWrapper(const std::string& handle); |
michael@0 | 683 | |
michael@0 | 684 | PeerConnectionImpl *impl() { return impl_; } |
michael@0 | 685 | |
michael@0 | 686 | private: |
michael@0 | 687 | nsRefPtr<PeerConnectionImpl> impl_; |
michael@0 | 688 | }; |
michael@0 | 689 | |
michael@0 | 690 | } // end sipcc namespace |
michael@0 | 691 | |
michael@0 | 692 | #undef NS_IMETHODIMP_TO_ERRORRESULT |
michael@0 | 693 | #undef NS_IMETHODIMP_TO_ERRORRESULT_RETREF |
michael@0 | 694 | #endif // _PEER_CONNECTION_IMPL_H_ |