RtmpSinglePusherV2.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Vinno.FIS.TRTCClient.Common.Enum;
  5. using Vinno.vCloud.FIS.CrossPlatform.Common;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  9. namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTMP
  10. {
  11. public class RtmpSinglePusherV2 : PusherBase, ILiveVideoPusherV2
  12. {
  13. private DsRtmpPusher _pusher;
  14. public event EventHandler<ChannelStateEventArgsV2> ChannelStateChanged;
  15. public bool StartPusher(IExtendedData pushParams, IEnumerable<CPVideoDeviceOutputInfo> deviceInfos)
  16. {
  17. try
  18. {
  19. var rtmpParams = pushParams as RtmpExtendedData;
  20. if (rtmpParams == null || rtmpParams.UserInfos.Count == 0)
  21. {
  22. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Extended Data Is Null!");
  23. return false;
  24. }
  25. var url = rtmpParams.UserInfos.FirstOrDefault()?.PushUrl;
  26. if (string.IsNullOrEmpty(url))
  27. {
  28. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Push Url Is Null!");
  29. return false;
  30. }
  31. if (deviceInfos.Count() == 0)
  32. {
  33. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Push not have device info!");
  34. return false;
  35. }
  36. var userInfo = rtmpParams.UserInfos.FirstOrDefault();
  37. var deviceInfo = deviceInfos.FirstOrDefault(x => x.Category == userInfo.Category);
  38. if (deviceInfo == null)
  39. {
  40. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpSinglePusherV2 Category {userInfo.Category} No device found!");
  41. return false;
  42. }
  43. var device = (CPVideoDeviceOutputInfo)deviceInfo.Clone();
  44. _pusher = new DsRtmpPusher(device, url, pushParams.MicDeviceId, pushParams.IsMute, userInfo.Width, userInfo.Height);
  45. _pusher.LiveStatusChanged += OnLiveStatusChanged;
  46. _pusher.ImageFrameReceived += OnImageFrameReceived;
  47. _pusher.Start();
  48. return true;
  49. }
  50. catch (Exception ex)
  51. {
  52. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Start Rtmp Single Push Error:{ex}");
  53. }
  54. return false;
  55. }
  56. public void SetMute(bool isMute)
  57. {
  58. _pusher?.SetMute(isMute);
  59. }
  60. public bool StopPusher()
  61. {
  62. try
  63. {
  64. if (_pusher != null)
  65. {
  66. _pusher.LiveStatusChanged -= OnLiveStatusChanged;
  67. _pusher.ImageFrameReceived -= OnImageFrameReceived;
  68. _pusher.Stop();
  69. }
  70. return true;
  71. }
  72. catch (Exception ex)
  73. {
  74. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Stop Rtmp Single Push Error:{ex}");
  75. }
  76. return false;
  77. }
  78. private void OnImageFrameReceived(object sender, ImageFrameData e)
  79. {
  80. var category = (EnumLiveChannelCategory)sender;
  81. CopyPreviewData(category, e);
  82. }
  83. private void OnLiveStatusChanged(object sender, EnumLiveStatus e)
  84. {
  85. if (e == EnumLiveStatus.Connected)
  86. {
  87. var category = (EnumLiveChannelCategory)sender;
  88. ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(category, EnumDeviceLiveState.Pushing));
  89. }
  90. }
  91. public override void DoDispose()
  92. {
  93. if (_pusher != null)
  94. {
  95. _pusher.Dispose();
  96. _pusher = null;
  97. }
  98. base.DoDispose();
  99. }
  100. public void SwitchMic(string micId)
  101. {
  102. }
  103. public bool StartSpeedTest(uint appId, string userId, string userSign)
  104. {
  105. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpSinglePusher Doesn't support StartSpeedTest");
  106. return false;
  107. }
  108. }
  109. }