pans
2016-12-15 87e3ee273b2f84081ac45926be9d8e5be3166eaa
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
 /*
* Copyright(C) 2011,Hikvision Digital Technology Co., Ltd 
* File   name£ºmain.cpp
* Discription£ºdemo for muti thread get stream
* Version    £º1.0
* Author     £ºluoyuhua
* Create Date£º2011-12-10
* Modification History£º
*/
 
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "HCNetSDK.h"
#include "iniFile.h"
 
// È¡Á÷Ïà¹ØÐÅÏ¢£¬ÓÃÓÚÏ̴߳«µÝ
typedef struct tagREAL_PLAY_INFO
{
    char szIP[16];
    int iUserID;
    int iChannel;
}REAL_PLAY_INFO, *LPREAL_PLAY_INFO;
 
// »Øµ÷ʵʱÁ÷£¬ÓÉÓÚµ÷ÓÃNET_DVR_SaveRealData£¬´Ë´¦²»´¦Àí
void g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize,void* dwUser)
{
    //LPREAL_PLAY_INFO pPlayInfo = (LPREAL_PLAY_INFO)dwUser;
    //printf("[g_RealDataCallBack_V30]Get data, ip=%s, channel=%d, handle=%d, data size is %d, thread=%d\n", 
        //pPlayInfo->szIP, pPlayInfo->iChannel, lRealHandle, dwBufSize, pthread_self());
 
    //printf("[g_RealDataCallBack_V30]Get data, handle=%d, data size is %d, thread=%d\n", 
        //lRealHandle, dwBufSize, pthread_self());
    //NET_DVR_SaveRealData(lRealHandle, cFilename);
}
 
FILE *g_pFile = NULL;
 
void PsDataCallBack(LONG lRealHandle, DWORD dwDataType,BYTE *pPacketBuffer,DWORD nPacketSize, void* pUser)
{
 
    if (dwDataType  == NET_DVR_SYSHEAD)
    {    
        //дÈëÍ·Êý¾Ý
        g_pFile = fopen("./record/ps.dat", "wb");
        
        if (g_pFile == NULL)
        {
            printf("CreateFileHead fail\n");
            return;
        }
 
        //дÈëÍ·Êý¾Ý
        fwrite(pPacketBuffer, sizeof(unsigned char), nPacketSize, g_pFile);
        printf("write head len=%d\n", nPacketSize);
    }
    else
    {
        if(g_pFile != NULL)
        {
            fwrite(pPacketBuffer, sizeof(unsigned char), nPacketSize, g_pFile);
            printf("write data len=%d\n", nPacketSize);
        }
    }    
 
}
 
 
 
void GetStream()
{
    // ´ÓÅäÖÃÎļþ¶ÁÈ¡É豸ÐÅÏ¢ 
    IniFile ini("Device.ini");
    unsigned int dwSize = 0;
    char sSection[16] = "DEVICE";
 
    
    char *sIP = ini.readstring(sSection, "ip", "error", dwSize);
    int iPort = ini.readinteger(sSection, "port", 0);
    char *sUserName = ini.readstring(sSection, "username", "error", dwSize); 
    char *sPassword = ini.readstring(sSection, "password", "error", dwSize);
    int iChannel = ini.readinteger(sSection, "channel", 0);
        
    NET_DVR_DEVICEINFO_V30 struDeviceInfo;
    int iUserID = NET_DVR_Login_V30(sIP, iPort, sUserName, sPassword, &struDeviceInfo);
    if(iUserID >= 0)
    {
 
            //NET_DVR_CLIENTINFO ClientInfo = {0};
            //ClientInfo.lChannel     = iChannel;  //channel NO.
            //ClientInfo.lLinkMode    = 0;
            //ClientInfo.sMultiCastIP = NULL;
            //int iRealPlayHandle = NET_DVR_RealPlay_V30(iUserID, &ClientInfo, PsDataCallBack, NULL, 0);
        NET_DVR_PREVIEWINFO struPreviewInfo = {0};
        struPreviewInfo.lChannel =iChannel;
        struPreviewInfo.dwStreamType = 0;
        struPreviewInfo.dwLinkMode = 0;
        struPreviewInfo.bBlocked = 1;
        struPreviewInfo.bPassbackRecord  = 1;
        int iRealPlayHandle = NET_DVR_RealPlay_V40(iUserID, &struPreviewInfo, PsDataCallBack, NULL);
        if(iRealPlayHandle >= 0)
        {
            printf("[GetStream]---RealPlay %s:%d success, \n", sIP, iChannel, NET_DVR_GetLastError());
            //int iRet = NET_DVR_SaveRealData(iRealPlayHandle, "./record/realplay.dat");
            //NET_DVR_SetStandardDataCallBack(iRealPlayHandle, StandardDataCallBack, 0);
 
        }
        else
        {
            printf("[GetStream]---RealPlay %s:%d failed, error = %d\n", sIP, iChannel, NET_DVR_GetLastError());
        }
    }
    else
    {
        printf("[GetStream]---Login %s failed, error = %d\n", sIP, NET_DVR_GetLastError());
    }
}
 
 
int main()
{
    NET_DVR_Init();
    NET_DVR_SetLogToFile(3, "./record/");
    
    GetStream();
    
 
    char c = 0;
    while('q' != c)
    {
        printf("input 'q' to quit\n");
        printf("input: ");
        scanf("%c", &c);
    }
 
 
    NET_DVR_Cleanup();
    return 0;
}