Skip to content

Commit

Permalink
[video] use video_config in video_thread
Browse files Browse the repository at this point in the history
Each "board" (e.g. ARDrone2 or Bebop) defines a video_config_t for each camera,
then you only need to specify 'front_camera' or 'bottom_camera' instead of setting all parameters
  • Loading branch information
flixr committed Sep 14, 2015
1 parent f94ccb8 commit 6dd0b56
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 75 deletions.
5 changes: 1 addition & 4 deletions conf/airframes/BR/bebop_default.xml
Expand Up @@ -33,11 +33,8 @@
<define name="FILE_LOGGER_PATH" value="/data/ftp/internal_000"/>
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="video_rtp_stream.xml">
Expand Down
5 changes: 1 addition & 4 deletions conf/airframes/BR/bebop_indi.xml
Expand Up @@ -39,10 +39,7 @@
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="cv_blob_locator.xml"/>
Expand Down
5 changes: 1 addition & 4 deletions conf/airframes/BR/bebop_indi_frog.xml
Expand Up @@ -38,11 +38,8 @@
<define name="FILE_LOGGER_PATH" value="/data/ftp/internal_000"/>
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="video_rtp_stream.xml">
Expand Down
5 changes: 1 addition & 4 deletions conf/airframes/BR/bebop_indi_frog_flip.xml
Expand Up @@ -38,11 +38,8 @@
<define name="FILE_LOGGER_PATH" value="/data/ftp/internal_000"/>
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="video_rtp_stream.xml">
Expand Down
5 changes: 1 addition & 4 deletions conf/airframes/CDW/bebop.xml
Expand Up @@ -39,10 +39,7 @@
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="cv_blob_locator.xml"/>
Expand Down
4 changes: 3 additions & 1 deletion conf/airframes/ENAC/quadrotor/ard2_base_digit.xml
Expand Up @@ -36,7 +36,9 @@
<load name="agl_dist.xml">
<define name="USE_SONAR"/>
</load>
<load name="video_thread.xml"/>
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="front_camera"/>
</load>
<load name="video_rtp_stream.xml">
<configure name="VIEWVIDEO_USE_NC" value="TRUE"/>
</load>
Expand Down
4 changes: 3 additions & 1 deletion conf/airframes/ENAC/quadrotor/ard2_base_vision.xml
Expand Up @@ -36,7 +36,9 @@
<load name="agl_dist.xml">
<define name="USE_SONAR"/>
</load>
<load name="video_thread.xml"/>
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="front_camera"/>
</load>
<load name="video_rtp_stream.xml">
<define name="VIDEO_SOCK_OUT_OFFSET" value="$(AC_ID)"/>
<define name="VIDEO_DOWNSIZE_FACTOR" value="2"/>
Expand Down
6 changes: 5 additions & 1 deletion conf/airframes/examples/ardrone2.xml
Expand Up @@ -32,7 +32,11 @@
<load name="air_data.xml"/>
<load name="geo_mag.xml"/>
<!--load name="logger_file.xml"/-->
<!--load name="video_thread.xml"/-->
<!--
<load name="video_thread.xml">
<define name="VIDEO_THREAD_CAMERA" value="front_camera"/>
</load>
-->
<!--load name="video_rtp_stream.xml"/-->
</modules>

Expand Down
5 changes: 1 addition & 4 deletions conf/airframes/examples/bebop.xml
Expand Up @@ -33,10 +33,7 @@
</load-->
<load name="video_thread.xml">
<define name="VIDEO_THREAD_FPS" value="4"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video0"/>
<define name="VIDEO_THREAD_SUBDEV" value="/dev/v4l-subdev0"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="640,480"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="60"/>
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/ftp/internal_000/images"/>
</load>
<load name="video_rtp_stream.xml">
Expand Down
14 changes: 9 additions & 5 deletions conf/modules/video_thread.xml
Expand Up @@ -3,16 +3,20 @@
<module name="video_thread" dir="computer_vision">
<doc>
<description>
Video streaming for Linux devices
Read video in a thread.
Only for Linux devices.
To be used in other modules for further processing (e.g. opticflow, QR code, streaming).

- Sends a RTP/UDP stream of the camera
- Possibility to save an image(shot) on the internal memory (JPEG, full size, best quality)
</description>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video1" description="The video device to capture from"/>
<define name="VIDEO_THREAD_DEVICE_SIZE" value="1280,720" description="Video capture size (width, height)"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="10" description="Amount of V4L2 image buffers"/>
<define name="VIDEO_THREAD_CAMERA" value="bottom_camera|front_camera" description="which camera video config to use"/>
<define name="VIDEO_THREAD_FPS" value="4" description="Video stream frame rate"/>
<define name="VIDEO_THREAD_SHOT_PATH" value="/data/video/images" description="Path where the images should be saved"/>
<define name="VIDEO_THREAD_DEVICE" value="/dev/video1" description="The video device to capture from (only if VIDEO_THREAD_CAMERA is not defined)"/>
<define name="VIDEO_THREAD_DEVICE_VIDEO_WIDTH" value="1280" description="Video capture width (only if VIDEO_THREAD_CAMERA is not defined)"/>
<define name="VIDEO_THREAD_DEVICE_VIDEO_HEIGHT" value="720" description="Video capture height (only if VIDEO_THREAD_CAMERA is not defined)"/>
<define name="VIDEO_THREAD_DEVICE_BUFFERS" value="10" description="Amount of V4L2 image buffers (only if VIDEO_THREAD_CAMERA is not defined)"/>

</doc>
<settings>
<dl_settings>
Expand Down
6 changes: 4 additions & 2 deletions sw/airborne/boards/ardrone/board.c
Expand Up @@ -36,21 +36,23 @@

#include "peripherals/video_device.h"

struct video_device_t front_camera = {
struct video_config_t front_camera = {
.w = 1280,
.h = 720,
.dev_name = "/dev/video1",
.subdev_name = NULL,
.format = V4L2_PIX_FMT_UYVY,
.buf_cnt = 10,
.filters = NULL
};

struct video_device_t bottom_camera = {
struct video_config_t bottom_camera = {
.w = 320,
.h = 240,
.dev_name = "/dev/video2",
.subdev_name = NULL,
.format = V4L2_PIX_FMT_UYVY,
.buf_cnt = 10,
.filters = NULL
};

Expand Down
4 changes: 2 additions & 2 deletions sw/airborne/boards/ardrone2.h
Expand Up @@ -10,8 +10,8 @@
#endif

/* Cameras */
extern struct video_device_t bottom_camera;
extern struct video_device_t front_camera;
extern struct video_config_t bottom_camera;
extern struct video_config_t front_camera;

/* Default actuators driver */
#define DEFAULT_ACTUATORS "boards/ardrone/actuators.h"
Expand Down
4 changes: 2 additions & 2 deletions sw/airborne/boards/bebop.h
Expand Up @@ -37,8 +37,8 @@
#define ActuatorsDefaultCommit() ActuatorsBebopCommit()

/* Cameras */
extern struct video_device_t bottom_camera;
extern struct video_device_t front_camera;
extern struct video_config_t bottom_camera;
extern struct video_config_t front_camera;

/* by default activate onboard baro */
#ifndef USE_BARO_BOARD
Expand Down
6 changes: 4 additions & 2 deletions sw/airborne/boards/bebop/video.c
Expand Up @@ -39,21 +39,23 @@

#include "boards/bebop.h"

struct video_device_t bottom_camera = {
struct video_config_t bottom_camera = {
.w = 640,
.h = 480,
.dev_name = "/dev/video0",
.subdev_name = NULL,
.format = V4L2_PIX_FMT_UYVY,
.buf_cnt = 60,
.filters = NULL
};

struct video_device_t front_camera = {
struct video_config_t front_camera = {
.w = 1408,
.h = 2112,
.dev_name = "/dev/video1",
.subdev_name = "/dev/v4l-subdev1",
.format = V4L2_PIX_FMT_SGBRG10,
.buf_cnt = 80,
.filters = NULL //{DeMosaic, AEC, ABW}
};

Expand Down
79 changes: 45 additions & 34 deletions sw/airborne/modules/computer_vision/video_thread.c
Expand Up @@ -38,6 +38,10 @@
#include "lib/v4l/v4l2.h"
#include "lib/vision/image.h"
#include "lib/encoding/jpeg.h"
#include "peripherals/video_device.h"

// include board for bottom_camera and front_camera on ARDrone2 and Bebop
#include BOARD_CONFIG

#if JPEG_WITH_EXIF_HEADER
#include "lib/exif/exif_module.h"
Expand All @@ -47,25 +51,32 @@
#include <pthread.h>
#include "rt_priority.h"

// The video device
#ifndef VIDEO_THREAD_DEVICE
#define VIDEO_THREAD_DEVICE /dev/video1
#endif
PRINT_CONFIG_VAR(VIDEO_THREAD_DEVICE)

// The video device size (width, height)
#ifndef VIDEO_THREAD_DEVICE_SIZE
#define VIDEO_THREAD_DEVICE_SIZE 1280,720
#endif
#define __SIZE_HELPER(x, y) #x", "#y
#define _SIZE_HELPER(x) __SIZE_HELPER(x)
PRINT_CONFIG_MSG("VIDEO_THREAD_DEVICE_SIZE = " _SIZE_HELPER(VIDEO_THREAD_DEVICE_SIZE))

/// The camera video config (usually bottom_camera or front_camera)
#ifndef VIDEO_THREAD_CAMERA
#warning "Are you sure you don't want to use the bottom_camera or front_camera?"
// The video device buffers (the amount of V4L2 buffers)
#ifndef VIDEO_THREAD_DEVICE_BUFFERS
#define VIDEO_THREAD_DEVICE_BUFFERS 10
#endif
PRINT_CONFIG_VAR(VIDEO_THREAD_DEVICE_BUFFERS)
#ifndef VIDEO_THREAD_SUBDEV
#define VIDEO_THREAD_SUBDEV NULL
#endif
#ifndef VIDEO_THREAD_FILTERS
#define VIDEO_THREAD_FILTERS NULL
#endif
struct video_config_t custom_camera = {
.w = VIDEO_THREAD_VIDEO_WIDTH,
.h = VIDEO_THREAD_VIDEO_HEIGHT,
.dev_name = STRINGIFY(VIDEO_THREAD_DEVICE),
.subdev_name = VIDEO_THREAD_SUBDEV,
.buf_cnt = VIDEO_THREAD_DEVICE_BUFFERS,
.filters = VIDEO_THREAD_FILTERS
};
#define VIDEO_THREAD_CAMERA custom_camera
#endif
PRINT_CONFIG_VAR(VIDEO_THREAD_CAMERA)


// Frames Per Seconds
#ifndef VIDEO_THREAD_FPS
Expand Down Expand Up @@ -95,8 +106,26 @@ struct video_thread_t video_thread = {
* Handles all the video streaming and saving of the image shots
* This is a sepereate thread, so it needs to be thread safe!
*/
static void *video_thread_function(void *data __attribute__((unused)))
static void *video_thread_function(void *data)
{
struct video_config_t *vid = (struct video_config_t *)data;

// Initialize the V4L2 subdevice if needed
if (vid->subdev_name != NULL) {
if (!v4l2_init_subdev(vid->subdev_name, 0, 1, vid->format, vid->w, vid->h)) {
printf("[video_thread] Could not initialize the %s subdevice.\n", vid->subdev_name);
return 0;
}
}

// Initialize the V4L2 device
video_thread.dev = v4l2_init(vid->dev_name, vid->w, vid->h, vid->buf_cnt, vid->format);
if (video_thread.dev == NULL) {
printf("[video_thread] Could not initialize the %s V4L2 device.\n", vid->dev_name);
return 0;
}


// Start the streaming of the V4L2 device
if (!v4l2_start_capture(video_thread.dev)) {
printf("[video_thread-thread] Could not start capture of %s.\n", video_thread.dev->name);
Expand Down Expand Up @@ -176,24 +205,6 @@ static void *video_thread_function(void *data __attribute__((unused)))
*/
void video_thread_init(void)
{
#ifdef VIDEO_THREAD_SUBDEV
PRINT_CONFIG_MSG("[video_thread] Configuring a subdevice!")
PRINT_CONFIG_VAR(VIDEO_THREAD_SUBDEV)

// Initialize the V4L2 subdevice (TODO: fix hardcoded path, which and code)
if (!v4l2_init_subdev(STRINGIFY(VIDEO_THREAD_SUBDEV), 0, 1, V4L2_MBUS_FMT_UYVY8_2X8, VIDEO_THREAD_DEVICE_SIZE)) {
printf("[video_thread] Could not initialize the %s subdevice.\n", STRINGIFY(VIDEO_THREAD_SUBDEV));
return;
}
#endif

// Initialize the V4L2 device
video_thread.dev = v4l2_init(STRINGIFY(VIDEO_THREAD_DEVICE), VIDEO_THREAD_DEVICE_SIZE, VIDEO_THREAD_DEVICE_BUFFERS, V4L2_PIX_FMT_UYVY);
if (video_thread.dev == NULL) {
printf("[video_thread] Could not initialize the %s V4L2 device.\n", STRINGIFY(VIDEO_THREAD_DEVICE));
return;
}

// Create the shot directory
char save_name[128];
sprintf(save_name, "mkdir -p %s", STRINGIFY(VIDEO_THREAD_SHOT_PATH));
Expand All @@ -215,7 +226,7 @@ void video_thread_start(void)

// Start the streaming thread
pthread_t tid;
if (pthread_create(&tid, NULL, video_thread_function, NULL) != 0) {
if (pthread_create(&tid, NULL, video_thread_function, (void*)(&VIDEO_THREAD_CAMERA)) != 0) {
printf("[vievideo] Could not create streaming thread.\n");
return;
}
Expand Down
3 changes: 2 additions & 1 deletion sw/airborne/peripherals/video_device.h
Expand Up @@ -32,12 +32,13 @@
#include "modules/computer_vision/lib/v4l/v4l2.h"

/** V4L2 device settings */
struct video_device_t {
struct video_config_t {
int w; ///< Width
int h; ///< Height
char* dev_name; ///< path to device
char* subdev_name; ///< path to sub device
uint32_t format; ///< Video format
uint8_t buf_cnt; ///< Amount of V4L2 video device buffers
void* filters; ///< filters to use
};

Expand Down

0 comments on commit 6dd0b56

Please sign in to comment.