forked from fajiao/OnvifServer-CSharp
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.
66 lines
1.9 KiB
66 lines
1.9 KiB
using EC.App.ThatBLL.Onvif.Intf;
|
|
using EC.App.ThatService.Onvif;
|
|
using EC.Onvif;
|
|
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EC.App.ThatBLL.Onvif
|
|
{
|
|
public class MediaBLL : IMediaBLL
|
|
{
|
|
private readonly OnvifClientService _ocService;
|
|
|
|
public MediaBLL(OnvifClientService onvifClientService)
|
|
{
|
|
_ocService = onvifClientService;
|
|
}
|
|
|
|
public bool IsConnected(string ip)
|
|
{
|
|
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
|
|
bool ret = flag && onvifClient.IsMediaContected();
|
|
return ret;
|
|
}
|
|
|
|
public async Task<string> GetStreamUrl(string ip)
|
|
{
|
|
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
|
|
if (!flag) return string.Empty;
|
|
string url = await onvifClient.GetStreamUri();
|
|
return url;
|
|
}
|
|
|
|
public async Task<string> GetSnapshotUrl(string ip)
|
|
{
|
|
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
|
|
if (!flag) return string.Empty;
|
|
string url = await onvifClient.GetSnapshotUri();
|
|
return url;
|
|
}
|
|
|
|
public async Task<string> GetSnapshot(string ip)
|
|
{
|
|
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
|
|
if (!flag) return string.Empty;
|
|
string url = await onvifClient.GetSnapshotUri();
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = "GET";
|
|
request.PreAuthenticate = true;
|
|
request.Credentials = new NetworkCredential(onvifClient.Username, onvifClient.Password);
|
|
using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
using Stream stream = response.GetResponseStream();
|
|
using MemoryStream mStream = new();
|
|
byte[] buffer = new byte[1024];
|
|
int byteCount;
|
|
do
|
|
{
|
|
byteCount = stream.Read(buffer, 0, buffer.Length);
|
|
mStream.Write(buffer, 0, byteCount);
|
|
} while (byteCount > 0);
|
|
mStream.Position = 0;
|
|
return Convert.ToBase64String(mStream.ToArray());
|
|
}
|
|
}
|
|
}
|