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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
   /*
 * Copyright(C) 2010,Custom Co., Ltd 
 *    FileName: playback.cpp
 * Description: 
 *     Version: 1.0
 *      Author: panyadong
 * Create Date: 2010-4-24
 * Modification History£º
 */
 
#include "public.h"
#include <stdio.h>
#include <time.h>
#include "playback.h"
#include <stdlib.h>
#include <memory.h>
 
#ifdef _WIN32
#else
#include   <unistd.h> 
 
#endif
 
int saveFile(int userId,char * srcfile,char * destfile);
 
int g_hPlayback=0;
 
void CALLBACK fPlayDataCallBack(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize,DWORD dwUser)
{
    int pos = NET_DVR_GetDownloadPos(g_hPlayback);
    if (pos > 98)
    {
        fprintf(stderr, "pyd---Current Time:%d, data size:%d\n", time(NULL), dwBufSize);
        printf("Play over!%d\n", pos);
    }
}
 
int Demo_PlayBack(LONG lUserID)
{
    //Demo_PlayBackByTime(lUserID);
    //Demo_PlayBackByName();
    //Demo_FindEvent(lUserID);
    Demo_FindFile(lUserID);
    return HPR_OK;
}
 
int Demo_FindFile(LONG lUserID)
{
    if(lUserID >= 0)
    {
        //find file
        NET_DVR_TIME struStartTime, struStopTime;
        struStartTime.dwYear    = 2010;
        struStartTime.dwMonth    = 6;
        struStartTime.dwDay        = 1;
        struStartTime.dwHour    = 0;
        struStartTime.dwMinute    = 0;
        struStartTime.dwSecond    =0;
 
        struStopTime.dwYear        = 2010;
        struStopTime.dwMonth    = 6;
        struStopTime.dwDay        = 1;
        struStopTime.dwHour        = 1;
        struStopTime.dwMinute    = 0;
        struStopTime.dwSecond    = 0;
 
 
        int lFindHandle = NET_DVR_FindFile(lUserID, 1, 0xff, &struStartTime, &struStopTime);
        if(lFindHandle < 0)
        {
            printf("find file fail,last error %d\n",NET_DVR_GetLastError());
            return 1;
        }
        NET_DVR_FIND_DATA struFileData;
 
        while(true)
        {
            int result = NET_DVR_FindNextFile(lFindHandle, &struFileData);
            if(result == NET_DVR_ISFINDING)
            {
                //nsleep(0, 20000000L); // sleep for 2/100 second;
                continue;
            }
            else if(result == NET_DVR_FILE_SUCCESS)
            {
                //char strFileName[256] = {0};
                //sprintf(strFileName, "./%s", struFileData.sFileName);
                printf("%s, %d, %d, %d-----%d,%d,%d.", struFileData.sFileName, 
                    struFileData.struStartTime.dwYear, struFileData.struStartTime.dwMonth,  struFileData.struStartTime.dwDay,
                    struFileData.struStopTime.dwYear, struFileData.struStopTime.dwMonth,  struFileData.struStopTime.dwDay);
                //saveFile(lUserID, struFileData.sFileName,  strFileName);
                break;
            }
            else if(result == NET_DVR_FILE_NOFIND || result == NET_DVR_NOMOREFILE)
            {    
                break;
            }
            else
            {
                printf("find file fail for illegal get file state");
                break;
            }
 
        }
 
        NET_DVR_FindClose(lFindHandle);
    }
 
    return HPR_OK;
}
 
int Demo_PlayBackByName()
{
    //init
    NET_DVR_Init();
    //login
    long lUserID;
    //login
    NET_DVR_DEVICEINFO struDeviceInfo;
    lUserID = NET_DVR_Login("172.9.204.5", 8000, "admin", "12345", &struDeviceInfo);
    if (lUserID < 0)
    {
        printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
        return HPR_ERROR;
    }
 
    //for (int i = 0; i < 30; i++)
    {
        //printf("%d.\n", i);
#if defined(_WIN32) || defined(_APPLE_)
        HWND hWnd = NULL;
#else
        HWND hWnd = {0};
#endif
 
        LONG lFindPicHandle = NET_DVR_PlayBackByName(lUserID, "ch01_02010100141000000", hWnd);
        if (lFindPicHandle < 0)
        {
            printf("pyd---NET_DVR_PlayBackControl fail: %d.\n", NET_DVR_GetLastError()); 
        }
        else
        {
            if(!NET_DVR_SetPlayDataCallBack(lFindPicHandle, fPlayDataCallBack,1))
            {
                printf("NET_DVR_SetPlayDataCallBack fail!\n");
            }
 
            //if (!NET_DVR_PlayBackControl(lFindPicHandle, NET_DVR_PLAYSTART, 0, NULL))
         if (!NET_DVR_PlayBackControl_V40(lFindPicHandle, NET_DVR_PLAYSTART,  NULL, 0, NULL, NULL))
            {
                printf("pyd---NET_DVR_PlayBackControl fail.%d\n", NET_DVR_GetLastError());
            }
        }
 
#ifdef _WIN32
        Sleep(5000);  //millisecond
#elif  defined(__linux__) || defined(__APPLE__)
        sleep(500);   //second
#endif
    }
 
    NET_DVR_Logout(lUserID);
    //cleanup
    NET_DVR_Cleanup();
 
    return HPR_OK;
}
 
int Demo_PlayBackByTime(LONG lUserID)
{
    int hPlayback;
 
        //find file
        NET_DVR_TIME struStartTime, struStopTime;
        struStartTime.dwYear    = 2010;
        struStartTime.dwMonth    = 6;
        struStartTime.dwDay        = 1;
        struStartTime.dwHour    = 0;
        struStartTime.dwMinute    = 0;
        struStartTime.dwSecond    =0;
 
        struStopTime.dwYear        = 2012;
        struStopTime.dwMonth    = 6;
        struStopTime.dwDay        = 1;
        struStopTime.dwHour        = 0;
        struStopTime.dwMinute    = 7;
        struStopTime.dwSecond    = 0;
 
#if defined(__linux__)
        HWND hWnd = {0};
#else
        HWND hWnd = NULL;
#endif
 
        //°´Ê±¼äÏÂÔØ
        hPlayback = NET_DVR_PlayBackByTime(lUserID, 1, &struStartTime, &struStopTime, hWnd);
        //HWND struPlayRect = {0};//!!!!must init.
        //hPlayback = NET_DVR_PlayBackByTime(lUserID, 1, &struStartTime, &struStopTime, struPlayRect);
        if(hPlayback < 0)
        {
            printf("NET_DVR_GetFileByTime fail,last error %d\n",NET_DVR_GetLastError());
            return 1;
        } 
 
    g_hPlayback = hPlayback;
 
    //ÉèÖÃÈ¡Á÷»Øµ÷
    if(!NET_DVR_SetPlayDataCallBack(hPlayback, fPlayDataCallBack,1))
    {
        printf("NET_DVR_SetPlayDataCallBack fail!\n");
    }
 
    if(!NET_DVR_PlayBackControl(hPlayback, NET_DVR_PLAYSTART, 0, NULL))
    {
        printf("play back control failed [%d]\n",NET_DVR_GetLastError());
        return HPR_ERROR;
    }
 
#if defined(_WIN32)
    Sleep(1000);
#else
    sleep(5);
#endif
 
    //sleep(180); //˯3·ÖÖÓ
 
    //if(!NET_DVR_StopPlayBack(hPlayback))
    //{
    //    printf("failed to stop get file [%d]\n",NET_DVR_GetLastError());
    //    return HPR_ERROR;
    //}
 
    //NET_DVR_Logout(lUserID);
    //NET_DVR_Logout_V30(lUserID);
    //cleanup
    NET_DVR_Cleanup();
 
    return HPR_OK;
}
 
int Demo_GetFileByTime()
{
    //init
    NET_DVR_Init();
    //login
    long lUserID;
    //login
    NET_DVR_DEVICEINFO struDeviceInfo;
    lUserID = NET_DVR_Login("172.4.4.190", 8000, "admin", "12345", &struDeviceInfo);
    if (lUserID < 0)
    {
        printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
        return HPR_ERROR;
    }
 
    int hPlayback;
 
    if(lUserID >= 0)
    {
        //find file
        NET_DVR_TIME struStartTime, struStopTime;
        struStartTime.dwYear    = 2010;
        struStartTime.dwMonth    = 6;
        struStartTime.dwDay        = 1;
        struStartTime.dwHour    = 0;
        struStartTime.dwMinute    = 0;
        struStartTime.dwSecond    =0;
 
        struStopTime.dwYear        = 2010;
        struStopTime.dwMonth    = 6;
        struStopTime.dwDay        = 1;
        struStopTime.dwHour        = 0;
        struStopTime.dwMinute    = 7;
        struStopTime.dwSecond    = 0;
        //°´Ê±¼äÏÂÔØ
        hPlayback = NET_DVR_GetFileByTime(lUserID, 1, &struStartTime, &struStopTime, "./test.mp4");
        //HWND struPlayRect = {0};//!!!!must init.
        //hPlayback = NET_DVR_PlayBackByTime(lUserID, 1, &struStartTime, &struStopTime, struPlayRect);
        if(hPlayback < 0)
        {
            printf("NET_DVR_GetFileByTime fail,last error %d\n",NET_DVR_GetLastError());
            return 1;
        } 
    }
 
    g_hPlayback = hPlayback;
 
    //ÉèÖÃÈ¡Á÷»Øµ÷
    if(!NET_DVR_SetPlayDataCallBack(hPlayback, fPlayDataCallBack,1))
    {
        printf("NET_DVR_SetPlayDataCallBack fail!\n");
    }
 
    if(!NET_DVR_PlayBackControl(hPlayback, NET_DVR_PLAYSTART, 0, NULL))
    {
        printf("play back control failed [%d]\n",NET_DVR_GetLastError());
        return HPR_ERROR;
    }
 
    int pos=0;
    for(pos = 0;  pos < 100&&pos>=0; pos = NET_DVR_GetDownloadPos(hPlayback))
    {
#ifdef _WIN32
        Sleep(5000);  //millisecond
#elif  defined(__linux__)
        sleep(500);   //second
#endif
    }
 
    printf("have got %d\n", pos);
    if(!NET_DVR_StopGetFile(hPlayback))
    {
        printf("failed to stop get file [%d]\n",NET_DVR_GetLastError());
        return HPR_ERROR;
    }
 
    printf("StopGetFile\n");
    if(pos<0||pos>100)
    {
        printf("download err [%d]\n",NET_DVR_GetLastError());
        return HPR_ERROR;
 
    }else
    {
        return 0;
    }
 
 
    //sleep(180); //˯3·ÖÖÓ
    NET_DVR_Logout(lUserID);
    //cleanup
    NET_DVR_Cleanup();
 
    return HPR_OK;
}
 
 
int saveFile(int userId,char * srcfile,char * destfile)
{
 
    int bRes = 1;
 
    int hPlayback=0;
    if( (hPlayback = NET_DVR_GetFileByName(userId, srcfile, destfile)) < 0 )
    {
        printf( "GetFileByName failed. error[%d]\n", NET_DVR_GetLastError());
        bRes= -1;
        return bRes;
    }
 
 
 
    if(!NET_DVR_PlayBackControl(hPlayback, NET_DVR_PLAYSTART, 0, NULL))
    {
 
        printf("play back control failed [%d]\n",NET_DVR_GetLastError());
 
        bRes=-1;
 
        return bRes;
 
    }
 
    int pos=0;
 
    for(pos = 0;  pos < 100&&pos>=0; pos = NET_DVR_GetDownloadPos(hPlayback))
    {
#ifdef _WIN32
        Sleep(5000);  //millisecond
#elif  defined(__linux__)
        sleep(500);   //second
#endif
    }
 
    printf("have got %d\n", pos);
 
    if(!NET_DVR_StopGetFile(hPlayback))
    {
        printf("failed to stop get file [%d]\n",NET_DVR_GetLastError());
        bRes = -1;
        return bRes;
    }
 
    printf("StopGetFile\n");
 
    printf("%s\n",srcfile);
 
    if(pos<0||pos>100){
 
        printf("download err [%d]\n",NET_DVR_GetLastError());
 
        bRes=-1;
 
        return bRes;
 
    }else{
 
        return 0;
 
    }
 
}
 
 
//Find event
int Demo_FindEvent(LONG lUserID)
{
    NET_DVR_SEARCH_EVENT_PARAM struEventParams;
    memset(&struEventParams, 0, sizeof(struEventParams));
    struEventParams.wMajorType = 0;
    struEventParams.wMinorType = 0xffff;
    
    struEventParams.uSeniorParam.struMotionParam.byMotDetChanNo[0] = 1;
    
    
    int lFindHandle = NET_DVR_FindFileByEvent(lUserID,  &struEventParams);
    if(lFindHandle < 0)
    {
        printf("find file fail,last error %d\n",NET_DVR_GetLastError());
        return 1;
    }
    
    NET_DVR_SEARCH_EVENT_RET struEventInfo;
    while(true)
    {
        memset(&struEventInfo, 0, sizeof(NET_DVR_SEARCH_EVENT_RET));
        int result = NET_DVR_FindNextEvent(lFindHandle, &struEventInfo);
        if(result == NET_DVR_ISFINDING)
        {
            //nsleep(0, 20000000L); // sleep for 2/100 second;
            continue;
        }
        else if(result == NET_DVR_FILE_SUCCESS)
        {
            printf("Get a event!\n");
            continue;
        }
        else if(result == NET_DVR_FILE_NOFIND || result == NET_DVR_NOMOREFILE)
        {    
            break;
        }
        else
        {
            printf("find file fail for illegal get file state");
            break;
        }
        
    }
    
    NET_DVR_FindClose(lFindHandle);
    return 0;
}