123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using fis.media.Library.Media.Players;
- using fis.media.Library.Media.Rooms;
- using fis.media.Library.Players;
- using fis.media.ThirdPartLibrary.Daniu.Player;
- using fis.media.ThirdPartLibrary.Tencent;
- namespace fis.media.Managers
- {
- public class PlayerManager:IDisposable
- {
- private static PlayerManager? _instance;
- private SmartPlayer? _smartPlayer { get; set; }
- public EventHandler<RemoteVideoFrameData>? VideoFrameReceived;
- private RoomTypeEnum _roomType;
- private string _rtmpPullUrl;
- private PlayerManager() {
-
- }
- public static PlayerManager Instance => _instance ?? (_instance = new PlayerManager());
-
- /// <summary>
- /// 注册SmartPlayerSDK
- /// </summary>
- public static void RegistSmartPlayerSDK()
- {
- SmartPlayer.RegistSmartPlayerSDK();
- }
- /// <summary>
- /// 开始RTMP拉流
- /// </summary>
- /// <param name="url"></param>
- private void StartRTMP()
- {
- if (_smartPlayer == null)
- {
- _smartPlayer = new SmartPlayer();
- //_smartPlayer.VideoFrameReceived += VideoFrameReceived;
- }
- _smartPlayer.Play(_rtmpPullUrl);
- }
- /// <summary>
- /// 开始TRTC拉流
- /// </summary>
- /// <param name="url"></param>
- private void StartTRTC()
- {
- TRTCChatRoom.Instance.RemoteVideoFrameArrived += OnRemoteVideoFrameArrived;
- TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived += OnRemoteVideoScreenFrameArrived;
- }
- public void Start(string[] args)
- {
- if (SetRoomPara(args))
- {
- if (_roomType == RoomTypeEnum.TRTCRoom)
- {
- StartTRTC();
- }
- if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
- {
- StartRTMP();
- }
- }
- }
- private bool SetRoomPara(string[] args)
- {
- if (args == null || args.Length < 1)
- {
- return false;
- }
- _roomType = (RoomTypeEnum)Enum.Parse(typeof(RoomTypeEnum), args[0], true);
- if (_roomType == RoomTypeEnum.RTMPVirtualRoom && args.Length == 2)
- {
- var url = args[1];
- _rtmpPullUrl = url;
- }
- return true;
- }
- private void OnRemoteVideoScreenFrameArrived(object? sender, RemoteVideoFrameData e)
- {
- throw new NotImplementedException();
- }
- private void OnRemoteVideoFrameArrived(object? sender, RemoteVideoFrameData e)
- {
- VideoFrameReceived?.Invoke(this,e);
- }
- /// <summary>
- /// 停止拉流
- /// </summary>
- public void Stop()
- {
- _smartPlayer?.Stop();
- TRTCChatRoom.Instance.RemoteVideoFrameArrived -= OnRemoteVideoFrameArrived;
- TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived -= OnRemoteVideoScreenFrameArrived;
- }
- /// <summary>
- /// 切换拉流地址
- /// </summary>
- /// <param name="url"></param>
- public void SwitchUrl(string url)
- {
- _smartPlayer?.SwitchUrl(url);
- }
- /// <summary>
- /// 设置录制参数
- /// </summary>
- /// <param name="recordingDirectory"></param>
- /// <param name="maxFileSize"></param>
- /// <param name="filePrefix"></param>
- /// <param name="attachDateTime"></param>
- public void SetRecordParameter(string recordingDirectory, int maxFileSize, string filePrefix, bool attachDateTime)
- {
- _smartPlayer?.SetRecordParameter(recordingDirectory, maxFileSize, filePrefix, attachDateTime);
- }
- /// <summary>
- /// 开始录制
- /// </summary>
- public void StartRecord()
- {
- _smartPlayer?.StartRecord();
- }
- /// <summary>
- /// 停止录制
- /// </summary>
- public void StopRecord()
- {
- _smartPlayer?.StopRecord();
- }
- /// <summary>
- /// 设置静音
- /// </summary>
- /// <param name="isMute"></param>
- public void SetMute(bool isMute)
- {
- _smartPlayer?.SetMute(isMute);
- }
- public void Dispose()
- {
- Stop();
- if (_smartPlayer != null)
- {
- //_smartPlayer.VideoFrameReceived -= VideoFrameReceived;
- }
- }
- }
- }
|