Of course, try my code. Just keep in mind i've made it for DVR i dont know if there is any diffrence for direct connection to camera.
import of DLLs:
#region RealPlay_V40
public delegate void fRealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser);
[DllImport(@"\Hikvision\CppDlls\HCNetSDK.dll", EntryPoint = "NET_DVR_RealPlay_V40")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, fRealDataCallBack_V30 fRealDataCallBack_V30, IntPtr pUser);
#endregion RealPlay_V40
structure definition:
#region RealPlay_V40
public struct NET_DVR_PREVIEWINFO
{
public int lChannel; // Channel no.
public uint dwStreamType; // Stream type 0-main stream,1-sub stream,2-third stream,3-forth stream,4-fifth stream,5-sixth stream,7-seventh stream,8-eighth stream,9-ninth stream,10-tenth stream
public uint dwLinkMode; // Protocol type: 0-TCP, 1-UDP, 2-Muticast, 3-RTP,4-RTP/RTSP, 5-RSTP/HTTP
public IntPtr hPlayWnd; // Play window's handle; set NULL to disable preview
public uint bBlocked; //If data stream requesting process is blocked or not: 0-no, 1-yes
//if true, the SDK Connect failure return until 5s timeout , not suitable for polling to preview.
public uint bPassbackRecord; //0- not enable ,1 enable
public byte byPreviewMode; // Preview mode 0-normal preview,2-delay preview
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = SDKConst.STREAM_ID_LEN, ArraySubType = UnmanagedType.I1)]
public byte[] byStreamID;//Stream ID
public byte byProtoType; //0-private,1-RTSP
public byte byRes1;
public byte byVideoCodingType;
public uint dwDisplayBufNum; //soft player display buffer size(number of frames), range:1-50, default:1
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 216, ArraySubType = UnmanagedType.I1)]
public byte[] byRes;
}
#endregion RealPlay_V40
call:
public Result RealPlayTest_V40(IntPtr phWnd)
{
Result result = new Result(true);
//tt = WrapperClass.NET_DVR_Init();
SDKMethods.NET_DVR_Init();
SDKMethods.NET_DVR_SetConnectTime(2000, 1);
SDKMethods.NET_DVR_SetConnectTime(10000, 5);
int lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo = new NET_DVR_DEVICEINFO_V30();
string ip = "192.168.1.240";
ushort port = 8000;
string user = "user";
string password = "password";
lUserID = SDKMethods.NET_DVR_Login_V30(ip, port, user, password, ref struDeviceInfo);
if (lUserID < 0)
{
result.AddError(string.Format("Login failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError()));
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
SDKMethods.fRealDataCallBack_V30 callback = new SDKMethods.fRealDataCallBack_V30(g_RealDataCallBack_V30);
int realPlayId = 0;
NET_DVR_PREVIEWINFO PrevInfo = new NET_DVR_PREVIEWINFO();
PrevInfo.hPlayWnd = phWnd;
PrevInfo.lChannel = 36;
PrevInfo.dwStreamType = 0;
PrevInfo.dwLinkMode = 1;
PrevInfo.bBlocked = 0;
PrevInfo.byPreviewMode = 0;
realPlayId = SDKMethods.NET_DVR_RealPlay_V40(lUserID, ref PrevInfo, callback, IntPtr.Zero);
if (realPlayId < 0)
{
string message = string.Format("RealPlay_v40 failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError());
Console.WriteLine(message);
result.AddError(message);
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
//Streaming by ~10 seconds
int counter = 0;
while (counter < 10)
{
System.Threading.Thread.Sleep(1000);
counter++;
}
if (!SDKMethods.NET_DVR_StopRealPlay(realPlayId))
{
string message = string.Format("RealPlay_V40 failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError());
Console.WriteLine(message);
result.AddError(message);
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
return result;
}
public void g_RealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser)
{
Console.WriteLine("Hello from g_RealDataCallBack_V30"); // for testing purposes
}
additional thing:
public class Result
{
private bool _isSuccessful;
private List<string> _errors;
private int _intValue = -1;
public Result()
{
_errors = new List<string>();
}
public Result(bool isSuccess)
{
_isSuccessful = isSuccess;
_errors = new List<string>();
}
public bool IsSuccessful
{
get
{
return _isSuccessful;
}
set
{
_isSuccessful = value;
}
}
public List<string> Errors
{
get
{
return _errors;
}
set
{
_errors = value;
}
}
public int IntValue
{
get
{
return _intValue;
}
set
{
_intValue = value;
}
}
public void AddError(string message)
{
_errors.Add(message);
}
}