peerconnection.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. struct DataChannelInit {
  47. Reliability reliability = {};
  48. bool negotiated = false;
  49. std::optional<uint16_t> id = nullopt;
  50. string protocol = "";
  51. };
  52. class PeerConnection final : public std::enable_shared_from_this<PeerConnection> {
  53. public:
  54. enum class State : int {
  55. New = RTC_NEW,
  56. Connecting = RTC_CONNECTING,
  57. Connected = RTC_CONNECTED,
  58. Disconnected = RTC_DISCONNECTED,
  59. Failed = RTC_FAILED,
  60. Closed = RTC_CLOSED
  61. };
  62. enum class GatheringState : int {
  63. New = RTC_GATHERING_NEW,
  64. InProgress = RTC_GATHERING_INPROGRESS,
  65. Complete = RTC_GATHERING_COMPLETE
  66. };
  67. enum class SignalingState : int {
  68. Stable = RTC_SIGNALING_STABLE,
  69. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  70. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  71. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  72. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  73. } rtcSignalingState;
  74. PeerConnection();
  75. PeerConnection(const Configuration &config);
  76. ~PeerConnection();
  77. void close();
  78. const Configuration *config() const;
  79. State state() const;
  80. GatheringState gatheringState() const;
  81. SignalingState signalingState() const;
  82. bool hasLocalDescription() const;
  83. bool hasRemoteDescription() const;
  84. bool hasMedia() const;
  85. std::optional<Description> localDescription() const;
  86. std::optional<Description> remoteDescription() const;
  87. std::optional<string> localAddress() const;
  88. std::optional<string> remoteAddress() const;
  89. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  90. void setLocalDescription(Description::Type type = Description::Type::Unspec);
  91. void setRemoteDescription(Description description);
  92. void addRemoteCandidate(Candidate candidate);
  93. std::shared_ptr<DataChannel> addDataChannel(string label, DataChannelInit init = {});
  94. // Equivalent to calling addDataChannel() and setLocalDescription()
  95. std::shared_ptr<DataChannel> createDataChannel(string label, DataChannelInit init = {});
  96. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  97. void onLocalDescription(std::function<void(Description description)> callback);
  98. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  99. void onStateChange(std::function<void(State state)> callback);
  100. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  101. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  102. // Stats
  103. void clearStats();
  104. size_t bytesSent();
  105. size_t bytesReceived();
  106. std::optional<std::chrono::milliseconds> rtt();
  107. // Track media support requires compiling with libSRTP
  108. std::shared_ptr<Track> addTrack(Description::Media description);
  109. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  110. private:
  111. std::shared_ptr<IceTransport> initIceTransport(Description::Role role);
  112. std::shared_ptr<DtlsTransport> initDtlsTransport();
  113. std::shared_ptr<SctpTransport> initSctpTransport();
  114. void closeTransports();
  115. void endLocalCandidates();
  116. bool checkFingerprint(const std::string &fingerprint) const;
  117. void forwardMessage(message_ptr message);
  118. void forwardMedia(message_ptr message);
  119. void forwardBufferedAmount(uint16_t stream, size_t amount);
  120. std::optional<std::string> getMidFromSsrc(uint32_t ssrc);
  121. std::shared_ptr<DataChannel> emplaceDataChannel(Description::Role role, string label,
  122. DataChannelInit init);
  123. std::shared_ptr<DataChannel> findDataChannel(uint16_t stream);
  124. void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
  125. void openDataChannels();
  126. void closeDataChannels();
  127. void remoteCloseDataChannels();
  128. void incomingTrack(Description::Media description);
  129. void openTracks();
  130. void validateRemoteDescription(const Description &description);
  131. void processLocalDescription(Description description);
  132. void processLocalCandidate(Candidate candidate);
  133. void processRemoteDescription(Description description);
  134. void processRemoteCandidate(Candidate candidate);
  135. void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
  136. void triggerTrack(std::shared_ptr<Track> track);
  137. bool changeState(State state);
  138. bool changeGatheringState(GatheringState state);
  139. bool changeSignalingState(SignalingState state);
  140. void resetCallbacks();
  141. void outgoingMedia(message_ptr message);
  142. const init_token mInitToken = Init::Token();
  143. const Configuration mConfig;
  144. const future_certificate_ptr mCertificate;
  145. const std::unique_ptr<Processor> mProcessor;
  146. std::optional<Description> mLocalDescription, mRemoteDescription;
  147. std::optional<Description> mCurrentLocalDescription;
  148. mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
  149. std::shared_ptr<IceTransport> mIceTransport;
  150. std::shared_ptr<DtlsTransport> mDtlsTransport;
  151. std::shared_ptr<SctpTransport> mSctpTransport;
  152. std::unordered_map<uint16_t, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID
  153. std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid
  154. std::vector<std::weak_ptr<Track>> mTrackLines; // by SDP order
  155. std::shared_mutex mDataChannelsMutex, mTracksMutex;
  156. std::unordered_map<uint32_t, string> mMidFromSsrc; // cache
  157. std::atomic<State> mState;
  158. std::atomic<GatheringState> mGatheringState;
  159. std::atomic<SignalingState> mSignalingState;
  160. std::atomic<bool> mNegotiationNeeded;
  161. synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
  162. synchronized_callback<Description> mLocalDescriptionCallback;
  163. synchronized_callback<Candidate> mLocalCandidateCallback;
  164. synchronized_callback<State> mStateChangeCallback;
  165. synchronized_callback<GatheringState> mGatheringStateChangeCallback;
  166. synchronized_callback<SignalingState> mSignalingStateChangeCallback;
  167. synchronized_callback<std::shared_ptr<Track>> mTrackCallback;
  168. };
  169. } // namespace rtc
  170. std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
  171. std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::GatheringState state);
  172. std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::SignalingState state);
  173. #endif