using Cis.Application.Cb;
using Cis.Application.Core.Component.Onvif;
using Cis.Application.Core.Component.ZLMediaKit;
using EC.Helper.Onvif;
using Furion.DataEncryption;
namespace Cis.Application.Core.Service;
///
/// zlmediakit 服务
///
[ApiDescriptionSettings(CoreInfo.GroupName, Order = CoreInfo.ZlmGroupOrder)]
public class ZlmService : IDynamicApiController, ITransient
{
#region Attr
private readonly SqlSugarRepository _cbCameraRep;
private readonly IZlmServer _zlmServer;
private readonly IOnvifServer _onvifServer;
#endregion Attr
public ZlmService(SqlSugarRepository cbCameraRep, IZlmServer zlmServer, IOnvifServer onvifServer)
{
_cbCameraRep = cbCameraRep;
_zlmServer = zlmServer;
_onvifServer = onvifServer;
}
#region Base Method
///
/// 添加码流拉流代理(只支持H264/H265/aac/G711负载)
///
/// cbCameraId
/// 码流级别[0,2], default:0
///
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId
streamLevel:码流级别[0,2],default:0")]
public async Task AddStreamProxy([Required][FromForm] long cameraId, [FromForm][Range(0, 2)] int streamLevel = 0)
{
CbCamera camera = await _cbCameraRep.GetByIdAsync(cameraId);
if (camera == null) return default;
string stream = MD5Encryption.Encrypt($"{camera.Ip}:{streamLevel}");
bool isOnline = await _zlmServer.IsMediaOnline(stream);
if (isOnline) return _zlmServer.GetStreamConnInfo(stream);
bool ret = _onvifServer.IsExists(camera.Id);
if (!ret) ret = await _onvifServer.RegisterAsync(camera);
if (!ret) return default;
ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return default;
string rtspUrl = streamLevel switch
{
0 => await client.GetStreamUrl(),
1 => await client.GetSubStreamUrl(),
2 => await client.GetThirdStreamUrl(),
_ => await client.GetStreamUrl()
};
return await _zlmServer.AddStreamProxy(stream, rtspUrl);
}
///
/// 关闭拉流代理
///
/// cbCameraId
/// 码流级别[0,2], default:0
///
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId
streamLevel:码流级别[0,2],default:0")]
public async Task DelStreamProxy([Required][FromForm] long cameraId, [FromForm][Range(0, 2)] int streamLevel = 0)
{
CbCamera camera = await _cbCameraRep.GetByIdAsync(cameraId);
if (camera == null) return false;
string stream = MD5Encryption.Encrypt($"{camera.Ip}:{streamLevel}");
return await _zlmServer.DelStreamProxy(stream);
}
///
/// 获取流列表
///
/// cbCameraId
/// 码流级别[0,2], default:0
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId
streamLevel:码流级别[0,2],default:0")]
public async Task