-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtspd.c
1459 lines (1315 loc) · 44.2 KB
/
rtspd.c
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
/**
* @file rtspd.c
* Simple RTSP server demo
* Copyright (C) 2013 GM Corp. (http://www.grain-media.com)
*
* $Revision: 1.5 $
* $Date: 2014/12/30 05:37:57 $
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <dirent.h>
#include "librtsp.h"
#include "gmlib.h"
#define DVR_ENC_EBST_ENABLE 0x55887799
#define DVR_ENC_EBST_DISABLE 0
#define ENC_TYPE_H264 0
#define ENC_TYPE_MPEG4 1
#define ENC_TYPE_MJPEG 2
#define CAP_CH_NUM 1
#define RTSP_NUM_PER_CAP 4
#define CAP_PATH_NUM 4
#define ENC_TRACK_NUM 4
#define SDPSTR_MAX 128
#define SR_MAX 64
#define VQ_MAX (SR_MAX)
#define VQ_LEN 5
#define AQ_MAX 64 /* 1 MP2 and 1 AMR for live streaming, another 2 for file streaming. */
#define AQ_LEN 2 /* 1 MP2 and 1 AMR for live streaming, another 2 for file streaming. */
#define AV_NAME_MAX 127
#define RTP_HZ 90000
#define ERR_GOTO(x, y) do { ret = x; goto y; } while(0)
#define MUTEX_FAILED(x) (x == ERR_MUTEX)
#define VIDEO_FRAME_NUMBER VQ_LEN+1
#define NONE_BS_EVENT 0
#define START_BS_EVENT 1
#define STOP_BS_EVENT 2
#define CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num) \
do { \
if((ch_num >= CAP_CH_NUM || ch_num < 0) || \
(sub_num >= RTSP_NUM_PER_CAP || sub_num < 0)) { \
fprintf(stderr, "%s: ch_num=%d, sub_num=%d error!\n",__FUNCTION__, ch_num, sub_num); \
return -1; \
} \
} while(0) \
typedef struct {
void *obj;
gm_cap_attr_t cap_attr;
gm_3dnr_attr_t dnr_attr;
} gm_cap_info_t;
typedef struct {
void *obj;
int enc_type;
union {
gm_h264e_attr_t h264e_attr;
gm_mpeg4e_attr_t mpeg4e_attr;
gm_mjpege_attr_t mjpege_attr;
} codec;
} gm_enc_info_t;
typedef struct {
gm_cap_info_t cap;
gm_enc_info_t enc[ENC_TRACK_NUM];
void *bindfd[ENC_TRACK_NUM];
} gm_enc_t;
void *enc_groupfd;
gm_enc_t enc_param[CAP_CH_NUM][CAP_PATH_NUM];
typedef int (*open_container_fn)(int ch_num, int sub_num);
typedef int (*close_container_fn)(int ch_num, int sub_num);
typedef enum st_opt_type {
OPT_NONE=0,
RTSP_LIVE_STREAMING,
} opt_type_t;
typedef struct st_vbs {
int enabled; //DVR_ENC_EBST_ENABLE: enabled, DVR_ENC_EBST_DISABLE: disabled
int enc_type; // 0:ENC_TYPE_H264, 1:ENC_TYPE_MPEG4, 2:ENC_TYPE_MJPEG
} vbs_t;
typedef struct st_priv_vbs {
char sdpstr[SDPSTR_MAX];
int qno;
int offs;
int len;
unsigned int tv_ms;
int cap_ch;
int cap_path;
int rec_track;
char *bs_buf;
unsigned int bs_buf_len;
pthread_mutex_t priv_vbs_mutex;
} priv_vbs_t;
typedef struct st_bs {
int event; // config change please set 1 for enqueue_thread to config this
int enabled; //DVR_ENC_EBST_ENABLE: enabled, DVR_ENC_EBST_DISABLE: disabled
opt_type_t opt_type; /* 1:rtsp_live_streaming, 2: file_avi_recording 3:file_h264_recording */
vbs_t video; /* VIDEO, 0: main-bitstream, 1: sub1-bitstream, 2:sub2-bitstream */
} avbs_t;
typedef struct st_priv_bs {
int play;
int congest;
int sr;
char name[AV_NAME_MAX];
open_container_fn open;
close_container_fn close;
priv_vbs_t video; /* VIDEO, 0: main-bitstream, 1: sub1-bitstream, 2:sub2-bitstream */
} priv_avbs_t;
typedef struct st_av {
/* public data */
avbs_t bs[RTSP_NUM_PER_CAP]; /* VIDEO, 0: main-bitstream, 1: sub1-bitstream, 2:sub2-bitstream */
/* update date */
pthread_mutex_t ubs_mutex;
/* private data */
int enabled; //DVR_ENC_EBST_ENABLE: enabled, DVR_ENC_EBST_DISABLE: disabled
priv_avbs_t priv_bs[RTSP_NUM_PER_CAP];
} av_t;
pthread_t enqueue_thread_id = 0;
pthread_t encode_thread_id = 0;
unsigned int sys_tick = 0;
struct timeval sys_sec = {-1, -1};
int sys_port = 554;
char *ipptr = NULL;
static int rtspd_sysinit=0;
static int rtspd_set_event=0;
static int rtspd_set_1ch=0;
static int rtspd_set_enc_type=0;
static int rtspd_avail_ch=0;
pthread_mutex_t stream_queue_mutex;
av_t enc[CAP_CH_NUM];
gm_system_t gm_system;
void *groupfd; // return of gm_new_groupfd()
void *bindfd; // return of gm_bind()
void *capture_object;
void *encode_object;
static char *rtsp_enc_type_str[] = {
"H264 ",
"MPEG4",
"MJPEG"
};
static char getch(void)
{
int n = 1;
unsigned char ch;
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 10;
tv.tv_usec = 0;
n = select(1, &rfds, NULL, NULL, &tv);
if (n > 0) {
n = read(0, &ch, 1);
if (n == 1)
return ch;
return n;
}
return -1;
}
static int do_queue_alloc(int type)
{
int rc;
do {
rc = stream_queue_alloc(type);
} while MUTEX_FAILED(rc);
return rc;
}
static unsigned int get_tick_gm(unsigned int tv_ms)
{
sys_tick=tv_ms*(RTP_HZ/1000);
return sys_tick;
}
static int convert_gmss_media_type(int type)
{
int media_type;
switch(type) {
case ENC_TYPE_H264:
media_type = GM_SS_TYPE_H264;
break;
case ENC_TYPE_MPEG4:
media_type = GM_SS_TYPE_MP4;
break;
case ENC_TYPE_MJPEG:
media_type = GM_SS_TYPE_MJPEG;
break;
default:
media_type = -1;
fprintf(stderr, "convert_gmss_media_type: type=%d, error!\n", type);
break;
}
return media_type;
}
static int open_live_streaming(int ch_num, int sub_num)
{
int media_type;
avbs_t *b;
priv_avbs_t *pb;
char livename[64];
CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num);
b = &enc[ch_num].bs[sub_num];
pb = &enc[ch_num].priv_bs[sub_num];
media_type = convert_gmss_media_type(b->video.enc_type);
pb->video.qno = do_queue_alloc(media_type);
sprintf(livename, "live/ch%02d_%d", ch_num, sub_num);
pb->sr = stream_reg(livename, pb->video.qno, pb->video.sdpstr,
0, 0, 1, 0, 0, 0, 0, 0, 0);
if (pb->sr < 0)
fprintf(stderr, "open_live_streaming: ch_num=%d, sub_num=%d setup error\n", ch_num, sub_num);
strcpy(pb->name, livename);
return 0;
}
#define TIMEVAL_DIFF(start, end) (((end.tv_sec)-(start.tv_sec))*1000000+((end.tv_usec)-(start.tv_usec)))
//struct timeval test_st_tv={0,0},test_ed_tv={0,0};
static int write_rtp_frame_ext(int ch_num, int sub_num, void *data,
int data_len, unsigned int tv_ms) //struct timeval *tv)
{
int ret, media_type;
avbs_t *b;
priv_avbs_t *pb;
gm_ss_entity entity;
struct timeval curr_tval; //,test_st_tv={0,0},test_ed_tv={0,0};
static struct timeval err_print_tval;
//static unsigned int prv_stamp=0;
pb = &enc[ch_num].priv_bs[sub_num];
b = &enc[ch_num].bs[sub_num];
if( pb->play == 0 || (b->event != NONE_BS_EVENT) )
{
ret = 1;
goto exit_free_as_buf;
}
entity.data = (char *) data;
entity.size = data_len;
//entity.timestamp = get_tick(NULL );
entity.timestamp = get_tick_gm(tv_ms);
media_type = convert_gmss_media_type(b->video.enc_type);
//gettimeofday(&test_st_tv,NULL);
pthread_mutex_lock(&stream_queue_mutex);
ret = stream_media_enqueue(media_type, pb->video.qno, &entity);
pthread_mutex_unlock(&stream_queue_mutex);
//gettimeofday(&test_ed_tv,NULL);
//if(prv_stamp==0) prv_stamp= entity.timestamp;
//printf("dstamp=%d\n",entity.timestamp-prv_stamp);
//prv_stamp= entity.timestamp;
if( ret < 0 )
{
gettimeofday(&curr_tval, NULL );
if( ret == ERR_FULL)
{
pb->congest = 1;
if( TIMEVAL_DIFF(err_print_tval, curr_tval) > 5000000 )
{
fprintf(stderr,
"ext enqueue queue ch_num=%d, sub_num=%d full\n",
ch_num, sub_num);
}
}
else if((ret != ERR_NOTINIT)&& (ret != ERR_MUTEX) && (ret != ERR_NOTRUN))
{
if (TIMEVAL_DIFF(err_print_tval, curr_tval) > 5000000)
{
fprintf(stderr, "ext enqueue queue ch_num=%d, sub_num=%d error %d\n",
ch_num, sub_num, ret);
}
}
if( TIMEVAL_DIFF(err_print_tval, curr_tval) > 5000000)
{
fprintf(stderr,
"ext enqueue queue ch_num=%d, sub_num=%d error %d\n",
ch_num, sub_num, ret);
gettimeofday(&err_print_tval, NULL );
}
goto exit_free_audio_buf;
}
return 0;
exit_free_audio_buf:
//put_video_frame(pb->video.qno, fs);
exit_free_as_buf:
//free_bs_data(ch_num, sub_num, q);
return 1;
}
static int close_live_streaming(int ch_num, int sub_num)
{
avbs_t *b;
priv_avbs_t *pb;
int ret = 0;
CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num);
b = &enc[ch_num].bs[sub_num];
pb = &enc[ch_num].priv_bs[sub_num];
if (pb->sr >= 0) {
ret = stream_dereg(pb->sr, 1);
if (ret < 0)
goto err_exit;
pb->sr = -1;
pb->video.qno = -1;
pb->play = 0;
}
err_exit:
if(ret < 0)
fprintf(stderr, "%s: stream_dereg(%d) err %d\n", __func__, pb->sr, ret);
return ret;
}
int open_bs(int ch_num, int sub_num)
{
avbs_t *b;
priv_avbs_t *pb;
CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num);
pb = &enc[ch_num].priv_bs[sub_num];
b = &enc[ch_num].bs[sub_num];
enc[ch_num].enabled = DVR_ENC_EBST_ENABLE;
enc[ch_num].bs[sub_num].enabled = DVR_ENC_EBST_ENABLE;
enc[ch_num].bs[sub_num].video.enabled = DVR_ENC_EBST_ENABLE;
switch (b->opt_type) {
case RTSP_LIVE_STREAMING:
//set_sdpstr(pb->video.sdpstr, b->video.enc_type);
pb->open = open_live_streaming;
pb->close = close_live_streaming;
break;
case OPT_NONE:
default:
break;
}
return 0;
}
int close_bs(int ch_num, int sub_num)
{
av_t *e;
int sub, is_close_channel = 1;
CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num);
e = &enc[ch_num];
e->bs[sub_num].video.enabled = DVR_ENC_EBST_DISABLE;
e->bs[sub_num].enabled = DVR_ENC_EBST_DISABLE;
for (sub = 0; sub < RTSP_NUM_PER_CAP; sub++) {
if (e->bs[sub].video.enabled == DVR_ENC_EBST_ENABLE) {
is_close_channel = 0;
break;
}
}
if (is_close_channel == 1)
enc[ch_num].enabled = DVR_ENC_EBST_DISABLE;
return 0;
}
static int bs_check_event(void)
{
int ch_num, sub_num, ret = 0;
avbs_t *b;
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
for (sub_num = 0; sub_num<RTSP_NUM_PER_CAP; sub_num++) {
b = &enc[ch_num].bs[sub_num];
if (b->event != NONE_BS_EVENT) {
ret = 1;
break;
}
}
}
return ret;
}
void bs_new_event(void)
{
int ch_num, sub_num;
avbs_t *b;
priv_avbs_t *pb;
if (bs_check_event() == 0) {
rtspd_set_event = 0;
return;
}
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
pthread_mutex_lock(&enc[ch_num].ubs_mutex);
for (sub_num = 0; sub_num < RTSP_NUM_PER_CAP; sub_num++) {
b = &enc[ch_num].bs[sub_num];
pb = &enc[ch_num].priv_bs[sub_num];
switch (b->event) {
case START_BS_EVENT:
open_bs(ch_num, sub_num);
if(pb->open) pb->open(ch_num, sub_num);
b->event = NONE_BS_EVENT;
break;
case STOP_BS_EVENT:
pb->open = NULL;
if(pb->close) { /* for recording */
pb->close(ch_num, sub_num);
pb->close = NULL;
close_bs(ch_num, sub_num);
}
b->event = NONE_BS_EVENT;
break;
default:
break;
}
}
pthread_mutex_unlock(&enc[ch_num].ubs_mutex);
}
}
int env_set_bs_new_event(int ch_num, int sub_num, int event)
{
avbs_t *b;
int ret = 0;
CHECK_CHANNUM_AND_SUBNUM(ch_num, sub_num);
b = &enc[ch_num].bs[sub_num];
switch (event) {
case START_BS_EVENT:
if (b->opt_type == OPT_NONE)
goto err_exit;
if (b->enabled == DVR_ENC_EBST_ENABLE) {
fprintf(stderr, "Already enabled: ch_num=%d, sub_num=%d\n", ch_num, sub_num);
ret = -1;
goto err_exit;
}
break;
case STOP_BS_EVENT:
if (b->enabled != DVR_ENC_EBST_ENABLE) {
fprintf(stderr, "Already disabled: ch_num=%d, sub_num=%d\n", ch_num, sub_num);
ret = -1;
goto err_exit;
}
break;
default:
fprintf(stderr, "env_set_bs_new_event: ch_num=%d, sub_num=%d, event=%d, error\n",
ch_num, sub_num, event);
ret = -1;
goto err_exit;
}
b->event = event;
rtspd_set_event = 1;
err_exit:
return ret;
}
int set_poll_event(void)
{
int ch_num, sub_num, ret = -1;
av_t *e;
avbs_t *b;
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
e = &enc[ch_num];
if (e->enabled != DVR_ENC_EBST_ENABLE)
continue;
for (sub_num = 0; sub_num < RTSP_NUM_PER_CAP; sub_num++) {
b = &e->bs[sub_num];
if (b->video.enabled == DVR_ENC_EBST_ENABLE) {
ret = 0;
}
}
}
return ret;
}
void get_enc_res(gm_enc_info_t *enc, int *enc_type, int *width, int *height)
{
int w, h;
gm_h264e_attr_t *h264e_attr;
gm_mpeg4e_attr_t *mpeg4e_attr;
gm_mjpege_attr_t *mjpege_attr;
switch (enc->enc_type) {
case ENC_TYPE_H264:
h264e_attr = &enc->codec.h264e_attr;
w = h264e_attr->dim.width;
h = h264e_attr->dim.height;
break;
case ENC_TYPE_MPEG4:
mpeg4e_attr = &enc->codec.mpeg4e_attr;
w = mpeg4e_attr->dim.width;
h = mpeg4e_attr->dim.height;
break;
case ENC_TYPE_MJPEG:
mjpege_attr = &enc->codec.mjpege_attr;
w = mjpege_attr->dim.width;
h = mjpege_attr->dim.height;
break;
}
if (enc_type)
*enc_type = enc->enc_type;
if (width)
*width = w;
if (height)
*height = h;
}
#define PRINT_INTERVAL_MS 5000 /* millisecond */
static unsigned int frame_counts[CAP_CH_NUM][RTSP_NUM_PER_CAP] = {{0}};
static unsigned int rec_bs_len[CAP_CH_NUM][RTSP_NUM_PER_CAP] = {{0}};
static void print_enc_average(int ch_num, int sub_num, int bs_len, struct timeval *cur_timeval)
{
int enc_type, w, h;
static struct timeval last_timeval;
static unsigned int total_ms, print_init = 0;
unsigned int diff_ms, i, j;
char res_str[20];
priv_avbs_t *pb;
gm_enc_info_t *gm_enc;
frame_counts[ch_num][sub_num]++;
rec_bs_len[ch_num][sub_num] += bs_len;
if (print_init == 0) {
last_timeval.tv_sec = cur_timeval->tv_sec;
last_timeval.tv_usec = cur_timeval->tv_usec;
print_init = 1;
total_ms = 0;
return;
}
/* get diff time */
if (cur_timeval->tv_sec > last_timeval.tv_sec) {
diff_ms = 1000 + (cur_timeval->tv_usec / 1000) - (last_timeval.tv_usec / 1000);
diff_ms += (cur_timeval->tv_sec - last_timeval.tv_sec - 1) * 1000;
} else {
diff_ms = (cur_timeval->tv_usec - last_timeval.tv_usec) / 1000;
}
total_ms += diff_ms;
/* show statistic */
if (total_ms >= PRINT_INTERVAL_MS) {
for (i = 0; i < CAP_CH_NUM; i++) {
for (j = 0; j < RTSP_NUM_PER_CAP; j++) {
if (frame_counts[i][j] == 0)
continue;
pb = &enc[i].priv_bs[j];
gm_enc = &enc_param[pb->video.cap_ch][pb->video.cap_path].enc[pb->video.rec_track];
get_enc_res(gm_enc, &enc_type, &w, &h);
sprintf(res_str, "%dx%d", w, h);
printf("/live/ch%02d_%d: cap%d_%d %9s %s %d.%02dfps %dkbps\n", i, j,
pb->video.cap_ch, pb->video.cap_path,
res_str,
rtsp_enc_type_str[enc_type],
(frame_counts[i][j] * 1000 / total_ms),
(frame_counts[i][j] * 100000 / total_ms) % 100,
(rec_bs_len[i][j] * 8 / 1024) * 1000 / total_ms);
frame_counts[i][j] = 0;
rec_bs_len[i][j] = 0;
}
}
printf("\n");
total_ms = 0;
}
last_timeval.tv_sec = cur_timeval->tv_sec;
last_timeval.tv_usec = cur_timeval->tv_usec;
}
#define POLL_WAIT_TIME 15000 /* microseconds */
static unsigned int poll_wait_time = 0;
static void env_release_resources(void)
{
int ret, ch_num;
av_t *e;
if ((ret = stream_server_stop()))
fprintf(stderr, "stream_server_stop() error %d\n", ret);
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
e = &enc[ch_num];
pthread_mutex_destroy(&e->ubs_mutex);
}
}
static int frm_cb(int type, int qno, gm_ss_entity *entity)
{
priv_avbs_t *pb;
int ch_num, sub_num;
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
for (sub_num = 0; sub_num < RTSP_NUM_PER_CAP; sub_num++) {
pb = &enc[ch_num].priv_bs[sub_num];
if (pb->video.offs == (int)(entity->data)
&& pb->video.len == entity->size && pb->video.qno==qno) {
pthread_mutex_lock(&pb->video.priv_vbs_mutex);
pb->video.offs = 0;
pb->video.len = 0;
pthread_mutex_unlock(&pb->video.priv_vbs_mutex);
}
}
}
return 0;
}
priv_avbs_t *find_file_sr(char *name, int srno)
{
int ch_num, sub_num, hit=0;
priv_avbs_t *pb;
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
for (sub_num = 0; sub_num < RTSP_NUM_PER_CAP; sub_num++) {
pb = &enc[ch_num].priv_bs[sub_num];
if ((pb->sr == srno) && (pb->name) && (strcmp(pb->name, name) == 0)) {
hit = 1;
break;
}
}
if (hit)
break;
}
return (hit ? pb : NULL);
}
static int cmd_cb(char *name, int sno, int cmd, void *p)
{
int ret = -1;
priv_avbs_t *pb;
switch(cmd)
{
case GM_STREAM_CMD_OPTION:
ret = 0;
break;
case GM_STREAM_CMD_DESCRIBE:
ret = 0;
break;
case GM_STREAM_CMD_OPEN:
printf("%s:%d <GM_STREAM_CMD_OPEN>\n", __FUNCTION__, __LINE__);
ERR_GOTO(-10, cmd_cb_err);
break;
case GM_STREAM_CMD_SETUP:
ret = 0;
break;
case GM_STREAM_CMD_PLAY:
if( strncmp(name, "live/", 5) == 0 ) {
if ((pb = find_file_sr(name, sno)) == NULL)
ERR_GOTO(-1, cmd_cb_err);
if (pb->video.qno >= 0)
pb->play = 1;
}
ret = 0;
break;
case GM_STREAM_CMD_PAUSE:
printf("%s:%d <GM_STREAM_CMD_PAUSE>\n", __FUNCTION__, __LINE__);
ret = 0;
break;
case GM_STREAM_CMD_TEARDOWN:
if( strncmp(name, "live/", 5) == 0 ) {
if ((pb = find_file_sr(name, sno)) == NULL)
ERR_GOTO(-1, cmd_cb_err);
pb->play = 0;
}
ret = 0;
break;
default:
fprintf(stderr, "%s: not support cmd %d\n", __func__, cmd);
break;
}
cmd_cb_err:
if( ret < 0 ) {
fprintf(stderr, "%s: cmd %d error %d\n", __func__, cmd, ret);
}
return ret;
}
void *enqueue_thread(void *ptr)
{
while (rtspd_sysinit) {
if (rtspd_set_event)
bs_new_event();
if (set_poll_event() < 0) {
sleep(1);
continue;
}
usleep(1000);
}
env_release_resources();
pthread_exit(NULL);
return NULL;
}
void gm_update_bs_info(void)
{
int cap_ch,cap_path,rec_track;
int ch=0;
avbs_t *avbs;
gm_enc_t *param;
for (cap_ch = 0; cap_ch < CAP_CH_NUM; cap_ch++) {
for (cap_path = 0; cap_path < CAP_PATH_NUM; cap_path++) {
param = &enc_param[cap_ch][cap_path];
for (rec_track = 0; rec_track < ENC_TRACK_NUM; rec_track++) {
if (param->bindfd[rec_track]) {
avbs = &enc[cap_ch].bs[ch];
avbs->video.enc_type = param->enc[rec_track].enc_type;
ch++;
}
}
}
}
}
int env_init(void)
{
int ret = 0;
int ch_num, sub_num;
av_t *e;
avbs_t *b;
priv_avbs_t *pb;
memset(enc,0,sizeof(enc));
/* private data initial */
for (ch_num = 0; ch_num < CAP_CH_NUM; ch_num++) {
e = &enc[ch_num];
if (pthread_mutex_init(&e->ubs_mutex, NULL)) {
perror("env_init: mutex init failed:");
exit(-1);
}
for (sub_num = 0; sub_num < RTSP_NUM_PER_CAP; sub_num++) {
b = &e->bs[sub_num];
b->opt_type = RTSP_LIVE_STREAMING;
b->video.enc_type = ENC_TYPE_H264;
b->event = NONE_BS_EVENT;
b->enabled = DVR_ENC_EBST_DISABLE;
b->video.enabled = DVR_ENC_EBST_DISABLE;
pb = &e->priv_bs[sub_num];
pb->video.qno = -1;
pb->video.offs=0;
pb->video.len=0;
pb->sr = -1;
if (pthread_mutex_init(&pb->video.priv_vbs_mutex, NULL)) {
perror("env_enc_init: priv_vbs mutex init failed:");
exit(-1);
}
}
}
//update bs info from decoder
gm_update_bs_info();
srand((unsigned int)time(NULL));
if ((ret = stream_server_init(ipptr, (int) sys_port, 0, 1444, 256, SR_MAX, VQ_MAX, VQ_LEN,
AQ_MAX, AQ_LEN, frm_cb, cmd_cb)) < 0)
fprintf(stderr, "stream_server_init, ret %d\n", ret);
if ((ret = stream_server_start()) < 0)
fprintf(stderr, "stream_server_start, ret %d\n", ret);
return ret;
}
static unsigned chipid;
void gm_enc_init(int cap_ch, int cap_path, int rec_track, int enc_type, int mode, int framerate,
int bitrate, int width, int height)
{
gm_enc_t *param;
DECLARE_ATTR(cap_attr, gm_cap_attr_t);
DECLARE_ATTR(h264e_attr, gm_h264e_attr_t);
DECLARE_ATTR(mpeg4e_attr, gm_mpeg4e_attr_t);
DECLARE_ATTR(dnr_attr, gm_3dnr_attr_t);
DECLARE_ATTR(mjpege_attr, gm_mjpege_attr_t);
param = &enc_param[cap_ch][cap_path];
if (param->cap.obj == NULL) {
param->cap.obj = gm_new_obj(GM_CAP_OBJECT); // new capture object
cap_attr.cap_vch = cap_ch;
//GM813x capture path 0(liveview), 1(substream), 2(substream), 3(mainstream)
cap_attr.path = cap_path;
cap_attr.enable_mv_data = 0;
gm_set_attr(param->cap.obj, &cap_attr); // set capture attribute
//enable 3dnr if resolution > capture dim / 2
if ((width >= (gm_system.cap[0].dim.width / 2)) &&
(height >= (gm_system.cap[0].dim.height / 2))) {
dnr_attr.enabled = 1;
gm_set_attr(param->cap.obj, &dnr_attr);
}
memcpy(¶m->cap.cap_attr, &cap_attr, sizeof(gm_cap_attr_t));
memcpy(¶m->cap.dnr_attr, &dnr_attr, sizeof(gm_3dnr_attr_t));
}
param->enc[rec_track].obj = gm_new_obj(GM_ENCODER_OBJECT); // new encoder object
param->enc[rec_track].enc_type = enc_type;
switch (enc_type) {
case ENC_TYPE_H264:
h264e_attr.dim.width = width;
h264e_attr.dim.height = height;
h264e_attr.frame_info.framerate = framerate;
h264e_attr.ratectl.mode = mode;
h264e_attr.ratectl.gop = 60;
h264e_attr.ratectl.bitrate = bitrate;
h264e_attr.ratectl.bitrate_max = bitrate;
h264e_attr.b_frame_num = 0; // B-frames per GOP (H.264 high profile)
h264e_attr.enable_mv_data = 0; // disable H.264 motion data output
h264e_attr.ratectl.init_quant = 25;
h264e_attr.ratectl.min_quant = 20;
h264e_attr.ratectl.max_quant = 51;
gm_set_attr(param->enc[rec_track].obj, &h264e_attr);
memcpy(¶m->enc[rec_track].codec.h264e_attr, &h264e_attr, sizeof(gm_h264e_attr_t));
break;
case ENC_TYPE_MPEG4:
mpeg4e_attr.dim.width = width;
mpeg4e_attr.dim.height = height;
mpeg4e_attr.frame_info.framerate = framerate;
mpeg4e_attr.ratectl.mode = mode;
mpeg4e_attr.ratectl.gop = 60;
mpeg4e_attr.ratectl.bitrate = bitrate;
mpeg4e_attr.ratectl.bitrate_max = bitrate;
gm_set_attr(param->enc[rec_track].obj, &mpeg4e_attr);
memcpy(¶m->enc[rec_track].codec.mpeg4e_attr, &mpeg4e_attr, sizeof(gm_mpeg4e_attr_t));
break;
case ENC_TYPE_MJPEG:
mjpege_attr.dim.width = width;
mjpege_attr.dim.height = height;
mjpege_attr.frame_info.framerate = framerate;
mjpege_attr.quality = 30;
gm_set_attr(param->enc[rec_track].obj, &mjpege_attr);
memcpy(¶m->enc[rec_track].codec.mjpege_attr, &mjpege_attr, sizeof(gm_mjpege_attr_t));
break;
default:
printf("Not support %s\n", rtsp_enc_type_str[enc_type]);
break;
}
// bind channel recording
param->bindfd[rec_track] = gm_bind(enc_groupfd, param->cap.obj, param->enc[rec_track].obj);
rtspd_avail_ch++;
}
int gm_get_chipinfo(void)
{
FILE *fp;
char buffer[256];
int i;
int chipid;
char *match;
fp = fopen("/proc/pmu/chipver","r");
i = fread(buffer,1,sizeof(buffer),fp);
fclose(fp);
if (i == 0)
return 0;
buffer[i] = '\0';
match = strstr(buffer, "81");
if (match == NULL)
return 0;
sscanf(match,"%X",&chipid);
return chipid;
}
static int gm_get_max_bandwidth(char *list)
{
int bandwidth=0;
int i;
int ch;
int tmp;
char token[] = "\n \t";
int sensor_res[] = {800,500,400,300,200,130,100,34,30,7};
char *str;
str = strtok(list,token);
for (i=0;i<5;i++) {
if (str == NULL)
break;
sscanf(str,"%02d",&ch);
str+=3;
sscanf(str,"%03d",&tmp);
str = strtok(NULL,token);
if (ch == 0 || tmp == 0)
continue;
tmp = tmp * sensor_res[i];
tmp = tmp / ch;
if (i <= 3) {
bandwidth = 8*30;
break;
}
if (bandwidth <= (tmp /100))
bandwidth = tmp/100;
}
return bandwidth;
}
int gm_get_bandwidth_info(void) // 0:2m,1:8m
{
FILE *fp;
char buffer[2048];
int i;
char *match;
fp = fopen("/proc/videograph/vpd/spec_info","r");
i = fread(buffer,1,sizeof(buffer),fp);
fclose(fp);
if (i == 0)
buffer[i] = '\0';
match = strstr(buffer,"[ENC CAPTURE]");
if (match == NULL)
return 0;
match = strstr(match,"CH_0");
if (match == NULL)
return 0;
match += 4;
sscanf(match,"%X",&i);
if (i == 0)
return 0;
if (gm_get_max_bandwidth((match+1)) > 62)
return 1;
else
return 0;
}
void gm_graph_init(void)
{
int cap_fps;
int cap_h;
int cap_w;
int cap_resolution;
int cap_bandwidth;
//int chipid;
gm_init(); //gmlib initial
gm_get_sysinfo(&gm_system);
if (gm_system.cap[0].framerate)
poll_wait_time = 1000000 / (gm_system.cap[0].framerate + 2);
else
poll_wait_time = 15000; //microsecond
cap_fps = gm_system.cap[0].framerate;
cap_h = gm_system.cap[0].dim.height;
cap_w = gm_system.cap[0].dim.width;
cap_bandwidth = cap_fps * cap_h * cap_w;
cap_resolution = cap_h * cap_w;
memset(enc_param, 0, sizeof(enc_param));
enc_groupfd = gm_new_groupfd();
chipid = gm_get_chipinfo();
chipid = (chipid >> 16) & 0x0000ffff;
if (gm_get_bandwidth_info() == 1)
rtspd_set_1ch = 1;
rtspd_avail_ch = 0;
if (rtspd_set_1ch == 1 && rtspd_set_enc_type == 0) {//single chaneel ,h264 case
gm_enc_init(0, 3, 0, ENC_TYPE_H264, GM_CBR, cap_fps, 8192,
gm_system.cap[0].dim.width, gm_system.cap[0].dim.height);
} else if (rtspd_set_1ch == 1 && rtspd_set_enc_type == 1) {
//single chaneel ,mpeg4 case,mpeg4 max_fps=10 bs=2048
gm_enc_init(0, 3, 0, ENC_TYPE_MPEG4, GM_CBR, 10, 2048,
gm_system.cap[0].dim.width, gm_system.cap[0].dim.height);
} else if (rtspd_set_1ch == 1 && rtspd_set_enc_type == 2) {//single chaneel ,mjpeg case
gm_enc_init(0, 3, 0, ENC_TYPE_MJPEG, GM_CBR, cap_fps, 8192,