【General】Starting Live View-2

サンプルプログラム

//
// Device Network SDK (General)
// Sample Code of Starting Live View
// Decode and Display via PlayCtrl Library (for API named by the prefix “PlayM4_”,
// refer to User Manual of Cross-Platform PlayCtrl Library SDK)
//
#include <stdio.h>
#include <iostream>
#include “Windows.h”
#include “HCNetSDK.h”
#include <time.h>

using namespace std;

typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();

PROCGETCONSOLEWINDOW GetConsoleWindowAPI;

void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
    char tempbuf[256] = {0};
    switch(dwType)
    {
    case EXCEPTION_RECONNECT:    //Reconnect during live view
        printf(“———-reconnect——–%d\n”, time(NULL));
    break;
default:
    break;
    }

}

void main() {
  //—————————————
  // Initialize
  NET_DVR_Init();
  //Set connection time and reconnection time
  NET_DVR_SetConnectTime(2000, 1);

NET_DVR_SetReconnect(10000, true);

  //—————————————
  //Set callback function for exceptional message

NET_DVR_SetExceptionCallBack_V30(0, NULL,g_ExceptionCallBack, NULL);

  //—————————————
  // Get the window handle
  HMODULE hKernel32 = GetModuleHandle(“kernel32”);

GetConsoleWindowAPI = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,”GetConsoleWindow”);

  //—————————————
  // Log in to device

LONG lUserID;

  //Login parameters, including device IP address, user name, password, and so on.
  NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
  struLoginInfo.bUseAsynLogin = 0; //Synchronous login mode
  strcpy(struLoginInfo.sDeviceAddress, “10.13.35.229”); //Device IP address
  struLoginInfo.wPort = 8000; //Service port No.
  strcpy(struLoginInfo.sUserName, “admin”); //User name

strcpy(struLoginInfo.sPassword, “abcd1234”); //Password

  //Device information, output parameter
  NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};

lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);

  if (lUserID < 0)
  {
      printf(“Login failed, error code: %d\n”, NET_DVR_GetLastError());
      NET_DVR_Cleanup();
      return;

}

  //—————————————
  //Start live view and set stream callback function
  LONG lRealPlayHandle;
  HWND hWnd = GetConsoleWindowAPI();     //Get window handle
  NET_DVR_PREVIEWINFO struPlayInfo = {0};
  struPlayInfo.hPlayWnd = hWnd;         //Set the handle as valid for SDK decoding; set the handle as null for streaming only
  struPlayInfo.lChannel     = 1;       //Live view channel No.
  struPlayInfo.dwStreamType = 0;       //0-Main Stream, 1-Sub Stream, 2-Stream 3, 3-Stream 4, and so on
  struPlayInfo.dwLinkMode   = 0;       //0- TCP Mode, 1- UDP Mode, 2- Multicast Mode, 3- RTP Mode, 4-RTP/RTSP, 5-RSTP/HTTP

struPlayInfo.bBlocked     = 1;       //0- Non-blocking Streaming, 1- Blocking Streaming

  lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, NULL, NULL);
  if (lRealPlayHandle < 0)
  {
      printf(“NET_DVR_RealPlay_V40 error\n”);
      NET_DVR_Logout(lUserID);
      NET_DVR_Cleanup();
      return;

}

  Sleep(10000);
  //—————————————
  //Stop live view
  NET_DVR_StopRealPlay(lRealPlayHandle);
  //Log out
  NET_DVR_Logout(lUserID);
  //Release SDK resource
  NET_DVR_Cleanup();
  return;

}

Decode and Display via PlayCtrl Library (for API named by the prefix “PlayM4_”, refer to User Manual of Cross-Platform PlayCtrl Library SDK)
#include <stdio.h>
#include <iostream>
#include “Windows.h”
#include “HCNetSDK.h”
#include “plaympeg4.h”
#include <time.h>

using namespace std;

typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
PROCGETCONSOLEWINDOW GetConsoleWindowAPI;

LONG lPort; //Port No. of global player SDK

void CALLBACK g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize,void* dwUser)
{
    HWND hWnd=GetConsoleWindowAPI();
switch (dwDataType)
{

case NET_DVR_SYSHEAD: //System header

if (m_lPort >= 0)
{
break;  //The handle of this channel is areadly obtained before streaming. There is no need to call it in the following.

}

if (!PlayM4_GetPort(&lPort))  //Get unused channel No. of player
{
break;
}

//m_iPort = lPort; //The firstly called back is system header. The ontained player SDK port No. will be assigned to global port No. for next time play.

if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME))  //Set play mode of real-time stream
{
break;

}

if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, 1024*1024)) //Open stream API
{
break;

}

if (!PlayM4_Play(lPort, hWnd)) //Start playing
{
break;
}
}
        break;
case NET_DVR_STREAMDATA:   //Stream data
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
        break;
    default: //Other data
        if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
        break;
}

}

void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
    char tempbuf[256] = {0};
    switch(dwType)
    {
    case EXCEPTION_RECONNECT:    //Reconnect during live view
    printf(“———-reconnect——–%d\n”, time(NULL));
    break;
default:
    break;
    }

}

void main() {
  //—————————————
  // Initialize
  NET_DVR_Init();
  //Set connection time and reconnection time
  NET_DVR_SetConnectTime(2000, 1);

NET_DVR_SetReconnect(10000, true);

  //—————————————
  //Set callback function for exceptional message

NET_DVR_SetExceptionCallBack_V30(0, NULL,g_ExceptionCallBack, NULL);

  //—————————————
  // Get the window handle
  HMODULE hKernel32 = GetModuleHandle(“kernel32”);

GetConsoleWindowAPI = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,”GetConsoleWindow”);

  //—————————————
  // Log in to device

LONG lUserID;

  //Login parameters, including device IP address, user name, password, and so on.
  NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
  struLoginInfo.bUseAsynLogin = 0; //Synchronous login mode
  strcpy(struLoginInfo.sDeviceAddress, “10.13.35.229”); //Device IP address
  struLoginInfo.wPort = 8000; //Service port No.
  strcpy(struLoginInfo.sUserName, “admin”); //User name

strcpy(struLoginInfo.sPassword, “abcd1234”); //Password

  //Device information, output parameter

NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};

  lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
  if (lUserID < 0)
  {
      printf(“Login failed, error code: %d\n”, NET_DVR_GetLastError());
      NET_DVR_Cleanup();
      return;

}

  //—————————————
  //Start live view and set stream callback function
  LONG lRealPlayHandle;
  NET_DVR_PREVIEWINFO struPlayInfo = {0};
  struPlayInfo.hPlayWnd     = NULL;    //Set the handle as valid for SDK decoding; set the handle as null for streaming only
  struPlayInfo.lChannel     = 1;       //Live view channel No.
  struPlayInfo.dwStreamType = 0;       //0-Main Stream, 1-Sub Stream, 2-Stream 3, 3-Stream 4, and so on
  struPlayInfo.dwLinkMode   = 0;       //0- TCP Mode, 1- UDP Mode, 2- Multicast Mode, 3- RTP Mode, 4-RTP/RTSP, 5-RSTP/HTTP
  struPlayInfo.bBlocked     = 1;       //0- Non-blocking Streaming, 1- Blocking Streaming

lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, g_RealDataCallBack_V30, NULL);

  if (lRealPlayHandle < 0)
  {
      printf(“NET_DVR_RealPlay_V40 error, %d\n”, NET_DVR_GetLastError());
      NET_DVR_Logout(lUserID);
      NET_DVR_Cleanup();
      return;

}

  Sleep(10000);
  //—————————————
  //Stop live view

NET_DVR_StopRealPlay(lRealPlayHandle);

  //Release player SDK resources
  PlayM4_Stop(lPort);
  PlayM4_CloseStream(lPort);

PlayM4_FreePort(lPort);

  //Log out
  NET_DVR_Logout(lUserID);
  NET_DVR_Cleanup();
  return;
}

関連記事