peerconnection.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "message.hpp"
  26. #include "reliability.hpp"
  27. #include "track.hpp"
  28. #include <chrono>
  29. #include <functional>
  30. namespace rtc {
  31. namespace impl {
  32. struct PeerConnection;
  33. }
  34. struct RTC_CPP_EXPORT DataChannelInit {
  35. Reliability reliability = {};
  36. bool negotiated = false;
  37. optional<uint16_t> id = nullopt;
  38. string protocol = "";
  39. };
  40. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  41. public:
  42. enum class State : int {
  43. New = RTC_NEW,
  44. Connecting = RTC_CONNECTING,
  45. Connected = RTC_CONNECTED,
  46. Disconnected = RTC_DISCONNECTED,
  47. Failed = RTC_FAILED,
  48. Closed = RTC_CLOSED
  49. };
  50. enum class GatheringState : int {
  51. New = RTC_GATHERING_NEW,
  52. InProgress = RTC_GATHERING_INPROGRESS,
  53. Complete = RTC_GATHERING_COMPLETE
  54. };
  55. enum class SignalingState : int {
  56. Stable = RTC_SIGNALING_STABLE,
  57. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  58. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  59. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  60. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  61. } rtcSignalingState;
  62. PeerConnection();
  63. PeerConnection(Configuration config);
  64. ~PeerConnection();
  65. void close();
  66. const Configuration *config() const;
  67. State state() const;
  68. GatheringState gatheringState() const;
  69. SignalingState signalingState() const;
  70. bool hasMedia() const;
  71. optional<Description> localDescription() const;
  72. optional<Description> remoteDescription() const;
  73. optional<string> localAddress() const;
  74. optional<string> remoteAddress() 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. shared_ptr<DataChannel> createDataChannel(string label, DataChannelInit init = {});
  80. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  81. 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. // Stats
  89. void clearStats();
  90. size_t bytesSent();
  91. size_t bytesReceived();
  92. optional<std::chrono::milliseconds> rtt();
  93. };
  94. } // namespace rtc
  95. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
  96. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  97. rtc::PeerConnection::GatheringState state);
  98. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  99. rtc::PeerConnection::SignalingState state);
  100. #endif