Skip to content

Commit

Permalink
add pushMedaiserveripaddress get function for my company
Browse files Browse the repository at this point in the history
  • Loading branch information
chatop2020 committed Dec 20, 2023
1 parent f90d94b commit 3d364ac
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
3 changes: 2 additions & 1 deletion AKStreamWeb/Config/AKStreamWeb.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"LocalizationKingBaseDb": false,
"ForwardUrlIn": "http://",
"ForwardUrlOut": "http://",
"ForwardUrlOnRecord": "http://"
"ForwardUrlOnRecord": "http://",
"PushStreamIpGetUrl": ""
}
10 changes: 10 additions & 0 deletions AKStreamWeb/Misc/AKStreamWebConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AKStreamWebConfig
private string? _forwardUrlIn = "";
private string? _forwardUrlOut = "";
private string? _forwardUrlOnRecord = "";
private string? _pushStreamIpGetUrl = "";


/// <summary>
Expand Down Expand Up @@ -188,5 +189,14 @@ public string ForwardUrlOnRecord
get => _forwardUrlOnRecord;
set => _forwardUrlOnRecord = value;
}

/// <summary>
/// GB28181获取设备推流地址的接口
/// </summary>
public string PushStreamIpGetUrl
{
get => _pushStreamIpGetUrl;
set => _pushStreamIpGetUrl = value;
}
}
}
182 changes: 182 additions & 0 deletions AKStreamWeb/Services/SipServerService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, string> headers = new Dictionary<string, string>();


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<ResPushStreamGetIP>(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
Expand Down Expand Up @@ -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<string, string> headers = new Dictionary<string, string>();


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<ResPushStreamGetIP>(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
Expand Down Expand Up @@ -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<string, string> headers = new Dictionary<string, string>();


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<ResPushStreamGetIP>(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
Expand Down
18 changes: 18 additions & 0 deletions LibCommon/Structs/Other/ReqPushStreamGetIP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace LibCommon.Structs.Other;

[Serializable]
public class ReqPushStreamGetIP
{
private string? _deviceId;

/// <summary>
/// sip设备id
/// </summary>
public string? DeviceId
{
get => _deviceId;
set => _deviceId = value;
}
}
28 changes: 28 additions & 0 deletions LibCommon/Structs/Other/ResPushStreamGetIP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace LibCommon.Structs.Other;

[Serializable]
public class ResPushStreamGetIP
{
private string? _ipAddress;
private string? _deviceId;

/// <summary>
/// 设备对应的流媒体服务器ip地址
/// </summary>
public string? IpAddress
{
get => _ipAddress;
set => _ipAddress = value;
}

/// <summary>
/// sip设备id
/// </summary>
public string? DeviceId
{
get => _deviceId;
set => _deviceId = value;
}
}

0 comments on commit 3d364ac

Please sign in to comment.