123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System;
- using System.Collections.Concurrent;
- using System.Linq;
- using System.Text.Json;
- namespace VRTC.WpfClient
- {
- public interface IPeerConnectionManager
- {
- /// <summary>
- /// Join room
- /// </summary>
- /// <param name="roomId">The room id you want to join</param>
- /// <param name="localClientId">The local client id who join the room</param>
- void Join(uint roomId, string localClientId);
- /// <summary>
- /// Get a peer connection according to client id
- /// </summary>
- /// <param name="remoteClientId">The remote client id</param>
- /// <returns></returns>
- PeerConnetion GetPeerConnetion(string remoteClientId);
- /// <summary>
- /// Close all connection
- /// </summary>
- void Close();
- /// <summary>
- /// Implement answer after receiving offer
- /// </summary>
- /// <param name="clientId">The remove client id</param>
- /// <param name="sdp">The remote sdp</param>
- void Answer(string clientId, string sdp);
- void SetRemoteDesc(string remoteClientId, string sdp);
- void AddIceCandidate(string remoteClientId, string sdpMid, int sdpMLineIndex, string candidate);
- void Offer(string remoteClientId);
- }
- public class PeerConnectionManager : IPeerConnectionManager
- {
- private ConcurrentDictionary<string, PeerConnetion> _peerConnections = new ConcurrentDictionary<string,PeerConnetion>();
- private uint _roomId;
- private string _localClientId;
- private readonly Action<string> _sendMessageToSever;
- private readonly Action<VideoFrameData> onLocalFrameArrived;
- private readonly Action<VideoFrameData> onRemoteFrameArrived;
- private readonly string _vidoeDevice;
- public PeerConnectionManager(Action<string> sendMessageToSever, Action<VideoFrameData> onLocalFrameArrived, Action<VideoFrameData> onRemoteFrameArrived, string vidoeDevice)
- {
- this._sendMessageToSever = sendMessageToSever;
- this.onLocalFrameArrived = onLocalFrameArrived;
- this.onRemoteFrameArrived = onRemoteFrameArrived;
- this._vidoeDevice = vidoeDevice;
- }
- /// <summary>
- /// Join room
- /// </summary>
- /// <param name="roomId">The room id you want to join</param>
- /// <param name="localClientId">The local client id who join the room</param>
- public void Join(uint roomId, string localClientId)
- {
- _roomId = roomId;
- _localClientId = localClientId;
- var jsonMsg = new VrtcClientMessage();
- jsonMsg.Command = VrtcCommands.Join;
- jsonMsg.RoomId = _roomId;
- jsonMsg.ClientId = _localClientId;
- var msg = jsonMsg.ToJson();
- _sendMessageToSever(msg);
- }
- public void Offer(string remoteClientId)
- {
- var connection = GetPeerConnetion(remoteClientId);
- if (connection != null)
- {
- connection.Offer();
- }
- }
-
- /// <summary>
- /// Get a peer connection according to client id
- /// </summary>
- /// <param name="clientId">The remote client id</param>
- /// <returns></returns>
- public PeerConnetion GetPeerConnetion(string clientId)
- {
- var conn = _peerConnections.GetOrAdd(clientId, (id) => {
- Logger.WriteLineInfo($"add new client {clientId}");
- var connection = new PeerConnetion(_roomId, _localClientId, clientId, _sendMessageToSever, onLocalFrameArrived, onRemoteFrameArrived, _vidoeDevice);
-
- return connection;
- });
- return conn;
- }
- /// <summary>
- /// Close all connection
- /// </summary>
- public void Close()
- {
- var connections = _peerConnections.Values.ToList();
- foreach (var connection in connections)
- {
- connection.Close();
- }
- }
- /// <summary>
- /// Implement answer after receiving offer
- /// </summary>
- /// <param name="remoteClientId">The remove client id</param>
- /// <param name="sdp">The remote sdp</param>
- public void Answer(string remoteClientId, string sdp)
- {
- var connection = GetPeerConnetion(remoteClientId);
- if (connection != null)
- {
- connection.Answer(sdp);
- }
- }
- public void SetRemoteDesc(string remoteClientId, string sdp)
- {
- var connection = GetPeerConnetion(remoteClientId);
- if (connection != null)
- {
- connection.OnOfferReply(sdp);
- }
- }
- public void AddIceCandidate(string remoteClientId, string sdpMid, int sdpMLineIndex, string candidate)
- {
- var connection = GetPeerConnetion(remoteClientId);
- if (connection != null)
- {
- connection.AddIceCandidate(sdpMid, sdpMLineIndex, candidate);
- }
- }
- }
- }
|