peerconnection.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(const Configuration &config);
  38. ~PeerConnection();
  39. const Configuration *config() const;
  40. std::optional<Description> localDescription() const;
  41. std::optional<Description> remoteDescription() const;
  42. void setRemoteDescription(Description description);
  43. void setRemoteCandidate(Candidate candidate);
  44. std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
  45. const Reliability &reliability = {});
  46. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  47. void onLocalDescription(std::function<void(const Description &description)> callback);
  48. void onLocalCandidate(std::function<void(const std::optional<Candidate> &candidate)> callback);
  49. private:
  50. void initIceTransport(Description::Role role);
  51. void initDtlsTransport();
  52. void initSctpTransport();
  53. bool checkFingerprint(const std::string &fingerprint) const;
  54. void forwardMessage(message_ptr message);
  55. void openDataChannels(void);
  56. void processLocalDescription(Description description);
  57. void processLocalCandidate(std::optional<Candidate> candidate);
  58. void triggerDataChannel(std::shared_ptr<DataChannel> dataChannel);
  59. const Configuration mConfig;
  60. const std::shared_ptr<Certificate> mCertificate;
  61. std::optional<Description> mLocalDescription;
  62. std::optional<Description> mRemoteDescription;
  63. std::shared_ptr<IceTransport> mIceTransport;
  64. std::shared_ptr<DtlsTransport> mDtlsTransport;
  65. std::shared_ptr<SctpTransport> mSctpTransport;
  66. std::unordered_map<unsigned int, std::shared_ptr<DataChannel>> mDataChannels;
  67. std::function<void(std::shared_ptr<DataChannel> dataChannel)> mDataChannelCallback;
  68. std::function<void(const Description &description)> mLocalDescriptionCallback;
  69. std::function<void(const std::optional<Candidate> &candidate)> mLocalCandidateCallback;
  70. };
  71. } // namespace rtc
  72. #endif