using Furion.RemoteRequest.Extensions; using Newtonsoft.Json.Linq; namespace Cis.Application.Core.Component.ZLMediaKit; public class ZlmServer : IZlmServer, ISingleton { #region Attr private readonly ZLMediaKitOptions _options; #region Url private string AddStreamProxyUrl { get; set; } private string DelStreamProxyUrl { get; set; } private string GetMediaListUrl { get; set; } private string IsMediaOnlineUrl { get; set; } #endregion Url #endregion Attr public ZlmServer() { _options = App.GetOptions(); InitUrl(); } #region Util Method private void InitUrl() { string baseUrl = $"http://{_options.Ip}:{_options.Port}"; AddStreamProxyUrl = $"{baseUrl}/index/api/addStreamProxy"; DelStreamProxyUrl = $"{baseUrl}/index/api/delStreamProxy"; GetMediaListUrl = $"{baseUrl}/index/api/getMediaList"; IsMediaOnlineUrl = $"{baseUrl}/index/api/isMediaOnline"; } private bool JudgeResult(JObject data) { ZlmCode code = (ZlmCode)data["code"].ToInt(); if (code == ZlmCode.Success) return false; string msg = data["msg"].ToString(); int result = data["result"].ToInt(); throw ZlmException.New(code, msg, result); } #endregion Util Method #region Base Method public async Task AddStreamProxy(string stream, string rtspUrl) { string resp = await AddStreamProxyUrl.SetBody(new { secret = _options.Secret, vhost = _options.DefaultVhost, app = _options.DefaultApp, stream = stream, url = rtspUrl, rtp_type = _options.RtpType, timeout_sec = _options.TimeoutSec, retry_count = _options.RetryCount, enable_rtmp = 1, enable_hls = 0, enable_mp4 = 0, enable_ts = 0, enable_fmp4 = 0, }).PostAsStringAsync(); JObject respJObj = resp.ToJObject(); JudgeResult(respJObj); return StreamConnInfo.New(_options.DefaultVhost, _options.DefaultApp, stream); } public async Task DelStreamProxy(string stream) { string resp = await DelStreamProxyUrl.SetBody(new { secret = _options.Secret, key = $"{_options.DefaultVhost}/{_options.DefaultApp}/{stream}", }).PostAsStringAsync(); JObject respJObj = resp.ToJObject(); JudgeResult(respJObj); return respJObj["data"]["flag"].ToBoolean(); } public async Task GetMediaList(string stream) { string resp = await GetMediaListUrl.SetBody(new { secret = _options.Secret, vhost = _options.DefaultVhost, app = _options.DefaultApp, stream = stream, schema = "rtsp", }).PostAsStringAsync(); JObject respJObj = resp.ToJObject(); JudgeResult(respJObj); return respJObj["data"]?.ToObject(); } public async Task IsMediaOnline(string stream) { string resp = await IsMediaOnlineUrl.SetBody(new { secret = _options.Secret, vhost = _options.DefaultVhost, app = _options.DefaultApp, stream = stream, schema = "rtsp", }).PostAsStringAsync(); JObject respJObj = resp.ToJObject(); JudgeResult(respJObj); return respJObj["online"].ToBoolean(); } public StreamConnInfo GetStreamConnInfo(string stream) { return StreamConnInfo.New(_options.DefaultVhost, _options.DefaultApp, stream); } #endregion Base Method }