peerconnection.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_PEER_CONNECTION_H
  9. #define RTC_PEER_CONNECTION_H
  10. #include "candidate.hpp"
  11. #include "common.hpp"
  12. #include "configuration.hpp"
  13. #include "datachannel.hpp"
  14. #include "description.hpp"
  15. #include "reliability.hpp"
  16. #include "track.hpp"
  17. #include <chrono>
  18. #include <functional>
  19. namespace rtc {
  20. namespace impl {
  21. struct PeerConnection;
  22. }
  23. struct RTC_CPP_EXPORT DataChannelInit {
  24. Reliability reliability = {};
  25. bool negotiated = false;
  26. optional<uint16_t> id = nullopt;
  27. string protocol = "";
  28. };
  29. class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
  30. public:
  31. enum class State : int {
  32. New = RTC_NEW,
  33. Connecting = RTC_CONNECTING,
  34. Connected = RTC_CONNECTED,
  35. Disconnected = RTC_DISCONNECTED,
  36. Failed = RTC_FAILED,
  37. Closed = RTC_CLOSED
  38. };
  39. enum class GatheringState : int {
  40. New = RTC_GATHERING_NEW,
  41. InProgress = RTC_GATHERING_INPROGRESS,
  42. Complete = RTC_GATHERING_COMPLETE
  43. };
  44. enum class SignalingState : int {
  45. Stable = RTC_SIGNALING_STABLE,
  46. HaveLocalOffer = RTC_SIGNALING_HAVE_LOCAL_OFFER,
  47. HaveRemoteOffer = RTC_SIGNALING_HAVE_REMOTE_OFFER,
  48. HaveLocalPranswer = RTC_SIGNALING_HAVE_LOCAL_PRANSWER,
  49. HaveRemotePranswer = RTC_SIGNALING_HAVE_REMOTE_PRANSWER,
  50. };
  51. PeerConnection();
  52. PeerConnection(Configuration config);
  53. ~PeerConnection();
  54. void close();
  55. const Configuration *config() const;
  56. State state() const;
  57. GatheringState gatheringState() const;
  58. SignalingState signalingState() const;
  59. bool hasMedia() const;
  60. optional<Description> localDescription() const;
  61. optional<Description> remoteDescription() const;
  62. optional<string> localAddress() const;
  63. optional<string> remoteAddress() const;
  64. uint16_t maxDataChannelId() const;
  65. bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
  66. void setLocalDescription(Description::Type type = Description::Type::Unspec);
  67. void setRemoteDescription(Description description);
  68. void addRemoteCandidate(Candidate candidate);
  69. [[nodiscard]] shared_ptr<DataChannel> createDataChannel(string label, DataChannelInit init = {});
  70. void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
  71. [[nodiscard]] shared_ptr<Track> addTrack(Description::Media description);
  72. void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
  73. void onLocalDescription(std::function<void(Description description)> callback);
  74. void onLocalCandidate(std::function<void(Candidate candidate)> callback);
  75. void onStateChange(std::function<void(State state)> callback);
  76. void onGatheringStateChange(std::function<void(GatheringState state)> callback);
  77. void onSignalingStateChange(std::function<void(SignalingState state)> callback);
  78. void resetCallbacks();
  79. // Stats
  80. void clearStats();
  81. size_t bytesSent();
  82. size_t bytesReceived();
  83. optional<std::chrono::milliseconds> rtt();
  84. };
  85. } // namespace rtc
  86. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
  87. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  88. rtc::PeerConnection::GatheringState state);
  89. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
  90. rtc::PeerConnection::SignalingState state);
  91. #endif