peerconnection.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "message.hpp"
  26. #include "reliability.hpp"
  27. #include "rtc.hpp"
  28. #include <atomic>
  29. #include <functional>
  30. #include <list>
  31. #include <mutex>
  32. #include <thread>
  33. #include <unordered_map>
  34. namespace rtc {
  35. class Certificate;
  36. class IceTransport;
  37. class DtlsTransport;
  38. class SctpTransport;
  39. class PeerConnection : public std::enable_shared_from_this<PeerConnection> {
  40. public:
  41. enum class State : int {
  42. New = RTC_NEW,
  43. Connecting = RTC_CONNECTING,
  44. Connected = RTC_CONNECTED,
  45. Disconnected = RTC_DISCONNECTED,
  46. Failed = RTC_FAILED,
  47. Closed = RTC_CLOSED,
  48. Destroying = RTC_DESTROYING
  49. };
  50. enum class GatheringState : int {
  51. New = RTC_GATHERING_NEW,
  52. InProgress = RTC_GATHERING_INPROGRESS,
  53. Complete = RTC_GATHERING_COMPLETE,
  54. };
  55. PeerConnection(void);
  56. PeerConnection(const Configuration &config);
  57. ~PeerConnection();
  58. void close();
  59. const Configuration *config() const;
  60. State state() const;
  61. GatheringState gatheringState() const;
  62. std::optional<Description> localDescription() const;
  63. std::optional<Description> remoteDescription() const;
  64. std::optional<string> localAddress() const;
  65. std::optional<string> remoteAddress() const;
  66. void setRemoteDescription(Description description);
  67. void addRemoteCandidate(Candidate candidate);
  68. std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
  69. const Reliability &reliability = {});
  70. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  71. void onLocalDescription(std::function<void(const Description &description)> callback);
  72. void onLocalCandidate(std::function<void(const Candidate &candidate)> callback);
  73. void onStateChange(std::function<void(State state)> callback);
  74. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  75. private:
  76. std::shared_ptr<IceTransport> initIceTransport(Description::Role role);
  77. std::shared_ptr<DtlsTransport> initDtlsTransport();
  78. std::shared_ptr<SctpTransport> initSctpTransport();
  79. void endLocalCandidates();
  80. bool checkFingerprint(const std::string &fingerprint) const;
  81. void forwardMessage(message_ptr message);
  82. void forwardBufferedAmount(uint16_t stream, size_t amount);
  83. void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
  84. void openDataChannels();
  85. void closeDataChannels();
  86. void remoteCloseDataChannels();
  87. void processLocalDescription(Description description);
  88. void processLocalCandidate(Candidate candidate);
  89. void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
  90. void changeState(State state);
  91. void changeGatheringState(GatheringState state);
  92. const Configuration mConfig;
  93. const std::shared_ptr<Certificate> mCertificate;
  94. std::optional<Description> mLocalDescription, mRemoteDescription;
  95. mutable std::recursive_mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
  96. std::shared_ptr<IceTransport> mIceTransport;
  97. std::shared_ptr<DtlsTransport> mDtlsTransport;
  98. std::shared_ptr<SctpTransport> mSctpTransport;
  99. std::recursive_mutex mInitMutex;
  100. std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels;
  101. std::atomic<State> mState;
  102. std::atomic<GatheringState> mGatheringState;
  103. synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
  104. synchronized_callback<const Description &> mLocalDescriptionCallback;
  105. synchronized_callback<const Candidate &> mLocalCandidateCallback;
  106. synchronized_callback<State> mStateChangeCallback;
  107. synchronized_callback<GatheringState> mGatheringStateChangeCallback;
  108. };
  109. } // namespace rtc
  110. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state);
  111. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::GatheringState &state);
  112. #endif