/** * Copyright (c) 2019 Paul-Louis Ageneau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef RTC_PEER_CONNECTION_H #define RTC_PEER_CONNECTION_H #include "candidate.hpp" #include "configuration.hpp" #include "datachannel.hpp" #include "description.hpp" #include "include.hpp" #include "message.hpp" #include "reliability.hpp" #include "rtc.hpp" #include #include #include #include #include namespace rtc { class Certificate; class IceTransport; class DtlsTransport; class SctpTransport; class PeerConnection : public std::enable_shared_from_this { public: enum class State : int { New = RTC_NEW, Connecting = RTC_CONNECTING, Connected = RTC_CONNECTED, Disconnected = RTC_DISCONNECTED, Failed = RTC_FAILED, Closed = RTC_CLOSED }; enum class GatheringState : int { New = RTC_GATHERING_NEW, InProgress = RTC_GATHERING_INPROGRESS, Complete = RTC_GATHERING_COMPLETE, }; PeerConnection(void); PeerConnection(const Configuration &config); ~PeerConnection(); const Configuration *config() const; State state() const; GatheringState gatheringState() const; std::optional localDescription() const; std::optional remoteDescription() const; std::optional localAddress() const; std::optional remoteAddress() const; void setRemoteDescription(Description description); void addRemoteCandidate(Candidate candidate); std::shared_ptr createDataChannel(const string &label, const string &protocol = "", const Reliability &reliability = {}); void onDataChannel(std::function dataChannel)> callback); void onLocalDescription(std::function callback); void onLocalCandidate(std::function callback); void onStateChange(std::function callback); void onGatheringStateChange(std::function callback); private: void initIceTransport(Description::Role role); void initDtlsTransport(); void initSctpTransport(); bool checkFingerprint(std::weak_ptr weak_this, const std::string &fingerprint) const; void forwardMessage(std::weak_ptr weak_this, message_ptr message); void forwardSent(std::weak_ptr weak_this, uint16_t stream); void iterateDataChannels(std::function channel)> func); void openDataChannels(); void closeDataChannels(); void processLocalDescription(Description description); void processLocalCandidate(std::weak_ptr weak_this, Candidate candidate); void triggerDataChannel(std::weak_ptr weak_this, std::weak_ptr weakDataChannel); void changeState(State state); void changeGatheringState(GatheringState state); const Configuration mConfig; const std::shared_ptr mCertificate; std::optional mLocalDescription; std::optional mRemoteDescription; std::shared_ptr mIceTransport; std::shared_ptr mDtlsTransport; std::shared_ptr mSctpTransport; std::unordered_map> mDataChannels; std::atomic mState; std::atomic mGatheringState; synchronized_callback> mDataChannelCallback; synchronized_callback mLocalDescriptionCallback; synchronized_callback mLocalCandidateCallback; synchronized_callback mStateChangeCallback; synchronized_callback mGatheringStateChangeCallback; }; } // namespace rtc std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state); std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::GatheringState &state); #endif