peerconnection.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  30. public:
  31. enum class State : int {
  32. New = RTC_NEW,
  33. Connecting = RTC_CONNECTING,
  34. Connected = RTC_CONNECTED,
  35. Disconnected = RTC_DISCONNECTED,
  36. Failed = RTC_FAILED,
  37. Closed = RTC_CLOSED
  38. };
  39. enum class IceState : int {
  40. New = RTC_ICE_NEW,
  41. Checking = RTC_ICE_CHECKING,
  42. Connected = RTC_ICE_CONNECTED,
  43. Completed = RTC_ICE_COMPLETED,
  44. Failed = RTC_ICE_FAILED,
  45. Disconnected = RTC_ICE_DISCONNECTED,
  46. Closed = RTC_ICE_CLOSED
  47. };
  48. enum class GatheringState : int {
  49. New = RTC_GATHERING_NEW,
  50. InProgress = RTC_GATHERING_INPROGRESS,
  51. Complete = RTC_GATHERING_COMPLETE
  52. };
  53. enum class SignalingState : int {
  54. Stable = RTC_SIGNALING_STABLE,
  55. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  56. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  57. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  58. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  59. };
  60. PeerConnection();
  61. PeerConnection(Configuration config);
  62. ~PeerConnection();
  63. void close();
  64. const Configuration *config() const;
  65. State state() const;
  66. IceState iceState() const;
  67. GatheringState gatheringState() const;
  68. SignalingState signalingState() const;
  69. bool hasMedia() const;
  70. optional<Description> localDescription() const;
  71. optional<Description> remoteDescription() const;
  72. size_t remoteMaxMessageSize() const;
  73. optional<string> localAddress() const;
  74. optional<string> remoteAddress() const;
  75. uint16_t maxDataChannelId() const;
  76. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  77. void setLocalDescription(Description::Type type = Description::Type::Unspec);
  78. void setRemoteDescription(Description description);
  79. void addRemoteCandidate(Candidate candidate);
  80. void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});
  81. void setMediaHandler(shared_ptr<MediaHandler> handler);
  82. shared_ptr<MediaHandler> getMediaHandler();
  83. [[nodiscard]] shared_ptr<DataChannel> createDataChannel(string label,
  84. DataChannelInit init = {});
  85. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  86. [[nodiscard]] shared_ptr<Track> addTrack(Description::Media description);
  87. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  88. void onLocalDescription(std::function<void(Description description)> callback);
  89. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  90. void onStateChange(std::function<void(State state)> callback);
  91. void onIceStateChange(std::function<void(IceState state)> callback);
  92. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  93. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  94. void resetCallbacks();
  95. // Stats
  96. void clearStats();
  97. size_t bytesSent();
  98. size_t bytesReceived();
  99. optional<std::chrono::milliseconds> rtt();
  100. };
  101. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::State state);
  102. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::IceState state);
  103. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::GatheringState state);
  104. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, PeerConnection::SignalingState state);
  105. } // namespace rtc
  106. #endif