海康威视官网下载最新版本设备网络SDK:http://www.hikvision.com/Cn/download_more_401.html
下载好SDK后,将需要的DLL导入到Unity的Plugins文件夹内
还有案例里的CHCNetSDK脚本导入Unity内
新建物体挂载脚本,下面是脚本
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
//Frame Info
public struct Frame_Info
{
public int nWidth;
public int nHeight;
public int nStamp;
public int nType;
public int nFrameRate;
public uint dwFrameNum;
public void Init()
{
nWidth = 0;
nHeight = 0;
nStamp = 0;
nType = 0;
nFrameRate = 0;
dwFrameNum = 0;
}
}
public class HIKCamera : MonoBehaviour
{
// add by gb 080131 version 4.9.0.1
[DllImport("PlayCtrl")]
public static extern bool PlayM4_GetPort(ref int nPort);
[DllImport("PlayCtrl")]
public static extern uint PlayM4_GetLastError(int nPort);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_SetStreamOpenMode(int nPort, uint nMode);
//Stream type
public const int STREAME_REALTIME = 0;
public const int STREAME_FILE = 1;
[DllImport("PlayCtrl")]
public static extern bool PlayM4_OpenStream(int nPort, IntPtr pFileHeadBuf, uint nSize, uint nBufPoolSize);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_SetDisplayBuf(int nPort, uint nNum);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_SetOverlayMode(int nPort, int bOverlay, uint colorKey);
public delegate void DECCBFUN(int nPort, IntPtr pBuf, int nSize, ref Frame_Info pFrameInfo, int nReserved1, int nReserved2);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_SetDecCallBackEx(int nPort, DECCBFUN DecCBFun, IntPtr pDest, int nDestSize);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_Play(int nPort, IntPtr hWnd);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_InputData(int nPort, IntPtr pBuf, uint nSize);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_SetDecCallBack(int nPort, DECCBFUN DecCBFun);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_Stop(int nPort);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_CloseStream(int nPort);
[DllImport("PlayCtrl")]
public static extern bool PlayM4_FreePort(int nPort);
private bool m_bInitSDK = false;
private bool m_bRecord = false;
private uint iLastErr = 0;
public Int32 m_lUserID = -1;
private Int32 m_lRealHandle = -1;
private string str;
private Int32 m_lPort = -1;
private IntPtr m_ptrRealHandle;
private CHCNetSDK.REALDATACALLBACK RealData = null;
public CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIpParaCfgV40;
public CHCNetSDK.NET_DVR_STREAM_MODE m_struStreamMode;
public CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo;
public CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40;
private DECCBFUN m_fDisplayFun = null;
int width = 3840;//视屏的宽
int height = 2160;//视屏的高
public bool isPlaying;
void Start()
{
InitSDK();
}
/// <summary>
/// 初始化,一次就行(初始化->登录->获取视屏流)
/// </summary>
private void InitSDK()
{
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
Debug.LogError("初始化失败");
return;
}
else
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
Debug.Log("初始化成功");
Login("192.168.1.64", "8000", "admin", "你的密码");
}
}
/// <summary>
/// 登录(初始化->登录->获取视屏流)
/// </summary>
/// <param name="Ip"></param>
/// <param name="Port"></param>
/// <param name="UserName"></param>
/// <param name="PassWord"></param>
/// <param name="action"></param>
public void Login(string Ip, string Port, string UserName, string PassWord)
{
if (m_lUserID < 0)
{
//登录设备
m_lUserID = CHCNetSDK.NET_DVR_Login_V30(Ip, Int16.Parse(Port), UserName, PassWord, ref DeviceInfo);
if (m_lUserID < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
Debug.LogError("登录失败:错误号+" + iLastErr);
return;
}
else
{
Debug.Log("登录成功");
}
}
VideoBtnClick(1);
}
/// <summary>
/// 登出
/// </summary>
public void LogOut()
{
//注销登录 Logout the device
if (m_lRealHandle >= 0)
{
Debug.LogError("Please stop live view firstly"); //登出前先停止预览 Stop live view before logout
CloseVideo();
//return;
}
if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Logout failed, error code= " + iLastErr;
Debug.LogError(str);
return;
}
Debug.Log("NET_DVR_Logout success!");
m_lUserID = -1;
}
/// <summary>
/// 获取视屏流(初始化->登录->获取视屏流)
/// </summary>
/// <param name="CameraIndex">多路摄像机,具体那一路</param>
public void VideoBtnClick(int CameraIndex)
{
if (m_lUserID < 0)
{
Debug.LogError("Please login the device firstly!");
return;
}
if (m_bRecord)
{
Debug.LogError("Please stop recording firstly!");
return;
}
if (m_lRealHandle < 0)
{
CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
lpPreviewInfo.lChannel = CameraIndex;//预览的设备通道 the device channel number
lpPreviewInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推 可以切换码流类型查看不同的效果,选择最佳的类型
lpPreviewInfo.dwLinkMode = 0;//连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
lpPreviewInfo.bBlocked = true; //0- 非阻塞取流,1- 阻塞取流
lpPreviewInfo.dwDisplayBufNum = 15; //播放库显示缓冲区最大帧数
IntPtr pUser = IntPtr.Zero;//用户数据 user data
//直接打开视屏
lpPreviewInfo.hPlayWnd = IntPtr.Zero;//预览窗口 live view window
RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);//预览实时流回调函数 real-time stream callback function
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, RealData, pUser);
if (m_lRealHandle < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
Debug.LogError("预览失败, 错误号" + iLastErr);
}
else
{
//预览成功
Debug.Log("预览成功");
}
}
else
{
Debug.LogError("m_lRealHandle Error!");
}
}
/// <summary>
/// 停止视屏预览
/// </summary>
public void CloseVideo()
{
//停止预览 Stop live view
if (!CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_StopRealPlay failed, error code= " + iLastErr + "\n" + m_lRealHandle;
Debug.Log(str);
return;
}
if ((m_lPort >= 0))
{
if (!PlayM4_Stop(m_lPort))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_Stop failed, error code= " + iLastErr;
Debug.LogError(str);
}
if (!PlayM4_CloseStream(m_lPort))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_CloseStream failed, error code= " + iLastErr;
Debug.LogError(str);
}
if (!PlayM4_FreePort(m_lPort))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_FreePort failed, error code= " + iLastErr;
Debug.LogError(str);
}
m_lPort = -1;
}
Debug.Log("NET_DVR_StopRealPlay succ!");
m_lRealHandle = -1;
isDataGet = false;
isPlaying = false;
}
private UInt32 dwBufSizes;
private IntPtr pBuffers;
/// <summary>
/// 视屏回调
/// </summary>
/// <param name="lRealHandle"></param>
/// <param name="dwDataType"></param>
/// <param name="pBuffer"></param>
/// <param name="dwBufSize"></param>
/// <param name="pUser"></param>
public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser)
{
dwBufSizes = dwBufSize;
pBuffers = pBuffer;
//Debug.Log("RealDataCallBack:"+pBuffer);
//Debug.Log(dwDataType);
switch (dwDataType)
{
case CHCNetSDK.NET_DVR_SYSHEAD: // sys head
if (dwBufSize > 0)
{
if (m_lPort >= 0)
{
return; //同一路码流不需要多次调用开流接口
}
//获取播放句柄 Get the port to play
if (!PlayM4_GetPort(ref m_lPort))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_GetPort failed, error code= " + iLastErr;
Debug.LogError(str);
break;
}
//设置流播放模式 Set the stream mode: real-time stream mode
if (!PlayM4_SetStreamOpenMode(m_lPort, STREAME_REALTIME))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "Set STREAME_REALTIME mode failed, error code= " + iLastErr;
Debug.LogError(str);
}
//打开码流,送入头数据 Open stream
if (!PlayM4_OpenStream(m_lPort, pBuffer, dwBufSize, 2 * 1024 * 1024))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_OpenStream failed, error code= " + iLastErr;
Debug.LogError(str);
break;
}
//设置显示缓冲区个数 Set the display buffer number
if (!PlayM4_SetDisplayBuf(m_lPort, 15))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_SetDisplayBuf failed, error code= " + iLastErr;
Debug.LogError(str);
}
//设置显示模式 Set the display mode
if (!PlayM4_SetOverlayMode(m_lPort, 0, 0/* COLORREF(0)*/)) //play off screen
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_SetOverlayMode failed, error code= " + iLastErr;
Debug.LogError(str);
}
//设置解码回调函数,获取解码后音视频原始数据 Set callback function of decoded data
m_fDisplayFun = new DECCBFUN(DecCallbackFUN);
if (!PlayM4_SetDecCallBack(m_lPort, m_fDisplayFun))
{
Debug.LogError("PlayM4 CallBack Failed!");
}
//开始解码 Start to play
if (!PlayM4_Play(m_lPort, m_ptrRealHandle))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_Play failed, error code= " + iLastErr;
Debug.LogError(str);
break;
}
}
break;
case CHCNetSDK.NET_DVR_STREAMDATA: // video stream data
//Debug.Log("GetStreamData!");
if (dwBufSizes > 0 && m_lPort != -1)
{
for (int i = 0; i < 999; i++)
{
//送入码流数据进行解码 Input the stream data to decode
if (!PlayM4_InputData(m_lPort, pBuffers, dwBufSizes))
{
iLastErr = PlayM4_GetLastError(m_lPort);
str = "PlayM4_InputData failed, error code= " + iLastErr;
Debug.LogError(str);
Thread.Sleep(2);
}
else
{
break;
}
}
}
break;
default:
Debug.Log("GetOtherData!");
break;
}
}
private byte[] VideoData;
private bool isDataGet = false;
/// <summary>
/// 解码后视屏回调(YUV格式)
/// </summary>
/// <param name="nPort"></param>
/// <param name="pBuf"></param>
/// <param name="nSize"></param>
/// <param name="pFrameInfo"></param>
/// <param name="nReserved1"></param>
/// <param name="nReserved2"></param>
private void DecCallbackFUN(int nPort, IntPtr pBuf, int nSize, ref Frame_Info pFrameInfo, int nReserved1, int nReserved2)
{
if (pFrameInfo.nType == 3) //#define T_YV12 3
{
width = pFrameInfo.nWidth;
height = pFrameInfo.nHeight;
Debug.LogError("Video Width:" + width + " Height:" + height + " Size:" + nSize);
VideoData = new byte[nSize];
Marshal.Copy(pBuf, VideoData, 0, nSize);
isDataGet = true;
}
}
void FixedUpdate()
{
if (isDataGet)
{
ShowVideo(VideoData);
}
}
private DateTime StartTime;
private DateTime EndTime;
/// <summary>
/// 显示视屏流
/// </summary>
/// <param name="data">YV12视频流</param>
private void ShowVideo(byte[] data)
{
StartTime = DateTime.Now;
if ((StartTime - EndTime).TotalMilliseconds > 25)
{
ShowYUVFrames(width, height, data);
EndTime = DateTime.Now;
}
}
public Material VideoMaterial;
private Texture2D texY, texU, texV;
/// <summary>
/// 显示YUV帧图片
/// </summary>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="data">YUV比特流</param>
private void ShowYUVFrames(int width, int height, byte[] data)
{
StartCoroutine(readFrames(width, height, data));
}
/// <summary>
/// 读取YUV帧协程
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="data"></param>
/// <returns></returns>
private IEnumerator readFrames(int Width, int Height, byte[] data)
{
yield return null;
if (data.Length > 0)
{
byte[] dataY = new byte[Width * Height];
byte[] dataU = new byte[Width * Height / 4];
byte[] dataV = new byte[Width * Height / 4];
Buffer.BlockCopy(data, 0, dataY, 0, Width * Height);
Buffer.BlockCopy(data, Width * Height, dataU, 0, Width * Height / 4);
Buffer.BlockCopy(data, Width * Height * 5 / 4, dataV, 0, Width * Height / 4);
CreateTexture(Width, Height, dataY, dataU, dataV);
}
}
/// <summary>
/// 通过YUV数据创建图片
/// </summary>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="dataY">灰度图片Y</param>
/// <param name="dataU">灰度图片U</param>
/// <param name="dataV">灰度图片V</param>
private void CreateTexture(int width, int height, byte[] dataY, byte[] dataU, byte[] dataV)
{
//Y
if (texY)
{
Destroy(texY);
}
texY = new Texture2D(width, height, TextureFormat.Alpha8, false);
texY.LoadRawTextureData(dataY);
texY.Apply();
//U
if (texU)
{
Destroy(texU);
}
texU = new Texture2D(width / 2, height / 2, TextureFormat.Alpha8, false);
texU.LoadRawTextureData(dataU);
texU.Apply();
//V
if (texV)
{
Destroy(texV);
}
texV = new Texture2D(width / 2, height / 2, TextureFormat.Alpha8, false);
texV.LoadRawTextureData(dataV);
texV.Apply();
//VideoMaterial.mainTexture = texY;
VideoMaterial.SetTexture("_MainTex", texY);
VideoMaterial.SetTexture("_MainTexU", texU);
VideoMaterial.SetTexture("_MainTexV", texV);
}
void OnDestroy()
{
CloseVideo();
CHCNetSDK.NET_DVR_Cleanup();
Debug.Log("Quit Success! " + transform.name);
}
}
脚本写好后,新建材质球以及一个Shader,下面是Shader,名为YUVShader
// Unlit alpha-blended shader.
// - no lighting
// - no lightmap support
// - no per-material color
Shader "Unlit/YUVShader"
{
Properties
{
_MainTex("Y", 2D) = "white" {}
_MainTexU("U", 2D) = "white" {}
_MainTexV("V", 2D) = "white" {}
}
SubShader
{
Tags {"IgnoreProjector" = "True"}
LOD 100
//Cull Front
ZWrite Off
CGPROGRAM
#pragma surface surf No noforwardadd noambient
sampler2D _MainTex;
sampler2D _MainTexU;
sampler2D _MainTexV;
struct Input
{
half2 uv_MainTex;
half2 uv_MainTexU;
half2 uv_MainTexV;
};
void surf(Input IN, inout SurfaceOutput o)
{
half2 uv = IN.uv_MainTex;
//uv.x = 1 - uv.x;
uv.y = 1 - uv.y;
half y = tex2D(_MainTex, uv).a;
//dx9,dx11
//int r = tex2D(_MainTexUV, IN.uv_MainTexUV).r*65535.0h;
//half u = tex2D(_MainTexUV, IN.uv_MainTexUV).r;
//half v = (uint(u*65535.0h)%256)*0.00392159h;
//opengl
//half4 rgba = tex2D(_MainTexUV, IN.uv_MainTexUV);
uv = IN.uv_MainTexU;
//uv.x = 1 - uv.x;
uv.y = 1 - uv.y;
half u = tex2D(_MainTexU, uv).a;
uv = IN.uv_MainTexV;
//uv.x = 1 - uv.x;
uv.y = 1 - uv.y;
half v = tex2D(_MainTexV, uv).a;
o.Albedo.r = y - 1.6 *(v - 128. / 255.);
o.Albedo.g = y - 1.6 *(u - 128. / 255.) - 1.6 *(v - 128. / 255.);
o.Albedo.b = y - 1.6 *(u - 128. / 255.);
/*o.Albedo.r = y + 1.370705*(v - 128. / 255.);
o.Albedo.g = y - (0.698001*(u - 128. / 255.)) - (0.703125*(v - 128. / 255.));
o.Albedo.b = y + 1.732446*(u - 128. / 255.);*/
/*o.Albedo.r = y + 1.596 * (v - 128. / 255.);
o.Albedo.g = y - 0.391 * (u - 128. / 255.) - 0.813 * (v - 128. / 255.);
o.Albedo.b = y + 2.018 * (u - 128. / 255.);*/
}
inline fixed4 LightingNo(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed4 c = 0;
c.rgb = s.Albedo;
return c;
}
ENDCG
}
}
将新建的材质球托给脚本,场景内新建一个Plane,将材质球赋给Plane,运行即可。显示会有些许色差,抱歉。