基于MinGW的海康视频监控开发
工程配置
项 | 说明 |
集成开发环境 | Eclipse 4.3.2 + MinGW工具链(TDM-GCC 4.8) |
工程配置 | 工程类型:C++ Project 宏定义:_WIN32、UNICODE 头文件路径:D:\CPP\tools\CH-HCNetSDK\win32-4.3.0.6\include 依赖库:HCNetSDK 库路径:D:\CPP\tools\CH-HCNetSDK\win32-4.3.0.6\dll Linker flags:-mwindows |
基于SDK直接解码的直播样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
/* * SDKDirectPlayBack.cpp * * Created on: May 14, 2014 * Author: WangZhen */ #include <stdio.h> #include <windows.h> #include "HCNetSDK.h" #include <time.h> class RealPlayInfo { public: LONG lUserID; LONG lRealPlayHandle; RealPlayInfo() : lUserID( -1 ), lRealPlayHandle( -1 ) { } ; bool isSucceed() { return this->lUserID >= 0; } }; void CALLBACK g_ExceptionCallBack( DWORD dwType, LONG lUserID, LONG lHandle, void* pUser ) { switch ( dwType ) { case EXCEPTION_RECONNECT : printf( "Reconnecting...%d\n", time( NULL ) ); break; default : break; } } RealPlayInfo* StartRealPlay( HWND hWnd, char* ip, int port = 8000, int chnl = 1, char* user = "admin", char* pswd = "12345" ) { RealPlayInfo* info = new RealPlayInfo; NET_DVR_DEVICEINFO_V30 struDeviceInfo; info->lUserID = NET_DVR_Login_V30( ip, 8000, user, pswd, &struDeviceInfo ); //登录 if ( info->lUserID < 0 ) { printf( "Failed to logged on to %s, Error Code: %d\n", ip, NET_DVR_GetLastError() ); return info; } else { printf( "Logon successful.\n" ); } NET_DVR_SetExceptionCallBack_V30( 0, NULL, g_ExceptionCallBack, NULL ); //启动预览并设置回调数据流 NET_DVR_PREVIEWINFO struPlayInfo = { 0 }; //使用SDK直接解码 struPlayInfo.hPlayWnd = hWnd; //预览通道号 struPlayInfo.lChannel = chnl; //0主码流,1子码流 struPlayInfo.dwStreamType = 0; //0 TCP,1 UDP,2 多播,3 RTP,4 RTP/RTSP,5 RSTP/HTTP struPlayInfo.dwLinkMode = 0; //0- 非阻塞取流,1- 阻塞取流 struPlayInfo.bBlocked = 1; info->lRealPlayHandle = NET_DVR_RealPlay_V40( info->lUserID, &struPlayInfo, NULL, NULL ); if ( info->lRealPlayHandle < 0 ) { printf( "Failed to start RealPlay.\n" ); NET_DVR_Logout( info->lUserID ); return info; } else { printf( "RealPlay started successfully.\n" ); } return info; } void StopRealPlay( RealPlayInfo* info ) { if ( !info->isSucceed() ) return; printf( "Stopping RealPlay.\n" ); //停止预览 NET_DVR_StopRealPlay( info->lRealPlayHandle ); //注销用户 NET_DVR_Logout( info->lUserID ); } LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); //窗口过程声明 int main() { HINSTANCE hInstance = GetModuleHandle( NULL ); static TCHAR szAppName[] = TEXT( "HikSDKDirectPlayBack" ); HWND hWnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wndclass.hCursor = LoadCursor( NULL, IDC_ARROW ); wndclass.hbrBackground = ( HBRUSH ) GetStockObject( BLACK_BRUSH ); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; RegisterClass( &wndclass ); int w = 640; int h = 480; hWnd = CreateWindow( szAppName, TEXT("MinGW海康SDK直播实例"), WS_OVERLAPPEDWINDOW, (GetSystemMetrics(SM_CXSCREEN) - w)/2, (GetSystemMetrics(SM_CYSCREEN) - h)/2, w, h, NULL, NULL, hInstance, NULL ); printf( "Main window created.\n" ); printf( "Prepare to render main window.\n" ); ShowWindow( hWnd, 10 ); UpdateWindow( hWnd ); while ( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return msg.wParam; } //窗口过程定义 LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM IParam ) { HDC hdc; PAINTSTRUCT ps; RECT rect; static RealPlayInfo* info; switch ( message ) { case WM_CREATE : NET_DVR_Init(); NET_DVR_SetConnectTime( 2000, 1 ); NET_DVR_SetReconnect( 10000, true ); info = StartRealPlay( hwnd, "192.168.0.196" ); return 0; case WM_DESTROY : PostQuitMessage( 0 ); StopRealPlay( info ); NET_DVR_Cleanup(); return 0; } return DefWindowProc( hwnd, message, wParam, IParam ); } |
Leave a Reply