peerconnection.hpp 4.2 KB

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