Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
viewvideo.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2014 The Paparazzi Community
3 * 2015 Freek van Tienen <freek.v.tienen@gmail.com>
4 *
5 * This file is part of Paparazzi.
6 *
7 * Paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * Paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 */
22
31
32// Own header
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <string.h>
39#include <unistd.h>
40#include <sys/time.h>
41#include <math.h>
42
43// Video
44#include "lib/vision/image.h"
45#include "lib/encoding/jpeg.h"
46#include "lib/encoding/rtp.h"
47#include "udp_socket.h"
48
49#include BOARD_CONFIG
50
51// Downsize factor for video stream
52#ifndef VIEWVIDEO_DOWNSIZE_FACTOR
53#define VIEWVIDEO_DOWNSIZE_FACTOR 4
54#endif
56
57// From 0 to 99 (99=high)
58#ifndef VIEWVIDEO_QUALITY_FACTOR
59#define VIEWVIDEO_QUALITY_FACTOR 50
60#endif
62
63// Define stream framerate
64#ifndef VIEWVIDEO_FPS
65#define VIEWVIDEO_FPS 5
66#endif
68
69// Define stream priority
70#ifndef VIEWVIDEO_NICE_LEVEL
71#define VIEWVIDEO_NICE_LEVEL 5
72#endif
74
75// Check if we are using netcat instead of RTP/UDP
76#ifndef VIEWVIDEO_USE_NETCAT
77#define VIEWVIDEO_USE_NETCAT FALSE
78#endif
79
80#if !VIEWVIDEO_USE_NETCAT && !(defined VIEWVIDEO_USE_RTP)
81#define VIEWVIDEO_USE_RTP TRUE
82#endif
83
84#if VIEWVIDEO_USE_NETCAT
85#include <sys/wait.h>
86PRINT_CONFIG_MSG("[viewvideo] Using netcat.")
87#else
90PRINT_CONFIG_MSG("[viewvideo] Using RTP/UDP stream.")
92#endif
93
94/* These are defined with configure */
95PRINT_CONFIG_VAR(VIEWVIDEO_HOST)
96
97// Host "auto" (set by video_rtp_stream.xml when VIEWVIDEO_HOST is auto, the default when MODEM_HOST is auto):
98// send to the GCS computers found by the telemetry link (see udp_arch.c)
99#ifndef VIEWVIDEO_AUTO_HOST
100#define VIEWVIDEO_AUTO_HOST 0
101#endif
102#if VIEWVIDEO_AUTO_HOST
103#include "mcu_periph/udp_arch.h"
104#ifndef VIEWVIDEO_AUTO_MAX_GCS
105#define VIEWVIDEO_AUTO_MAX_GCS 4
106#endif
107#define VIEWVIDEO_SOCKET_HOST "255.255.255.255"
108#define VIEWVIDEO_SOCKET_BROADCAST true
109#else
110#define VIEWVIDEO_SOCKET_HOST STRINGIFY(VIEWVIDEO_HOST)
111#define VIEWVIDEO_SOCKET_BROADCAST VIEWVIDEO_BROADCAST
112#endif
113PRINT_CONFIG_VAR(VIEWVIDEO_PORT_OUT)
114PRINT_CONFIG_VAR(VIEWVIDEO_PORT2_OUT)
115
116// Initialize the viewvideo structure with the defaults
118 .is_streaming = FALSE,
119 .downsize_factor = VIEWVIDEO_DOWNSIZE_FACTOR,
120 .quality_factor = VIEWVIDEO_QUALITY_FACTOR,
121#if !VIEWVIDEO_USE_NETCAT
122 .use_rtp = VIEWVIDEO_USE_RTP,
123#endif
124};
125
130static struct image_t *viewvideo_function(struct UdpSocket *viewvideo_socket, struct image_t *img, uint16_t *rtp_packet_nr, uint32_t *rtp_frame_time,
131 struct image_t *img_small, struct image_t *img_jpeg)
132{
133 // Resize small image if needed
134 if(img_small->buf_size < img->buf_size/(viewvideo.downsize_factor*viewvideo.downsize_factor)){
135 if(img_small->buf != NULL){
136 image_free(img_small);
137 }
138 image_create(img_small,
139 img->w / viewvideo.downsize_factor,
140 img->h / viewvideo.downsize_factor,
142 }
143
144 // Resize JPEG encoded image if needed
145 if(img_jpeg->w != img_small->w || img_jpeg->h != img_small->h)
146 {
147 if(img_jpeg->buf != NULL){
148 image_free(img_jpeg);
149 }
150 image_create(img_jpeg, img_small->w, img_small->h, IMAGE_JPEG);
151 }
152
153#if VIEWVIDEO_USE_NETCAT
154 char nc_cmd[64];
155 sprintf(nc_cmd, "nc %s %d 2>/dev/null", STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT_OUT);
156#endif
157
158 if (viewvideo.is_streaming) {
159 // Only resize when needed
160 if (viewvideo.downsize_factor > 1) {
161 image_yuv422_downsample(img, img_small, viewvideo.downsize_factor);
163 } else {
165 }
166
167#if VIEWVIDEO_USE_NETCAT
168 // Open process to send using netcat (in a fork because sometimes kills itself???)
169 pid_t pid = fork();
170
171 if (pid < 0) {
172 printf("[viewvideo] Could not create netcat fork.\n");
173 } else if (pid == 0) {
174 // We are the child and want to send the image
175 FILE *netcat = popen(nc_cmd, "w");
176 if (netcat != NULL) {
177 fwrite(img_jpeg->buf, sizeof(uint8_t), img_jpeg->buf_size, netcat);
178 pclose(netcat); // Ignore output, because it is too much when not connected
179 } else {
180 printf("[viewvideo] Failed to open netcat process.\n");
181 }
182
183 // Exit the program since we don't want to continue after transmitting
184 exit(0);
185 } else {
186 // We want to wait until the child is finished
187 wait(NULL);
188 }
189#else
190 if (viewvideo.use_rtp) {
191#if VIEWVIDEO_AUTO_HOST
192 // Host "auto": to every GCS found by the telemetry link (unicast: Wi-Fi retries lost frames),
193 // broadcast when there is none; every GCS gets the same RTP sequence numbers and timestamps
194 struct in_addr gcs[VIEWVIDEO_AUTO_MAX_GCS];
195 int nb_gcs = udp_arch_auto_gcs(gcs, VIEWVIDEO_AUTO_MAX_GCS);
196 uint16_t packet_nr = *rtp_packet_nr;
197 uint32_t frame_time = *rtp_frame_time;
198 for (int i = 0; i < (nb_gcs > 0 ? nb_gcs : 1); i++) {
199 viewvideo_socket->addr_out.sin_addr.s_addr = nb_gcs > 0 ? gcs[i].s_addr : htonl(INADDR_BROADCAST);
200 *rtp_packet_nr = packet_nr;
201 *rtp_frame_time = frame_time;
202 rtp_frame_send(viewvideo_socket, img_jpeg, 0, VIEWVIDEO_QUALITY_FACTOR, 0, VIEWVIDEO_FPS,
203 rtp_packet_nr, rtp_frame_time);
204 }
205#else
206 // Send image with RTP
208 viewvideo_socket, // UDP socket
209 img_jpeg,
210 0, // Format 422
211 VIEWVIDEO_QUALITY_FACTOR, // Jpeg-Quality
212 0, // DRI Header
214 //(img->ts.tv_sec * 1000000 + img->ts.tv_usec),
215 rtp_packet_nr,
216 rtp_frame_time
217 );
218#endif
219 }
220#endif
221 }
222
223 return NULL; // No new images were created
224}
225
226#ifdef VIEWVIDEO_CAMERA
227static struct image_t *viewvideo_function1(struct image_t *img, uint8_t camera_id __attribute__((unused)))
228{
229 static uint16_t rtp_packet_nr = 0;
230 static uint32_t rtp_frame_time = 0;
231 static struct image_t img_small = {.buf=NULL, .buf_size=0};
232 static struct image_t img_jpeg = {.buf=NULL, .buf_size=0};
233 return viewvideo_function(&video_sock1, img, &rtp_packet_nr, &rtp_frame_time, &img_small, &img_jpeg);
234}
235#endif
236
237#ifdef VIEWVIDEO_CAMERA2
238static struct image_t *viewvideo_function2(struct image_t *img, uint8_t camera_id __attribute__((unused)))
239{
240 static uint16_t rtp_packet_nr = 0;
241 static uint32_t rtp_frame_time = 0;
242 static struct image_t img_small = {.buf=NULL, .buf_size=0};
243 static struct image_t img_jpeg = {.buf=NULL, .buf_size=0};
244 return viewvideo_function(&video_sock2, img, &rtp_packet_nr, &rtp_frame_time, &img_small, &img_jpeg);
245}
246#endif
247
252{
253 viewvideo.is_streaming = true;
254
255 // safety check
256 if(viewvideo.downsize_factor < 1){
257 viewvideo.downsize_factor = 1;
258 }
259
260 if(viewvideo.quality_factor > 99){
261 viewvideo.quality_factor = 99;
262 }
263
264#if VIEWVIDEO_USE_NETCAT
265 // Create an Netcat receiver file for the streaming
266 sprintf(save_name, "%s/netcat-recv.sh", STRINGIFY(VIEWVIDEO_SHOT_PATH));
267 FILE *fp = fopen(save_name, "w");
268 if (fp != NULL) {
269 fprintf(fp, "i=0\n");
270 fprintf(fp, "while true\n");
271 fprintf(fp, "do\n");
272 fprintf(fp, "\tn=$(printf \"%%04d\" $i)\n");
273 fprintf(fp, "\tnc -l 0.0.0.0 %d > img_${n}.jpg\n", (int)(VIEWVIDEO_PORT_OUT));
274 fprintf(fp, "\ti=$((i+1))\n");
275 fprintf(fp, "done\n");
276 fclose(fp);
277 } else {
278 printf("[viewvideo] Failed to create netcat receiver file.\n");
279 }
280#else
281 // Open udp socket (host "auto": broadcast socket, the destination is set per frame)
282#ifdef VIEWVIDEO_CAMERA
284 printf("[viewvideo]: failed to open view video socket, HOST=%s, port=%d\n", STRINGIFY(VIEWVIDEO_HOST),
285 VIEWVIDEO_PORT_OUT);
286 }
287#endif
288
289#ifdef VIEWVIDEO_CAMERA2
291 printf("[viewvideo]: failed to open view video socket, HOST=%s, port=%d\n", STRINGIFY(VIEWVIDEO_HOST),
292 VIEWVIDEO_PORT2_OUT);
293 }
294#endif
295#endif
296
297#ifdef VIEWVIDEO_CAMERA
298 cv_add_to_device_async(&VIEWVIDEO_CAMERA, viewvideo_function1,
300 fprintf(stderr, "[viewvideo] Added asynchronous video streamer listener for CAMERA1 at %u FPS \n", VIEWVIDEO_FPS);
301#endif
302
303#ifdef VIEWVIDEO_CAMERA2
304 cv_add_to_device_async(&VIEWVIDEO_CAMERA2, viewvideo_function2,
306 fprintf(stderr, "[viewvideo] Added asynchronous video streamer listener for CAMERA2 at %u FPS \n", VIEWVIDEO_FPS);
307#endif
308}
struct video_listener * cv_add_to_device_async(struct video_config_t *device, cv_function func, int nice_level, uint16_t fps, uint8_t id)
Definition cv.c:83
Computer vision framework for onboard processing.
void image_yuv422_downsample(struct image_t *input, struct image_t *output, uint8_t downsample)
Simplified high-speed low CPU downsample function without averaging downsample factor must be 1,...
Definition image.c:318
void image_free(struct image_t *img)
Free the image.
Definition image.c:75
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition image.c:43
Image helper functions like resizing, color filter, converters...
uint32_t buf_size
The buffer size.
Definition image.h:53
void * buf
Image buffer (depending on the image_type).
Definition image.h:54
uint16_t h
Image height.
Definition image.h:47
uint16_t w
Image width.
Definition image.h:46
@ IMAGE_YUV422
UYVY format (uint16 per pixel).
Definition image.h:36
@ IMAGE_JPEG
An JPEG encoded image (not per pixel encoded).
Definition image.h:38
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool add_dri_header)
Encode an YUV422 image.
Definition jpeg.c:408
Encode images with the use of the JPEG encoding.
int udp_arch_auto_gcs(struct in_addr *addr, int max)
GCS computers found by the links with host "auto", each IP address once.
Definition udp_arch.c:174
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
void rtp_frame_send(struct UdpSocket *udp, struct image_t *img, uint8_t format_code, uint8_t quality_code, uint8_t has_dri_header, float average_frame_rate, uint16_t *packet_number, uint32_t *rtp_time_counter)
Send an RTP frame.
Definition rtp.c:127
Encodes a video stream with RTP Format 26 (Motion JPEG).
#define FALSE
Definition std.h:5
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool broadcast)
Create UDP socket and bind it.
Definition udp_socket.c:49
Easily create and use UDP sockets.
struct sockaddr_in addr_out
Definition udp_socket.h:37
static struct image_t * viewvideo_function(struct UdpSocket *viewvideo_socket, struct image_t *img, uint16_t *rtp_packet_nr, uint32_t *rtp_frame_time, struct image_t *img_small, struct image_t *img_jpeg)
Handles all the video streaming and saving of the image shots This is a separate thread,...
Definition viewvideo.c:130
struct UdpSocket video_sock1
Definition viewvideo.c:88
#define VIEWVIDEO_USE_NETCAT
Definition viewvideo.c:77
#define VIEWVIDEO_SOCKET_BROADCAST
Definition viewvideo.c:111
struct viewvideo_t viewvideo
Definition viewvideo.c:117
#define VIEWVIDEO_NICE_LEVEL
Definition viewvideo.c:71
#define VIEWVIDEO_USE_RTP
Definition viewvideo.c:81
#define VIEWVIDEO_DOWNSIZE_FACTOR
Definition viewvideo.c:53
void viewvideo_init(void)
Initialize the view video.
Definition viewvideo.c:251
#define VIEWVIDEO_QUALITY_FACTOR
Definition viewvideo.c:59
struct UdpSocket video_sock2
Definition viewvideo.c:89
#define VIEWVIDEO_SOCKET_HOST
Definition viewvideo.c:110
#define VIEWVIDEO_FPS
Definition viewvideo.c:65
Get live images from a RTP/UDP stream and save pictures on internal memory.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.