#pragma once #include #include "AudioTrackSinkAdapter.h" #include "DummyAudioDevice.h" #include "GraphicsDevice/IGraphicsDevice.h" #include "PeerConnectionObject.h" #include "UnityVideoRenderer.h" #include "UnityVideoTrackSource.h" namespace unity { namespace webrtc { using namespace ::webrtc; const std::map statsTypes = { { "codec", 0 }, { "inbound-rtp", 1 }, { "outbound-rtp", 2 }, { "remote-inbound-rtp", 3 }, { "remote-outbound-rtp", 4 }, { "media-source", 5 }, { "csrc", 6 }, { "peer-connection", 7 }, { "data-channel", 8 }, { "stream", 9 }, { "track", 10 }, { "transceiver", 11 }, { "sender", 12 }, { "receiver", 13 }, { "transport", 14 }, { "sctp-transport", 15 }, { "candidate-pair", 16 }, { "local-candidate", 17 }, { "remote-candidate", 18 }, { "certificate", 19 }, { "ice-server", 20 } }; class IGraphicsDevice; class ProfilerMarkerFactory; struct ContextDependencies { IGraphicsDevice* device; ProfilerMarkerFactory* profiler; }; class Context; class MediaStreamObserver; class SetSessionDescriptionObserver; class ContextManager { public: static ContextManager* GetInstance(); ~ContextManager(); Context* GetContext(int uid) const; Context* CreateContext(int uid, ContextDependencies& dependencies); void DestroyContext(int uid); void SetCurContext(Context*); bool Exists(Context* context); using ContextPtr = std::unique_ptr; Context* curContext = nullptr; std::mutex mutex; private: std::map m_contexts; static std::unique_ptr s_instance; }; class Context { public: explicit Context(ContextDependencies& dependencies); ~Context(); bool ExistsRefPtr(const rtc::RefCountInterface* ptr) const { return m_mapRefPtr.find(ptr) != m_mapRefPtr.end(); } template void AddRefPtr(rtc::scoped_refptr refptr) { m_mapRefPtr.emplace(refptr.get(), refptr); } void AddRefPtr(rtc::RefCountInterface* ptr) { m_mapRefPtr.emplace(ptr, ptr); } template void RemoveRefPtr(rtc::scoped_refptr& refptr) { std::lock_guard lock(mutex); m_mapRefPtr.erase(refptr.get()); } template void RemoveRefPtr(T* ptr) { std::lock_guard lock(mutex); m_mapRefPtr.erase(ptr); } // MediaStream webrtc::MediaStreamInterface* CreateMediaStream(const std::string& streamId); void RegisterMediaStreamObserver(webrtc::MediaStreamInterface* stream); void UnRegisterMediaStreamObserver(webrtc::MediaStreamInterface* stream); MediaStreamObserver* GetObserver(const webrtc::MediaStreamInterface* stream); // Audio Source webrtc::AudioSourceInterface* CreateAudioSource(); // Audio Renderer AudioTrackSinkAdapter* CreateAudioTrackSinkAdapter(); void DeleteAudioTrackSinkAdapter(AudioTrackSinkAdapter* sink); // Video Source webrtc::VideoTrackSourceInterface* CreateVideoSource(); // MediaStreamTrack webrtc::VideoTrackInterface* CreateVideoTrack(const std::string& label, webrtc::VideoTrackSourceInterface* source); webrtc::AudioTrackInterface* CreateAudioTrack(const std::string& label, webrtc::AudioSourceInterface* source); void StopMediaStreamTrack(webrtc::MediaStreamTrackInterface* track); // PeerConnection PeerConnectionObject* CreatePeerConnection(const webrtc::PeerConnectionInterface::RTCConfiguration& config); void DeletePeerConnection(PeerConnectionObject* obj); // StatsReport std::mutex mutexStatsReport; void AddStatsReport(const rtc::scoped_refptr& report); const RTCStats** GetStatsList(const RTCStatsReport* report, size_t* length, uint32_t** types); void DeleteStatsReport(const webrtc::RTCStatsReport* report); // DataChannel DataChannelInterface* CreateDataChannel(PeerConnectionObject* obj, const char* label, const DataChannelInit& options); void AddDataChannel(DataChannelInterface* channel, PeerConnectionObject& pc); DataChannelObject* GetDataChannelObject(const DataChannelInterface* channel); void DeleteDataChannel(DataChannelInterface* channel); // Renderer UnityVideoRenderer* CreateVideoRenderer(DelegateVideoFrameResize callback, bool needFlipVertical); std::shared_ptr GetVideoRenderer(uint32_t id); void DeleteVideoRenderer(UnityVideoRenderer* renderer); // RtpSender void GetRtpSenderCapabilities(cricket::MediaType kind, RtpCapabilities* capabilities) const; // RtpReceiver void GetRtpReceiverCapabilities(cricket::MediaType kind, RtpCapabilities* capabilities) const; // AudioDevice rtc::scoped_refptr GetAudioDevice() const { return m_audioDevice; } // mutex; std::mutex mutex; private: std::unique_ptr m_workerThread; std::unique_ptr m_signalingThread; std::unique_ptr m_taskQueueFactory; rtc::scoped_refptr m_peerConnectionFactory; rtc::scoped_refptr m_audioDevice; std::vector> m_listStatsReport; std::map> m_mapClients; std::map> m_mapMediaStreamObserver; std::map> m_mapDataChannels; std::map> m_mapVideoRenderer; std::map> m_mapAudioTrackAndSink; std::map> m_mapRefPtr; static uint32_t s_rendererId; static uint32_t GenerateRendererId(); }; extern bool Convert(const std::string& str, webrtc::PeerConnectionInterface::RTCConfiguration& config); } // end namespace webrtc } // end namespace unity