123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #pragma once
- #include <mutex>
- #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<std::string, uint32_t> 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>;
- Context* curContext = nullptr;
- std::mutex mutex;
- private:
- std::map<int, ContextPtr> m_contexts;
- static std::unique_ptr<ContextManager> 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<typename T>
- void AddRefPtr(rtc::scoped_refptr<T> refptr)
- {
- m_mapRefPtr.emplace(refptr.get(), refptr);
- }
- void AddRefPtr(rtc::RefCountInterface* ptr) { m_mapRefPtr.emplace(ptr, ptr); }
- template<typename T>
- void RemoveRefPtr(rtc::scoped_refptr<T>& refptr)
- {
- std::lock_guard<std::mutex> lock(mutex);
- m_mapRefPtr.erase(refptr.get());
- }
- template<typename T>
- void RemoveRefPtr(T* ptr)
- {
- std::lock_guard<std::mutex> 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<const webrtc::RTCStatsReport>& 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<UnityVideoRenderer> 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<DummyAudioDevice> GetAudioDevice() const { return m_audioDevice; }
- // mutex;
- std::mutex mutex;
- private:
- std::unique_ptr<rtc::Thread> m_workerThread;
- std::unique_ptr<rtc::Thread> m_signalingThread;
- std::unique_ptr<TaskQueueFactory> m_taskQueueFactory;
- rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> m_peerConnectionFactory;
- rtc::scoped_refptr<DummyAudioDevice> m_audioDevice;
- std::vector<rtc::scoped_refptr<const webrtc::RTCStatsReport>> m_listStatsReport;
- std::map<const PeerConnectionObject*, std::unique_ptr<PeerConnectionObject>> m_mapClients;
- std::map<const webrtc::MediaStreamInterface*, std::unique_ptr<MediaStreamObserver>> m_mapMediaStreamObserver;
- std::map<const DataChannelInterface*, std::unique_ptr<DataChannelObject>> m_mapDataChannels;
- std::map<const uint32_t, std::shared_ptr<UnityVideoRenderer>> m_mapVideoRenderer;
- std::map<const AudioTrackSinkAdapter*, std::unique_ptr<AudioTrackSinkAdapter>> m_mapAudioTrackAndSink;
- std::map<const rtc::RefCountInterface*, rtc::scoped_refptr<rtc::RefCountInterface>> 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
|