RtmpMultiPusherV2.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Vinno.FIS.TRTCClient.Common.Enum;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  9. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  10. namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTMP
  11. {
  12. public class RtmpMultiPusherV2 : PusherBase, ILiveVideoPusherV2
  13. {
  14. private readonly ConcurrentDictionary<EnumLiveChannelCategory, DsRtmpPusher> _pushers;
  15. public event EventHandler<ChannelStateEventArgsV2> ChannelStateChanged;
  16. public RtmpMultiPusherV2()
  17. {
  18. _pushers = new ConcurrentDictionary<EnumLiveChannelCategory, DsRtmpPusher>();
  19. }
  20. public bool StartPusher(IExtendedData pushParams, IEnumerable<CPVideoDeviceOutputInfo> deviceInfos)
  21. {
  22. var rtmpParams = pushParams as RtmpExtendedData;
  23. if (rtmpParams == null || rtmpParams.UserInfos.Count == 0)
  24. {
  25. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpMultiPusherV2 Extended Data Is Null!");
  26. return false;
  27. }
  28. var userInfos = rtmpParams.UserInfos;
  29. for (int i = 0; i < userInfos.Count(); i++)
  30. {
  31. var deviceInfo = deviceInfos.FirstOrDefault(x => x.Category == userInfos[i].Category);
  32. if (deviceInfo != null)
  33. {
  34. var device = (CPVideoDeviceOutputInfo)deviceInfo.Clone();
  35. bool isMute = true;
  36. string micId = "";
  37. if (i == 0)
  38. {
  39. isMute = rtmpParams.IsMute;
  40. micId = rtmpParams.MicDeviceId;
  41. }
  42. var pusher = new DsRtmpPusher(device, userInfos[i].PushUrl, micId, isMute, userInfos[i].Width, userInfos[i].Height);
  43. if (_pushers.TryAdd(userInfos[i].Category, pusher))
  44. {
  45. pusher.LiveStatusChanged += OnLiveStatusChanged;
  46. pusher.ImageFrameReceived += OnImageFrameReceived;
  47. pusher.Start();
  48. }
  49. else
  50. {
  51. pusher.Dispose();
  52. }
  53. }
  54. else
  55. {
  56. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpMultiPusherV2 Can not find {userInfos[i].Category} User Info!");
  57. }
  58. }
  59. return true;
  60. }
  61. public void SetMute(bool isMute)
  62. {
  63. if (_pushers.Count > 0)
  64. {
  65. _pushers[0].SetMute(isMute);
  66. }
  67. }
  68. private void OnLiveStatusChanged(object sender, EnumLiveStatus e)
  69. {
  70. if (e == EnumLiveStatus.Connected)
  71. {
  72. var category = (EnumLiveChannelCategory)sender;
  73. ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(category, EnumDeviceLiveState.Pushing));
  74. }
  75. }
  76. private void OnImageFrameReceived(object sender, ImageFrameData e)
  77. {
  78. var category = (EnumLiveChannelCategory)sender;
  79. CopyPreviewData(category, e);
  80. }
  81. public bool StopPusher()
  82. {
  83. foreach (var pusher in _pushers.Values)
  84. {
  85. pusher.LiveStatusChanged -= OnLiveStatusChanged;
  86. pusher.ImageFrameReceived -= OnImageFrameReceived;
  87. pusher.Stop();
  88. }
  89. return true;
  90. }
  91. public override void DoDispose()
  92. {
  93. foreach (var pusher in _pushers.Values)
  94. {
  95. pusher.Dispose();
  96. }
  97. _pushers.Clear();
  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($"RtmpMultiPusherV2 Doesn't support StartSpeedTest");
  106. return false;
  107. }
  108. }
  109. }