forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcam_rtsp.c
1053 lines (908 loc) · 33.9 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 RTSP 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_setup_rtsp
* netcam_connect_rtsp
* netcam_shutdown_rtsp
* netcam_next_rtsp
* are called from netcam.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.
*
***********************************************************/
#include <stdio.h>
#include "rotate.h" /* already includes motion.h */
#include "netcam_rtsp.h"
#ifdef HAVE_FFMPEG
#include "ffmpeg.h"
static int netcam_rtsp_resize(netcam_context_ptr netcam);
static int netcam_rtsp_open_sws(netcam_context_ptr netcam);
/**
* netcam_check_pixfmt
*
* Determine whether pix_format is YUV420P
*/
static int netcam_check_pixfmt(netcam_context_ptr netcam){
int retcd;
retcd = -1;
if ((netcam->rtsp->codec_context->pix_fmt == MY_PIX_FMT_YUV420P) ||
(netcam->rtsp->codec_context->pix_fmt == MY_PIX_FMT_YUVJ420P)) retcd = 0;
return retcd;
}
/**
* netcam_rtsp_null_context
*
* Null all the context
*/
static void netcam_rtsp_null_context(netcam_context_ptr netcam){
netcam->rtsp->swsctx = NULL;
netcam->rtsp->swsframe_in = NULL;
netcam->rtsp->swsframe_out = NULL;
netcam->rtsp->frame = NULL;
netcam->rtsp->codec_context = NULL;
netcam->rtsp->format_context = NULL;
netcam->rtsp->active = 0;
}
/**
* netcam_rtsp_close_context
*
* Close all the context that could be open
*/
static void netcam_rtsp_close_context(netcam_context_ptr netcam){
if (netcam->rtsp->swsctx != NULL) sws_freeContext(netcam->rtsp->swsctx);
if (netcam->rtsp->swsframe_in != NULL) my_frame_free(netcam->rtsp->swsframe_in);
if (netcam->rtsp->swsframe_out != NULL) my_frame_free(netcam->rtsp->swsframe_out);
if (netcam->rtsp->frame != NULL) my_frame_free(netcam->rtsp->frame);
if (netcam->rtsp->codec_context != NULL) my_avcodec_close(netcam->rtsp->codec_context);
if (netcam->rtsp->format_context != NULL) avformat_close_input(&netcam->rtsp->format_context);
netcam_rtsp_null_context(netcam);
}
static int rtsp_decode_video(AVPacket *packet, AVFrame *frame, AVCodecContext *ctx_codec){
#if (LIBAVFORMAT_VERSION_MAJOR >= 58) || ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR >= 41))
int retcd;
char errstr[128];
if (packet) {
retcd = avcodec_send_packet(ctx_codec, packet);
if (retcd < 0 && retcd != AVERROR_EOF){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Error sending packet to codec: %s", errstr);
return -1;
}
}
retcd = avcodec_receive_frame(ctx_codec, frame);
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...
*
* TODO: This error-tolerance should be limited/conditionalized, or
* else Motion could end up accepting bogus video data indefinitely
*/
if (retcd == AVERROR_INVALIDDATA) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Ignoring packet with invalid data");
return 0;
}
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, 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 (!packet) return 0;
retcd = avcodec_decode_video2(ctx_codec, frame, &check, packet);
if (retcd == AVERROR_INVALIDDATA) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Ignoring packet with invalid data");
return 0;
}
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Error decoding packet: %s",errstr);
return -1;
}
if (check == 0 || retcd == 0) return 0;
return 1;
#endif
}
/**
* rtsp_decode_packet
*
* This routine takes in the packet from the read and decodes it into
* the frame. It then takes the frame and copies it into the netcam
* buffer
*
* Parameters:
* packet The packet that was read from av_read, or NULL
* buffer The buffer that is the final destination
* frame The frame into which we decode the packet
*
*
* Returns:
* Error Negative value
* No result 0(zero)
* Success The size of the frame decoded
*/
static int rtsp_decode_packet(AVPacket *packet, netcam_buff_ptr buffer, AVFrame *frame, AVCodecContext *ctx_codec){
int frame_size;
int retcd;
retcd = rtsp_decode_video(packet, frame, ctx_codec);
if (retcd <= 0) return retcd;
frame_size = my_image_get_buffer_size(ctx_codec->pix_fmt, ctx_codec->width, ctx_codec->height);
netcam_check_buffsize(buffer, frame_size);
retcd = my_image_copy_to_buffer(frame, (uint8_t *)buffer->ptr,ctx_codec->pix_fmt,ctx_codec->width,ctx_codec->height, frame_size);
if (retcd < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Error decoding video packet: Copying to buffer");
return -1;
}
buffer->used = frame_size;
return frame_size;
}
/**
* netcam_open_codec
*
* This routine opens the codec context for the indicated stream
*
* Parameters:
* stream_idx The index of the stream that was found as "best"
* fmt_ctx The format context that was created upon opening the stream
* type The type of media type (This is a constant)
*
*
* Returns:
* Failure Error code from FFmpeg (Negative number)
* Success 0(Zero)
*/
static int netcam_open_codec(netcam_context_ptr netcam){
int retcd;
char errstr[128];
AVStream *st;
AVCodec *decoder = NULL;
retcd = av_find_best_stream(netcam->rtsp->format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Could not find stream in input!: %s",errstr);
return (retcd < 0) ? retcd : -1;
}
netcam->rtsp->video_stream_index = retcd;
st = netcam->rtsp->format_context->streams[netcam->rtsp->video_stream_index];
#if (LIBAVFORMAT_VERSION_MAJOR >= 58) || ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR >= 41))
decoder = avcodec_find_decoder(st->codecpar->codec_id);
if (decoder == NULL) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to find codec!");
return -1;
}
netcam->rtsp->codec_context = avcodec_alloc_context3(decoder);
if (netcam->rtsp->codec_context == NULL) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to allocate decoder!");
return -1;
}
if ((retcd = avcodec_parameters_to_context(netcam->rtsp->codec_context, st->codecpar)) < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to copy decoder parameters!: %s", errstr);
return -1;
}
#else
netcam->rtsp->codec_context = st->codec;
decoder = avcodec_find_decoder(netcam->rtsp->codec_context->codec_id);
if (decoder == NULL) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to find codec!");
return -1;
}
#endif
retcd = avcodec_open2(netcam->rtsp->codec_context, decoder, NULL);
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to open codec!: %s", errstr);
return (retcd < 0) ? retcd : -1;
}
return 0;
}
/**
* rtsp_new_context
*
* Create a new RTSP context structure.
*
* Parameters
*
* None
*
* Returns: Pointer to the newly-created structure, NULL if error.
*
*/
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;
}
/**
* netcam_interrupt_rtsp
*
* This function is called during the FFmpeg blocking functions.
* These include the opening of the format context as well as the
* reading of the packets from the stream. Since this is called
* during all blocking functions, the process uses the readingframe
* flag to determine whether to timeout the process.
*
* Parameters
*
* ctx We pass in the netcam context to use it to look for the
* readingframe flag as well as the time that we started
* the read attempt.
*
* Returns:
* Failure -1(which triggers an interrupt)
* Success 0(zero which indicates to let process continue)
*
*/
static int netcam_interrupt_rtsp(void *ctx){
netcam_context_ptr netcam = (netcam_context_ptr)ctx;
struct rtsp_context *rtsp = netcam->rtsp;
if (netcam->finish) {
/* netcam_cleanup() wants us to stop */
rtsp->interrupted = 1;
return 1;
}
if (rtsp->status == RTSP_CONNECTED) {
return 0;
} else if (rtsp->status == RTSP_READINGIMAGE) {
struct timeval interrupttime;
if (gettimeofday(&interrupttime, NULL) < 0) {
MOTION_LOG(WRN, TYPE_NETCAM, SHOW_ERRNO, "get interrupt time failed");
}
if ((interrupttime.tv_sec - rtsp->startreadtime.tv_sec ) > 10){
MOTION_LOG(WRN, TYPE_NETCAM, NO_ERRNO, "Camera timed out for %s", rtsp->netcam_url);
rtsp->interrupted = 1;
return 1;
} else{
return 0;
}
} 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.
*/
struct timeval interrupttime;
if (gettimeofday(&interrupttime, NULL) < 0) {
MOTION_LOG(WRN, TYPE_NETCAM, SHOW_ERRNO, "get interrupt time failed");
}
if ((interrupttime.tv_sec - rtsp->startreadtime.tv_sec ) > 30){
MOTION_LOG(WRN, TYPE_NETCAM, NO_ERRNO, "Camera timed out for %s", rtsp->netcam_url);
rtsp->interrupted = 1;
return 1;
} else{
return 0;
}
}
//should not be possible to get here
return 0;
}
/**
* netcam_read_rtsp_image
*
* This function reads the packet from the camera.
* It is called extensively so only absolutely essential
* functions and allocations are performed.
*
* Parameters
*
* netcam The netcam context to read from
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
int netcam_read_rtsp_image(netcam_context_ptr netcam){
struct timeval curtime;
netcam_buff_ptr buffer;
AVPacket packet;
int size_decoded;
/* Point to our working buffer. */
buffer = netcam->receiving;
buffer->used = 0;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
if (gettimeofday(&curtime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
netcam->rtsp->startreadtime = curtime;
netcam->rtsp->interrupted = 0;
netcam->rtsp->status = RTSP_READINGIMAGE;
/* Per ffmpeg documentation, only send NULL when it is desired to drain the codec.
* Since this function is our normal capture frame function, set the size decoded to
* zero and get another frame to send to codec.
*/
size_decoded = 0;
while (size_decoded == 0 && av_read_frame(netcam->rtsp->format_context, &packet) >= 0) {
if (packet.stream_index == netcam->rtsp->video_stream_index)
size_decoded = rtsp_decode_packet(&packet, buffer, netcam->rtsp->frame, netcam->rtsp->codec_context);
my_packet_unref(packet);
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
}
netcam->rtsp->status = RTSP_CONNECTED;
if ((size_decoded <= 0) || (netcam->rtsp->interrupted == 1)) {
// something went wrong, end of stream? interrupted?
netcam_rtsp_close_context(netcam);
return -1;
}
if ((netcam->width != (unsigned)netcam->rtsp->codec_context->width) ||
(netcam->height != (unsigned)netcam->rtsp->codec_context->height) ||
(netcam_check_pixfmt(netcam) != 0) ){
if (netcam_rtsp_resize(netcam) < 0)
return -1;
}
netcam_image_read_complete(netcam);
return 0;
}
/**
* netcam_rtsp_resize_ntc
*
* This function notifies the user of the need to transcode
* the netcam image which uses a lot of CPU resources
*
* Parameters
*
* netcam The netcam context to read from
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
static int netcam_rtsp_resize_ntc(netcam_context_ptr netcam){
if ((netcam->width != (unsigned)netcam->rtsp->codec_context->width) ||
(netcam->height != (unsigned)netcam->rtsp->codec_context->height) ||
(netcam_check_pixfmt(netcam) != 0) ){
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "");
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "****************************************************************");
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "The network camera is sending pictures in a different");
if ((netcam->width != (unsigned)netcam->rtsp->codec_context->width) ||
(netcam->height != (unsigned)netcam->rtsp->codec_context->height)) {
if (netcam_check_pixfmt(netcam) != 0) {
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, "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"
,netcam->rtsp->codec_context->width,netcam->rtsp->codec_context->height
,netcam->width,netcam->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;
}
/**
* netcam_rtsp_open_context
*
* This function opens the format context for the camera.
*
* Parameters
*
* netcam The netcam context to read from
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
static int netcam_rtsp_open_context(netcam_context_ptr netcam){
int retcd;
char errstr[128];
char optsize[10], optfmt[8], optfps[5];
if (netcam->rtsp->path == NULL) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Null path passed to connect");
}
return -1;
}
// open the network connection
AVDictionary *opts = 0;
netcam->rtsp->format_context = avformat_alloc_context();
netcam->rtsp->format_context->interrupt_callback.callback = netcam_interrupt_rtsp;
netcam->rtsp->format_context->interrupt_callback.opaque = netcam;
netcam->rtsp->interrupted = 0;
if (gettimeofday(&netcam->rtsp->startreadtime, NULL) < 0) {
MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday");
}
if (strncmp(netcam->rtsp->path, "http", 4) == 0 ){
netcam->rtsp->format_context->iformat = av_find_input_format("mjpeg");
} else if (strncmp(netcam->rtsp->path, "rtsp", 4) == 0 ){
if (netcam->cnt->conf.rtsp_uses_tcp) {
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
if (netcam->rtsp->status == RTSP_NOTCONNECTED)
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "Using tcp transport");
} else {
av_dict_set(&opts, "rtsp_transport", "udp", 0);
av_dict_set(&opts, "max_delay", "500000", 0); //100000 is the default
if (netcam->rtsp->status == RTSP_NOTCONNECTED)
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "Using udp transport");
}
} else {
netcam->rtsp->format_context->iformat = av_find_input_format("video4linux2");
if (netcam->cnt->conf.v4l2_palette == 8) {
sprintf(optfmt, "%s","mjpeg");
av_dict_set(&opts, "input_format", optfmt, 0);
} else if (netcam->cnt->conf.v4l2_palette == 21){
sprintf(optfmt, "%s","h264");
av_dict_set(&opts, "input_format", optfmt, 0);
} else{
sprintf(optfmt, "%s","default");
}
sprintf(optfps, "%d",netcam->cnt->conf.frame_limit);
av_dict_set(&opts, "framerate", optfps, 0);
sprintf(optsize, "%dx%d",netcam->cnt->conf.width,netcam->cnt->conf.height);
av_dict_set(&opts, "video_size", optsize, 0);
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "v4l2 input_format %s",optfmt);
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "v4l2 framerate %s", optfps);
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "v4l2 video_size %s", optsize);
}
}
retcd = avformat_open_input(&netcam->rtsp->format_context, netcam->rtsp->path, NULL, &opts);
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to open input(%s): %s", netcam->rtsp->netcam_url, errstr);
}
av_dict_free(&opts);
//The format context gets freed upon any error from open_input.
return (retcd < 0) ? retcd : -1;
}
av_dict_free(&opts);
// fill out stream information
retcd = avformat_find_stream_info(netcam->rtsp->format_context, NULL);
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to find stream info: %s", errstr);
}
netcam_rtsp_close_context(netcam);
return -1;
}
/* there is no way to set the avcodec thread names, but they inherit
* our thread name - so temporarily change our thread name to the
* desired name */
{
char newtname[16];
char curtname[16] = "unknown";
#if ((!defined(BSD) && HAVE_PTHREAD_SETNAME_NP) || defined(__APPLE__))
pthread_getname_np(pthread_self(), curtname, sizeof(curtname));
#endif
snprintf(newtname, sizeof(newtname), "av%d%s%s",
netcam->cnt->threadnr,
netcam->cnt->conf.camera_name ? ":" : "",
netcam->cnt->conf.camera_name ? netcam->cnt->conf.camera_name : "");
MOTION_PTHREAD_SETNAME(newtname);
retcd = netcam_open_codec(netcam);
MOTION_PTHREAD_SETNAME(curtname);
}
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
av_strerror(retcd, errstr, sizeof(errstr));
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to open codec context: %s", errstr);
}
netcam_rtsp_close_context(netcam);
return -1;
}
if (netcam->rtsp->codec_context->width <= 0 ||
netcam->rtsp->codec_context->height <= 0)
{
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Camera image size is invalid");
netcam_rtsp_close_context(netcam);
return -1;
}
netcam->rtsp->frame = my_frame_alloc();
if (netcam->rtsp->frame == NULL) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to allocate frame. Fatal error. Check FFmpeg/Libav configuration");
}
netcam_rtsp_close_context(netcam);
return -1;
}
if (netcam_rtsp_open_sws(netcam) < 0) return -1;
/*
* Validate that the previous steps opened the camera
*/
retcd = netcam_read_rtsp_image(netcam);
if ((retcd < 0) || (netcam->rtsp->interrupted == 1)){
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Failed to read first image");
}
netcam_rtsp_close_context(netcam);
return -1;
}
netcam->rtsp->active = 1;
return 0;
}
/**
* netcam_rtsp_open_sws
*
* This function opens the rescaling context components.
*
* Parameters
*
* netcam The netcam context to read from
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
static int netcam_rtsp_open_sws(netcam_context_ptr netcam){
netcam->width = ((netcam->cnt->conf.width / 8) * 8);
netcam->height = ((netcam->cnt->conf.height / 8) * 8);
netcam->rtsp->swsframe_in = my_frame_alloc();
if (netcam->rtsp->swsframe_in == NULL) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to allocate frame. Fatal error. Check FFmpeg/Libav configuration");
}
netcam_rtsp_close_context(netcam);
return -1;
}
netcam->rtsp->swsframe_out = my_frame_alloc();
if (netcam->rtsp->swsframe_out == NULL) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to allocate frame. Fatal error. Check FFmpeg/Libav configuration");
}
netcam_rtsp_close_context(netcam);
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.
*/
netcam->rtsp->swsctx = sws_getContext(
netcam->rtsp->codec_context->width
,netcam->rtsp->codec_context->height
,netcam->rtsp->codec_context->pix_fmt
,netcam->width
,netcam->height
,MY_PIX_FMT_YUV420P
,SWS_BICUBIC,NULL,NULL,NULL);
if (netcam->rtsp->swsctx == NULL) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to allocate scaling context. Fatal error. Check FFmpeg/Libav configuration");
}
netcam_rtsp_close_context(netcam);
return -1;
}
netcam->rtsp->swsframe_size = my_image_get_buffer_size(
MY_PIX_FMT_YUV420P
,netcam->width
,netcam->height);
if (netcam->rtsp->swsframe_size <= 0) {
if (netcam->rtsp->status == RTSP_NOTCONNECTED){
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "Error determining size of frame out");
}
netcam_rtsp_close_context(netcam);
return -1;
}
/* the image buffers must be big enough to hold the final frame after resizing */
netcam_check_buffsize(netcam->receiving, netcam->rtsp->swsframe_size);
netcam_check_buffsize(netcam->latest, netcam->rtsp->swsframe_size);
return 0;
}
/**
* netcam_rtsp_resize
*
* This function reencodes the image to yuv420p with the desired size
*
* Parameters
*
* netcam The netcam context to read from
* image The destination image.
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
static int netcam_rtsp_resize(netcam_context_ptr netcam){
int retcd;
char errstr[128];
uint8_t *buffer_out;
retcd=my_image_fill_arrays(
netcam->rtsp->swsframe_in
,(uint8_t*)netcam->receiving->ptr
,netcam->rtsp->codec_context->pix_fmt
,netcam->rtsp->codec_context->width
,netcam->rtsp->codec_context->height);
if (retcd < 0) {
if (netcam->rtsp->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(netcam);
return -1;
}
buffer_out=(uint8_t *)av_malloc(netcam->rtsp->swsframe_size*sizeof(uint8_t));
retcd=my_image_fill_arrays(
netcam->rtsp->swsframe_out
,buffer_out
,MY_PIX_FMT_YUV420P
,netcam->width
,netcam->height);
if (retcd < 0) {
if (netcam->rtsp->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(netcam);
return -1;
}
retcd = sws_scale(
netcam->rtsp->swsctx
,(const uint8_t* const *)netcam->rtsp->swsframe_in->data
,netcam->rtsp->swsframe_in->linesize
,0
,netcam->rtsp->codec_context->height
,netcam->rtsp->swsframe_out->data
,netcam->rtsp->swsframe_out->linesize);
if (retcd < 0) {
if (netcam->rtsp->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(netcam);
return -1;
}
retcd=my_image_copy_to_buffer(
netcam->rtsp->swsframe_out
,(uint8_t *)netcam->receiving->ptr
,MY_PIX_FMT_YUV420P
,netcam->width
,netcam->height
,netcam->rtsp->swsframe_size);
if (retcd < 0) {
if (netcam->rtsp->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(netcam);
return -1;
}
netcam->receiving->used = netcam->rtsp->swsframe_size;
av_free(buffer_out);
return 0;
}
/*********************************************************
* This ends the section of functions that rely upon FFmpeg
***********************************************************/
#endif /* End HAVE_FFMPEG */
/**
* netcam_connect_rtsp
*
* This function initiates the connection to the rtsp camera.
*
* Parameters
*
* netcam The netcam context to open.
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
int netcam_connect_rtsp(netcam_context_ptr netcam){
#ifdef HAVE_FFMPEG
if (netcam_rtsp_open_context(netcam) < 0) return -1;
if (netcam_rtsp_resize_ntc(netcam) < 0 ) return -1;
if (netcam_read_rtsp_image(netcam) < 0) return -1;
netcam->rtsp->status = RTSP_CONNECTED;
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "Camera connected");
return 0;
#else /* No FFmpeg/Libav */
if (netcam)
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "FFmpeg/Libav not found on computer. No RTSP support");
return -1;
#endif /* End #ifdef HAVE_FFMPEG */
}
/**
* netcam_shutdown_rtsp
*
* This function closes and frees all the items for rtsp
*
* Parameters
*
* netcam The netcam context to free.
*
* Returns:
* Failure nothing
* Success nothing
*
*/
void netcam_shutdown_rtsp(netcam_context_ptr netcam){
#ifdef HAVE_FFMPEG
if (netcam->rtsp->status == RTSP_CONNECTED ||
netcam->rtsp->status == RTSP_READINGIMAGE) {
netcam_rtsp_close_context(netcam);
MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "netcam shut down");
}
free(netcam->rtsp->path);
free(netcam->rtsp->user);
free(netcam->rtsp->pass);
free(netcam->rtsp);
netcam->rtsp = NULL;
#else /* No FFmpeg/Libav */
/* Stop compiler warnings */
if (netcam)
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "FFmpeg/Libav not found on computer. No RTSP support");
#endif /* End #ifdef HAVE_FFMPEG */
}
/**
* netcam_setup_rtsp
*
* This function sets up all the necessary items for the
* rtsp camera.
*
* Parameters
*
* netcam The netcam context to free.
* url The URL of the camera
*
* Returns:
* Failure -1
* Success 0(zero)
*
*/
int netcam_setup_rtsp(netcam_context_ptr netcam, struct url_t *url){
#ifdef HAVE_FFMPEG
struct context *cnt = netcam->cnt;
const char *ptr;
int ret = -1;
netcam->caps.streaming = NCS_RTSP;
netcam->rtsp = rtsp_new_context();
netcam_rtsp_null_context(netcam);
if (netcam->rtsp == NULL) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "unable to create rtsp context");
netcam_shutdown_rtsp(netcam);
return -1;
}
/*
* Allocate space for a working string to contain the path.
* The extra 5 is for "://", ":" and string terminator.
*/
// force port to a sane value
if (netcam->connect_port > 65536) {
netcam->connect_port = 65536;
} else if (netcam->connect_port < 0) {
netcam->connect_port = 0;
}
if (cnt->conf.netcam_userpass != NULL) {
ptr = cnt->conf.netcam_userpass;
} else {
ptr = url->userpass; /* Don't set this one NULL, gets freed. */
}
if (ptr != NULL) {
char *cptr;
if ((cptr = strchr(ptr, ':')) == NULL) {
netcam->rtsp->user = mystrdup(ptr);
} else {
netcam->rtsp->user = mymalloc((cptr - ptr)+2); //+2 for string terminator
memcpy(netcam->rtsp->user, ptr,(cptr - ptr));
netcam->rtsp->pass = mystrdup(cptr + 1);
}
}
/*
* Need a method to query the path and
* determine the authentication type
*/
if (strcmp(url->service, "v4l2") == 0) {
ptr = mymalloc(strlen(url->path));
sprintf((char *)ptr, "%s",url->path);
} else if ((netcam->rtsp->user != NULL) && (netcam->rtsp->pass != NULL)) {
ptr = mymalloc(strlen(url->service) + strlen(netcam->connect_host)
+ 5 + strlen(url->path) + 5
+ strlen(netcam->rtsp->user) + strlen(netcam->rtsp->pass) + 4 );
sprintf((char *)ptr, "%s://%s:%s@%s:%d%s",
url->service,netcam->rtsp->user,netcam->rtsp->pass,
netcam->connect_host, netcam->connect_port, url->path);
} else {
ptr = mymalloc(strlen(url->service) + strlen(netcam->connect_host)
+ 5 + strlen(url->path) + 5);
sprintf((char *)ptr, "%s://%s:%d%s", url->service,
netcam->connect_host, netcam->connect_port, url->path);
}
netcam->rtsp->path = (char *)ptr;
netcam_url_free(url);
/*
* Keep a pointer to the original URL for logging purposes
* (we don't want to put passwords into the log)
*/
netcam->rtsp->netcam_url = cnt->conf.netcam_url;
/*
* Now we need to set some flags
*/
netcam->rtsp->status = RTSP_NOTCONNECTED;
/*
* Warn and fix dimensions as needed.
*/
if (netcam->cnt->conf.width % 8) {
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO, "Image width (%d) requested is not modulo 8.", netcam->cnt->conf.width);
netcam->cnt->conf.width = netcam->cnt->conf.width - (netcam->cnt->conf.width % 8) + 8;
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO, "Adjusting width to next higher multiple of 8 (%d).", netcam->cnt->conf.width);
}
if (netcam->cnt->conf.height % 8) {
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO, "Image height (%d) requested is not modulo 8.", netcam->cnt->conf.height);
netcam->cnt->conf.height = netcam->cnt->conf.height - (netcam->cnt->conf.height % 8) + 8;
MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO, "Adjusting height to next higher multiple of 8 (%d).", netcam->cnt->conf.height);
}
/*
* The RTSP context should be all ready to attempt a connection with
* the server, so we try ....