Context.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #include <mutex>
  3. #include "AudioTrackSinkAdapter.h"
  4. #include "DummyAudioDevice.h"
  5. #include "GraphicsDevice/IGraphicsDevice.h"
  6. #include "PeerConnectionObject.h"
  7. #include "UnityVideoRenderer.h"
  8. #include "UnityVideoTrackSource.h"
  9. namespace unity
  10. {
  11. namespace webrtc
  12. {
  13. using namespace ::webrtc;
  14. const std::map<std::string, uint32_t> statsTypes = { { "codec", 0 },
  15. { "inbound-rtp", 1 },
  16. { "outbound-rtp", 2 },
  17. { "remote-inbound-rtp", 3 },
  18. { "remote-outbound-rtp", 4 },
  19. { "media-source", 5 },
  20. { "csrc", 6 },
  21. { "peer-connection", 7 },
  22. { "data-channel", 8 },
  23. { "stream", 9 },
  24. { "track", 10 },
  25. { "transceiver", 11 },
  26. { "sender", 12 },
  27. { "receiver", 13 },
  28. { "transport", 14 },
  29. { "sctp-transport", 15 },
  30. { "candidate-pair", 16 },
  31. { "local-candidate", 17 },
  32. { "remote-candidate", 18 },
  33. { "certificate", 19 },
  34. { "ice-server", 20 } };
  35. class IGraphicsDevice;
  36. class ProfilerMarkerFactory;
  37. struct ContextDependencies
  38. {
  39. IGraphicsDevice* device;
  40. ProfilerMarkerFactory* profiler;
  41. };
  42. class Context;
  43. class MediaStreamObserver;
  44. class SetSessionDescriptionObserver;
  45. class ContextManager
  46. {
  47. public:
  48. static ContextManager* GetInstance();
  49. ~ContextManager();
  50. Context* GetContext(int uid) const;
  51. Context* CreateContext(int uid, ContextDependencies& dependencies);
  52. void DestroyContext(int uid);
  53. void SetCurContext(Context*);
  54. bool Exists(Context* context);
  55. using ContextPtr = std::unique_ptr<Context>;
  56. Context* curContext = nullptr;
  57. std::mutex mutex;
  58. private:
  59. std::map<int, ContextPtr> m_contexts;
  60. static std::unique_ptr<ContextManager> s_instance;
  61. };
  62. class Context
  63. {
  64. public:
  65. explicit Context(ContextDependencies& dependencies);
  66. ~Context();
  67. bool ExistsRefPtr(const rtc::RefCountInterface* ptr) const
  68. {
  69. return m_mapRefPtr.find(ptr) != m_mapRefPtr.end();
  70. }
  71. template<typename T>
  72. void AddRefPtr(rtc::scoped_refptr<T> refptr)
  73. {
  74. m_mapRefPtr.emplace(refptr.get(), refptr);
  75. }
  76. void AddRefPtr(rtc::RefCountInterface* ptr) { m_mapRefPtr.emplace(ptr, ptr); }
  77. template<typename T>
  78. void RemoveRefPtr(rtc::scoped_refptr<T>& refptr)
  79. {
  80. std::lock_guard<std::mutex> lock(mutex);
  81. m_mapRefPtr.erase(refptr.get());
  82. }
  83. template<typename T>
  84. void RemoveRefPtr(T* ptr)
  85. {
  86. std::lock_guard<std::mutex> lock(mutex);
  87. m_mapRefPtr.erase(ptr);
  88. }
  89. // MediaStream
  90. webrtc::MediaStreamInterface* CreateMediaStream(const std::string& streamId);
  91. void RegisterMediaStreamObserver(webrtc::MediaStreamInterface* stream);
  92. void UnRegisterMediaStreamObserver(webrtc::MediaStreamInterface* stream);
  93. MediaStreamObserver* GetObserver(const webrtc::MediaStreamInterface* stream);
  94. // Audio Source
  95. webrtc::AudioSourceInterface* CreateAudioSource();
  96. // Audio Renderer
  97. AudioTrackSinkAdapter* CreateAudioTrackSinkAdapter();
  98. void DeleteAudioTrackSinkAdapter(AudioTrackSinkAdapter* sink);
  99. // Video Source
  100. webrtc::VideoTrackSourceInterface* CreateVideoSource();
  101. // MediaStreamTrack
  102. webrtc::VideoTrackInterface*
  103. CreateVideoTrack(const std::string& label, webrtc::VideoTrackSourceInterface* source);
  104. webrtc::AudioTrackInterface* CreateAudioTrack(const std::string& label, webrtc::AudioSourceInterface* source);
  105. void StopMediaStreamTrack(webrtc::MediaStreamTrackInterface* track);
  106. // PeerConnection
  107. PeerConnectionObject* CreatePeerConnection(const webrtc::PeerConnectionInterface::RTCConfiguration& config);
  108. void DeletePeerConnection(PeerConnectionObject* obj);
  109. // StatsReport
  110. std::mutex mutexStatsReport;
  111. void AddStatsReport(const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report);
  112. const RTCStats** GetStatsList(const RTCStatsReport* report, size_t* length, uint32_t** types);
  113. void DeleteStatsReport(const webrtc::RTCStatsReport* report);
  114. // DataChannel
  115. DataChannelInterface*
  116. CreateDataChannel(PeerConnectionObject* obj, const char* label, const DataChannelInit& options);
  117. void AddDataChannel(DataChannelInterface* channel, PeerConnectionObject& pc);
  118. DataChannelObject* GetDataChannelObject(const DataChannelInterface* channel);
  119. void DeleteDataChannel(DataChannelInterface* channel);
  120. // Renderer
  121. UnityVideoRenderer* CreateVideoRenderer(DelegateVideoFrameResize callback, bool needFlipVertical);
  122. std::shared_ptr<UnityVideoRenderer> GetVideoRenderer(uint32_t id);
  123. void DeleteVideoRenderer(UnityVideoRenderer* renderer);
  124. // RtpSender
  125. void GetRtpSenderCapabilities(cricket::MediaType kind, RtpCapabilities* capabilities) const;
  126. // RtpReceiver
  127. void GetRtpReceiverCapabilities(cricket::MediaType kind, RtpCapabilities* capabilities) const;
  128. // AudioDevice
  129. rtc::scoped_refptr<DummyAudioDevice> GetAudioDevice() const { return m_audioDevice; }
  130. // mutex;
  131. std::mutex mutex;
  132. private:
  133. std::unique_ptr<rtc::Thread> m_workerThread;
  134. std::unique_ptr<rtc::Thread> m_signalingThread;
  135. std::unique_ptr<TaskQueueFactory> m_taskQueueFactory;
  136. rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> m_peerConnectionFactory;
  137. rtc::scoped_refptr<DummyAudioDevice> m_audioDevice;
  138. std::vector<rtc::scoped_refptr<const webrtc::RTCStatsReport>> m_listStatsReport;
  139. std::map<const PeerConnectionObject*, std::unique_ptr<PeerConnectionObject>> m_mapClients;
  140. std::map<const webrtc::MediaStreamInterface*, std::unique_ptr<MediaStreamObserver>> m_mapMediaStreamObserver;
  141. std::map<const DataChannelInterface*, std::unique_ptr<DataChannelObject>> m_mapDataChannels;
  142. std::map<const uint32_t, std::shared_ptr<UnityVideoRenderer>> m_mapVideoRenderer;
  143. std::map<const AudioTrackSinkAdapter*, std::unique_ptr<AudioTrackSinkAdapter>> m_mapAudioTrackAndSink;
  144. std::map<const rtc::RefCountInterface*, rtc::scoped_refptr<rtc::RefCountInterface>> m_mapRefPtr;
  145. static uint32_t s_rendererId;
  146. static uint32_t GenerateRendererId();
  147. };
  148. extern bool Convert(const std::string& str, webrtc::PeerConnectionInterface::RTCConfiguration& config);
  149. } // end namespace webrtc
  150. } // end namespace unity