/** * 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 "init.hpp" #include "message.hpp" #include "reliability.hpp" #include "rtc.hpp" #include #include #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(); void close(); 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); bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote); // Stats void clearStats(); size_t bytesSent(); size_t bytesReceived(); std::optional rtt(); private: std::shared_ptr initIceTransport(Description::Role role); std::shared_ptr initDtlsTransport(); std::shared_ptr initSctpTransport(); void closeTransports(); void endLocalCandidates(); bool checkFingerprint(const std::string &fingerprint) const; void forwardMessage(message_ptr message); void forwardBufferedAmount(uint16_t stream, size_t amount); std::shared_ptr emplaceDataChannel(Description::Role role, const string &label, const string &protocol, const Reliability &reliability); std::shared_ptr findDataChannel(uint16_t stream); void iterateDataChannels(std::function channel)> func); void openDataChannels(); void closeDataChannels(); void remoteCloseDataChannels(); void processLocalDescription(Description description); void processLocalCandidate(Candidate candidate); void triggerDataChannel(std::weak_ptr weakDataChannel); bool changeState(State state); bool changeGatheringState(GatheringState state); void resetCallbacks(); const Configuration mConfig; const std::shared_ptr mCertificate; init_token mInitToken = Init::Token(); std::optional mLocalDescription, mRemoteDescription; mutable std::recursive_mutex mLocalDescriptionMutex, mRemoteDescriptionMutex; std::shared_ptr mIceTransport; std::shared_ptr mDtlsTransport; std::shared_ptr mSctpTransport; std::unordered_map> mDataChannels; std::shared_mutex mDataChannelsMutex; 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