RtmpService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections.Concurrent;
  2. using WingRtmpService.EmbeddedRtmpServer.Net;
  3. using WingServerCommon.Log;
  4. using WingServerCommon.Service;
  5. using WingServerCommon.Config;
  6. using WingInterfaceLibrary.Internal.Request;
  7. using WingInterfaceLibrary.Internal.Interface;
  8. namespace WingRtmpService
  9. {
  10. public partial class RtmpService : JsonRpcService, IRtmpService
  11. {
  12. private PushLiveModel _defaultPushLiveModel;
  13. private RtmpServer _embeddedServer;
  14. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  15. // private IChannelManager _channelManager;
  16. private readonly ConcurrentDictionary<string, ChannelData> _terminalTestChannels = new ConcurrentDictionary<string, ChannelData>();
  17. private int _validTime;//hour
  18. // private int _applyCount;
  19. private const int Internal = 3;//Minute
  20. private int _shareExpirationTime;//hour
  21. private Version _terminalLiveFlagVersion;// new or old terminal version flag
  22. private Version _boxLiveFlagVersion = new Version("1.7.22.19661");// new or old box version flag
  23. private readonly ConcurrentDictionary<string, DateTime> _syncTerminalPushLiveConfigFailIds = new ConcurrentDictionary<string, DateTime>();
  24. private readonly ConcurrentDictionary<string, DateTime> _syncPushLiveConfigFailIds = new ConcurrentDictionary<string, DateTime>();
  25. public RtmpService()
  26. {
  27. Initialize();
  28. }
  29. private void Initialize()
  30. {
  31. InitEmbeddedServer();
  32. // _applyCount = Config.GetValue(ServiceId.Id, "ChannelCount", 20);
  33. _validTime = ConfigurationManager.GetValue("TODO", "ValidTime", 2);
  34. var defaultIsMergeChannel = ConfigurationManager.GetValue("TODO", "DefaultIsMergeChannel", true);
  35. var defaultLiveProtocol = LiveProtocol.Rtmp;
  36. _defaultPushLiveModel = new PushLiveModel(string.Empty, string.Empty, defaultIsMergeChannel, defaultLiveProtocol);
  37. _shareExpirationTime = ConfigurationManager.GetValue("TODO", "ShareExpirationTime", 12);
  38. _terminalLiveFlagVersion = new Version(ConfigurationManager.GetValue("TODO", "TerminalLiveFlagVersion", "1.0.0.0"));//add TerminalLiveFlagVersion: check new or old terminal version flag
  39. }
  40. public async Task<GetChannelResult> GetChannelAsync(GetChannelRequest request)
  41. {
  42. InitEmbeddedServer();
  43. var channel = ChannelOperationHelper.ApplyChannel();
  44. if (channel != null)
  45. {
  46. var channelId = channel.ChannelInfo.Cid;
  47. var pushUrl = channel.ChannelInfo.PushUrl;
  48. var rtmpPullUrl = channel.ChannelInfo.RtmpPullUrl;
  49. var httpUrl = channel.ChannelInfo.HttpPullUrl;
  50. var hlsUrl = channel.ChannelInfo.HlsPullUrl;
  51. var resultData = new GetChannelResult
  52. {
  53. Cid = channelId,
  54. PushUrl = pushUrl,
  55. RtmpPullUrl = rtmpPullUrl,
  56. HttpPullUrl = httpUrl,
  57. HlsPullUrl = hlsUrl,
  58. };
  59. return await Task.FromResult(resultData);
  60. }
  61. return null;
  62. }
  63. private ChannelData GetChannel()
  64. {
  65. var channel = ChannelOperationHelper.ApplyChannel();
  66. if (channel != null)
  67. {
  68. var channelId = channel.ChannelInfo.Cid;
  69. var uploadUrl = channel.ChannelInfo.PushUrl;
  70. var downloadUrl = channel.ChannelInfo.RtmpPullUrl;
  71. var httpUrl = channel.ChannelInfo.HttpPullUrl;
  72. var hlsUrl = channel.ChannelInfo.HlsPullUrl;
  73. return new ChannelData(channelId, uploadUrl, downloadUrl, httpUrl, hlsUrl);
  74. }
  75. return null;
  76. }
  77. private void InitEmbeddedServer()
  78. {
  79. try
  80. {
  81. if (_embeddedServer == null)
  82. {
  83. var channelProvider = EnvironmentConfigs.General.LiveProtocol.ToLower() == "rtmp" ? EnvironmentConfigs.Rtmp.RTMPType : string.Empty; //ConfigurationManager.GetValue("LiveVideo", "ChannelProvider", "Netease");
  84. if (channelProvider.Equals("Embedded", StringComparison.InvariantCultureIgnoreCase))
  85. {
  86. //Start embedded server.
  87. var app = ConfigurationManager.GetValue("LiveVideo", "App", string.Empty);
  88. RtmpServerConfig.DefaultApp = app;
  89. _embeddedServer = new RtmpServer();
  90. _embeddedServer.Start();
  91. }
  92. }
  93. }
  94. catch(Exception ex)
  95. {
  96. Logger.WriteLineWarn($"RtmpService InitEmbeddedServer failed, ex:{ex}");
  97. }
  98. }
  99. protected override void Disposing()
  100. {
  101. _waitEvent.Set();
  102. }
  103. private void OnChannelClosed(object sender, IEnumerable<ChannelData> channelDatas)
  104. {
  105. Logger.WriteLineInfo($"CloseChannel:count={channelDatas.Count()}");
  106. foreach (var channel in channelDatas)
  107. {
  108. if (channel != null)
  109. {
  110. ChannelOperationHelper.CloseChannel(channel.Id);
  111. }
  112. }
  113. }
  114. }
  115. }