peerconnection.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "common.hpp"
  22. #include "configuration.hpp"
  23. #include "datachannel.hpp"
  24. #include "description.hpp"
  25. #include "reliability.hpp"
  26. #include "track.hpp"
  27. #include <chrono>
  28. #include <functional>
  29. namespace rtc {
  30. namespace impl {
  31. struct PeerConnection;
  32. }
  33. struct RTC_CPP_EXPORT DataChannelInit {
  34. Reliability reliability = {};
  35. bool negotiated = false;
  36. optional<uint16_t> id = nullopt;
  37. string protocol = "";
  38. };
  39. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  40. public:
  41. enum class State : int {
  42. New = RTC_NEW,
  43. Connecting = RTC_CONNECTING,
  44. Connected = RTC_CONNECTED,
  45. Disconnected = RTC_DISCONNECTED,
  46. Failed = RTC_FAILED,
  47. Closed = RTC_CLOSED
  48. };
  49. enum class GatheringState : int {
  50. New = RTC_GATHERING_NEW,
  51. InProgress = RTC_GATHERING_INPROGRESS,
  52. Complete = RTC_GATHERING_COMPLETE
  53. };
  54. enum class SignalingState : int {
  55. Stable = RTC_SIGNALING_STABLE,
  56. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  57. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  58. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  59. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  60. };
  61. PeerConnection();
  62. PeerConnection(Configuration config);
  63. ~PeerConnection();
  64. void close();
  65. const Configuration *config() const;
  66. State state() const;
  67. GatheringState gatheringState() const;
  68. SignalingState signalingState() const;
  69. bool hasMedia() const;
  70. optional<Description> localDescription() const;
  71. optional<Description> remoteDescription() const;
  72. optional<string> localAddress() const;
  73. optional<string> remoteAddress() const;
  74. uint16_t maxDataChannelId() const;
  75. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  76. void setLocalDescription(Description::Type type = Description::Type::Unspec);
  77. void setRemoteDescription(Description description);
  78. void addRemoteCandidate(Candidate candidate);
  79. [[nodiscard]] shared_ptr<DataChannel> createDataChannel(string label, DataChannelInit init = {});
  80. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  81. [[nodiscard]] shared_ptr<Track> addTrack(Description::Media description);
  82. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  83. void onLocalDescription(std::function<void(Description description)> callback);
  84. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  85. void onStateChange(std::function<void(State state)> callback);
  86. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  87. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  88. void resetCallbacks();
  89. // Stats
  90. void clearStats();
  91. size_t bytesSent();
  92. size_t bytesReceived();
  93. optional<std::chrono::milliseconds> rtt();
  94. };
  95. } // namespace rtc
  96. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
  97. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  98. rtc::PeerConnection::GatheringState state);
  99. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  100. rtc::PeerConnection::SignalingState state);
  101. #endif