peerconnection.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "datachannel.hpp"
  22. #include "description.hpp"
  23. #include "configuration.hpp"
  24. #include "include.hpp"
  25. #include "message.hpp"
  26. #include "reliability.hpp"
  27. #include <atomic>
  28. #include <functional>
  29. #include <unordered_map>
  30. namespace rtc {
  31. class Certificate;
  32. class IceTransport;
  33. class DtlsTransport;
  34. class SctpTransport;
  35. class PeerConnection {
  36. public:
  37. PeerConnection(void);
  38. PeerConnection(const Configuration &config);
  39. ~PeerConnection();
  40. const Configuration *config() const;
  41. std::optional<Description> localDescription() const;
  42. std::optional<Description> remoteDescription() const;
  43. void setRemoteDescription(Description description);
  44. void addRemoteCandidate(Candidate candidate);
  45. std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
  46. const Reliability &reliability = {});
  47. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  48. void onLocalDescription(std::function<void(const Description &description)> callback);
  49. void onLocalCandidate(std::function<void(const std::optional<Candidate> &candidate)> callback);
  50. private:
  51. void initIceTransport(Description::Role role);
  52. void initDtlsTransport();
  53. void initSctpTransport();
  54. bool checkFingerprint(const std::string &fingerprint) const;
  55. void forwardMessage(message_ptr message);
  56. void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
  57. void openDataChannels();
  58. void closeDataChannels();
  59. void processLocalDescription(Description description);
  60. void processLocalCandidate(std::optional<Candidate> candidate);
  61. void triggerDataChannel(std::shared_ptr<DataChannel> dataChannel);
  62. const Configuration mConfig;
  63. const std::shared_ptr<Certificate> mCertificate;
  64. std::optional<Description> mLocalDescription;
  65. std::optional<Description> mRemoteDescription;
  66. std::shared_ptr<IceTransport> mIceTransport;
  67. std::shared_ptr<DtlsTransport> mDtlsTransport;
  68. std::shared_ptr<SctpTransport> mSctpTransport;
  69. std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels;
  70. std::function<void(std::shared_ptr<DataChannel> dataChannel)> mDataChannelCallback;
  71. std::function<void(const Description &description)> mLocalDescriptionCallback;
  72. std::function<void(const std::optional<Candidate> &candidate)> mLocalCandidateCallback;
  73. };
  74. } // namespace rtc
  75. #endif