peerconnection.hpp 5.9 KB

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