forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netcam_rtsp.c
1581 lines (1315 loc) · 58.7 KB
/
netcam_rtsp.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
/***********************************************************
* In the top section are the functions that are used
* when processing the camera feed. Since these functions
* are internal to the RTSP module, and many require FFmpeg
* structures in their declarations, they are within the
* HAVE_FFMPEG block that eliminates them entirely when
* FFmpeg is not present.
*
* The functions:
* netcam_rtsp_setup
* netcam_rtsp_next
* netcam_rtsp_cleanup
* are called from video_common.c therefore must be defined even
* if FFmpeg is not present. They must also not have FFmpeg
* structures in the declarations. Simple error
* messages are raised if called when no FFmpeg is found.
*
* Additional note: Although this module is called netcam_rtsp,
* it actually handles more camera types than just rtsp.
* Within its current construct, it could be set up to handle
* whatever types of capture devices that ffmpeg can use.
* As of this writing it includes rtsp, http and v4l2.
*
***********************************************************/
#include <stdio.h>
#include "translate.h"
#include "rotate.h" /* already includes motion.h */
#include "netcam_rtsp.h"
#include "video_v4l2.h" /* Needed to validate palette for v4l2 via netcam */
#ifdef HAVE_FFMPEG
#include "ffmpeg.h"
static int netcam_rtsp_check_pixfmt(struct rtsp_context *rtsp_data){
/* Determine if the format is YUV420P */
int retcd;
retcd = -1;
if ((rtsp_data->codec_context->pix_fmt == MY_PIX_FMT_YUV420P) ||
(rtsp_data->codec_context->pix_fmt == MY_PIX_FMT_YUVJ420P)) retcd = 0;
return retcd;
}
static void netcam_rtsp_pktarray_free(struct rtsp_context *rtsp_data){
int indx;
pthread_mutex_lock(&rtsp_data->mutex_pktarray);
if (rtsp_data->pktarray_size > 0){
for(indx = 0; indx < rtsp_data->pktarray_size; indx++) {
if (rtsp_data->pktarray[indx].packet.data != NULL) {
my_packet_unref(rtsp_data->pktarray[indx].packet);
}
}
}
free(rtsp_data->pktarray);
rtsp_data->pktarray = NULL;
rtsp_data->pktarray_size = 0;
rtsp_data->pktarray_index = -1;
pthread_mutex_unlock(&rtsp_data->mutex_pktarray);
}
static void netcam_rtsp_null_context(struct rtsp_context *rtsp_data){
rtsp_data->swsctx = NULL;
rtsp_data->swsframe_in = NULL;
rtsp_data->swsframe_out = NULL;
rtsp_data->frame = NULL;
rtsp_data->codec_context = NULL;
rtsp_data->format_context = NULL;
rtsp_data->pktarray = NULL;
rtsp_data->transfer_format = NULL;
}
static void netcam_rtsp_close_context(struct rtsp_context *rtsp_data){
if (rtsp_data->swsctx != NULL) sws_freeContext(rtsp_data->swsctx);
if (rtsp_data->swsframe_in != NULL) my_frame_free(rtsp_data->swsframe_in);
if (rtsp_data->swsframe_out != NULL) my_frame_free(rtsp_data->swsframe_out);
if (rtsp_data->frame != NULL) my_frame_free(rtsp_data->frame);
if (rtsp_data->codec_context != NULL) my_avcodec_close(rtsp_data->codec_context);
if (rtsp_data->format_context != NULL) avformat_close_input(&rtsp_data->format_context);
if (rtsp_data->pktarray != NULL) netcam_rtsp_pktarray_free(rtsp_data);
if (rtsp_data->transfer_format != NULL) avformat_close_input(&rtsp_data->transfer_format);
netcam_rtsp_null_context(rtsp_data);
}
static void netcam_rtsp_pktarray_resize(struct context *cnt, int is_highres){
/* This is called from netcam_rtsp_next and is on the motion loop thread
* The rtsp_data->mutex is locked around the call to this function.
*/
/* Remember that this is a ring and we have two threads chasing around it
* the ffmpeg is writing out of this ring while we are filling it up. "Bad"
* things will occur if the "add" thread catches up with the "write" thread.
* We need this ring to be big enough so they don't collide.
* The alternative is that we'd need to make a copy of the entire packet
* array in the ffmpeg module and do our writing from that copy. The
* downside is that is a lot to be copying around for each image we want
* to write out. And putting a mutex on the array during adding function would
* slow down the capture thread to the speed of the writing thread. And that
* writing thread operates at the user specified FPS which could be really slow
* ...So....make this array big enough so we never catch our tail. :)
*/
int64_t idnbr_last, idnbr_first;
int indx;
int resize_pktarray;
struct rtsp_context *rtsp_data;
struct packet_item *tmp;
int newsize;
resize_pktarray = TRUE;
if (is_highres){
idnbr_last = cnt->imgs.image_ring[cnt->imgs.image_ring_out].idnbr_high;
idnbr_first = cnt->imgs.image_ring[cnt->imgs.image_ring_in].idnbr_high;
rtsp_data = cnt->rtsp_high;
} else {
idnbr_last = cnt->imgs.image_ring[cnt->imgs.image_ring_out].idnbr_norm;
idnbr_first = cnt->imgs.image_ring[cnt->imgs.image_ring_in].idnbr_norm;
rtsp_data = cnt->rtsp;
}
if (!rtsp_data->passthrough) return;
/* The 30 is arbitrary */
/* Double the size plus double last diff so we don't catch our tail */
newsize =((idnbr_first - idnbr_last) * 2 );
newsize = newsize + ((rtsp_data->idnbr - idnbr_last ) * 2);
if (newsize < 30) newsize = 30;
if (rtsp_data->pktarray_size >= newsize) resize_pktarray = FALSE;
if ((resize_pktarray) || (rtsp_data->pktarray_size < 30)){
tmp = mymalloc(newsize * sizeof(struct packet_item));
if (rtsp_data->pktarray_size > 0 ){
memcpy(tmp, rtsp_data->pktarray, sizeof(struct packet_item) * rtsp_data->pktarray_size);
}
for(indx = rtsp_data->pktarray_size; indx < newsize; indx++) {
av_init_packet(&tmp[indx].packet);
tmp[indx].packet.data=NULL;
tmp[indx].packet.size=0;
tmp[indx].idnbr = 0;
tmp[indx].iskey = FALSE;
tmp[indx].iswritten = FALSE;
}
pthread_mutex_lock(&rtsp_data->mutex_pktarray);
if (rtsp_data->pktarray != NULL) free(rtsp_data->pktarray);
rtsp_data->pktarray = tmp;
rtsp_data->pktarray_size = newsize;
pthread_mutex_unlock(&rtsp_data->mutex_pktarray);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Resized packet array to %d"), rtsp_data->cameratype,newsize);
}
}
static void netcam_rtsp_pktarray_add(struct rtsp_context *rtsp_data){
int indx_next;
int retcd;
char errstr[128];
if (rtsp_data->pktarray_size == 0) return;
/* Recall pktarray_size is one based but pktarray is zero based */
if (rtsp_data->pktarray_index == (rtsp_data->pktarray_size-1) ){
indx_next = 0;
} else {
indx_next = rtsp_data->pktarray_index + 1;
}
rtsp_data->pktarray[indx_next].idnbr = rtsp_data->idnbr;
my_packet_unref(rtsp_data->pktarray[indx_next].packet);
av_init_packet(&rtsp_data->pktarray[indx_next].packet);
rtsp_data->pktarray[indx_next].packet.data = NULL;
rtsp_data->pktarray[indx_next].packet.size = 0;
retcd = my_copy_packet(&rtsp_data->pktarray[indx_next].packet, &rtsp_data->packet_recv);
if ((rtsp_data->interrupted) || (retcd < 0)) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: av_copy_packet: %s ,Interrupt: %s")
,rtsp_data->cameratype
,errstr, rtsp_data->interrupted ? _("True"):_("False"));
my_packet_unref(rtsp_data->pktarray[indx_next].packet);
rtsp_data->pktarray[indx_next].packet.data = NULL;
rtsp_data->pktarray[indx_next].packet.size = 0;
}
if (rtsp_data->pktarray[indx_next].packet.flags & AV_PKT_FLAG_KEY) {
rtsp_data->pktarray[indx_next].iskey = TRUE;
} else {
rtsp_data->pktarray[indx_next].iskey = FALSE;
}
rtsp_data->pktarray[indx_next].iswritten = FALSE;
rtsp_data->pktarray[indx_next].timestamp_tv.tv_sec = rtsp_data->img_recv->image_time.tv_sec;
rtsp_data->pktarray[indx_next].timestamp_tv.tv_usec = rtsp_data->img_recv->image_time.tv_usec;
rtsp_data->pktarray_index = indx_next;
}
/* netcam_rtsp_decode_video
*
* Return values:
* <0 error
* 0 invalid but continue
* 1 valid data
*/
static int netcam_rtsp_decode_video(struct rtsp_context *rtsp_data){
#if (LIBAVFORMAT_VERSION_MAJOR >= 58) || ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR >= 41))
int retcd;
char errstr[128];
if (rtsp_data->finish) return 0; /* This just speeds up the shutdown time */
retcd = avcodec_send_packet(rtsp_data->codec_context, &rtsp_data->packet_recv);
if ((rtsp_data->interrupted) || (rtsp_data->finish)) return -1;
if (retcd < 0 && retcd != AVERROR_EOF){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Error sending packet to codec: %s"), errstr);
return -1;
}
retcd = avcodec_receive_frame(rtsp_data->codec_context, rtsp_data->frame);
if ((rtsp_data->interrupted) || (rtsp_data->finish)) return -1;
if (retcd == AVERROR(EAGAIN)) return 0;
/*
* At least one netcam (Wansview K1) is known to always send a bogus
* packet at the start of the stream. Just grin and bear it...
*/
if (retcd == AVERROR_INVALIDDATA) {
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Ignoring packet with invalid data"));
return 0;
}
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Error receiving frame from codec: %s"), errstr);
return -1;
}
return 1;
#else
int retcd;
int check = 0;
char errstr[128];
if (rtsp_data->finish) return 0; /* This just speeds up the shutdown time */
retcd = avcodec_decode_video2(rtsp_data->codec_context, rtsp_data->frame, &check, &rtsp_data->packet_recv);
if ((rtsp_data->interrupted) || (rtsp_data->finish)) return -1;
if (retcd == AVERROR_INVALIDDATA) {
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO, _("Ignoring packet with invalid data"));
return 0;
}
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO, _("Error decoding packet: %s"),errstr);
return -1;
}
if (check == 0 || retcd == 0) return 0;
return 1;
#endif
}
static int netcam_rtsp_decode_packet(struct rtsp_context *rtsp_data){
int frame_size;
int retcd;
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
retcd = netcam_rtsp_decode_video(rtsp_data);
if (retcd <= 0) return retcd;
frame_size = my_image_get_buffer_size(rtsp_data->codec_context->pix_fmt
,rtsp_data->codec_context->width
,rtsp_data->codec_context->height);
netcam_check_buffsize(rtsp_data->img_recv, frame_size);
netcam_check_buffsize(rtsp_data->img_latest, frame_size);
retcd = my_image_copy_to_buffer(rtsp_data->frame
,(uint8_t *)rtsp_data->img_recv->ptr
,rtsp_data->codec_context->pix_fmt
,rtsp_data->codec_context->width
,rtsp_data->codec_context->height
,frame_size);
if ((retcd < 0) || (rtsp_data->interrupted)) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("Error decoding video packet: Copying to buffer"));
return -1;
}
rtsp_data->img_recv->used = frame_size;
return frame_size;
}
static int netcam_rtsp_open_codec(struct rtsp_context *rtsp_data){
#if (LIBAVFORMAT_VERSION_MAJOR >= 58) || ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR >= 41))
int retcd;
char errstr[128];
AVStream *st;
AVCodec *decoder = NULL;
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
retcd = av_find_best_stream(rtsp_data->format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if ((retcd < 0) || (rtsp_data->interrupted)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: av_find_best_stream: %s,Interrupt %s")
,rtsp_data->cameratype, errstr, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
rtsp_data->video_stream_index = retcd;
st = rtsp_data->format_context->streams[rtsp_data->video_stream_index];
decoder = avcodec_find_decoder(st->codecpar->codec_id);
if ((decoder == NULL) || (rtsp_data->interrupted)){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_find_decoder: Failed,Interrupt %s")
,rtsp_data->cameratype, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
rtsp_data->codec_context = avcodec_alloc_context3(decoder);
if ((rtsp_data->codec_context == NULL) || (rtsp_data->interrupted)){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_alloc_context3: Failed,Interrupt %s")
,rtsp_data->cameratype, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
retcd = avcodec_parameters_to_context(rtsp_data->codec_context, st->codecpar);
if ((retcd < 0) || (rtsp_data->interrupted)) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_parameters_to_context: %s,Interrupt %s")
,rtsp_data->cameratype, errstr, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
retcd = avcodec_open2(rtsp_data->codec_context, decoder, NULL);
if ((retcd < 0) || (rtsp_data->interrupted)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_open2: %s,Interrupt %s")
,rtsp_data->cameratype, errstr, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
return 0;
#else
int retcd;
char errstr[128];
AVStream *st;
AVCodec *decoder = NULL;
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
retcd = av_find_best_stream(rtsp_data->format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if ((retcd < 0) || (rtsp_data->interrupted)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: av_find_best_stream: %s,Interrupt %s")
,rtsp_data->cameratype, errstr, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
rtsp_data->video_stream_index = retcd;
st = rtsp_data->format_context->streams[rtsp_data->video_stream_index];
rtsp_data->codec_context = st->codec;
decoder = avcodec_find_decoder(rtsp_data->codec_context->codec_id);
if ((decoder == NULL) || (rtsp_data->interrupted)) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_find_decoder: Failed,Interrupt %s")
,rtsp_data->cameratype, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
retcd = avcodec_open2(rtsp_data->codec_context, decoder, NULL);
if ((retcd < 0) || (rtsp_data->interrupted)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s: avcodec_open2: %s,Interrupt %s")
,rtsp_data->cameratype, errstr, rtsp_data->interrupted ? _("True"):_("False"));
return -1;
}
return 0;
#endif
}
static struct rtsp_context *rtsp_new_context(void){
struct rtsp_context *ret;
/* Note that mymalloc will exit on any problem. */
ret = mymalloc(sizeof(struct rtsp_context));
memset(ret, 0, sizeof(struct rtsp_context));
return ret;
}
static int netcam_rtsp_interrupt(void *ctx){
struct rtsp_context *rtsp_data = ctx;
if (rtsp_data->finish){
rtsp_data->interrupted = TRUE;
return TRUE;
}
if (rtsp_data->status == RTSP_CONNECTED) {
return FALSE;
} else if (rtsp_data->status == RTSP_READINGIMAGE) {
if (gettimeofday(&rtsp_data->interruptcurrenttime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
if ((rtsp_data->interruptcurrenttime.tv_sec - rtsp_data->interruptstarttime.tv_sec ) > rtsp_data->interruptduration){
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Camera reading (%s) timed out")
, rtsp_data->cameratype, rtsp_data->camera_name);
rtsp_data->interrupted = TRUE;
return TRUE;
} else{
return FALSE;
}
} else {
/* This is for NOTCONNECTED and RECONNECTING status. We give these
* options more time because all the ffmpeg calls that are inside the
* rtsp_connect function will use the same start time. Otherwise we
* would need to reset the time before each call to a ffmpeg function.
*/
if (gettimeofday(&rtsp_data->interruptcurrenttime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
if ((rtsp_data->interruptcurrenttime.tv_sec - rtsp_data->interruptstarttime.tv_sec ) > rtsp_data->interruptduration){
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Camera (%s) timed out")
, rtsp_data->cameratype, rtsp_data->camera_name);
rtsp_data->interrupted = TRUE;
return TRUE;
} else{
return FALSE;
}
}
/* should not be possible to get here */
return FALSE;
}
static int netcam_rtsp_resize(struct rtsp_context *rtsp_data){
int retcd;
char errstr[128];
uint8_t *buffer_out;
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
retcd=my_image_fill_arrays(
rtsp_data->swsframe_in
,(uint8_t*)rtsp_data->img_recv->ptr
,rtsp_data->codec_context->pix_fmt
,rtsp_data->codec_context->width
,rtsp_data->codec_context->height);
if (retcd < 0) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("Error allocating picture in: %s"), errstr);
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
buffer_out=(uint8_t *)av_malloc(rtsp_data->swsframe_size*sizeof(uint8_t));
retcd=my_image_fill_arrays(
rtsp_data->swsframe_out
,buffer_out
,MY_PIX_FMT_YUV420P
,rtsp_data->imgsize.width
,rtsp_data->imgsize.height);
if (retcd < 0) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("Error allocating picture out: %s"), errstr);
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
retcd = sws_scale(
rtsp_data->swsctx
,(const uint8_t* const *)rtsp_data->swsframe_in->data
,rtsp_data->swsframe_in->linesize
,0
,rtsp_data->codec_context->height
,rtsp_data->swsframe_out->data
,rtsp_data->swsframe_out->linesize);
if (retcd < 0) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("Error resizing/reformatting: %s"), errstr);
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
retcd=my_image_copy_to_buffer(
rtsp_data->swsframe_out
,(uint8_t *)rtsp_data->img_recv->ptr
,MY_PIX_FMT_YUV420P
,rtsp_data->imgsize.width
,rtsp_data->imgsize.height
,rtsp_data->swsframe_size);
if (retcd < 0) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("Error putting frame into output buffer: %s"), errstr);
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
rtsp_data->img_recv->used = rtsp_data->swsframe_size;
av_free(buffer_out);
return 0;
}
static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data){
int size_decoded;
int retcd;
int haveimage;
char errstr[128];
netcam_buff *xchg;
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
av_init_packet(&rtsp_data->packet_recv);
rtsp_data->packet_recv.data = NULL;
rtsp_data->packet_recv.size = 0;
rtsp_data->interrupted=FALSE;
if (gettimeofday(&rtsp_data->interruptstarttime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
rtsp_data->interruptduration = 10;
rtsp_data->status = RTSP_READINGIMAGE;
rtsp_data->img_recv->used = 0;
size_decoded = 0;
haveimage = FALSE;
while ((!haveimage) && (!rtsp_data->interrupted)) {
retcd = av_read_frame(rtsp_data->format_context, &rtsp_data->packet_recv);
if ((rtsp_data->interrupted) || (retcd < 0)) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: av_read_frame: %s ,Interrupt: %s")
,rtsp_data->cameratype
,errstr, rtsp_data->interrupted ? _("True"):_("False"));
my_packet_unref(rtsp_data->packet_recv);
netcam_rtsp_close_context(rtsp_data);
return -1;
}
if (rtsp_data->packet_recv.stream_index == rtsp_data->video_stream_index){
/* For a high resolution pass-through we don't decode the image */
if (rtsp_data->high_resolution && rtsp_data->passthrough){
if (rtsp_data->packet_recv.data != NULL) size_decoded = 1;
} else {
size_decoded = netcam_rtsp_decode_packet(rtsp_data);
}
}
if (size_decoded > 0 ){
haveimage = TRUE;
} else if (size_decoded == 0){
/* Did not fail, just didn't get anything. Try again */
my_packet_unref(rtsp_data->packet_recv);
av_init_packet(&rtsp_data->packet_recv);
rtsp_data->packet_recv.data = NULL;
rtsp_data->packet_recv.size = 0;
} else {
my_packet_unref(rtsp_data->packet_recv);
netcam_rtsp_close_context(rtsp_data);
return -1;
}
}
if (gettimeofday(&rtsp_data->img_recv->image_time, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
/* Skip status change on our first image to keep the "next" function waiting
* until the handler thread gets going
*/
if (!rtsp_data->first_image) rtsp_data->status = RTSP_CONNECTED;
/* Skip resize/pix format for high pass-through */
if (!(rtsp_data->high_resolution && rtsp_data->passthrough)){
if ((rtsp_data->imgsize.width != rtsp_data->codec_context->width) ||
(rtsp_data->imgsize.height != rtsp_data->codec_context->height) ||
(netcam_rtsp_check_pixfmt(rtsp_data) != 0) ){
if (netcam_rtsp_resize(rtsp_data) < 0){
my_packet_unref(rtsp_data->packet_recv);
netcam_rtsp_close_context(rtsp_data);
return -1;
}
}
}
pthread_mutex_lock(&rtsp_data->mutex);
rtsp_data->idnbr++;
if (rtsp_data->passthrough) netcam_rtsp_pktarray_add(rtsp_data);
if (!(rtsp_data->high_resolution && rtsp_data->passthrough)) {
xchg = rtsp_data->img_latest;
rtsp_data->img_latest = rtsp_data->img_recv;
rtsp_data->img_recv = xchg;
}
pthread_mutex_unlock(&rtsp_data->mutex);
my_packet_unref(rtsp_data->packet_recv);
return 0;
}
static int netcam_rtsp_resize_ntc(struct rtsp_context *rtsp_data){
if ((rtsp_data->finish) || (!rtsp_data->first_image)) return 0;
if ((rtsp_data->imgsize.width != rtsp_data->codec_context->width) ||
(rtsp_data->imgsize.height != rtsp_data->codec_context->height) ||
(netcam_rtsp_check_pixfmt(rtsp_data) != 0) ){
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "");
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "****************************************************************");
if ((rtsp_data->imgsize.width != rtsp_data->codec_context->width) ||
(rtsp_data->imgsize.height != rtsp_data->codec_context->height)) {
if (netcam_rtsp_check_pixfmt(rtsp_data) != 0) {
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("The network camera is sending pictures in a different"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("size than specified in the config and also a "));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("different picture format. The picture is being"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("transcoded to YUV420P and into the size requested"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("in the config file. If possible change netcam to"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("be in YUV420P format and the size requested in the"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("config to possibly lower CPU usage."));
} else {
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("The network camera is sending pictures in a different"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("size than specified in the configuration file."));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("The picture is being transcoded into the size "));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("requested in the configuration. If possible change"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("netcam or configuration to indicate the same size"));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("to possibly lower CPU usage."));
}
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("Netcam: %d x %d => Config: %d x %d")
,rtsp_data->codec_context->width,rtsp_data->codec_context->height
,rtsp_data->imgsize.width,rtsp_data->imgsize.height);
} else {
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("format than YUV420P. The image sent is being "));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("trancoded to YUV420P. If possible change netcam "));
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, _("picture format to YUV420P to possibly lower CPU usage."));
}
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "****************************************************************");
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "");
}
return 0;
}
static int netcam_rtsp_open_sws(struct rtsp_context *rtsp_data){
if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */
rtsp_data->swsframe_in = my_frame_alloc();
if (rtsp_data->swsframe_in == NULL) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate swsframe_in."));
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
rtsp_data->swsframe_out = my_frame_alloc();
if (rtsp_data->swsframe_out == NULL) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate swsframe_out."));
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
/*
* The scaling context is used to change dimensions to config file and
* also if the format sent by the camera is not YUV420.
*/
rtsp_data->swsctx = sws_getContext(
rtsp_data->codec_context->width
,rtsp_data->codec_context->height
,rtsp_data->codec_context->pix_fmt
,rtsp_data->imgsize.width
,rtsp_data->imgsize.height
,MY_PIX_FMT_YUV420P
,SWS_BICUBIC,NULL,NULL,NULL);
if (rtsp_data->swsctx == NULL) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate scaling context."));
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
rtsp_data->swsframe_size = my_image_get_buffer_size(
MY_PIX_FMT_YUV420P
,rtsp_data->imgsize.width
,rtsp_data->imgsize.height);
if (rtsp_data->swsframe_size <= 0) {
if (rtsp_data->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Error determining size of frame out"));
}
netcam_rtsp_close_context(rtsp_data);
return -1;
}
/* the image buffers must be big enough to hold the final frame after resizing */
netcam_check_buffsize(rtsp_data->img_recv, rtsp_data->swsframe_size);
netcam_check_buffsize(rtsp_data->img_latest, rtsp_data->swsframe_size);
return 0;
}
static void netcam_rtsp_set_http(struct rtsp_context *rtsp_data){
rtsp_data->format_context->iformat = av_find_input_format("mjpeg");
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting http input_format mjpeg"),rtsp_data->cameratype);
}
static void netcam_rtsp_set_rtsp(struct rtsp_context *rtsp_data){
if (rtsp_data->rtsp_uses_tcp) {
av_dict_set(&rtsp_data->opts, "rtsp_transport", "tcp", 0);
av_dict_set(&rtsp_data->opts, "allowed_media_types", "video", 0);
if (rtsp_data->status == RTSP_NOTCONNECTED)
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting rtsp transport to tcp"),rtsp_data->cameratype);
} else {
av_dict_set(&rtsp_data->opts, "rtsp_transport", "udp", 0);
av_dict_set(&rtsp_data->opts, "max_delay", "500000", 0); /* 100000 is the default */
if (rtsp_data->status == RTSP_NOTCONNECTED)
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting rtsp transport to udp"),rtsp_data->cameratype);
}
}
static void netcam_rtsp_set_v4l2(struct rtsp_context *rtsp_data){
char optsize[10], optfmt[10], optfps[10];
char *fourcc;
rtsp_data->format_context->iformat = av_find_input_format("video4linux2");
fourcc=malloc(5*sizeof(char));
v4l2_palette_fourcc(rtsp_data->v4l2_palette, fourcc);
if (strcmp(fourcc,"MJPG") == 0) {
if (v4l2_palette_valid(rtsp_data->path,rtsp_data->v4l2_palette)){
sprintf(optfmt, "%s","mjpeg");
av_dict_set(&rtsp_data->opts, "input_format", optfmt, 0);
} else {
sprintf(optfmt, "%s","default");
}
} else if (strcmp(fourcc,"H264") == 0){
if (v4l2_palette_valid(rtsp_data->path,rtsp_data->v4l2_palette)){
sprintf(optfmt, "%s","h264");
av_dict_set(&rtsp_data->opts, "input_format", optfmt, 0);
} else {
sprintf(optfmt, "%s","default");
}
} else {
sprintf(optfmt, "%s","default");
}
if (strcmp(optfmt,"default") != 0) {
if (v4l2_parms_valid(rtsp_data->path
,rtsp_data->v4l2_palette
,rtsp_data->frame_limit
,rtsp_data->imgsize.width
,rtsp_data->imgsize.height)) {
sprintf(optfps, "%d",rtsp_data->frame_limit);
av_dict_set(&rtsp_data->opts, "framerate", optfps, 0);
sprintf(optsize, "%dx%d",rtsp_data->imgsize.width,rtsp_data->imgsize.height);
av_dict_set(&rtsp_data->opts, "video_size", optsize, 0);
} else {
sprintf(optfps, "%s","default");
sprintf(optsize, "%s","default");
}
}
if (rtsp_data->status == RTSP_NOTCONNECTED){
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Requested v4l2_palette option: %d")
,rtsp_data->cameratype,rtsp_data->v4l2_palette);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Requested FOURCC code: %s"),rtsp_data->cameratype,fourcc);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting v4l2 input_format: %s"),rtsp_data->cameratype,optfmt);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting v4l2 framerate: %s"),rtsp_data->cameratype, optfps);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s: Setting v4l2 video_size: %s"),rtsp_data->cameratype, optsize);
}
free(fourcc);
}
static void netcam_rtsp_set_path (struct context *cnt, struct rtsp_context *rtsp_data ) {
char *userpass = NULL;
struct url_t url;
rtsp_data->path = NULL;
memset(&url, 0, sizeof(url));
if (rtsp_data->high_resolution){
netcam_url_parse(&url, cnt->conf.netcam_highres);
} else {
netcam_url_parse(&url, cnt->conf.netcam_url);
}
if (cnt->conf.netcam_proxy) {
MOTION_LOG(WRN, TYPE_NETCAM, NO_ERRNO
,_("Proxies not supported using for %s"),url.service);
}
if (cnt->conf.netcam_userpass != NULL) {
userpass = mystrdup(cnt->conf.netcam_userpass);
} else if (url.userpass != NULL) {
userpass = mystrdup(url.userpass);
}
if (strcmp(url.service, "v4l2") == 0) {
rtsp_data->path = mymalloc(strlen(url.path));
sprintf(rtsp_data->path, "%s",url.path);
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Setting up v4l2 via ffmpeg netcam"));
} else {
if (!strcmp(url.service, "mjpeg")) {
sprintf(url.service, "%s","http");
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Setting up http via ffmpeg netcam"));
} else {
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Setting up %s via ffmpeg netcam"),url.service);
}
if (userpass != NULL) {
rtsp_data->path = mymalloc(strlen(url.service) + 3 + strlen(userpass)
+ 1 + strlen(url.host) + 6 + strlen(url.path) + 2 );
sprintf((char *)rtsp_data->path, "%s:https://%s@%s:%d%s",
url.service, userpass, url.host, url.port, url.path);
} else {
rtsp_data->path = mymalloc(strlen(url.service) + 3 + strlen(url.host)
+ 6 + strlen(url.path) + 2);
sprintf((char *)rtsp_data->path, "%s:https://%s:%d%s", url.service,
url.host, url.port, url.path);
}
}
sprintf(rtsp_data->service, "%s",url.service);
netcam_url_free(&url);
if (userpass) free (userpass);
}
static void netcam_rtsp_set_parms (struct context *cnt, struct rtsp_context *rtsp_data ) {
/* Set the parameters to be used with our camera */
if (rtsp_data->high_resolution) {
rtsp_data->imgsize.width = 0;
rtsp_data->imgsize.height = 0;
snprintf(rtsp_data->cameratype,29, "%s",_("High resolution"));
} else {
rtsp_data->imgsize.width = cnt->conf.width;
rtsp_data->imgsize.height = cnt->conf.height;
snprintf(rtsp_data->cameratype,29, "%s",_("Normal resolution"));
}
MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("Setting up %s stream."),rtsp_data->cameratype);
util_check_passthrough(cnt); /* In case it was turned on via webcontrol */
rtsp_data->status = RTSP_NOTCONNECTED;
rtsp_data->rtsp_uses_tcp =cnt->conf.rtsp_uses_tcp;
rtsp_data->v4l2_palette = cnt->conf.v4l2_palette;
rtsp_data->frame_limit = cnt->conf.frame_limit;
rtsp_data->camera_name = cnt->conf.camera_name;
rtsp_data->img_recv = mymalloc(sizeof(netcam_buff));
rtsp_data->img_recv->ptr = mymalloc(NETCAM_BUFFSIZE);
rtsp_data->img_latest = mymalloc(sizeof(netcam_buff));
rtsp_data->img_latest->ptr = mymalloc(NETCAM_BUFFSIZE);
rtsp_data->pktarray_size = 0;
rtsp_data->pktarray_index = -1;
rtsp_data->handler_finished = TRUE;
rtsp_data->first_image = TRUE;
snprintf(rtsp_data->threadname, 15, "%s",_("Unknown"));
if (gettimeofday(&rtsp_data->interruptstarttime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
if (gettimeofday(&rtsp_data->interruptcurrenttime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
/* If this is the norm and we have a highres, then disable passthru on the norm */
if ((!rtsp_data->high_resolution) &&
(cnt->conf.netcam_highres)) {
rtsp_data->passthrough = FALSE;
} else {
rtsp_data->passthrough = util_check_passthrough(cnt);
}
rtsp_data->interruptduration = 5;
rtsp_data->interrupted = FALSE;
netcam_rtsp_set_path(cnt, rtsp_data);
}
static int netcam_rtsp_set_dimensions (struct context *cnt) {
cnt->imgs.width = 0;
cnt->imgs.height = 0;
cnt->imgs.size_norm = 0;
cnt->imgs.motionsize = 0;
cnt->imgs.width_high = 0;
cnt->imgs.height_high = 0;
cnt->imgs.size_high = 0;
if (cnt->conf.width % 8) {
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO
,_("Image width (%d) requested is not modulo 8."), cnt->conf.width);
cnt->conf.width = cnt->conf.width - (cnt->conf.width % 8) + 8;
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO
,_("Adjusting width to next higher multiple of 8 (%d)."), cnt->conf.width);
}
if (cnt->conf.height % 8) {
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO
,_("Image height (%d) requested is not modulo 8."), cnt->conf.height);
cnt->conf.height = cnt->conf.height - (cnt->conf.height % 8) + 8;
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO
,_("Adjusting height to next higher multiple of 8 (%d)."), cnt->conf.height);
}
/* Fill in camera details into context structure. */
cnt->imgs.width = cnt->conf.width;
cnt->imgs.height = cnt->conf.height;
cnt->imgs.size_norm = (cnt->conf.width * cnt->conf.height * 3) / 2;
cnt->imgs.motionsize = cnt->conf.width * cnt->conf.height;
return 0;