peerconnection.hpp 7.1 KB

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