|
|
|
|
@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration; |
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
using System.Net.Http.Json; |
|
|
|
|
using System.Text.Json; |
|
|
|
|
using Video.DomainService.Dahvision; |
|
|
|
|
|
|
|
|
|
namespace Video.DomainService |
|
|
|
|
{ |
|
|
|
|
@ -37,7 +38,7 @@ namespace Video.DomainService |
|
|
|
|
/// <param name="dto"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
/// <exception cref="NotImplementedException"></exception> |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RecordVideoUrl(PlaybackReqDto dto) |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RecordVideoUrl(PlaybackReqDto dto, string? ipaddress) |
|
|
|
|
{ |
|
|
|
|
// 1) 参数校验 + 早退 |
|
|
|
|
if (dto == null || string.IsNullOrWhiteSpace(dto.Data.ChannelId)) |
|
|
|
|
@ -48,20 +49,23 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 2) Token:优先入参,其次缓存/获取(建议返回完整的 "Bearer xxx") |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("hls等录像回放:token无效"); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
} |
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/admin/API/video/stream/record"; |
|
|
|
|
if (ipaddress != null) |
|
|
|
|
{ |
|
|
|
|
url = $"https://{ipaddress}/evo-apigw/admin/API/video/stream/record"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3) 构造请求(把 dto 放进 Body),并用 SendAsync 发送,才能带上头 |
|
|
|
|
using var req = new HttpRequestMessage(HttpMethod.Post, url) |
|
|
|
|
{ |
|
|
|
|
Content = JsonContent.Create(dto) // 关键:别再丢 body 了 |
|
|
|
|
Content = JsonContent.Create(dto.Data) // 关键:别再丢 body 了 |
|
|
|
|
}; |
|
|
|
|
req.Headers.TryAddWithoutValidation("Authorization", token); |
|
|
|
|
|
|
|
|
|
@ -80,7 +84,12 @@ namespace Video.DomainService |
|
|
|
|
_logger.LogWarning("录像请求业务失败: {Body}", body); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1008", Msg = "录像请求失败" }; |
|
|
|
|
} |
|
|
|
|
result.Data!.Url = result.Data.Url + "?token=" + token; |
|
|
|
|
result.Data!.Url = UrlHostReplacer.ReplaceHost( |
|
|
|
|
result.Data.Url, |
|
|
|
|
ipaddress, |
|
|
|
|
_configuration, |
|
|
|
|
"Time" |
|
|
|
|
) + "?token=" + result.Data.Token; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
@ -107,7 +116,7 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("查询普通录像信息列表:token无效"); |
|
|
|
|
return new DaHApiResult<RecordsResDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
@ -116,7 +125,7 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
using var req = new HttpRequestMessage(HttpMethod.Post, url) |
|
|
|
|
{ |
|
|
|
|
Content = JsonContent.Create(dto) // 关键:把 dto 放进请求体 |
|
|
|
|
Content = JsonContent.Create(dto.Data) // 关键:把 dto 放进请求体 |
|
|
|
|
}; |
|
|
|
|
req.Headers.TryAddWithoutValidation("Authorization", token); |
|
|
|
|
|
|
|
|
|
@ -164,7 +173,7 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("通道分页查询失败:token无效"); |
|
|
|
|
return new DaHApiResult<PageInfoDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
@ -211,7 +220,7 @@ namespace Video.DomainService |
|
|
|
|
/// <param name="dto"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
/// <exception cref="NotImplementedException"></exception> |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RealtimeStreamUrl(StreamReqDto dto) |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RealtimeStreamUrl(StreamReqDto dto, string? ipaddress) |
|
|
|
|
{ |
|
|
|
|
// 1) 参数校验 + 早退 |
|
|
|
|
if (dto == null || dto.Data == null) |
|
|
|
|
@ -225,7 +234,7 @@ namespace Video.DomainService |
|
|
|
|
// 2) Token:优先用入参;否则走缓存/获取(建议返回已带前缀的 "Bearer xxx") |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning(" HLS实时流请求失败:token无效"); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
@ -233,10 +242,15 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/admin/API/video/stream/realtime"; |
|
|
|
|
|
|
|
|
|
if (ipaddress != null) |
|
|
|
|
{ |
|
|
|
|
url = $"https://{ipaddress}/evo-apigw/admin/API/video/stream/realtime"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3) 用 HttpRequestMessage 发送,才能带上自定义头 |
|
|
|
|
using var req = new HttpRequestMessage(HttpMethod.Post, url) |
|
|
|
|
{ |
|
|
|
|
Content = JsonContent.Create(dto) // 等价 PostAsJsonAsync,但不会丢头 |
|
|
|
|
Content = JsonContent.Create(dto.Data) // 等价 PostAsJsonAsync,但不会丢头 |
|
|
|
|
}; |
|
|
|
|
req.Headers.TryAddWithoutValidation("Authorization", token); |
|
|
|
|
|
|
|
|
|
@ -255,7 +269,12 @@ namespace Video.DomainService |
|
|
|
|
_logger.LogWarning("实时流请求业务失败: {Body}", body); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1010", Msg = "实时流请求失败" }; |
|
|
|
|
} |
|
|
|
|
result.Data!.Url = result.Data.Url + "?token=" + token; |
|
|
|
|
result.Data!.Url = UrlHostReplacer.ReplaceHost( |
|
|
|
|
result.Data.Url, |
|
|
|
|
ipaddress, |
|
|
|
|
_configuration, |
|
|
|
|
"Real" |
|
|
|
|
) + "?token=" + result.Data.Token; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
@ -318,12 +337,12 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// rtsp录像回放 |
|
|
|
|
/// (播放命令:ffplay -rtsp_transport tcp -i "rtsp://demo.weienergy.cn:15211/playback/pu/3?token=3")强制走tcp |
|
|
|
|
/// (播放命令:ffplay -rtsp_transport tcp -i "rtsp://demo.weienergy.cn:15210/dss/monitor/param/cameraid=1000021%24104%26substream=1?token=430")强制走tcp |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="dto"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
/// <exception cref="NotImplementedException"></exception> |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RtspPlaybackByTime(RtspPlayBackReqDto dto) |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RtspPlaybackByTime(RtspPlayBackReqDto dto, string? ipaddress) |
|
|
|
|
{ |
|
|
|
|
// 参数校验 + 早退 |
|
|
|
|
if (dto == null || string.IsNullOrWhiteSpace(dto.Data.ChannelId)) |
|
|
|
|
@ -335,17 +354,21 @@ namespace Video.DomainService |
|
|
|
|
// 先用缓存里的 token,不足5分钟过期再刷新(按你之前的口径来) |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("rtsp录像回放:token无效"); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime"; |
|
|
|
|
if (ipaddress != null) |
|
|
|
|
{ |
|
|
|
|
url = $"https://{ipaddress}/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
using var req = new HttpRequestMessage(HttpMethod.Post, url) |
|
|
|
|
{ |
|
|
|
|
Content = JsonContent.Create(dto) // 关键:把 dto 放进请求体 |
|
|
|
|
Content = JsonContent.Create(dto.Data) // 关键:把 dto 放进请求体 |
|
|
|
|
}; |
|
|
|
|
req.Headers.TryAddWithoutValidation("Authorization", token); |
|
|
|
|
|
|
|
|
|
@ -366,7 +389,12 @@ namespace Video.DomainService |
|
|
|
|
_logger.LogWarning("录像请求业务失败: {Body}", body); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1008", Msg = "录像请求失败" }; |
|
|
|
|
} |
|
|
|
|
result.Data!.Url = result.Data.Url.Replace(_configuration["DahuaAuth:TimeRootHost"], _configuration["DahuaAuth:TimeReplaceHost"]) + "?token=" + result.Data.Token; |
|
|
|
|
result.Data!.Url = UrlHostReplacer.ReplaceHost( |
|
|
|
|
result.Data.Url, |
|
|
|
|
ipaddress, |
|
|
|
|
_configuration, |
|
|
|
|
"Time" |
|
|
|
|
) + "?token=" + result.Data.Token; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
@ -383,7 +411,7 @@ namespace Video.DomainService |
|
|
|
|
/// <param name="dto"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
/// <exception cref="NotImplementedException"></exception> |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RtspStartVideoUrl(StreamRtspReqDto dto) |
|
|
|
|
public async Task<DaHApiResult<UrlDataDto>> RtspStartVideoUrl(StreamRtspReqDto dto, string? ipaddress) |
|
|
|
|
{ |
|
|
|
|
if (dto == null || dto.Data == null) |
|
|
|
|
{ |
|
|
|
|
@ -393,13 +421,17 @@ namespace Video.DomainService |
|
|
|
|
|
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("rtsp实时预览接口方式:token无效"); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1009", Msg = "token无效" }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/admin/API/MTS/Video/StartVideo"; |
|
|
|
|
if (ipaddress != null) |
|
|
|
|
{ |
|
|
|
|
url = $"https://{ipaddress}/evo-apigw/admin/API/MTS/Video/StartVideo"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
using var req = new HttpRequestMessage(HttpMethod.Post, url) |
|
|
|
|
{ |
|
|
|
|
@ -424,7 +456,12 @@ namespace Video.DomainService |
|
|
|
|
_logger.LogWarning("实时流请求业务失败: {Body}", body); |
|
|
|
|
return new DaHApiResult<UrlDataDto> { Success = false, Code = "1010", Msg = "实时流请求失败" }; |
|
|
|
|
} |
|
|
|
|
result.Data!.Url = result.Data.Url.Replace(_configuration["DahuaAuth:RealRootHost"], _configuration["DahuaAuth:RealReplaceHost"]) + "?token=" + result.Data!.Token; |
|
|
|
|
result.Data!.Url = UrlHostReplacer.ReplaceHost( |
|
|
|
|
result.Data.Url, |
|
|
|
|
ipaddress, |
|
|
|
|
_configuration, |
|
|
|
|
"Real" |
|
|
|
|
) + "?token=" + result.Data.Token; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
@ -444,13 +481,18 @@ namespace Video.DomainService |
|
|
|
|
{ |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
if (_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
if (!_tokenProviderService.IsTokenValid(token)) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("下载:token无效"); |
|
|
|
|
return "下载地址无效"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return _configuration["DahuaAuth:Host"] + $"/evo-apigw/evo-httpnode/vod/cam/download.mp4?vcuid={dto.Vcuid}&subtype={dto.Subtype}&starttime={dto.StartTime}endtime={dto.EndTime}&videoType={dto.VideoType}&token={token}&recordType={dto.RecordType}"; |
|
|
|
|
var url = _configuration["DahuaAuth:Host"]; |
|
|
|
|
if (dto.IpAddress != null) |
|
|
|
|
{ |
|
|
|
|
url = dto.IpAddress; |
|
|
|
|
} |
|
|
|
|
return url + $"/evo-apigw/evo-httpnode/vod/cam/download.mp4?vcuid={dto.Vcuid}&subtype={dto.Subtype}&starttime={dto.StartTime}endtime={dto.EndTime}&videoType={dto.VideoType}&token={token}&recordType={dto.RecordType}"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |