peerconnection.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_PEER_CONNECTION_H
  9. #define RTC_PEER_CONNECTION_H
  10. #include "candidate.hpp"
  11. #include "common.hpp"
  12. #include "configuration.hpp"
  13. #include "datachannel.hpp"
  14. #include "description.hpp"
  15. #include "reliability.hpp"
  16. #include "track.hpp"
  17. #include <chrono>
  18. #include <functional>
  19. namespace rtc {
  20. namespace impl {
  21. struct PeerConnection;
  22. }
  23. struct RTC_CPP_EXPORT DataChannelInit {
  24. Reliability reliability = {};
  25. bool negotiated = false;
  26. optional<uint16_t> id = nullopt;
  27. string protocol = "";
  28. };
  29. struct RTC_CPP_EXPORT LocalDescriptionInit {
  30. optional<string> iceUfrag;
  31. optional<string> icePwd;
  32. };
  33. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  34. public:
  35. enum class State : int {
  36. New = RTC_NEW,
  37. Connecting = RTC_CONNECTING,
  38. Connected = RTC_CONNECTED,
  39. Disconnected = RTC_DISCONNECTED,
  40. Failed = RTC_FAILED,
  41. Closed = RTC_CLOSED
  42. };
  43. enum class IceState : int {
  44. New = RTC_ICE_NEW,
  45. Checking = RTC_ICE_CHECKING,
  46. Connected = RTC_ICE_CONNECTED,
  47. Completed = RTC_ICE_COMPLETED,
  48. Failed = RTC_ICE_FAILED,
  49. Disconnected = RTC_ICE_DISCONNECTED,
  50. Closed = RTC_ICE_CLOSED
  51. };
  52. enum class GatheringState : int {
  53. New = RTC_GATHERING_NEW,
  54. InProgress = RTC_GATHERING_INPROGRESS,
  55. Complete = RTC_GATHERING_COMPLETE
  56. };
  57. enum class SignalingState : int {
  58. Stable = RTC_SIGNALING_STABLE,
  59. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  60. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  61. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  62. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  63. };
  64. PeerConnection();
  65. PeerConnection(Configuration config);
  66. ~PeerConnection();
  67. void close();
  68. const Configuration *config() const;
  69. State state() const;
  70. IceState iceState() const;
  71. GatheringState gatheringState() const;
  72. SignalingState signalingState() const;
  73. bool negotiationNeeded() const;
  74. bool hasMedia() const;
  75. optional<Description> localDescription() const;
  76. optional<Description> remoteDescription() const;
  77. size_t remoteMaxMessageSize() const;
  78. optional<string> localAddress() const;
  79. optional<string> remoteAddress() const;
  80. uint16_t maxDataChannelId() const;
  81. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  82. void setLocalDescription(Description::Type type = Description::Type::Unspec, LocalDescriptionInit init = {});
  83. void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});
  84. void setRemoteDescription(Description description);
  85. void addRemoteCandidate(Candidate candidate);
  86. // For specific use cases only
  87. Description createOffer();
  88. Description createAnswer();
  89. void setMediaHandler(shared_ptr<MediaHandler> handler);
  90. shared_ptr<MediaHandler> getMediaHandler();
  91. [[nodiscard]] shared_ptr<DataChannel> createDataChannel(string label,
  92. DataChannelInit init = {});
  93. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  94. [[nodiscard]] shared_ptr<Track> addTrack(Description::Media description);
  95. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  96. void onLocalDescription(std::function<void(Description description)> callback);
  97. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  98. void onStateChange(std::function<void(State state)> callback);
  99. void onIceStateChange(std::function<void(IceState state)> callback);
  100. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  101. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  102. void resetCallbacks();
  103. CertificateFingerprint remoteFingerprint();
  104. // Stats
  105. void clearStats();
  106. size_t bytesSent();
  107. size_t bytesReceived();
  108. optional<std::chrono::milliseconds> rtt();
  109. };
  110. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::State state);
  111. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::IceState state);
  112. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::GatheringState state);
  113. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::SignalingState state);
  114. } // namespace rtc
  115. #endif