Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
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
55 PRINT_CONFIG_VAR(VIEWVIDEO_DOWNSIZE_FACTOR)
56 
57 // From 0 to 99 (99=high)
58 #ifndef VIEWVIDEO_QUALITY_FACTOR
59 #define VIEWVIDEO_QUALITY_FACTOR 50
60 #endif
61 PRINT_CONFIG_VAR(VIEWVIDEO_QUALITY_FACTOR)
62 
63 // RTP time increment at 90kHz (default: 0 for automatic)
64 #ifndef VIEWVIDEO_RTP_TIME_INC
65 #define VIEWVIDEO_RTP_TIME_INC 0
66 #endif
67 PRINT_CONFIG_VAR(VIEWVIDEO_RTP_TIME_INC)
68 
69 // Define stream framerate
70 #ifndef VIEWVIDEO_FPS
71 #define VIEWVIDEO_FPS 5
72 #endif
73 PRINT_CONFIG_VAR(VIEWVIDEO_FPS)
74 
75 // Define stream priority
76 #ifndef VIEWVIDEO_NICE_LEVEL
77 #define VIEWVIDEO_NICE_LEVEL 5
78 #endif
79 PRINT_CONFIG_VAR(VIEWVIDEO_NICE_LEVEL)
80 
81 // Check if we are using netcat instead of RTP/UDP
82 #ifndef VIEWVIDEO_USE_NETCAT
83 #define VIEWVIDEO_USE_NETCAT FALSE
84 #endif
85 
86 #if !VIEWVIDEO_USE_NETCAT && !(defined VIEWVIDEO_USE_RTP)
87 #define VIEWVIDEO_USE_RTP TRUE
88 #endif
89 
90 #if VIEWVIDEO_USE_NETCAT
91 #include <sys/wait.h>
92 PRINT_CONFIG_MSG("[viewvideo] Using netcat.")
93 #else
96 PRINT_CONFIG_MSG("[viewvideo] Using RTP/UDP stream.")
97 PRINT_CONFIG_VAR(VIEWVIDEO_USE_RTP)
98 #endif
99 
100 /* These are defined with configure */
101 PRINT_CONFIG_VAR(VIEWVIDEO_HOST)
102 PRINT_CONFIG_VAR(VIEWVIDEO_PORT_OUT)
103 PRINT_CONFIG_VAR(VIEWVIDEO_PORT2_OUT)
104 
105 // Initialize the viewvideo structure with the defaults
107  .is_streaming = FALSE,
108  .downsize_factor = VIEWVIDEO_DOWNSIZE_FACTOR,
109  .quality_factor = VIEWVIDEO_QUALITY_FACTOR,
110 #if !VIEWVIDEO_USE_NETCAT
111  .use_rtp = VIEWVIDEO_USE_RTP,
112 #endif
113 };
114 
119 static struct image_t *viewvideo_function(struct UdpSocket *socket, struct image_t *img, uint32_t *rtp_frame_nr)
120 {
121  // Resize image if needed
122  struct image_t img_small;
123  image_create(&img_small,
124  img->w / viewvideo.downsize_factor,
125  img->h / viewvideo.downsize_factor,
126  IMAGE_YUV422);
127 
128  // Create the JPEG encoded image
129  struct image_t img_jpeg;
130  image_create(&img_jpeg, img_small.w, img_small.h, IMAGE_JPEG);
131 
132 #if VIEWVIDEO_USE_NETCAT
133  char nc_cmd[64];
134  sprintf(nc_cmd, "nc %s %d 2>/dev/null", STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT_OUT);
135 #endif
136 
137  if (viewvideo.is_streaming) {
138 
139  // Only resize when needed
140  if (viewvideo.downsize_factor != 1) {
143  } else {
145  }
146 
147 #if VIEWVIDEO_USE_NETCAT
148  // Open process to send using netcat (in a fork because sometimes kills itself???)
149  pid_t pid = fork();
150 
151  if (pid < 0) {
152  printf("[viewvideo] Could not create netcat fork.\n");
153  } else if (pid == 0) {
154  // We are the child and want to send the image
155  FILE *netcat = popen(nc_cmd, "w");
156  if (netcat != NULL) {
157  fwrite(img_jpeg.buf, sizeof(uint8_t), img_jpeg.buf_size, netcat);
158  pclose(netcat); // Ignore output, because it is too much when not connected
159  } else {
160  printf("[viewvideo] Failed to open netcat process.\n");
161  }
162 
163  // Exit the program since we don't want to continue after transmitting
164  exit(0);
165  } else {
166  // We want to wait until the child is finished
167  wait(NULL);
168  }
169 #else
170  if (viewvideo.use_rtp) {
171  // Send image with RTP
173  socket, // UDP socket
174  &img_jpeg,
175  0, // Format 422
176  VIEWVIDEO_QUALITY_FACTOR, // Jpeg-Quality
177  0, // DRI Header
178  (img->ts.tv_sec * 1000000 + img->ts.tv_usec),
179  rtp_frame_nr
180  );
181  }
182 #endif
183  }
184 
185  // Free all buffers
186  image_free(&img_jpeg);
187  image_free(&img_small);
188  return NULL; // No new images were created
189 }
190 
191 #ifdef VIEWVIDEO_CAMERA
192 static struct image_t *viewvideo_function1(struct image_t *img)
193 {
194  static uint32_t rtp_frame_nr = 0;
195  return viewvideo_function(&video_sock1, img, &rtp_frame_nr);
196 }
197 #endif
198 
199 #ifdef VIEWVIDEO_CAMERA2
200 static struct image_t *viewvideo_function2(struct image_t *img)
201 {
202  static uint32_t rtp_frame_nr = 0;
203  return viewvideo_function(&video_sock2, img, &rtp_frame_nr);
204 }
205 #endif
206 
210 void viewvideo_init(void)
211 {
212  viewvideo.is_streaming = true;
213 
214 #if VIEWVIDEO_USE_NETCAT
215  // Create an Netcat receiver file for the streaming
216  sprintf(save_name, "%s/netcat-recv.sh", STRINGIFY(VIEWVIDEO_SHOT_PATH));
217  FILE *fp = fopen(save_name, "w");
218  if (fp != NULL) {
219  fprintf(fp, "i=0\n");
220  fprintf(fp, "while true\n");
221  fprintf(fp, "do\n");
222  fprintf(fp, "\tn=$(printf \"%%04d\" $i)\n");
223  fprintf(fp, "\tnc -l 0.0.0.0 %d > img_${n}.jpg\n", (int)(VIEWVIDEO_PORT_OUT));
224  fprintf(fp, "\ti=$((i+1))\n");
225  fprintf(fp, "done\n");
226  fclose(fp);
227  } else {
228  printf("[viewvideo] Failed to create netcat receiver file.\n");
229  }
230 #else
231  // Open udp socket
232 #ifdef VIEWVIDEO_CAMERA
233  if (udp_socket_create(&video_sock1, STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT_OUT, -1, VIEWVIDEO_BROADCAST)) {
234  printf("[viewvideo]: failed to open view video socket, HOST=%s, port=%d\n", STRINGIFY(VIEWVIDEO_HOST),
235  VIEWVIDEO_PORT_OUT);
236  }
237 #endif
238 
239 #ifdef VIEWVIDEO_CAMERA2
240  if (udp_socket_create(&video_sock2, STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT2_OUT, -1, VIEWVIDEO_BROADCAST)) {
241  printf("[viewvideo]: failed to open view video socket, HOST=%s, port=%d\n", STRINGIFY(VIEWVIDEO_HOST),
242  VIEWVIDEO_PORT2_OUT);
243  }
244 #endif
245 #endif
246 
247 #ifdef VIEWVIDEO_CAMERA
248  struct video_listener *listener1 = cv_add_to_device_async(&VIEWVIDEO_CAMERA, viewvideo_function1,
250  fprintf(stderr, "[viewvideo] Added asynchronous video streamer listener for CAMERA1 at %u FPS \n", VIEWVIDEO_FPS);
251 #endif
252 
253 #ifdef VIEWVIDEO_CAMERA2
254  struct video_listener *listener2 = cv_add_to_device_async(&VIEWVIDEO_CAMERA2, viewvideo_function2,
256  fprintf(stderr, "[viewvideo] Added asynchronous video streamer listener for CAMERA2 at %u FPS \n", VIEWVIDEO_FPS);
257 #endif
258 }
uint32_t buf_size
The buffer size.
Definition: image.h:52
struct viewvideo_t viewvideo
Definition: viewvideo.c:106
bool use_rtp
Stream over RTP.
Definition: viewvideo.h:42
void image_yuv422_downsample(struct image_t *input, struct image_t *output, uint16_t downsample)
Simplified high-speed low CPU downsample function without averaging downsample factor must be 1...
Definition: image.c:213
#define VIEWVIDEO_RTP_TIME_INC
Definition: viewvideo.c:65
#define VIEWVIDEO_QUALITY_FACTOR
Definition: viewvideo.c:59
#define VIEWVIDEO_DOWNSIZE_FACTOR
Definition: viewvideo.c:53
void image_free(struct image_t *img)
Free the image.
Definition: image.c:63
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:38
Easily create and use UDP sockets.
Definition: image.h:43
#define VIEWVIDEO_FPS
Definition: viewvideo.c:71
#define VIEWVIDEO_USE_NETCAT
Definition: viewvideo.c:83
#define FALSE
Definition: std.h:5
struct UdpSocket video_sock1
Definition: viewvideo.c:94
Image helper functions like resizing, color filter, converters...
void rtp_frame_send(struct UdpSocket *udp, struct image_t *img, uint8_t format_code, uint8_t quality_code, uint8_t has_dri_header, uint32_t frame_time, uint32_t *packet_number)
Send an RTP frame.
Definition: rtp.c:98
uint16_t w
Image width.
Definition: image.h:45
unsigned long uint32_t
Definition: types.h:18
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:46
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void * buf
Image buffer (depending on the image_type)
Definition: image.h:53
volatile bool is_streaming
When the device is streaming.
Definition: viewvideo.h:39
struct video_listener * cv_add_to_device_async(struct video_config_t *device, cv_function func, int nice_level, uint16_t fps)
Definition: cv.c:82
#define VIEWVIDEO_USE_RTP
Definition: viewvideo.c:87
#define VIEWVIDEO_NICE_LEVEL
Definition: viewvideo.c:77
Encode images with the use of the JPEG encoding.
void viewvideo_init(void)
Initialize the view video.
Definition: viewvideo.c:210
Get live images from a RTP/UDP stream and save pictures on internal memory.
static struct image_t * viewvideo_function(struct UdpSocket *socket, struct image_t *img, uint32_t *rtp_frame_nr)
Handles all the video streaming and saving of the image shots This is a separate thread, so it needs to be thread safe!
Definition: viewvideo.c:119
unsigned char uint8_t
Definition: types.h:14
struct timeval ts
The timestamp of creation.
Definition: image.h:47
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
UYVY format (uint16 per pixel)
Definition: image.h:36
uint8_t downsize_factor
Downsize factor during the stream.
Definition: viewvideo.h:40
Encodes a vide stream with RTP (JPEG)
struct UdpSocket video_sock2
Definition: viewvideo.c:95
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
An JPEG encoded image (not per pixel encoded)
Definition: image.h:38