peerconnection.hpp 6.4 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. std::optional<Description> localDescription() const;
  69. std::optional<Description> remoteDescription() const;
  70. std::optional<string> localAddress() const;
  71. std::optional<string> remoteAddress() const;
  72. bool hasMedia() const;
  73. void setLocalDescription();
  74. void processLocalDescription(Description description);
  75. void setRemoteDescription(Description description);
  76. void addRemoteCandidate(Candidate candidate);
  77. std::shared_ptr<DataChannel> addDataChannel(string label, string protocol = "",
  78. Reliability reliability = {});
  79. // Equivalent to calling addDataChannel() and setLocalDescription()
  80. std::shared_ptr<DataChannel> createDataChannel(string label, string protocol = "",
  81. Reliability reliability = {});
  82. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> 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. // Stats
  88. void clearStats();
  89. size_t bytesSent();
  90. size_t bytesReceived();
  91. std::optional<std::chrono::milliseconds> rtt();
  92. // Track media support requires compiling with libSRTP
  93. std::shared_ptr<Track> addTrack(Description::Media description);
  94. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  95. // libnice only
  96. bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
  97. private:
  98. std::shared_ptr<IceTransport> initIceTransport(Description::Role role);
  99. std::shared_ptr<DtlsTransport> initDtlsTransport();
  100. std::shared_ptr<SctpTransport> initSctpTransport();
  101. void closeTransports();
  102. void endLocalCandidates();
  103. bool checkFingerprint(const std::string &fingerprint) const;
  104. void forwardMessage(message_ptr message);
  105. void forwardMedia(message_ptr message);
  106. void forwardBufferedAmount(uint16_t stream, size_t amount);
  107. std::optional<std::string> getMidFromSSRC(SSRC ssrc);
  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 processLocalCandidate(Candidate candidate);
  118. void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
  119. void triggerTrack(std::shared_ptr<Track> track);
  120. bool changeState(State state);
  121. bool changeGatheringState(GatheringState state);
  122. void resetCallbacks();
  123. void outgoingMedia(message_ptr message);
  124. const init_token mInitToken = Init::Token();
  125. const Configuration mConfig;
  126. const future_certificate_ptr mCertificate;
  127. const std::unique_ptr<Processor> mProcessor;
  128. std::optional<Description> mLocalDescription, mRemoteDescription;
  129. mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
  130. std::shared_ptr<IceTransport> mIceTransport;
  131. std::shared_ptr<DtlsTransport> mDtlsTransport;
  132. std::shared_ptr<SctpTransport> mSctpTransport;
  133. std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID
  134. std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid
  135. std::vector<std::weak_ptr<Track>> mTrackLines; // by SDP order
  136. std::shared_mutex mDataChannelsMutex, mTracksMutex;
  137. std::unordered_map<unsigned int, string> mMidFromSssrc; // 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