// File: /workspace/github_c_makefile_star2_O2/NTU-ROSE[P]Hikvision_Video_Downloader/./Hikvision_Video_Downloader.cpp
// Function: main
int main(int argc, char** argv) {
    //---------------------------------------
    // 初始化
    NET_DVR_Init(); //设置连接时间与重连时间 
    NET_DVR_SetConnectTime(2000, 1); 
    NET_DVR_SetReconnect(10000, true);
    
    LONG lUserID;
    NET_DVR_DEVICEINFO_V30 struDeviceInfo;
    lUserID = NET_DVR_Login_V30(argv[1], 8000, argv[2], argv[3], &struDeviceInfo); 
    if (lUserID < 0)
    {
    printf("Login error, %d\n", NET_DVR_GetLastError()); NET_DVR_Cleanup();
    return 0;
    }
     
    NET_DVR_PLAYCOND struDownloadCond={0};
    struDownloadCond.dwChannel= atoi(argv[4]); //通道号
    struDownloadCond.struStartTime.dwYear = atoi(argv[5]);
    struDownloadCond.struStartTime.dwMonth = atoi(argv[6]);
    struDownloadCond.struStartTime.dwDay = atoi(argv[7]);
    struDownloadCond.struStartTime.dwHour = atoi(argv[8]);
    struDownloadCond.struStartTime.dwMinute = atoi(argv[9]); 
    struDownloadCond.struStartTime.dwSecond =atoi(argv[10]);
    
    struDownloadCond.struStopTime.dwYear = atoi(argv[5]);
    struDownloadCond.struStopTime.dwMonth = atoi(argv[6]);
    struDownloadCond.struStopTime.dwDay = atoi(argv[7]);
    struDownloadCond.struStopTime.dwHour = atoi(argv[11]);
    struDownloadCond.struStopTime.dwMinute = atoi(argv[12]);
    struDownloadCond.struStopTime.dwSecond = atoi(argv[13]);
    //---------------------------------------
    //按时间下载
    int hPlayback;
    hPlayback = NET_DVR_GetFileByTime_V40(lUserID, argv[14],&struDownloadCond);
    if(hPlayback < 0)
    {
    printf("NET_DVR_GetFileByTime_V40 fail,last error %d\n",NET_DVR_GetLastError());
    NET_DVR_Logout(lUserID);
    NET_DVR_Cleanup();
    return 0;
    }
    
    if(!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART, NULL, 0, NULL,NULL))
    {
        printf("Play back control failed [%d]\n",NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID); 
        NET_DVR_Cleanup(); 
        return 0 ;
    }
    
    int nPos = 0;
    for(nPos = 0; nPos < 100&&nPos>=0; nPos = NET_DVR_GetDownloadPos(hPlayback)) 
    {
        printf("Be downloading... %d %%\n",nPos); //下载进度
        usleep(5000); //millisecond 
    }
    
    
    if(!NET_DVR_StopGetFile(hPlayback)) {
        printf("failed to stop get file [%d]\n",NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    
    if(nPos<0||nPos>100) {
        printf("download err [%d]\n",NET_DVR_GetLastError()); NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    
    printf("Be downloading... %d %%\n",nPos);
    
    //注销用户 
    NET_DVR_Logout(lUserID); 
    //释放 SDK 资源 
    NET_DVR_Cleanup(); 
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
callq 0x1050
pushq $0x1
popq %rbx
movl $0x7d0, %edi            # imm = 0x7D0
movl %ebx, %esi
callq 0x1110
movl $0x2710, %edi           # imm = 0x2710
movl %ebx, %esi
callq 0x1080
movq 0x8(%r14), %rdi
movq 0x10(%r14), %rdx
movq 0x18(%r14), %rcx
leaq 0x4(%rsp), %r8
movl $0x1f40, %esi           # imm = 0x1F40
callq 0x1040
testl %eax, %eax
js 0x13e6
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0x54(%rsp), %r15
movups %xmm0, 0x64(%r15)
movups %xmm0, 0x54(%r15)
movups %xmm0, 0x44(%r15)
movups %xmm0, 0x34(%r15)
movq 0x20(%r14), %rdi
callq 0x1100
movl %eax, (%r15)
movq 0x28(%r14), %r12
movq %r12, %rdi
callq 0x1100
movl %eax, 0x4(%r15)
movq 0x30(%r14), %r13
movq %r13, %rdi
callq 0x1100
movl %eax, 0x8(%r15)
movq 0x38(%r14), %rbp
movq %rbp, %rdi
callq 0x1100
movl %eax, 0xc(%r15)
movq 0x40(%r14), %rdi
callq 0x1100
movl %eax, 0x10(%r15)
movq 0x48(%r14), %rdi
callq 0x1100
movl %eax, 0x14(%r15)
movq 0x50(%r14), %rdi
callq 0x1100
movl %eax, 0x18(%r15)
movq %r12, %rdi
callq 0x1100
movl %eax, 0x1c(%r15)
movq %r13, %rdi
callq 0x1100
movl %eax, 0x20(%r15)
movq %rbp, %rdi
callq 0x1100
movl %eax, 0x24(%r15)
movq 0x58(%r14), %rdi
callq 0x1100
movl %eax, 0x28(%r15)
movq 0x60(%r14), %rdi
callq 0x1100
movl %eax, 0x2c(%r15)
movq 0x68(%r14), %rdi
callq 0x1100
movl %eax, 0x30(%r15)
movq 0x70(%r14), %rsi
movl %ebx, %edi
movq %r15, %rdx
callq 0x1070
testl %eax, %eax
js 0x141d
movl %eax, %r14d
pushq $0x1
popq %rbp
movl %eax, %edi
movl %ebp, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1090
testl %eax, %eax
je 0x142b
leaq 0xca9(%rip), %r15       # 0x2062
xorl %r12d, %r12d
cmpl $0x63, %r12d
ja 0x13ff
movq %r15, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1030
movl $0x1388, %edi           # imm = 0x1388
callq 0x10e0
movl %r14d, %edi
callq 0x10a0
movl %eax, %r12d
jmp 0x13bc
callq 0x10d0
leaq 0xc12(%rip), %rdi       # 0x2004
xorl %ebp, %ebp
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x1465
movl %r14d, %edi
callq 0x10c0
testl %eax, %eax
je 0x1439
cmpl $0x64, %r12d
jne 0x1447
leaq 0xc4a(%rip), %rdi       # 0x2062
pushq $0x64
popq %rsi
jmp 0x1457
callq 0x10d0
leaq 0xbec(%rip), %rdi       # 0x2015
jmp 0x1453
callq 0x10d0
leaq 0xc0c(%rip), %rdi       # 0x2043
jmp 0x1453
callq 0x10d0
leaq 0xc36(%rip), %rdi       # 0x207b
jmp 0x1453
callq 0x10d0
leaq 0xc46(%rip), %rdi       # 0x2099
xorl %ebp, %ebp
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
movl %ebx, %edi
callq 0x1120
callq 0x10b0
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq