123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System.Collections.Concurrent;
- using WingRtmpService.EmbeddedRtmpServer.Net;
- using WingServerCommon.Log;
- using WingServerCommon.Service;
- using WingServerCommon.Config;
- using WingInterfaceLibrary.Internal.Request;
- using WingInterfaceLibrary.Internal.Interface;
- namespace WingRtmpService
- {
- public partial class RtmpService : JsonRpcService, IRtmpService
- {
- private PushLiveModel _defaultPushLiveModel;
- private RtmpServer _embeddedServer;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- // private IChannelManager _channelManager;
- private readonly ConcurrentDictionary<string, ChannelData> _terminalTestChannels = new ConcurrentDictionary<string, ChannelData>();
- private int _validTime;//hour
- // private int _applyCount;
- private const int Internal = 3;//Minute
- private int _shareExpirationTime;//hour
- private Version _terminalLiveFlagVersion;// new or old terminal version flag
- private Version _boxLiveFlagVersion = new Version("1.7.22.19661");// new or old box version flag
- private readonly ConcurrentDictionary<string, DateTime> _syncTerminalPushLiveConfigFailIds = new ConcurrentDictionary<string, DateTime>();
- private readonly ConcurrentDictionary<string, DateTime> _syncPushLiveConfigFailIds = new ConcurrentDictionary<string, DateTime>();
- public RtmpService()
- {
- Initialize();
- }
- private void Initialize()
- {
- InitEmbeddedServer();
- // _applyCount = Config.GetValue(ServiceId.Id, "ChannelCount", 20);
- _validTime = ConfigurationManager.GetValue("TODO", "ValidTime", 2);
- var defaultIsMergeChannel = ConfigurationManager.GetValue("TODO", "DefaultIsMergeChannel", true);
- var defaultLiveProtocol = LiveProtocol.Rtmp;
- _defaultPushLiveModel = new PushLiveModel(string.Empty, string.Empty, defaultIsMergeChannel, defaultLiveProtocol);
- _shareExpirationTime = ConfigurationManager.GetValue("TODO", "ShareExpirationTime", 12);
- _terminalLiveFlagVersion = new Version(ConfigurationManager.GetValue("TODO", "TerminalLiveFlagVersion", "1.0.0.0"));//add TerminalLiveFlagVersion: check new or old terminal version flag
- }
- public async Task<GetChannelResult> GetChannelAsync(GetChannelRequest request)
- {
- InitEmbeddedServer();
- var channel = ChannelOperationHelper.ApplyChannel();
- if (channel != null)
- {
- var channelId = channel.ChannelInfo.Cid;
- var pushUrl = channel.ChannelInfo.PushUrl;
- var rtmpPullUrl = channel.ChannelInfo.RtmpPullUrl;
- var httpUrl = channel.ChannelInfo.HttpPullUrl;
- var hlsUrl = channel.ChannelInfo.HlsPullUrl;
- var resultData = new GetChannelResult
- {
- Cid = channelId,
- PushUrl = pushUrl,
- RtmpPullUrl = rtmpPullUrl,
- HttpPullUrl = httpUrl,
- HlsPullUrl = hlsUrl,
- };
- return await Task.FromResult(resultData);
- }
- return null;
- }
- private ChannelData GetChannel()
- {
- var channel = ChannelOperationHelper.ApplyChannel();
- if (channel != null)
- {
- var channelId = channel.ChannelInfo.Cid;
- var uploadUrl = channel.ChannelInfo.PushUrl;
- var downloadUrl = channel.ChannelInfo.RtmpPullUrl;
- var httpUrl = channel.ChannelInfo.HttpPullUrl;
- var hlsUrl = channel.ChannelInfo.HlsPullUrl;
- return new ChannelData(channelId, uploadUrl, downloadUrl, httpUrl, hlsUrl);
- }
- return null;
- }
- private void InitEmbeddedServer()
- {
- try
- {
- if (_embeddedServer == null)
- {
- var channelProvider = EnvironmentConfigs.General.LiveProtocol.ToLower() == "rtmp" ? EnvironmentConfigs.Rtmp.RTMPType : string.Empty; //ConfigurationManager.GetValue("LiveVideo", "ChannelProvider", "Netease");
- if (channelProvider.Equals("Embedded", StringComparison.InvariantCultureIgnoreCase))
- {
- //Start embedded server.
- var app = ConfigurationManager.GetValue("LiveVideo", "App", string.Empty);
- RtmpServerConfig.DefaultApp = app;
- _embeddedServer = new RtmpServer();
- _embeddedServer.Start();
- }
- }
- }
- catch(Exception ex)
- {
- Logger.WriteLineWarn($"RtmpService InitEmbeddedServer failed, ex:{ex}");
- }
- }
- protected override void Disposing()
- {
- _waitEvent.Set();
- }
- private void OnChannelClosed(object sender, IEnumerable<ChannelData> channelDatas)
- {
- Logger.WriteLineInfo($"CloseChannel:count={channelDatas.Count()}");
- foreach (var channel in channelDatas)
- {
- if (channel != null)
- {
- ChannelOperationHelper.CloseChannel(channel.Id);
- }
- }
- }
- }
- }
|