123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System.Runtime.Versioning;
- using fis.media.Library.Media.Rooms;
- using fis.media.Library.Players;
- using fis.media.ThirdPartLibrary.Daniu.Common;
- using fis.media.ThirdPartLibrary.Daniu.Publisher;
- using fis.media.ThirdPartLibrary.Tencent;
- namespace fis.media.Managers
- {
- [SupportedOSPlatform("windows")]
- public class PusherManager : IDisposable
- {
- private static PusherManager? _instance;
- public static PusherManager Instance => _instance ?? (_instance = new PusherManager());
- public EventHandler<LocalPreviewVideoFrameData> PreviewImageReceived;
- private SmartPublisher? _smartPublisher;
- private string _userId;
- private int _appId;
- private string _userSign;
- private int _rtcRoomId;
- private RoomTypeEnum _roomType;
- private string _rtmpurl;
- private int _screenWidth;
- private int _screenHeight;
- private LocalPreviewVideoFrameData _localPreviewFrame;
- private PusherManager() { }
- /// <summary>
- /// 注册SmartPublisherSDK
- /// </summary>
- public static void RegistSmartPublisherSDK()
- {
- SmartPublisher.RegistSmartPublisherSDK();
- }
- public void Start(string[] args)
- {
- if (SetRoomPara(args))
- {
- if (_roomType == RoomTypeEnum.TRTCRoom)
- {
- StartTRTC();
- }
- if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
- {
- StartRtmp();
- }
- }
- }
- /// <summary>
- /// 开始推流
- /// </summary>
- /// <param name="url">推流地址</param>
- private void StartRtmp()
- {
- if (_smartPublisher == null)
- {
- _smartPublisher = new SmartPublisher(0, AudioMode.Mix);
- SetScreenSize(_screenWidth, _screenHeight);
- _smartPublisher.InitLayers();
- _smartPublisher.PreviewImageReceived += OnRTMPFrameReceived;
- }
- _smartPublisher?.SetEnablePreview(true);
- _smartPublisher?.Start(_rtmpurl);
- }
- private void OnRTMPFrameReceived(object? sender, VideoFrameData e)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// 开始推流(TRTC进入房间即开始推流)
- /// </summary>
- /// <param name="url">推流地址</param>
- private void StartTRTC()
- {
- TRTCChatRoom.Instance.Enter(_userId, _rtcRoomId, true, _userSign, _appId);
- TRTCChatRoom.Instance.LocalVideoFrameArrived += OnLocalVideoFrameArrived;
- TRTCChatRoom.Instance.LocalVideoScreenFrameArrived += OnLocalVideoScreenFrameArrived;
- }
- private void OnLocalVideoScreenFrameArrived(object? sender, VideoFrameData e)
- {
- ;
- }
- private void OnLocalVideoFrameArrived(object? sender, VideoFrameData e)
- {
- if (_localPreviewFrame == null)
- {
- _localPreviewFrame = new LocalPreviewVideoFrameData(_userId);
- }
- _localPreviewFrame.Data = e;
- PreviewImageReceived?.Invoke(this, _localPreviewFrame);
- }
- 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.TRTCRoom && args.Length == 5)
- {
- if (int.TryParse(args[1], out int appId))
- {
- return false;
- }
- var userId=args[2];
- var userSign = args[3];
- if (int.TryParse(args[4], out int intergeRoomId))
- {
- return false;
- }
- _userId = userId;
- _appId = appId;
- _userSign = userSign;
- _rtcRoomId = intergeRoomId;
- }
- if (_roomType == RoomTypeEnum.TRTCRoom && args.Length == 4)
- {
- if (int.TryParse(args[1], out int screenWidth))
- {
- return false;
- }
- if (int.TryParse(args[2], out int screenHeight))
- {
- return false;
- }
- var url = args[3];
- _rtmpurl = url;
- _screenWidth = screenWidth;
- _screenHeight = screenHeight;
- }
- return true;
- }
- public void EnableLayer(int index, bool isEnable)
- {
- _smartPublisher?.SetEnableLayer(index, isEnable);
- }
- public void SetEnablePreview(bool enablePreview)
- {
- _smartPublisher?.SetEnablePreview(enablePreview);
- }
- public void SetScreenSize(int screenWidth, int screenHeight)
- {
- _smartPublisher?.SetScreenSize(screenWidth, screenHeight);
- }
- /// <summary>
- /// 设置背景图路径
- /// </summary>
- /// <param name="path"></param>
- public void SetBackgrountPath(string path)
- {
- _smartPublisher?.SetBackgrountPath(path);
- }
- /// <summary>
- /// 设置是否静音
- /// </summary>
- /// <param name="isMute"></param>
- public void SetMute(bool isMute)
- {
- _smartPublisher?.SetMute(isMute);
- }
- /// <summary>
- /// 停止推流
- /// </summary>
- public void Stop()
- {
- if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
- {
- _smartPublisher?.Close();
- }
- if (_roomType == RoomTypeEnum.TRTCRoom)
- {
- TRTCChatRoom.Instance.LocalVideoFrameArrived -= OnLocalVideoFrameArrived;
- TRTCChatRoom.Instance.LocalVideoScreenFrameArrived -= OnLocalVideoScreenFrameArrived;
- TRTCChatRoom.Instance.Exit();
- }
- }
- public void Dispose()
- {
- Stop();
- if (_smartPublisher != null)
- {
- _smartPublisher.PreviewImageReceived -= OnRTMPFrameReceived;
- _smartPublisher?.Dispose();
- _smartPublisher = null;
- }
- }
- }
- }
|