PeerConnectionObject.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #include <api/peer_connection_interface.h>
  3. #include "DataChannelObject.h"
  4. #include "PeerConnectionStatsCollectorCallback.h"
  5. #include "WebRTCPlugin.h"
  6. namespace unity
  7. {
  8. namespace webrtc
  9. {
  10. using namespace ::webrtc;
  11. extern webrtc::SdpType ConvertSdpType(RTCSdpType type);
  12. extern RTCSdpType ConvertSdpType(webrtc::SdpType type);
  13. using DelegateCreateSDSuccess = void (*)(PeerConnectionObject*, RTCSdpType, const char*);
  14. using DelegateCreateSDFailure = void (*)(PeerConnectionObject*, RTCErrorType, const char*);
  15. using DelegateLocalSdpReady = void (*)(PeerConnectionObject*, const char*, const char*);
  16. using DelegateIceCandidate = void (*)(PeerConnectionObject*, const char*, const char*, const int);
  17. using DelegateOnIceConnectionChange = void (*)(PeerConnectionObject*, PeerConnectionInterface::IceConnectionState);
  18. using DelegateOnIceGatheringChange = void (*)(PeerConnectionObject*, PeerConnectionInterface::IceGatheringState);
  19. using DelegateOnConnectionStateChange =
  20. void (*)(PeerConnectionObject*, PeerConnectionInterface::PeerConnectionState);
  21. using DelegateOnDataChannel = void (*)(PeerConnectionObject*, DataChannelInterface*);
  22. using DelegateOnRenegotiationNeeded = void (*)(PeerConnectionObject*);
  23. using DelegateOnTrack = void (*)(PeerConnectionObject*, RtpTransceiverInterface*);
  24. using DelegateOnRemoveTrack = void (*)(PeerConnectionObject*, RtpReceiverInterface*);
  25. class PeerConnectionObject : public PeerConnectionObserver
  26. {
  27. public:
  28. PeerConnectionObject(Context& context);
  29. ~PeerConnectionObject() override;
  30. void Close();
  31. RTCErrorType SetLocalDescription(
  32. const RTCSessionDescription& desc, SetSessionDescriptionObserver* observer, std::string& error);
  33. RTCErrorType SetLocalDescriptionWithoutDescription(SetSessionDescriptionObserver* observer, std::string& error);
  34. RTCErrorType SetRemoteDescription(
  35. const RTCSessionDescription& desc, SetSessionDescriptionObserver* observer, std::string& error);
  36. bool GetSessionDescription(const SessionDescriptionInterface* sdp, RTCSessionDescription& desc) const;
  37. RTCErrorType SetConfiguration(const std::string& config);
  38. std::string GetConfiguration() const;
  39. void CreateOffer(const RTCOfferAnswerOptions& options, CreateSessionDescriptionObserver* observer);
  40. void CreateAnswer(const RTCOfferAnswerOptions& options, CreateSessionDescriptionObserver* observer);
  41. void ReceiveStatsReport(const rtc::scoped_refptr<const RTCStatsReport>& report);
  42. void RegisterCallbackCreateSD(DelegateCreateSDSuccess onSuccess, DelegateCreateSDFailure onFailure)
  43. {
  44. onCreateSDSuccess = onSuccess;
  45. onCreateSDFailure = onFailure;
  46. }
  47. void RegisterLocalSdpReady(DelegateLocalSdpReady callback) { onLocalSdpReady = callback; }
  48. void RegisterIceCandidate(DelegateIceCandidate callback) { onIceCandidate = callback; }
  49. void RegisterIceConnectionChange(DelegateOnIceConnectionChange callback) { onIceConnectionChange = callback; }
  50. void RegisterConnectionStateChange(DelegateOnConnectionStateChange callback)
  51. {
  52. onConnectionStateChange = callback;
  53. }
  54. void RegisterIceGatheringChange(DelegateOnIceGatheringChange callback) { onIceGatheringChange = callback; }
  55. void RegisterOnDataChannel(DelegateOnDataChannel callback) { onDataChannel = callback; }
  56. void RegisterOnRenegotiationNeeded(DelegateOnRenegotiationNeeded callback) { onRenegotiationNeeded = callback; }
  57. void RegisterOnTrack(DelegateOnTrack callback) { onTrack = callback; }
  58. void RegisterOnRemoveTrack(DelegateOnRemoveTrack callback) { onRemoveTrack = callback; }
  59. // webrtc::PeerConnectionObserver
  60. // Triggered when the SignalingState changed.
  61. void OnSignalingChange(PeerConnectionInterface::SignalingState new_state) override;
  62. // Triggered when media is received on a new stream from remote peer.
  63. void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override;
  64. // Triggered when a remote peer closes a stream.
  65. void OnRemoveStream(rtc::scoped_refptr<MediaStreamInterface> stream) override;
  66. // Triggered when a remote peer opens a data channel.
  67. void OnDataChannel(rtc::scoped_refptr<DataChannelInterface> data_channel) override;
  68. // Triggered when renegotiation is needed. For example, an ICE restart
  69. // has begun.
  70. void OnRenegotiationNeeded() override;
  71. // Called any time the IceConnectionState changes.
  72. void OnIceConnectionChange(PeerConnectionInterface::IceConnectionState new_state) override;
  73. // Called any time the PeerConnectionState changes.
  74. virtual void OnConnectionChange(PeerConnectionInterface::PeerConnectionState new_state) override;
  75. // Called any time the IceGatheringState changes.
  76. void OnIceGatheringChange(PeerConnectionInterface::IceGatheringState new_state) override;
  77. // A new ICE candidate has been gathered.
  78. void OnIceCandidate(const IceCandidateInterface* candidate) override;
  79. // Ice candidates have been removed.
  80. void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates) override { }
  81. // Called when the ICE connection receiving status changes.
  82. void OnIceConnectionReceivingChange(bool Receiving) override { }
  83. // This is called when signaling indicates a transceiver will be receiving
  84. // media from the remote endpoint. This is fired during a call to
  85. // SetRemoteDescription. The receiving track can be accessed by:
  86. // |transceiver->receiver()->track()| and its associated streams by
  87. // |transceiver->receiver()->streams()|.
  88. // Note: This will only be called if Unified Plan semantics are specified.
  89. // This behavior is specified in section 2.2.8.2.5 of the "Set the
  90. // RTCSessionDescription" algorithm:
  91. // https://w3c.github.io/webrtc-pc/#set-description
  92. void OnTrack(rtc::scoped_refptr<RtpTransceiverInterface> transceiver) override;
  93. void OnRemoveTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver) override;
  94. friend class DataChannelObject;
  95. DelegateCreateSDSuccess onCreateSDSuccess = nullptr;
  96. DelegateCreateSDFailure onCreateSDFailure = nullptr;
  97. DelegateIceCandidate onIceCandidate = nullptr;
  98. DelegateLocalSdpReady onLocalSdpReady = nullptr;
  99. DelegateOnConnectionStateChange onConnectionStateChange = nullptr;
  100. DelegateOnIceConnectionChange onIceConnectionChange = nullptr;
  101. DelegateOnIceGatheringChange onIceGatheringChange = nullptr;
  102. DelegateOnDataChannel onDataChannel = nullptr;
  103. DelegateOnRenegotiationNeeded onRenegotiationNeeded = nullptr;
  104. DelegateOnTrack onTrack = nullptr;
  105. DelegateOnRemoveTrack onRemoveTrack = nullptr;
  106. rtc::scoped_refptr<PeerConnectionInterface> connection = nullptr;
  107. private:
  108. Context& context;
  109. };
  110. } // end namespace webrtc
  111. } // end namespace unity