PeerConnectionManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. using System.Text.Json;
  5. namespace VRTC.WpfClient
  6. {
  7. public interface IPeerConnectionManager
  8. {
  9. /// <summary>
  10. /// Join room
  11. /// </summary>
  12. /// <param name="roomId">The room id you want to join</param>
  13. /// <param name="localClientId">The local client id who join the room</param>
  14. void Join(uint roomId, string localClientId);
  15. /// <summary>
  16. /// Get a peer connection according to client id
  17. /// </summary>
  18. /// <param name="remoteClientId">The remote client id</param>
  19. /// <returns></returns>
  20. PeerConnetion GetPeerConnetion(string remoteClientId);
  21. /// <summary>
  22. /// Close all connection
  23. /// </summary>
  24. void Close();
  25. /// <summary>
  26. /// Implement answer after receiving offer
  27. /// </summary>
  28. /// <param name="clientId">The remove client id</param>
  29. /// <param name="sdp">The remote sdp</param>
  30. void Answer(string clientId, string sdp);
  31. void SetRemoteDesc(string remoteClientId, string sdp);
  32. void AddIceCandidate(string remoteClientId, string sdpMid, int sdpMLineIndex, string candidate);
  33. void Offer(string remoteClientId);
  34. }
  35. public class PeerConnectionManager : IPeerConnectionManager
  36. {
  37. private ConcurrentDictionary<string, PeerConnetion> _peerConnections = new ConcurrentDictionary<string,PeerConnetion>();
  38. private uint _roomId;
  39. private string _localClientId;
  40. private readonly Action<string> _sendMessageToSever;
  41. private readonly Action<VideoFrameData> onLocalFrameArrived;
  42. private readonly Action<VideoFrameData> onRemoteFrameArrived;
  43. private readonly string _vidoeDevice;
  44. public PeerConnectionManager(Action<string> sendMessageToSever, Action<VideoFrameData> onLocalFrameArrived, Action<VideoFrameData> onRemoteFrameArrived, string vidoeDevice)
  45. {
  46. this._sendMessageToSever = sendMessageToSever;
  47. this.onLocalFrameArrived = onLocalFrameArrived;
  48. this.onRemoteFrameArrived = onRemoteFrameArrived;
  49. this._vidoeDevice = vidoeDevice;
  50. }
  51. /// <summary>
  52. /// Join room
  53. /// </summary>
  54. /// <param name="roomId">The room id you want to join</param>
  55. /// <param name="localClientId">The local client id who join the room</param>
  56. public void Join(uint roomId, string localClientId)
  57. {
  58. _roomId = roomId;
  59. _localClientId = localClientId;
  60. var jsonMsg = new VrtcClientMessage();
  61. jsonMsg.Command = VrtcCommands.Join;
  62. jsonMsg.RoomId = _roomId;
  63. jsonMsg.ClientId = _localClientId;
  64. var msg = jsonMsg.ToJson();
  65. _sendMessageToSever(msg);
  66. }
  67. public void Offer(string remoteClientId)
  68. {
  69. var connection = GetPeerConnetion(remoteClientId);
  70. if (connection != null)
  71. {
  72. connection.Offer();
  73. }
  74. }
  75. /// <summary>
  76. /// Get a peer connection according to client id
  77. /// </summary>
  78. /// <param name="clientId">The remote client id</param>
  79. /// <returns></returns>
  80. public PeerConnetion GetPeerConnetion(string clientId)
  81. {
  82. var conn = _peerConnections.GetOrAdd(clientId, (id) => {
  83. Logger.WriteLineInfo($"add new client {clientId}");
  84. var connection = new PeerConnetion(_roomId, _localClientId, clientId, _sendMessageToSever, onLocalFrameArrived, onRemoteFrameArrived, _vidoeDevice);
  85. return connection;
  86. });
  87. return conn;
  88. }
  89. /// <summary>
  90. /// Close all connection
  91. /// </summary>
  92. public void Close()
  93. {
  94. var connections = _peerConnections.Values.ToList();
  95. foreach (var connection in connections)
  96. {
  97. connection.Close();
  98. }
  99. }
  100. /// <summary>
  101. /// Implement answer after receiving offer
  102. /// </summary>
  103. /// <param name="remoteClientId">The remove client id</param>
  104. /// <param name="sdp">The remote sdp</param>
  105. public void Answer(string remoteClientId, string sdp)
  106. {
  107. var connection = GetPeerConnetion(remoteClientId);
  108. if (connection != null)
  109. {
  110. connection.Answer(sdp);
  111. }
  112. }
  113. public void SetRemoteDesc(string remoteClientId, string sdp)
  114. {
  115. var connection = GetPeerConnetion(remoteClientId);
  116. if (connection != null)
  117. {
  118. connection.OnOfferReply(sdp);
  119. }
  120. }
  121. public void AddIceCandidate(string remoteClientId, string sdpMid, int sdpMLineIndex, string candidate)
  122. {
  123. var connection = GetPeerConnetion(remoteClientId);
  124. if (connection != null)
  125. {
  126. connection.AddIceCandidate(sdpMid, sdpMLineIndex, candidate);
  127. }
  128. }
  129. }
  130. }