Repository navigation
Expand file tree
/
Copy pathnvr.cpp
More file actions
2425 lines (2021 loc) · 54 KB
/
Copy pathnvr.cpp
File metadata and controls
2425 lines (2021 loc) · 54 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include "nvr.h"
#include "mod_encode.h"
#include "ctrlprotocol.h"
#include "circlebuf.h"
extern "C" u32 GetLocalIp();
extern "C" u32 GetNetMask();
extern "C" u32 GetDefaultGateway();
extern "C" int ConnectWithTimeout(u32 dwHostIp, u16 wHostPort, u32 dwTimeOut, u16 *pwErrorCode);
extern "C" void SetKeepAlive(int Sock);
extern int DoStreamStateCallBack(int chn, real_stream_state_e msg, u32 nvrIP, u32 alarm_key);
/**********************************************************************
NVR_info: 保存每个连接上的NVR信息
CHN_info: 保存每个通道的信息,逻辑上处于NVR_client_info下级
eg:
如果连接了一个16路NVR,那么在当前模块这个NVR
将对应一个NVR_client_info结构和16个chn_info结构。
**********************************************************************/
#define NVR_DEBUG
//#undef NVR_DEBUG
#define INVALID_PID (-1)
#define INVALID_VAL (-1)
#define NvrLinkMax (8) //最大 NVR连接数
#define DestroyNvrDelayTime (10) //单位: 秒
struct chn_info;
typedef struct
{
int valid;//标示该结构是否有效
int cmd_fd;//命令通道SOCKET
struct chn_info **ppchn_info;//保存该NVR的所有通道信息的指针
//引用计数,用于在删除该NVR的所有通道后,销毁该结构
//成功申请该NVR的一个码流+1,断开一个码流-1
int ref_cnt;
//NVR通道0的图像显示在GUI界面上display_chn通道
//用于决定是否DestroyNvrInfo
int display_chn;
//当ref_cnt == 0时tm记录当前时间,延时DestroyNvrDelayTime后销毁
time_t tm;
ifly_DeviceInfo_t dev_info;
ifly_loginpara_t login;
pthread_mutex_t lock;
} NVR_info;
typedef struct chn_info
{
//private
u8 nvr_chn;//标示是已经连接上NVR的第几个通道,不区分主/子码流
u8 sys_chn;//标示是系统的第几个通道,区分主/子码流
u8 eventLoopWatchVariable;//码流接收线程退出条件
pthread_t pid;//码流接收线程ID
int cap_fd;//码流数据接收SOCKET
u32 link_id;//用于关闭码流
RealStreamCB pStreamCB;//数据回调
unsigned int dwContext;//回调参数
int video_width;//码流宽
int video_height;//码流高
//
NVR_info *pnvr_info;
pthread_mutex_t lock;
pthread_cond_t CondThreadOver;//等待码流线程结束
} CHN_info;
typedef struct
{
u8 nvr_index;//对应NVR_info结构在数组的下标
u8 type;//0: will be destroy
} MSG_TYPE;
#define MaxLinstenFds (20)
typedef struct
{
int linsten_fds[MaxLinstenFds];
pthread_mutex_t lock;
} SLinsten_info;
static SLinsten_info g_linsten_info;
static NVR_info *g_nvr_info = NULL;
static CHN_info *g_chn_info = NULL;
static unsigned int g_client_max = 0;
static unsigned char g_init_flag = 0;
static pthread_t g_ack_thread_id = 0;
static pthread_t g_msgrcv_thread_id = 0;
static pthread_t g_destroy_thread_id = 0;
//网络消息同步
#define BufSize (1024)
static char *g_pbuf_snd = NULL;
static char *g_pbuf_rcv = NULL;
static volatile int g_dwSyncAckLen = 0;
static volatile u16 g_wSyncNumber = 0;
static pthread_mutex_t g_hSyncMutex;//同一时刻只能有一处网络对话
static sem_t g_hSyncSem;//线程接收ACK后唤醒发送方
//搜索有关
static pthread_mutex_t g_search_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned char g_search_start = 0;
static ipc_node *g_search_head = NULL;
static ipc_node *g_search_tail = NULL;
/********************************************************************
内部函数
********************************************************************/
//Search
void SearchDevice_CB(ifly_DeviceInfo_t dev, void* pContext)
{
pthread_mutex_lock(&g_search_lock);
if (g_search_start == 0)//未开启搜索
{
pthread_mutex_unlock(&g_search_lock);
printf("%s Has not yet started\n", __func__);
return;
}
#if 0
struct in_addr in;
in.s_addr = dev.deviceIP;
printf("%s deviceIP: %s, nNVROrDecoder: %d\n", __func__, inet_ntoa(in), dev.nNVROrDecoder);
#endif
//搜索NVR,排除DEC
if (dev.nNVROrDecoder != 1)
{
pthread_mutex_unlock(&g_search_lock);
//printf("%s search device is DEC, return\n", __func__);
return;
}
//
//得到本机IP,排除本机
u32 localIP = GetLocalIp();
if (localIP == 0)
{
pthread_mutex_unlock(&g_search_lock);
printf("Error ****** %s, GetLocalIp failed\n", __func__);
return;
}
if (localIP == dev.deviceIP)
{
pthread_mutex_unlock(&g_search_lock);
printf("%s search device is itself, return\n", __func__);
return;
}
//
ipc_node *pNode = (ipc_node *)malloc(sizeof(ipc_node));
if(pNode == NULL)
{
printf("%s Not enough space to save new ipc info.\n", __func__);
pthread_mutex_unlock(&g_search_lock);
return;
}
memset(pNode, 0, sizeof(*pNode));
pNode->next = NULL;
//strncpy(pNode->ipcam.address, pstruDevInfo->aszMAC, sizeof(pNode->ipcam.address)-1);
strcpy(pNode->ipcam.user, "admin");
//strcpy(pNode->ipcam.pwd, "admin");//登录密码为空
pNode->ipcam.channel_no = 0;
pNode->ipcam.enable = 0;
pNode->ipcam.ipc_type = IPC_TYPE_720P;
pNode->ipcam.protocol_type = PRO_TYPE_NVR;
pNode->ipcam.stream_type = STREAM_TYPE_MAIN;
pNode->ipcam.trans_type = TRANS_TYPE_TCP;
pNode->ipcam.force_fps = 0;
pNode->ipcam.frame_rate = 30;
pNode->ipcam.dwIp = dev.deviceIP;
pNode->ipcam.wPort = dev.devicePort;
pNode->ipcam.main_audio_sw = dev.maxChnNum;//nvr最大通道数,AddIPC要用到
//strcpy(pNode->ipcam.uuid, pstruDevInfo->aszDevID);
//strcpy(pNode->ipcam.name, pstruDevInfo->aszDevName);
//pNode->ipcam.net_mask = inet_addr(pstruDevInfo->aszMASK);
//pNode->ipcam.net_gateway = inet_addr(pstruDevInfo->aszGTW);
//填充本机的掩码和网关
pNode->ipcam.net_mask = GetNetMask();
pNode->ipcam.net_gateway = GetDefaultGateway();
pNode->ipcam.dns1 = inet_addr("8.8.8.8");
pNode->ipcam.dns2 = inet_addr("4.4.4.4");
if(g_search_head == NULL)
{
g_search_head = pNode;
g_search_tail = pNode;
}
else
{
g_search_tail->next = pNode;
g_search_tail = pNode;
}
pthread_mutex_unlock(&g_search_lock);
}
/************************************
ret:
[0, NvrLinkMax) : 已在数组g_nvr_info 中,设备IP==给定IP。
不在数组g_nvr_info 中,返回第一个未使用的成员
caution: 1、正常返回会持有锁
2、必读和putNvrIno 成对使用
************************************/
static NVR_info *getNvrInfoByIP_lock(u32 dwIp)
{
int i = 0;
int first_valid = -1;
NVR_info *pNVR_info = NULL;
for (i=0; i<NvrLinkMax; i++)
{
pthread_mutex_lock(&g_nvr_info[i].lock);
if (0 == g_nvr_info[i].valid)
{
if (first_valid == -1)
{
first_valid = i;
continue;//注意未释放锁
}
}
else
{
if (dwIp == g_nvr_info[i].dev_info.deviceIP)
{
break;
}
}
pthread_mutex_unlock(&g_nvr_info[i].lock);
}
if (i < NvrLinkMax)
{
#ifdef NVR_DEBUG
printf("%s dev exist, pos: %d\n", __func__, i);
#endif
pNVR_info = &g_nvr_info[i];
//如果找到了,那么释放first_valid 锁
if (first_valid != -1)
{
pthread_mutex_unlock(&g_nvr_info[first_valid].lock);
}
}
else
{
if (first_valid != -1)
{
printf("%s dev not exist, pos: %d\n", __func__, first_valid);
pNVR_info = &g_nvr_info[first_valid];
}
else
{
printf("%s ***Error*** g_nvr_info all dev in used\n", __func__);
pNVR_info = NULL;
}
}
return pNVR_info;
}
static void putNvrInfo(NVR_info *pNVR_info)
{
if (NULL != pNVR_info)
{
pthread_mutex_unlock(&pNVR_info->lock);
}
}
static inline u16 getNo()
{
static u16 no = 0;
return no++;
}
//底层数据交互
static int NetDialogue(int sock, u16 event, const void *content, int length, void* ackbuf, int ackbuflen, int *realacklen)
{
//u8 abyBuf[1024];
int ret = SOCKET_SUCCESS;
int msglen = 0;
int rcvlen = 0;
u16 number = 0;
ifly_cp_header_t cp_head_snd;
ifly_cp_header_t cp_head_rcv;
struct timeval tv1;
if(sock < 0)
{
printf("%s ***Error*** sock invalid\n", __func__);
return CTRL_FAILED_PARAM;
}
if(length < 0)
{
printf("%s ***Error*** length: %d invalid\n", __func__, length);
return CTRL_FAILED_PARAM;
}
if(NULL == ackbuf)
{
printf("%s ***Error*** ackbuf invalid\n", __func__);
return CTRL_FAILED_PARAM;
}
if(NULL == realacklen)
{
printf("%s ***Error*** realacklen invalid\n", __func__);
return CTRL_FAILED_PARAM;
}
//全局回话锁
pthread_mutex_lock(&g_hSyncMutex);
//发送
msglen = sizeof(cp_head_snd)+length;
memset(&cp_head_snd, 0, sizeof(cp_head_snd));
cp_head_snd.length = htonl(msglen);
cp_head_snd.type = htons(CTRL_COMMAND);
cp_head_snd.event = htons(event);
number = getNo();
gettimeofday(&tv1, NULL);
printf("%s snd event: %d, number: %d, time: %u.%u\n", __func__, event, number, tv1.tv_sec, tv1.tv_usec);
cp_head_snd.number = htons(number);
cp_head_snd.version = htons(CTRL_VERSION);
if(msglen > BufSize)
{
printf("%s ***Error*** cp_head_snd.length(%d) > BufSize\n", __func__, msglen);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_PARAM;
}
memcpy(g_pbuf_snd, &cp_head_snd, sizeof(cp_head_snd));
if (content && length)
{
memcpy(g_pbuf_snd+sizeof(cp_head_snd), content, length);
}
g_wSyncNumber = ntohs(cp_head_snd.number);
ret = sem_trywait(&g_hSyncSem);//防止上一条消息的应答
if (ret)
{
if (EAGAIN == errno)
{
#ifdef NVR_DEBUG
//printf("%s sem_trywait EAGAIN\n", __func__);
#endif
}
else
{
printf("%s ***Error*** sem_trywait failed, error:[%d,%s]!\n",
__func__, errno, strerror(errno));
}
}
ret = loopsend(sock, (char *)g_pbuf_snd, msglen);
if(ret <= 0)
{
printf("%s ***Error*** loopsend failed\n", __func__);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_NETSND;
}
//printf("%s snd number2: %d\n", __func__, number);
//接收
struct timespec tm;
time_t cur_t;
u8 cnt = 0;
#if 1
do {
memset(&tm, 0, sizeof(tm));
time(&cur_t);
tm.tv_sec = cur_t+5;//未来时间,超时时间3秒
ret = sem_timedwait(&g_hSyncSem, &tm);
if (ret == 0)//success
{
#ifdef NVR_DEBUG
printf("%s sem_timedwait success\n", __func__);
#endif
break;
}
else
{
if (ETIMEDOUT == errno)
{
#ifdef NVR_DEBUG
printf("%s sem_timedwait timeout cnt: %d\n", __func__, cnt);
#endif
}
else
{
printf("%s ***Error*** sem_timedwait failed, error:[%d,%s]!\n",
__func__, errno, strerror(errno));
break;
}
}
cnt++;
} while (0); //(cnt > 1);
#else
ret = sem_wait(&g_hSyncSem);
#endif
gettimeofday(&tv1, NULL);
printf("%s sem_timedwait ret: %d, event: %d, number: %d, time: %u.%u\n", __func__, ret, event, number, tv1.tv_sec, tv1.tv_usec);
if (ret)//timeout or failed
{
printf("%s ***Error*** sem_timedwait failed, error:[%d,%s]!\n",
__func__, errno, strerror(errno));
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_NETRCV;
}
if (g_dwSyncAckLen < (int)sizeof(ifly_cp_header_t))
{
printf("%s ***Error*** g_dwSyncAckLen < sizeof(ifly_cp_header_t)\n", __func__);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_NETRCV;
}
memcpy(&cp_head_rcv, g_pbuf_rcv, sizeof(ifly_cp_header_t));
if (cp_head_rcv.event != CTRL_SUCCESS)
{
printf("%s ***Error*** cp_head_rcv.event: %d\n", __func__, cp_head_rcv.event);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_NETRCV;
}
rcvlen = g_dwSyncAckLen - sizeof(ifly_cp_header_t);
if(realacklen != NULL)
{
*realacklen = rcvlen;
}
if(rcvlen > 0)
{
if (ackbuf == NULL)
{
printf("%s ***Error*** ackbuf == NULL\n", __func__);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_NETRCV;
}
if(ackbuflen < rcvlen)
{
printf("%s ***Error*** ackbuflen(%d) < rcvlen(%d)\n",
__func__, ackbuflen, rcvlen);
pthread_mutex_unlock(&g_hSyncMutex);
return CTRL_FAILED_OUTOFMEMORY;
}
memcpy(ackbuf, g_pbuf_rcv+sizeof(ifly_cp_header_t), rcvlen);
}
pthread_mutex_unlock(&g_hSyncMutex);
return SOCKET_SUCCESS;
}
//ifly_DeviceInfo_t DeviceInfo;
static int GetDeviceInfo(int sock, ifly_DeviceInfo_t *pDeviceInfo)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
if (INVALID_SOCKET == sock || NULL == pDeviceInfo)
{
printf("%s param invalid\n", __func__);
return SOCKET_ERROR;
}
ret = NetDialogue(sock, CTRL_CMD_GETDEVICEINFO, NULL, 0, pDeviceInfo, sizeof(ifly_DeviceInfo_t), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s failed\n", __func__);
return SOCKET_ERROR;
}
//数据改变字节序
pDeviceInfo->devicePort = ntohs(pDeviceInfo->devicePort);//只改一处
#ifdef NVR_DEBUG
struct in_addr in;
in.s_addr = pDeviceInfo->deviceIP;
printf("%s get device info deviceIP:%s, devicePort:%d, device_name:%s, device_mode:%s, MaxChn=%d, nNVROrDecoder:%d\n", \
__func__,
inet_ntoa(in),
pDeviceInfo->devicePort,
pDeviceInfo->device_name,
pDeviceInfo->device_mode,
pDeviceInfo->maxChnNum,
pDeviceInfo->nNVROrDecoder);
#endif
return ret;
}
static int NvrLogin(int sock, ifly_loginpara_t *plogin)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
u8 buf[128];
ret = NetDialogue(sock, CTRL_CMD_LOGIN, plogin, sizeof(ifly_loginpara_t), buf, sizeof(buf), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s failed ret = %d\n", __func__, ret);
ret = SOCKET_ERROR;
}
return ret;
}
static int NvrLogout(int sock, ifly_loginpara_t *plogin)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
u8 buf[128];
ret = NetDialogue(sock, CTRL_CMD_LOGOFF, plogin, sizeof(ifly_loginpara_t), buf, sizeof(buf), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s failed ret = %d\n", __func__, ret);
ret = SOCKET_ERROR;
}
return ret;
}
//upload_enable [0-1]
static int NvrSetAlarmUpload(int sock, unsigned char upload_enable)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
u8 buf[128];
if (INVALID_SOCKET == sock)
{
printf("%s param invalid\n", __func__);
return SOCKET_ERROR;
}
if (0 != upload_enable && 1 != upload_enable)
{
printf("%s param invalid\n", __func__);
return SOCKET_ERROR;
}
ret = NetDialogue(sock, CTRL_CMD_ALARMUPLOADCENTER, &upload_enable,sizeof(upload_enable), buf, sizeof(buf), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s failed ret = %d\n", __func__, ret);
ret = SOCKET_ERROR;
}
return ret;
}
//查询NVR对应通道连接情况,即是否存在码流
static int checkNvrChnLinkStatus(NVR_info *pnvr, u8 req_nvr_chn , u8 bmain)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
ifly_ipc_chn_status_t ipc_chn_status;
ret = NetDialogue(pnvr->cmd_fd, CTRL_CMD_GET_IPCCHN_LINKSTATUS, NULL, 0, &ipc_chn_status, sizeof(ipc_chn_status), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s ***Error*** ret = %d\n", __func__, ret);
return -1;
}
u8 nvr_stream_chn = bmain ? req_nvr_chn : (req_nvr_chn + pnvr->dev_info.maxChnNum);
#ifdef NVR_DEBUG
printf("%s real_req_chn%d, NVR max_chn_num = %d, 0x%x, 0x%x, 0x%x, 0x%x\n", \
__func__, nvr_stream_chn, ipc_chn_status.max_chn_num, \
ipc_chn_status.chn_status[0], \
ipc_chn_status.chn_status[1], \
ipc_chn_status.chn_status[2], \
ipc_chn_status.chn_status[3]);
#endif
if (pnvr->dev_info.maxChnNum != ipc_chn_status.max_chn_num/2)
{
printf("%s ***Error*** pnvr->dev_info.maxChnNum(%d) != ipc_chn_status.max_chn_num/2(%d)\n", \
__func__, pnvr->dev_info.maxChnNum, ipc_chn_status.max_chn_num/2);
return -1;
}
//u8 nvr_stream_chn = bmain ? req_nvr_chn : (req_nvr_chn + pnvr->dev_info.maxChnNum);
if (nvr_stream_chn >= ipc_chn_status.max_chn_num)
{
printf("%s ***Error*** nvr_stream_chn%d >= ipc_chn_status.max_chn_num%d\n", \
__func__, nvr_stream_chn, ipc_chn_status.max_chn_num);
return -1;
}
ret = 0;
if (ipc_chn_status.chn_status[nvr_stream_chn/8] & (1<<(nvr_stream_chn%8)))
{
ret = 1;
}
return ret;
}
static int NVRStartStream(u32 dwIp, u16 wPort, u8 req_nvr_chn , u8 bmain, int *pstream_sock, u32 *plink_id)
{
int ret = 0;
int sock = INVALID_SOCKET;
ifly_TCP_Stream_Req req;
memset(&req, 0, sizeof(ifly_TCP_Stream_Req));
req.command = 0;
req.Monitor_t.chn = req_nvr_chn;
req.Monitor_t.type = bmain ? 0:2;
/*
req.reserved[0] = 0;
req.reserved[1] = 0;
req.reserved[6] = 0;
*/
struct in_addr in;
in.s_addr = dwIp;
//连接NVR
u16 wErrorCode = 0;
sock = ConnectWithTimeout(dwIp, wPort, 5000, &wErrorCode);
if(sock == INVALID_SOCKET)
{
printf("%s ***Error*** connect failed, NVR IP: %s, port: %d, wErrorCode:%d\n", \
__func__, inet_ntoa(in), wPort, wErrorCode);
return -1;
}
#ifdef NVR_DEBUG
printf("%s ConnectWithTimeout success\n", __func__);
#endif
//设置socket
struct linger m_sLinger;
m_sLinger.l_onoff = 1; //(在closesocket()调用,但是还有数据没发送完毕的时候容许逗留)
m_sLinger.l_linger = 0; //(容许逗留的时间为0秒)
ret = setsockopt(sock, SOL_SOCKET, SO_LINGER, (char*)&m_sLinger, sizeof(struct linger));
if(ret)
{
printf("%s setsockopt failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
close(sock);
return -1;
}
//连接类型,0x1:控制信令;0x2:码流传输;0x3:广播搜索;0x4 轮巡同步(只对已经使能轮巡的设备有效)
ret = SendTcpConnHead(sock, 0x2);
if (ret <= 0)
{
printf("%s SendTcpConnHead failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
close(sock);
return -1;
}
#ifdef NVR_DEBUG
printf("%s SendTcpConnHead success\n", __func__);
#endif
SetKeepAlive(sock);
pthread_mutex_lock(&g_hSyncMutex);
ret = loopsend(sock, (char *)&req, sizeof(ifly_TCP_Stream_Req));
if(ret <= 0)
{
printf("%s loopsend failed\n", __func__);
close(sock);
pthread_mutex_unlock(&g_hSyncMutex);
return -1;
}
#ifdef NVR_DEBUG
printf("%s Send Stream_Req success\n", __func__);
#endif
ifly_TCP_Stream_Ack ack;
memset(&ack, 0, sizeof(ifly_TCP_Stream_Ack));
#if 1
ret = loopRcvTimeoutNS(sock, (char *)&ack, sizeof(ifly_TCP_Stream_Ack), 5);//timeout 5s
if (SOCKET_SUCCESS != ret)
{
if (SOCKET_TIMEOUT == ret)
{
printf("%s ***Error*** cp_head_rcv timeout\n", __func__);
}
else
{
printf("%s loopRcvTimeoutNS cp_head_rcv failed\n", __func__);
}
close(sock);
pthread_mutex_unlock(&g_hSyncMutex);
return -1;
}
#else
ret = looprecv(sock, (char *)&ack, sizeof(ifly_TCP_Stream_Ack));//timeout 5s
if (ret <= 0)
{
printf("%s looprecv ifly_TCP_Stream_Ack failed\n", __func__);
close(sock);
pthread_mutex_unlock(&g_hSyncMutex);
return -1;
}
#endif
ack.ackid = ntohl(ack.ackid);
ack.errcode = ntohl(ack.errcode);
if (ack.errcode != 0)
{
printf("%s Stream_Ack errcode: %d\n", __func__, ack.errcode);
close(sock);
pthread_mutex_unlock(&g_hSyncMutex);
return -1;
}
#ifdef NVR_DEBUG
printf("%s Rcv Stream_Ack success\n", __func__);
#endif
*pstream_sock = sock;
*plink_id = ack.ackid;
pthread_mutex_unlock(&g_hSyncMutex);
return 0;
}
//nvr_cmd_sock : 登录NVR 的sockfd。
static int NVRStopStream(int nvr_cmd_sock, u32 link_id)
{
int realacklen = 0;
int ret = SOCKET_SUCCESS;
u8 buf[128];
if (INVALID_SOCKET == nvr_cmd_sock)
{
printf("%s INVALID_SOCKET == nvr_cmd_sock:%d\n", __func__, nvr_cmd_sock);
return -1;
}
#ifdef NVR_DEBUG
printf("%s link_id: %d\n", __func__, link_id);
#endif
link_id = htonl(link_id);
ret = NetDialogue(nvr_cmd_sock, CTRL_CMD_STOPVIDEOMONITOR, &link_id, sizeof(u32), buf, sizeof(buf), &realacklen);
if (SOCKET_SUCCESS != ret)
{
printf("%s failed ret = %d\n", __func__, ret);
ret = SOCKET_ERROR;
}
return ret;
}
static int AttachLinsten(int sock)
{
int i = 0;
pthread_mutex_lock(&g_linsten_info.lock);
for(i = 0; i < MaxLinstenFds; i++)
{
if (INVALID_SOCKET == g_linsten_info.linsten_fds[i]) //无效的标志
{
g_linsten_info.linsten_fds[i] = sock;
break;
}
}
pthread_mutex_unlock(&g_linsten_info.lock);
if (i < MaxLinstenFds)
{
#ifdef NVR_DEBUG
printf("%s success, pos: %d\n", __func__, i);
#endif
return 0;
}
printf("%s ***Error*** no unused sock\n", __func__);
return -1;
}
static int DetachLinsten(int sock)
{
int i = 0;
pthread_mutex_lock(&g_linsten_info.lock);
for(i = 0; i < MaxLinstenFds; i++)
{
if (sock == g_linsten_info.linsten_fds[i])
{
g_linsten_info.linsten_fds[i] = INVALID_SOCKET;
break;
}
}
pthread_mutex_unlock(&g_linsten_info.lock);
if (i < MaxLinstenFds)
{
#ifdef NVR_DEBUG
printf("%s success, pos: %d\n", __func__, i);
#endif
return 0;
}
printf("%s ***Error*** not found sock\n", __func__);
return -1;
}
static int CreateNvrInfo(NVR_info *pnvr, unsigned int dwIp, unsigned short wPort, char *user, char *pwd)
{
int ret = 0;
int cmd_sock = INVALID_SOCKET;
CHN_info **ppchn_info = NULL;
u16 wErrorCode = 0;
ifly_DeviceInfo_t DeviceInfo;
ifly_loginpara_t login;
struct in_addr in;
in.s_addr = dwIp;
if (NULL == pnvr \
|| NULL == user \
|| NULL == pwd)
{
printf("%s ***Error*** param valid\n", __func__);
goto exit1;
}
//连接NVR
cmd_sock = ConnectWithTimeout(dwIp, wPort, 5000, &wErrorCode);
if(cmd_sock == INVALID_SOCKET)
{
printf("%s ***Error*** cmd_sock connect NVR IP: %s, port: %d, wErrorCode:%d\n", \
__func__, inet_ntoa(in), wPort, wErrorCode);
goto exit1;
}
#ifdef NVR_DEBUG
printf("%s cmd_sock ConnectWithTimeout success\n", __func__);
#endif
//
ret = SetSockLinkOpt(cmd_sock);
if (CTRL_SUCCESS != ret)
{
printf("%s ***Error*** cmd_sock SetSockLinkOpt failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
goto exit2;
}
#ifdef NVR_DEBUG
printf("%s cmd_sock SetSockLinkOpt success\n", __func__);
#endif
//连接类型,0x1:控制信令;0x2:码流传输;0x3:广播搜索;0x4 轮巡同步(只对已经使能轮巡的设备有效)
ret = SendTcpConnHead(cmd_sock, 0x1);
if (ret <= 0)
{
printf("%s ***Error*** cmd_sock SendTcpConnHead failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
goto exit2;
}
#ifdef NVR_DEBUG
printf("%s cmd_sock SendTcpConnHead success\n", __func__);
#endif
SetKeepAlive(cmd_sock);
ret = AttachLinsten(cmd_sock);
if (ret < 0)
{
printf("%s ***Error*** AttachLinsten sock: %d failed\n", \
__func__, cmd_sock);
goto exit2;
}
//得到设备信息
memset(&DeviceInfo, 0, sizeof(ifly_DeviceInfo_t));
ret = GetDeviceInfo(cmd_sock, &DeviceInfo);
if (ret < 0)
{
printf("%s ***Error*** cmd_sock GetDeviceInfo failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
goto exit3;
}
#ifdef NVR_DEBUG
printf("%s cmd_sock GetDeviceInfo success\n", __func__);
#endif
//登录设备
memset(&login, 0, sizeof(ifly_loginpara_t));
login.ipAddr = GetLocalIp();
if (login.ipAddr == 0)
{
printf("%s GetLocalIp failed\n", __func__);
goto exit3;
}
strcpy(login.username, user);
strcpy(login.loginpass, pwd);
strcpy(login.macAddr, "00:00:00:00:00:00");
ret = NvrLogin(cmd_sock, &login);
if (ret < 0)
{
printf("%s ***Error*** cmd_sock NvrLogin failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
goto exit3;
}
#ifdef NVR_DEBUG
printf("%s cmd_sock NvrLogin success\n", __func__);
#endif
#if 0
//设置接收报警信息
ret = NvrSetAlarmUpload(cmd_sock, 1);
if (ret < 0)
{
printf("%s ***Error*** alarm_sock NvrSetAlarmUpload failed, NVR IP: %s, port: %d\n", \
__func__, inet_ntoa(in), wPort);
goto exit4;
}
#endif
#ifdef NVR_DEBUG
printf("%s NvrSetAlarmUpload success\n", __func__);
#endif
//主、子码流各对应一个CHN_info 结构
ppchn_info = (CHN_info **)malloc(DeviceInfo.maxChnNum * 2 * sizeof(CHN_info *));
if (NULL == ppchn_info)
{
printf("%s ppchn_info malloc failed\n", \
__func__);
goto exit6;
}
memset(ppchn_info, 0, DeviceInfo.maxChnNum * 2 * sizeof(CHN_info *));