Camera Information System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
3.6 KiB

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<ZLMediaKitOptions>();
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<StreamConnInfo> 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<bool> 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<object> 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<object>();
}
public async Task<bool> 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
}