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.
111 lines
4.3 KiB
111 lines
4.3 KiB
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;
|
|
|
|
/// <summary>
|
|
/// zlmediakit 服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(CoreInfo.GroupName, Order = CoreInfo.ZlmGroupOrder)]
|
|
public class ZlmService : IDynamicApiController, ITransient
|
|
{
|
|
#region Attr
|
|
|
|
private readonly SqlSugarRepository<CbCamera> _cbCameraRep;
|
|
|
|
private readonly IZlmServer _zlmServer;
|
|
|
|
private readonly IOnvifServer _onvifServer;
|
|
|
|
#endregion Attr
|
|
|
|
public ZlmService(SqlSugarRepository<CbCamera> cbCameraRep, IZlmServer zlmServer, IOnvifServer onvifServer)
|
|
{
|
|
_cbCameraRep = cbCameraRep;
|
|
_zlmServer = zlmServer;
|
|
_onvifServer = onvifServer;
|
|
}
|
|
|
|
#region Base Method
|
|
|
|
/// <summary>
|
|
/// 添加码流拉流代理(只支持H264/H265/aac/G711负载)
|
|
/// </summary>
|
|
/// <param name="cameraId">cbCameraId</param>
|
|
/// <param name="streamLevel">码流级别[0,2], default:0</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>streamLevel:码流级别[0,2],default:0")]
|
|
public async Task<StreamConnInfo> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭拉流代理
|
|
/// </summary>
|
|
/// <param name="cameraId">cbCameraId</param>
|
|
/// <param name="streamLevel">码流级别[0,2], default:0</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>streamLevel:码流级别[0,2],default:0")]
|
|
public async Task<bool> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取流列表
|
|
/// </summary>
|
|
/// <param name="cameraId">cbCameraId</param>
|
|
/// <param name="streamLevel">码流级别[0,2], default:0</param>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>streamLevel:码流级别[0,2],default:0")]
|
|
public async Task<object> GetMediaList([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}");
|
|
return await _zlmServer.GetMediaList(stream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断直播流是否在线
|
|
/// </summary>
|
|
/// <param name="cameraId">cbCameraId</param>
|
|
/// <param name="streamLevel">码流级别[0,2], default:0</param>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>streamLevel:码流级别[0,2],default:0")]
|
|
public async Task<bool> IsMediaOnline([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.IsMediaOnline(stream);
|
|
}
|
|
|
|
#endregion Base Method
|
|
}
|