peerconnection.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_PEER_CONNECTION_H
  19. #define RTC_PEER_CONNECTION_H
  20. #include "candidate.hpp"
  21. #include "configuration.hpp"
  22. #include "datachannel.hpp"
  23. #include "description.hpp"
  24. #include "common.hpp"
  25. #include "init.hpp"
  26. #include "message.hpp"
  27. #include "reliability.hpp"
  28. #include "track.hpp"
  29. #include <chrono>
  30. #include <functional>
  31. namespace rtc {
  32. namespace impl {
  33. struct PeerConnection;
  34. }
  35. struct RTC_CPP_EXPORT DataChannelInit {
  36. Reliability reliability = {};
  37. bool negotiated = false;
  38. std::optional<uint16_t> id = nullopt;
  39. string protocol = "";
  40. };
  41. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  42. public:
  43. enum class State : int {
  44. New = RTC_NEW,
  45. Connecting = RTC_CONNECTING,
  46. Connected = RTC_CONNECTED,
  47. Disconnected = RTC_DISCONNECTED,
  48. Failed = RTC_FAILED,
  49. Closed = RTC_CLOSED
  50. };
  51. enum class GatheringState : int {
  52. New = RTC_GATHERING_NEW,
  53. InProgress = RTC_GATHERING_INPROGRESS,
  54. Complete = RTC_GATHERING_COMPLETE
  55. };
  56. enum class SignalingState : int {
  57. Stable = RTC_SIGNALING_STABLE,
  58. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  59. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  60. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  61. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  62. } rtcSignalingState;
  63. PeerConnection();
  64. PeerConnection(Configuration config);
  65. ~PeerConnection();
  66. void close();
  67. const Configuration *config() const;
  68. State state() const;
  69. GatheringState gatheringState() const;
  70. SignalingState signalingState() const;
  71. bool hasLocalDescription() const;
  72. bool hasRemoteDescription() const;
  73. bool hasMedia() const;
  74. std::optional<Description> localDescription() const;
  75. std::optional<Description> remoteDescription() const;
  76. std::optional<string> localAddress() const;
  77. std::optional<string> remoteAddress() const;
  78. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  79. void setLocalDescription(Description::Type type = Description::Type::Unspec);
  80. void setRemoteDescription(Description description);
  81. void addRemoteCandidate(Candidate candidate);
  82. std::shared_ptr<DataChannel> addDataChannel(string label, DataChannelInit init = {});
  83. // Equivalent to calling addDataChannel() and setLocalDescription()
  84. std::shared_ptr<DataChannel> createDataChannel(string label, DataChannelInit init = {});
  85. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  86. void onLocalDescription(std::function<void(Description description)> callback);
  87. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  88. void onStateChange(std::function<void(State state)> callback);
  89. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  90. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  91. // Stats
  92. void clearStats();
  93. size_t bytesSent();
  94. size_t bytesReceived();
  95. std::optional<std::chrono::milliseconds> rtt();
  96. // Track media support requires compiling with libSRTP
  97. std::shared_ptr<Track> addTrack(Description::Media description);
  98. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  99. };
  100. } // namespace rtc
  101. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
  102. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  103. rtc::PeerConnection::GatheringState state);
  104. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  105. rtc::PeerConnection::SignalingState state);
  106. #endif