// File: /workspace/github_c_makefile_star2_O3/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
movl $0x7d0, %edi            # imm = 0x7D0
movl $0x1, %esi
callq 0x1110
movl $0x2710, %edi           # imm = 0x2710
movl $0x1, %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 0x1413
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 0x142c
movl %eax, %ebp
movl %eax, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1090
testl %eax, %eax
je 0x143a
leaq 0xca3(%rip), %r14       # 0x2062
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
movl $0x1388, %edi           # imm = 0x1388
callq 0x10e0
movl %ebp, %edi
callq 0x10a0
movl %eax, %r15d
cmpl $0x64, %eax
jb 0x13c2
movl %ebp, %edi
callq 0x10c0
testl %eax, %eax
je 0x1448
cmpl $0x64, %r15d
jne 0x1456
leaq 0xc62(%rip), %rdi       # 0x2062
movl $0x64, %esi
xorl %eax, %eax
callq 0x1030
movl $0x1, %ebp
jmp 0x146d
callq 0x10d0
leaq 0xbe5(%rip), %rdi       # 0x2004
xorl %ebp, %ebp
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x1474
callq 0x10d0
leaq 0xbdd(%rip), %rdi       # 0x2015
jmp 0x1462
callq 0x10d0
leaq 0xbfd(%rip), %rdi       # 0x2043
jmp 0x1462
callq 0x10d0
leaq 0xc27(%rip), %rdi       # 0x207b
jmp 0x1462
callq 0x10d0
leaq 0xc37(%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