diff --git a/AKStreamWeb/Config/AKStreamWeb.json b/AKStreamWeb/Config/AKStreamWeb.json index 22979d5..9aba99f 100644 --- a/AKStreamWeb/Config/AKStreamWeb.json +++ b/AKStreamWeb/Config/AKStreamWeb.json @@ -16,5 +16,6 @@ "LocalizationKingBaseDb": false, "ForwardUrlIn": "http://", "ForwardUrlOut": "http://", - "ForwardUrlOnRecord": "http://" + "ForwardUrlOnRecord": "http://", + "PushStreamIpGetUrl": "" } diff --git a/AKStreamWeb/Misc/AKStreamWebConfig.cs b/AKStreamWeb/Misc/AKStreamWebConfig.cs index d171321..7824362 100644 --- a/AKStreamWeb/Misc/AKStreamWebConfig.cs +++ b/AKStreamWeb/Misc/AKStreamWebConfig.cs @@ -23,6 +23,7 @@ public class AKStreamWebConfig private string? _forwardUrlIn = ""; private string? _forwardUrlOut = ""; private string? _forwardUrlOnRecord = ""; + private string? _pushStreamIpGetUrl = ""; /// @@ -188,5 +189,14 @@ public string ForwardUrlOnRecord get => _forwardUrlOnRecord; set => _forwardUrlOnRecord = value; } + + /// + /// GB28181获取设备推流地址的接口 + /// + public string PushStreamIpGetUrl + { + get => _pushStreamIpGetUrl; + set => _pushStreamIpGetUrl = value; + } } } \ No newline at end of file diff --git a/AKStreamWeb/Services/SipServerService.cs b/AKStreamWeb/Services/SipServerService.cs index 0b114d8..dccb2d0 100644 --- a/AKStreamWeb/Services/SipServerService.cs +++ b/AKStreamWeb/Services/SipServerService.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Threading; +using AKStreamWeb.Misc; using LibCommon; using LibCommon.Enums; using LibCommon.Structs; using LibCommon.Structs.DBModels; using LibCommon.Structs.GB28181; using LibCommon.Structs.GB28181.XML; +using LibCommon.Structs.Other; using LibCommon.Structs.WebRequest; using LibCommon.Structs.WebResponse; using LibGB28181SipServer; @@ -686,6 +688,66 @@ record = row.RecItems.FindLast(x => x.SsrcId.Equals(ssrcId)); pushMediaInfo.MediaServerIpAddress = mediaServer.IpV4Address; } + string ip = pushMediaInfo.MediaServerIpAddress; + + if (!string.IsNullOrEmpty(Common.AkStreamWebConfig.PushStreamIpGetUrl) && + UtilsHelper.IsUrl(Common.AkStreamWebConfig.PushStreamIpGetUrl)) + { + string url = Common.AkStreamWebConfig.PushStreamIpGetUrl.Trim(); + + try + { + string reqData = JsonHelper.ToJson(new ReqPushStreamGetIP() + { + DeviceId = sipDevice.DeviceId, + }); + Dictionary headers = new Dictionary(); + + + var httpRet = NetHelper.HttpPostRequest(url, headers, reqData, "utf-8", Common.AkStreamWebConfig.HttpClientTimeoutSec*1000); + if (!string.IsNullOrEmpty(httpRet)) + { + if (UtilsHelper.HttpClientResponseIsNetWorkError(httpRet)) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.Sys_HttpClientTimeout, + Message = ErrorMessage.ErrorDic![ErrorNumber.Sys_HttpClientTimeout], + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(rs)}"); + } + else + { + + var resRet = JsonHelper.FromJson(httpRet); + if (resRet != null && !string.IsNullOrEmpty(resRet.DeviceId) && + !string.IsNullOrEmpty(resRet.IpAddress)) + { + ip = resRet.IpAddress.Trim(); + GCommon.Logger.Debug( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址成功,将执行返回中指定的ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(resRet)}"); + + } + } + + } + } + catch (Exception ex) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.MediaServer_WebApiExcept, + Message = ErrorMessage.ErrorDic![ErrorNumber.MediaServer_WebApiExcept], + ExceptMessage = ex.Message, + ExceptStackTrace = ex.StackTrace, + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(rs)}"); + } + } + pushMediaInfo.MediaServerIpAddress=ip;//重新设置回去,如果成功获取,将会改写ip变量的值 + pushMediaInfo.PushStreamSocketType = videoChannel.RtpWithTcp == true ? PushStreamSocketType.TCP : PushStreamSocketType.UDP; try @@ -1095,6 +1157,66 @@ public static MediaServerStreamInfo LiveVideo(string deviceId, string channelId, pushMediaInfo.MediaServerIpAddress = mediaServer.IpV4Address; } + string ip = pushMediaInfo.MediaServerIpAddress; + + if (!string.IsNullOrEmpty(Common.AkStreamWebConfig.PushStreamIpGetUrl) && + UtilsHelper.IsUrl(Common.AkStreamWebConfig.PushStreamIpGetUrl)) + { + string url = Common.AkStreamWebConfig.PushStreamIpGetUrl.Trim(); + + try + { + string reqData = JsonHelper.ToJson(new ReqPushStreamGetIP() + { + DeviceId = deviceId, + }); + Dictionary headers = new Dictionary(); + + + var httpRet = NetHelper.HttpPostRequest(url, headers, reqData, "utf-8", Common.AkStreamWebConfig.HttpClientTimeoutSec*1000); + if (!string.IsNullOrEmpty(httpRet)) + { + if (UtilsHelper.HttpClientResponseIsNetWorkError(httpRet)) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.Sys_HttpClientTimeout, + Message = ErrorMessage.ErrorDic![ErrorNumber.Sys_HttpClientTimeout], + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{deviceId}-{channelId}->{JsonHelper.ToJson(rs)}"); + } + else + { + + var resRet = JsonHelper.FromJson(httpRet); + if (resRet != null && !string.IsNullOrEmpty(resRet.DeviceId) && + !string.IsNullOrEmpty(resRet.IpAddress)) + { + ip = resRet.IpAddress.Trim(); + GCommon.Logger.Debug( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址成功,将执行返回中指定的ip地址->{deviceId}-{channelId}->{JsonHelper.ToJson(resRet)}"); + + } + } + + } + } + catch (Exception ex) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.MediaServer_WebApiExcept, + Message = ErrorMessage.ErrorDic![ErrorNumber.MediaServer_WebApiExcept], + ExceptMessage = ex.Message, + ExceptStackTrace = ex.StackTrace, + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{deviceId}-{channelId}->{JsonHelper.ToJson(rs)}"); + } + } + pushMediaInfo.MediaServerIpAddress=ip;//重新设置回去,如果成功获取,将会改写ip变量的值 + pushMediaInfo.PushStreamSocketType = videoChannel.RtpWithTcp == true ? PushStreamSocketType.TCP : PushStreamSocketType.UDP; try @@ -1708,6 +1830,66 @@ record = row.RecItems.FindLast(x => x.SsrcId.Equals(ssrcId.ToString())); pushMediaInfo.MediaServerIpAddress = mediaServer.IpV4Address; } + string ip = pushMediaInfo.MediaServerIpAddress; + + if (!string.IsNullOrEmpty(Common.AkStreamWebConfig.PushStreamIpGetUrl) && + UtilsHelper.IsUrl(Common.AkStreamWebConfig.PushStreamIpGetUrl)) + { + string url = Common.AkStreamWebConfig.PushStreamIpGetUrl.Trim(); + + try + { + string reqData = JsonHelper.ToJson(new ReqPushStreamGetIP() + { + DeviceId = sipDevice.DeviceId, + }); + Dictionary headers = new Dictionary(); + + + var httpRet = NetHelper.HttpPostRequest(url, headers, reqData, "utf-8", Common.AkStreamWebConfig.HttpClientTimeoutSec*1000); + if (!string.IsNullOrEmpty(httpRet)) + { + if (UtilsHelper.HttpClientResponseIsNetWorkError(httpRet)) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.Sys_HttpClientTimeout, + Message = ErrorMessage.ErrorDic![ErrorNumber.Sys_HttpClientTimeout], + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(rs)}"); + } + else + { + + var resRet = JsonHelper.FromJson(httpRet); + if (resRet != null && !string.IsNullOrEmpty(resRet.DeviceId) && + !string.IsNullOrEmpty(resRet.IpAddress)) + { + ip = resRet.IpAddress.Trim(); + GCommon.Logger.Debug( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址成功,将执行返回中指定的ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(resRet)}"); + + } + } + + } + } + catch (Exception ex) + { + rs = new ResponseStruct() + { + Code = ErrorNumber.MediaServer_WebApiExcept, + Message = ErrorMessage.ErrorDic![ErrorNumber.MediaServer_WebApiExcept], + ExceptMessage = ex.Message, + ExceptStackTrace = ex.StackTrace, + }; + GCommon.Logger.Error( + $"[{Common.LoggerHead}]->请求Sip推流->获取指定流媒体服务器ip地址失败,将执行默认ip地址->{sipDevice.DeviceId}-{sipChannel.DeviceId}->{JsonHelper.ToJson(rs)}"); + } + } + pushMediaInfo.MediaServerIpAddress=ip;//重新设置回去,如果成功获取,将会改写ip变量的值 + pushMediaInfo.PushStreamSocketType = videoChannel.RtpWithTcp == true ? PushStreamSocketType.TCP : PushStreamSocketType.UDP; try diff --git a/LibCommon/Structs/Other/ReqPushStreamGetIP.cs b/LibCommon/Structs/Other/ReqPushStreamGetIP.cs new file mode 100644 index 0000000..2f1c64f --- /dev/null +++ b/LibCommon/Structs/Other/ReqPushStreamGetIP.cs @@ -0,0 +1,18 @@ +using System; + +namespace LibCommon.Structs.Other; + +[Serializable] +public class ReqPushStreamGetIP +{ + private string? _deviceId; + + /// + /// sip设备id + /// + public string? DeviceId + { + get => _deviceId; + set => _deviceId = value; + } +} \ No newline at end of file diff --git a/LibCommon/Structs/Other/ResPushStreamGetIP.cs b/LibCommon/Structs/Other/ResPushStreamGetIP.cs new file mode 100644 index 0000000..ed86cac --- /dev/null +++ b/LibCommon/Structs/Other/ResPushStreamGetIP.cs @@ -0,0 +1,28 @@ +using System; + +namespace LibCommon.Structs.Other; + +[Serializable] +public class ResPushStreamGetIP +{ + private string? _ipAddress; + private string? _deviceId; + + /// + /// 设备对应的流媒体服务器ip地址 + /// + public string? IpAddress + { + get => _ipAddress; + set => _ipAddress = value; + } + + /// + /// sip设备id + /// + public string? DeviceId + { + get => _deviceId; + set => _deviceId = value; + } +} \ No newline at end of file