peerconnection.hpp 6.3 KB

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