Repository navigation
Expand file tree
/
Copy pathI13.cpp
More file actions
2038 lines (1733 loc) · 44.1 KB
/
Copy pathI13.cpp
File metadata and controls
2038 lines (1733 loc) · 44.1 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 "I13.h"
typedef char HI_CHAR;
typedef char* HI_PCHAR;
typedef unsigned char HI_UCHAR;
typedef signed char HI_S8;
typedef short HI_S16;
typedef int HI_S32;
typedef unsigned char HI_U8;
typedef unsigned short HI_U16;
typedef unsigned int HI_U32;
typedef int SOCKET;
#define HI_SUCCESS 0
#define HI_FAILURE (-1)
#define TIMEOUT 4000//5000//csp modify 20131111
#define BUF_LEN 2048
#ifdef WIN32
#define NETSNDRCVFLAG 0
#else
#include <netinet/tcp.h>
#define NETSNDRCVFLAG MSG_NOSIGNAL
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#endif
typedef unsigned short u16;
typedef unsigned int u32;
extern "C" unsigned int GetLocalIp();
extern "C" int ConnectWithTimeout(u32 dwHostIp, u16 wHostPort, u32 dwTimeOut, u16 *pwErrorCode);
typedef struct
{
int cap_fd;
unsigned int dwIp;
unsigned short wPort;
char username[32];
char password[32];
int channel_info;
char stream_type[32];
RealStreamCB pStreamCB;
unsigned int dwContext;
int video_width;
int video_height;
pthread_t pid;
char eventLoopWatchVariable;
unsigned int frame_count;//csp modify 20140423
time_t last_t;//csp modify 20140423
u32 last_pts;//csp modify 20140423
pthread_mutex_t lock;
}i13_client_info;
static i13_client_info *g_i13c_info = NULL;
static unsigned int g_i13_client_count = 0;
static unsigned char g_init_flag = 0;
static HI_S32 SendData(SOCKET sock, HI_U8* pu8Buff, HI_U32 u32DataLen);
int ipnc_do_cgi_cmd(ipc_unit *ipcam, char* cgi);
int ipnc_exec_cgi_cmd(ipc_unit *ipcam, char* cgi, char *result);
/*------------------- define ---------------*/
#define PT_H264 96
#define PT_G726 97
#define PT_G711 8
#define PT_DATA 100
#define CHANNEL1 11
#define CHANNEL2 12
#define STREAM_TYPE1 "video_audio"//"video_audio_data"
#define STREAM_TYPE2 "video_audio"
#define STREAM_TYPE3 "video_data"
#define STREAM_TYPE4 "audio_data"
#define STREAM_TYPE5 "video"
#define STREAM_TYPE6 "audio"
//#define COMMAND "netconf set -ipaddr 192.168.1.52 -netmask 255.255.255.0 -gateway 192.168.1.1 -dhcp off -fdnsip 192.168.1.1 -sdnsip 211.23.12.13 -dnsstat 0 -hwaddr 00:01:89:11:11:07"
//#define COMMAND "netconf set -ipaddr %s -netmask %s -gateway %s -fdnsip %s -sdnsip %s -dhcp off"
#define COMMAND "netconf set -ipaddr %s -netmask %s -gateway %s -fdnsip %s -dhcp off"
//#define BUF_SIZE (512*1024*2)
typedef struct sRTP_HDR_S
{
/* byte 0 */
unsigned short cc :4; /* CSRC count */
unsigned short x :1; /* header extension flag */
unsigned short p :1; /* padding flag */
unsigned short version :2; /* protocol version */
/* byte 1 */
unsigned short pt :7; /* payload type */
unsigned short marker :1; /* marker bit */
/* bytes 2, 3 */
unsigned short seqno :16; /* sequence number */
/* bytes 4-7 */
unsigned int ts; /* timestamp in ms */
/* bytes 8-11 */
unsigned int ssrc; /* synchronization source */
} RTP_HDR_S;
typedef struct sRTSP_ITLEAVED_HDR_S
{
unsigned char daollar; /*8, $:dollar sign(24 decimal)*/
unsigned char channelid; /*8, channel id*/
unsigned short resv; /*16, reseved*/
unsigned int payloadLen; /*32, payload length*/
RTP_HDR_S rtpHead; /*rtp head*/
}RTSP_ITLEAVED_HDR_S;
/*--------------- main -------------------*/
#define HOST "192.168.1.88"//"192.168.1.105"
#define PORT 80
#define STRING_LEN 32
typedef struct sPARAM_S
{
char Uname[STRING_LEN]; //username
char Pword[STRING_LEN]; //password
char Ip[STRING_LEN]; //IPCam IP address
unsigned short Port; //IPCam http port
char* Buf; //data buffer
int State; //streaming thread state
int Socket; //socket handle
} PARAM_S;
/* send request packet of streaming to IPCam */
static int RequestStreamCommand(PARAM_S* pParam, int channel,char *stream_type)
{
char out_buffer[1024];
char content[256];
int ret;
memset(content, 0, 256);
memset(out_buffer, 0, 1024);
//sprintf(out_buffer, "GET http://%s:%d/livestream/%d?action=play&media=%s HTTP/1.1\r\n",
// pParam->Ip, pParam->Port, CHANNEL1, STREAM_TYPE5);
sprintf(out_buffer, "GET http://%s:%d/livestream/%d?action=play&media=%s HTTP/1.1\r\n",
pParam->Ip, pParam->Port, channel, stream_type);
strcpy(out_buffer+strlen(out_buffer), "User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n");
strcpy(out_buffer+strlen(out_buffer), "Connection: Keep-Alive\r\n");
strcpy(out_buffer+strlen(out_buffer), "Cache-Control: no-cache\r\n");
sprintf(out_buffer+strlen(out_buffer), "Authorization: %s %s\r\n",pParam->Uname, pParam->Pword);
strcpy(content, "Cseq: 1\r\n");
strcpy(content+strlen(content), "Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n\r\n");
sprintf(out_buffer+strlen(out_buffer), "Content-Length: %d\r\n", strlen(content));
strcpy(out_buffer+strlen(out_buffer), "\r\n");
strcpy(out_buffer+strlen(out_buffer), content);
//csp modify 20131111
//ret = send(pParam->Socket, out_buffer, strlen(out_buffer), 0);
//if(ret < 0)
//{
// printf("RequestStreamCommand send error \n");
// return -1;
//}
ret = SendData(pParam->Socket, (HI_U8 *)out_buffer, strlen(out_buffer));
if(HI_SUCCESS != ret)
{
printf("RequestStreamCommand SendData error \n");
return -1;
}
return 0;
}
/* get video and audio information */
static int GetVideoAndAudioAttr(char* buf,int chn)
{
char* tmp;
char str[2][16];
int video_format, video_width, video_height;
int audio_format;
tmp = strstr(buf, "m=video");
if(tmp != NULL)
{
sscanf(tmp,"%*s %d %*[^/]/%*[^/]/%32[^/]/%32[^\r]", &video_format,str[0],str[1]);
video_width = atoi(str[0]);
video_height = atoi(str[1]);
printf("#################chn%d width:%d,video height:%d#################\n", chn, video_width, video_height);
g_i13c_info[chn].video_width = video_width;
g_i13c_info[chn].video_height = video_height;
//IPC_SetStreamResolution(chn, video_width, video_height);
}
tmp = strstr(buf, "m=audio");
if(tmp != NULL)
{
sscanf(tmp,"%*s %d %*[^/]/%[^/]/%32[^\r]",&audio_format,str[0],str[1]);
if(audio_format == PT_G726)
{
//printf("audio format: G726 \n");
}
else if(audio_format == PT_G711)
{
//printf("audio format: G711a \n");
}
}
return 0;
}
/* receive response packet of streaming from IPCam */
static int ResponseStreamCommand(PARAM_S* pParam,int chn)
{
char buf[512];
int i, ret, n;
char* cur;
memset(buf, 0, 512);
i = 0;
n = 0;
cur = &buf[i];
ret = -1;//csp modify 20131111
while(1)
{
//csp modify 20131111
if(pParam->Socket == -1)
{
printf("ResponseStreamCommand socket error\n");
return -1;
}
int max_fd_num = pParam->Socket;
fd_set set;
FD_ZERO(&set);
FD_SET(pParam->Socket,&set);
//linux平台下timeout会被修改以表示剩余时间,故每次都要重新赋值
struct timeval timeout;
timeout.tv_sec = 6;//4;//csp modify for juan
timeout.tv_usec = 0;
int rtn = select(max_fd_num+1,&set,NULL,NULL,&timeout);
if(rtn <= 0)
{
printf("ResponseStreamCommand chn%d select error,result:%d,reason:%s\n",chn,rtn,buf);
return -1;
}
ret = recv(pParam->Socket, cur, 1, 0);
if(ret <= 0)
{
printf("ResponseStreamCommand chn%d recv error\n",chn);
return -1;
}
//csp modify 20131111
//if((buf[i] == '\n') && (buf[i-1] == '\r') && (i > 1))
if((i > 1) && (buf[i] == '\n') && (buf[i-1] == '\r'))
{
if(n == 0)
{
if(strstr(buf, "200 OK") == NULL)
{
printf("Response error\n");
ret = -1;
break;
}
n++;
}
else if(n == 1)
{
//csp modify 20131111
//if((buf[i-2] == '\n') && (buf[i-3] == '\r'))
if((i >= 3) && (buf[i-2] == '\n') && (buf[i-3] == '\r'))
{
n++;
}
}
else
{
//csp modify 20131111
//if((buf[i-2] == '\n') && (buf[i-3] == '\r'))
if((i >= 3) && (buf[i-2] == '\n') && (buf[i-3] == '\r'))
{
ret = 0;
//printf("ResponseStreamCommand result:%s\n", buf);
GetVideoAndAudioAttr(buf,chn);
break;
}
}
}
i++;
//csp modify 20131111
if(i >= (int)(sizeof(buf)-1))
{
printf("Response error\n");
ret = -1;
break;
}
cur = &buf[i];
}
return ret;
}
/* receive streaming data */
static int ReceiveData(int socket, char* buf, int recv_len)
{
int pack_len = 0;
int ret = -1;
char* cur_buf = NULL;
if(buf == NULL)
{
return -1;
}
cur_buf = buf;
while(pack_len < recv_len)
{
ret = recv(socket, cur_buf, recv_len-pack_len, 0);
if(ret <= 0)
{
printf("ReceiveData error,ret=%d,errno=%d,errstr=%s\n",ret,errno,strerror(errno));
return -1;
}
//pack_len = pack_len + ret;
//cur_buf = cur_buf + pack_len;
pack_len += ret;
cur_buf = buf+pack_len;
}
return 0;
}
/* skip streaming data */
int SkipData(int socket, int skip_len)
{
int remain_len = skip_len;
int ret = -1;
char buffer[4096];
while(remain_len > 0)
{
int size = (remain_len < ((int)sizeof(buffer))) ? remain_len : ((int)sizeof(buffer));
ret = recv(socket, buffer, size, 0);
if(ret <= 0)
{
printf("SkipData error,ret=%d,errno=%d,errstr=%s\n",ret,errno,strerror(errno));
return -1;
}
remain_len -= ret;
}
return 0;
}
/*------------------- define ---------------*/
int I13_Init(unsigned int max_client_num)
{
if(max_client_num <= 0)
{
return -1;
}
g_i13_client_count = max_client_num;
g_i13c_info = (i13_client_info *)malloc(g_i13_client_count*sizeof(i13_client_info));
if(g_i13c_info == NULL)
{
return -1;
}
memset(g_i13c_info,0,g_i13_client_count*sizeof(i13_client_info));
int i = 0;
for(i = 0; i < (int)g_i13_client_count; i++)
{
g_i13c_info[i].cap_fd = -1;
g_i13c_info[i].eventLoopWatchVariable = 0;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&g_i13c_info[i].lock, &attr);
pthread_mutexattr_destroy(&attr);
}
g_init_flag = 1;
return 0;
}
int I13_DeInit()
{
return 0;
}
int I13_GetNetworkParam(ipc_unit *ipcam, ipc_neteork_para_t *pnw)
{
if(!g_init_flag)
{
return -1;
}
if(ipcam == NULL)
{
return -1;
}
if(pnw == NULL)
{
return -1;
}
if(ipcam->net_mask)
{
pnw->ip_address = ipcam->dwIp;
pnw->net_mask = ipcam->net_mask;
pnw->net_gateway = ipcam->net_gateway;
pnw->dns1 = ipcam->dns1;
pnw->dns2 = ipcam->dns2;
return 0;
}
char cgi[1024];
sprintf(cgi, "/web/cgi-bin/hi3510/param.cgi?cmd=getnetattr");
char result[1024];
memset(result,0,sizeof(result));
if(ipnc_exec_cgi_cmd(ipcam, cgi, result) != 0)
{
printf("I13_GetNetworkParam failed\n");
return -1;
}
char *ip = strstr(result, " ip=\"");
if(ip == NULL)
{
printf("ip is null\n");
return -1;
}
char *netmask = strstr(result, "netmask=\"");
if(netmask == NULL)
{
printf("netmask is null\n");
return -1;
}
char *gateway = strstr(result, "gateway=\"");
if(gateway == NULL)
{
printf("gateway is null\n");
return -1;
}
char *fdnsip = strstr(result, "fdnsip=\"");
if(fdnsip == NULL)
{
printf("fdnsip is null\n");
//return -1;
}
char *sdnsip = strstr(result, "sdnsip=\"");
if(sdnsip == NULL)
{
printf("sdnsip is null\n");
//return -1;
}
ip += strlen(" ip=\"");
*strstr(ip, "\"") = 0;
printf("I13 ip : %s\n", ip);
netmask += strlen("netmask=\"");
*strstr(netmask, "\"") = 0;
printf("I13 netmask : %s\n", netmask);
gateway += strlen("gateway=\"");
*strstr(gateway, "\"") = 0;
printf("I13 gateway : %s\n", gateway);
if(fdnsip != NULL)
{
fdnsip += strlen("fdnsip=\"");
*strstr(fdnsip, "\"") = 0;
printf("I13 fdnsip : %s\n", fdnsip);
}
if(sdnsip != NULL)
{
sdnsip += strlen("sdnsip=\"");
*strstr(sdnsip, "\"") = 0;
printf("I13 sdnsip : %s\n", sdnsip);
}
pnw->ip_address = inet_addr(ip);
pnw->net_mask = inet_addr(netmask);
pnw->net_gateway = inet_addr(gateway);
if(fdnsip != NULL)
{
pnw->dns1 = inet_addr(fdnsip);
}
else
{
pnw->dns1 = 0;
}
if(sdnsip != NULL)
{
pnw->dns2 = inet_addr(sdnsip);
}
else
{
pnw->dns2 = 0;
}
return 0;
}
int I13_SetNetworkParam(ipc_unit *ipcam, ipc_neteork_para_t *pnw)
{
if(!g_init_flag)
{
return -1;
}
if(ipcam == NULL)
{
return -1;
}
if(pnw == NULL)
{
return -1;
}
struct sockaddr_in local, remote, from;
int sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock == -1)
{
printf("Set IPNC:socket error\n");
return -1;
}
int reuseflag = 1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&reuseflag,sizeof(reuseflag)) == SOCKET_ERROR)
{
printf("Set IPNC:port mulit error\n");
close(sock);
return -1;
}
unsigned int dwInterface = INADDR_ANY;
unsigned int dwMulticastGroup = inet_addr(MCASTADDR);
unsigned short iPort = MCASTPORT;
local.sin_family = AF_INET;
local.sin_port = htons(iPort);
local.sin_addr.s_addr = dwInterface;
if(bind(sock, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR)
{
printf("Set IPNC:bind error\n");
close(sock);
return -1;
}
struct ip_mreq mreq;
memset(&mreq,0,sizeof(struct ip_mreq));
mreq.imr_multiaddr.s_addr = inet_addr(MCASTADDR);
mreq.imr_interface.s_addr = GetLocalIp();
if(setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(const char*)&mreq,sizeof(mreq)) == SOCKET_ERROR)
{
printf("Set IPNC:join mulit error\n");
close(sock);
return -1;
}
//csp modify for klw
int optval = 0;
if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&optval, sizeof(optval)) == SOCKET_ERROR)
{
printf("setsockopt(IP_MULTICAST_LOOP) failed\n");
//close(sock);
//return 0;
}
remote.sin_family = AF_INET;
remote.sin_port = htons(iPort);
remote.sin_addr.s_addr = dwMulticastGroup;
char recvbuf[BUFSIZE], sendbuf[BUFSIZE];
char ip[20];
char netmask[20];
char gateway[20];
char fdnsip[20];
char sdnsip[20];
struct in_addr host;
host.s_addr = pnw->ip_address;
strcpy(ip, inet_ntoa(host));
host.s_addr = pnw->net_mask;
strcpy(netmask, inet_ntoa(host));
host.s_addr = pnw->net_gateway;
strcpy(gateway, inet_ntoa(host));
host.s_addr = pnw->dns1;
strcpy(fdnsip, inet_ntoa(host));
host.s_addr = pnw->dns2;
strcpy(sdnsip, inet_ntoa(host));
char cmd[320];
//sprintf(cmd,COMMAND,ip,netmask,gateway,fdnsip,sdnsip);
sprintf(cmd,COMMAND,ip,netmask,gateway,fdnsip);
printf("设置新的IP地址\n");
snprintf(sendbuf, BUFSIZE, "CMD * HDS/1.0\r\n"
"CSeq:3\r\n"
"Client-ID:nvmOPxEnYfQRAeLFdsMrpBbnMDbEPiMC\r\n"
"Accept-Type:text/HDP\r\n"
"Authorization:Basic %s:%s\r\n"
"Device-ID:%s\r\n"
"Content-Length:%d\r\n"
"\r\n%s",
ipcam->user, ipcam->pwd, ipcam->uuid, strlen(cmd), cmd);
//printf("hello:%s\n", sendbuf);
if(sendto(sock, (char *)sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&remote, sizeof(remote)) == SOCKET_ERROR)
{
printf("Set IPNC:sendto failed with: %d\n", errno);
close(sock);
return -1;
}
char ack[256];
sprintf(ack,"Device-ID:%s",ipcam->uuid);
unsigned char byAckFlag = 0;
int i;
for(i = 0; i < 100; i++)
{
fd_set r;
struct timeval t;
t.tv_sec = 0;
t.tv_usec = 4*1000;//100;//csp modify for klw
FD_ZERO(&r);
FD_SET(sock, &r);
int len = sizeof(struct sockaddr_in);
int ret = select(sock + 1, &r, NULL, NULL, &t);
if(ret > 0)
{
memset(recvbuf, 0, sizeof(recvbuf));//csp modify for klw
ret = recvfrom(sock, recvbuf, BUFSIZE, 0, (struct sockaddr *)&from, (socklen_t *)&len);
if(ret < 0)
{
usleep(1);
continue;
}
recvbuf[ret] = 0;
if((strstr(recvbuf, "MCTP/1.0 200 OK") && strstr(recvbuf, "Client-ID:nvmOPxEnYfQRAeLFdsMrpBbnMDbEPiMC") && strstr(recvbuf, ack)) ||
(strstr(recvbuf, "HDS/1.0 200 OK") && strstr(recvbuf, "Client-ID:nvmOPxEnYfQRAeLFdsMrpBbnMDbEPiMC") && strstr(recvbuf, ack)))//csp modify for klw
{
printf("i13 set netrork param ok\n");
byAckFlag = 1;
break;
}
else
{
printf("I13_SetNetworkParam recv error response from(%s,%d),msg:%s\n",inet_ntoa(from.sin_addr),ntohs(from.sin_port),recvbuf);
}
}
}
close(sock);
if(!byAckFlag)
{
printf("I13_SetNetworkParam no ack\n");
//csp modify for juan
if(ipcam->protocol_type == PRO_TYPE_JUAN)
{
return 0;
}
return -1;
}
return 0;
}
extern int DoStreamStateCallBack(int chn, real_stream_state_e msg);
void* ThreadPROCI13(void* pParam)
{
unsigned int time_out_count = 0;
char *enc_buf = NULL;
int enc_len = 0;
int chn = (int)pParam;
if(chn < 0 || chn >= (int)g_i13_client_count)
{
goto ProcQuit;
}
if(chn < (int)(g_i13_client_count/2))
{
enc_len = 500*1024;//400*1024;
}
else
{
enc_len = 200*1024;
}
enc_buf = (char *)malloc(enc_len);
if(enc_buf == NULL)
{
goto ProcOver;
}
while(g_init_flag)
{
if(g_i13c_info[chn].eventLoopWatchVariable)
{
break;
}
int max_fd_num = g_i13c_info[chn].cap_fd;
fd_set set;
FD_ZERO(&set);
FD_SET(g_i13c_info[chn].cap_fd,&set);
//linux平台下timeout会被修改以表示剩余时间,故每次都要重新赋值
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int ret = select(max_fd_num+1,&set,NULL,NULL,&timeout);
if(ret == 0)
{
//csp modify 20131111
//if(++time_out_count > 10)
if(++time_out_count > 5)
{
printf("ThreadPROCI13:chn%d select sock timeout & quit!!!\n",chn);
time_out_count = 0;
break;
}
else
{
printf("ThreadPROCI13:chn%d select sock timeout<%d> & continue!\n",chn,time_out_count);
continue;
}
}
if(ret < 0)
{
printf("ThreadPROCI13:chn%d select sock error:[%s]!\n",chn,strerror(errno));
break;
}
if(g_i13c_info[chn].cap_fd != INVALID_SOCKET && FD_ISSET(g_i13c_info[chn].cap_fd,&set))
{
time_out_count = 0;
RTSP_ITLEAVED_HDR_S rtsp_header;
ret = ReceiveData(g_i13c_info[chn].cap_fd, (char*)&rtsp_header, sizeof(rtsp_header));
if(ret != 0)
{
printf("chn%d recv header error\n",chn);
break;
}
unsigned int packet_len = ntohl(rtsp_header.payloadLen) - sizeof(RTP_HDR_S);
unsigned int timestamp = ntohl(rtsp_header.rtpHead.ts);
if(packet_len > (unsigned int)enc_len)
{
printf("chn%d skip data:framelen(%d) > bufferlen(%d)\n",chn,packet_len,enc_len);
ret = SkipData(g_i13c_info[chn].cap_fd, packet_len);
if(ret != 0)
{
printf("chn%d skip data error\n",chn);
break;
}
else
{
printf("chn%d skip data succ\n",chn);
continue;
}
}
char *recv_buf = (char*)enc_buf;
ret = ReceiveData(g_i13c_info[chn].cap_fd, (char*)recv_buf, packet_len);
if(ret != 0)
{
printf("chn%d recv data error\n",chn);
break;
}
//printf("chn%d recv data type:<%d> data len:<%d>\n",chn,rtsp_header.rtpHead.pt,packet_len);
if(rtsp_header.rtpHead.pt == PT_DATA)
{
continue;
}
if(rtsp_header.rtpHead.pt == PT_G711 || rtsp_header.rtpHead.pt == PT_G726)
{
if(g_i13c_info[chn].pStreamCB != NULL)
{
real_stream_s stream;
memset(&stream, 0, sizeof(stream));
stream.chn = chn;
stream.data = (unsigned char *)recv_buf + 4;
stream.len = packet_len - 4;
stream.pts = timestamp;
stream.pts *= 1000;
stream.media_type = (rtsp_header.rtpHead.pt == PT_G711) ? MEDIA_PT_G711 : MEDIA_PT_G726;
g_i13c_info[chn].pStreamCB(&stream, g_i13c_info[chn].dwContext);
}
continue;
}
if(rtsp_header.rtpHead.pt != PT_H264)
{
//printf("recv data NOT PT_H264:<%d>\n",rtsp_header.rtpHead.pt);
continue;
}
if(g_i13c_info[chn].pStreamCB != NULL)
{
//csp modify 20140423
if(timestamp != g_i13c_info[chn].last_pts)
{
g_i13c_info[chn].frame_count++;
if((g_i13c_info[chn].frame_count % 250) == 0)
{
//printf("i13 chn%d span %lds\n",chn,time(NULL)-g_i13c_info[chn].last_t);
g_i13c_info[chn].last_t = time(NULL);
}
g_i13c_info[chn].last_pts = timestamp;
}
real_stream_s stream;
//memset(&stream, 0, sizeof(stream));
stream.chn = chn;
stream.data = (unsigned char *)recv_buf;
stream.len = packet_len;
stream.pts = timestamp;
stream.pts *= 1000;
stream.media_type = MEDIA_PT_H264;
if((recv_buf[4] & 0x1f) == 0x07)
{
stream.frame_type = REAL_FRAME_TYPE_I;
//if(chn == 0)
//{
// printf("chn%d frame type:I\n",chn);
//}
}
else
{
stream.frame_type = REAL_FRAME_TYPE_P;
//if(chn == 0)
//{
// printf("chn%d frame type:P\n",chn);
//}
}
stream.rsv = 0;
stream.mdevent = 0;
stream.width = g_i13c_info[chn].video_width;
stream.height = g_i13c_info[chn].video_height;
g_i13c_info[chn].pStreamCB(&stream, g_i13c_info[chn].dwContext);
}
}
else
{
if(++time_out_count > 10)
{
printf("ThreadPROCI13:chn%d unknown sock error & quit!!!\n",chn);
time_out_count = 0;
break;
}
}
}
pthread_mutex_lock(&g_i13c_info[chn].lock);
g_i13c_info[chn].pid = 0;
g_i13c_info[chn].eventLoopWatchVariable = 0;
if(g_i13c_info[chn].cap_fd != -1)
{
close(g_i13c_info[chn].cap_fd);
g_i13c_info[chn].cap_fd = -1;
}
pthread_mutex_unlock(&g_i13c_info[chn].lock);
free(enc_buf);
enc_buf = NULL;
ProcOver:
DoStreamStateCallBack(chn, REAL_STREAM_STATE_LOST);
ProcQuit:
pthread_detach(pthread_self());
return 0;
}
int I13_Start(int chn, RealStreamCB pCB, unsigned int dwContext, char* streamInfo, unsigned int dwIp, unsigned short wPort, char *user, char *pwd, unsigned char rtsp_over_tcp)
{
if(!g_init_flag)
{
return -1;
}
if(chn < 0 || chn >= (int)g_i13_client_count)
{
return -1;
}
//printf("I13_Start - 1\n");
if(I13_GetLinkStatus(chn))
{
I13_Stop(chn);
}
//printf("I13_Start - 2\n");
//csp modify 20140613
unsigned char juan_ipc = 0;
if(streamInfo)
{
if(strcmp(streamInfo, "JUAN") == 0)
{
juan_ipc = 1;
}
}
PARAM_S params;
memset(¶ms, 0, sizeof(PARAM_S));
params.Buf = NULL;
params.State = 1;
params.Socket = -1;
strcpy(params.Uname, user);
strcpy(params.Pword, pwd);
struct in_addr serv;
serv.s_addr = dwIp;
strcpy(params.Ip,inet_ntoa(serv));
params.Port = wPort;
PARAM_S* pParam = (PARAM_S*)¶ms;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(pParam->Port);
addr.sin_addr.s_addr = inet_addr(pParam->Ip);
if(addr.sin_addr.s_addr == 0 || params.Port == 0 || addr.sin_addr.s_addr == (unsigned int)(-1))
{
//printf("ip:port error\n");
return -1;
}