peerconnection.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <future>
  32. #include <list>
  33. #include <mutex>
  34. #include <shared_mutex>
  35. #include <thread>
  36. #include <unordered_map>
  37. namespace rtc {
  38. class Certificate;
  39. class IceTransport;
  40. class DtlsTransport;
  41. class SctpTransport;
  42. using certificate_ptr = std::shared_ptr<Certificate>;
  43. using future_certificate_ptr = std::shared_future<certificate_ptr>;
  44. class PeerConnection final : public std::enable_shared_from_this<PeerConnection> {
  45. public:
  46. enum class State : int {
  47. New = RTC_NEW,
  48. Connecting = RTC_CONNECTING,
  49. Connected = RTC_CONNECTED,
  50. Disconnected = RTC_DISCONNECTED,
  51. Failed = RTC_FAILED,
  52. Closed = RTC_CLOSED
  53. };
  54. enum class GatheringState : int {
  55. New = RTC_GATHERING_NEW,
  56. InProgress = RTC_GATHERING_INPROGRESS,
  57. Complete = RTC_GATHERING_COMPLETE
  58. };
  59. PeerConnection(void);
  60. PeerConnection(const Configuration &config);
  61. ~PeerConnection();
  62. void close();
  63. const Configuration *config() const;
  64. State state() const;
  65. GatheringState gatheringState() const;
  66. std::optional<Description> localDescription() const;
  67. std::optional<Description> remoteDescription() const;
  68. std::optional<string> localAddress() const;
  69. std::optional<string> remoteAddress() const;
  70. void setLocalDescription(std::optional<Description> description = nullopt);
  71. void setRemoteDescription(Description description);
  72. void addRemoteCandidate(Candidate candidate);
  73. std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
  74. const Reliability &reliability = {});
  75. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  76. void onLocalDescription(std::function<void(const Description &description)> callback);
  77. void onLocalCandidate(std::function<void(const Candidate &candidate)> callback);
  78. void onStateChange(std::function<void(State state)> callback);
  79. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  80. // Stats
  81. void clearStats();
  82. size_t bytesSent();
  83. size_t bytesReceived();
  84. std::optional<std::chrono::milliseconds> rtt();
  85. // Media
  86. bool hasMedia() const;
  87. void sendMedia(const binary &packet);
  88. void sendMedia(const byte *packet, size_t size);
  89. void onMedia(std::function<void(const binary &packet)> callback);
  90. // libnice only
  91. bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
  92. private:
  93. std::shared_ptr<IceTransport> initIceTransport(Description::Role role);
  94. std::shared_ptr<DtlsTransport> initDtlsTransport();
  95. std::shared_ptr<SctpTransport> initSctpTransport();
  96. void closeTransports();
  97. void endLocalCandidates();
  98. bool checkFingerprint(const std::string &fingerprint) const;
  99. void forwardMessage(message_ptr message);
  100. void forwardMedia(message_ptr message);
  101. void forwardBufferedAmount(uint16_t stream, size_t amount);
  102. std::shared_ptr<DataChannel> emplaceDataChannel(Description::Role role, const string &label,
  103. const string &protocol,
  104. const Reliability &reliability);
  105. std::shared_ptr<DataChannel> findDataChannel(uint16_t stream);
  106. void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
  107. void openDataChannels();
  108. void closeDataChannels();
  109. void remoteCloseDataChannels();
  110. void processLocalDescription(Description description);
  111. void processLocalCandidate(Candidate candidate);
  112. void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
  113. bool changeState(State state);
  114. bool changeGatheringState(GatheringState state);
  115. void resetCallbacks();
  116. void outgoingMedia(message_ptr message);
  117. const Configuration mConfig;
  118. const future_certificate_ptr mCertificate;
  119. init_token mInitToken = Init::Token();
  120. std::optional<Description> mLocalDescription, mRemoteDescription;
  121. mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
  122. std::shared_ptr<IceTransport> mIceTransport;
  123. std::shared_ptr<DtlsTransport> mDtlsTransport;
  124. std::shared_ptr<SctpTransport> mSctpTransport;
  125. std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels;
  126. std::shared_mutex mDataChannelsMutex;
  127. std::atomic<State> mState;
  128. std::atomic<GatheringState> mGatheringState;
  129. synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
  130. synchronized_callback<const Description &> mLocalDescriptionCallback;
  131. synchronized_callback<const Candidate &> mLocalCandidateCallback;
  132. synchronized_callback<State> mStateChangeCallback;
  133. synchronized_callback<GatheringState> mGatheringStateChangeCallback;
  134. synchronized_callback<const binary &> mMediaCallback;
  135. };
  136. } // namespace rtc
  137. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state);
  138. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::GatheringState &state);
  139. #endif