peerconnection.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "include.hpp"
  25. #include "init.hpp"
  26. #include "message.hpp"
  27. #include "reliability.hpp"
  28. #include "rtc.hpp"
  29. #include <atomic>
  30. #include <functional>
  31. #include <list>
  32. #include <mutex>
  33. #include <shared_mutex>
  34. #include <thread>
  35. #include <unordered_map>
  36. namespace rtc {
  37. class Certificate;
  38. class IceTransport;
  39. class DtlsTransport;
  40. class SctpTransport;
  41. class PeerConnection : public std::enable_shared_from_this<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. PeerConnection(void);
  57. PeerConnection(const Configuration &config);
  58. ~PeerConnection();
  59. void close();
  60. const Configuration *config() const;
  61. State state() const;
  62. GatheringState gatheringState() const;
  63. std::optional<Description> localDescription() const;
  64. std::optional<Description> remoteDescription() const;
  65. std::optional<string> localAddress() const;
  66. std::optional<string> remoteAddress() const;
  67. void setRemoteDescription(Description description);
  68. void addRemoteCandidate(Candidate candidate);
  69. std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
  70. const Reliability &reliability = {});
  71. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  72. void onLocalDescription(std::function<void(const Description &description)> callback);
  73. void onLocalCandidate(std::function<void(const Candidate &candidate)> callback);
  74. void onStateChange(std::function<void(State state)> callback);
  75. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  76. bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
  77. // Stats
  78. void clearStats();
  79. size_t bytesSent();
  80. size_t bytesReceived();
  81. std::optional<std::chrono::milliseconds> rtt();
  82. private:
  83. std::shared_ptr<IceTransport> initIceTransport(Description::Role role);
  84. std::shared_ptr<DtlsTransport> initDtlsTransport();
  85. std::shared_ptr<SctpTransport> initSctpTransport();
  86. void closeTransports();
  87. void endLocalCandidates();
  88. bool checkFingerprint(const std::string &fingerprint) const;
  89. void forwardMessage(message_ptr message);
  90. void forwardBufferedAmount(uint16_t stream, size_t amount);
  91. std::shared_ptr<DataChannel> emplaceDataChannel(Description::Role role, const string &label,
  92. const string &protocol,
  93. const Reliability &reliability);
  94. std::shared_ptr<DataChannel> findDataChannel(uint16_t stream);
  95. void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
  96. void openDataChannels();
  97. void closeDataChannels();
  98. void remoteCloseDataChannels();
  99. void processLocalDescription(Description description);
  100. void processLocalCandidate(Candidate candidate);
  101. void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
  102. bool changeState(State state);
  103. bool changeGatheringState(GatheringState state);
  104. void resetCallbacks();
  105. const Configuration mConfig;
  106. const std::shared_ptr<Certificate> mCertificate;
  107. init_token mInitToken = Init::Token();
  108. std::optional<Description> mLocalDescription, mRemoteDescription;
  109. mutable std::recursive_mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
  110. std::shared_ptr<IceTransport> mIceTransport;
  111. std::shared_ptr<DtlsTransport> mDtlsTransport;
  112. std::shared_ptr<SctpTransport> mSctpTransport;
  113. std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels;
  114. std::shared_mutex mDataChannelsMutex;
  115. std::atomic<State> mState;
  116. std::atomic<GatheringState> mGatheringState;
  117. synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
  118. synchronized_callback<const Description &> mLocalDescriptionCallback;
  119. synchronized_callback<const Candidate &> mLocalCandidateCallback;
  120. synchronized_callback<State> mStateChangeCallback;
  121. synchronized_callback<GatheringState> mGatheringStateChangeCallback;
  122. };
  123. } // namespace rtc
  124. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state);
  125. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::GatheringState &state);
  126. #endif