1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-12 09:36:45 +02:00

Implemented video preview and preparations for ToxAV

This commit is contained in:
cnhenry
2015-07-30 01:49:27 -05:00
parent c8ea02376e
commit 9b6efb65de
11 changed files with 447 additions and 80 deletions

View File

@ -21,10 +21,13 @@
*/
#include "video_device.h"
#ifdef VIDEO
#include "video_call.h"
#endif /* VIDEO */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <vpx/vpx_image.h>
#ifdef __linux__
#include <sys/types.h>
@ -57,9 +60,9 @@ struct VideoBuffer {
typedef struct VideoDevice {
int fd; /* File descriptor of video device selected/opened */
VideoDataHandleCallback cb; /* Use this to handle data from input device usually */
void* cb_data; /* Data to be passed to callback */
int32_t friend_number; /* ToxAV friend number */
VideoDataHandleCallback cb; /* Use this to handle data from input device usually */
void* cb_data; /* Data to be passed to callback */
int32_t friend_number; /* ToxAV friend number */
struct v4l2_format fmt;
struct VideoBuffer *buffers;
@ -70,6 +73,13 @@ typedef struct VideoDevice {
pthread_mutex_t mutex[1];
uint16_t video_width;
uint16_t video_height;
vpx_image_t input;
Display *x_display;
Window x_window;
GC x_gc;
} VideoDevice;
const char *dvideo_device_names[2]; /* Default device */
@ -83,9 +93,9 @@ static ToxAV* av = NULL;
#endif /* VIDEO */
/* q_mutex */
#define lock pthread_mutex_lock(&mutex);
#define unlock pthread_mutex_unlock(&mutex);
pthread_mutex_t mutex;
#define lock pthread_mutex_lock(&video_mutex);
#define unlock pthread_mutex_unlock(&video_mutex);
pthread_mutex_t video_mutex;
bool video_thread_running = true,
video_thread_paused = true; /* Thread control */
@ -114,28 +124,31 @@ VideoDeviceError init_video_devices()
#ifdef __linux__
for(int i = 0; i <= MAX_DEVICES; ++i) {
size[vdt_input] = i;
int fd;
struct v4l2_capability cap;
char *device_address;
char device_address[] = "/dev/videoXX";
snprintf(device_address + 10, sizeof(device_address) - 10, "%i", i);
fd = open(device_address, O_RDWR | O_NONBLOCK, 0);
if (fd == -1)
break;
else {
video_devices_names[vdt_input][i] = cap.card;
}
close(fd);
size[vdt_input] = i;
if (fd == -1) {
break;
} else {
video_devices_names[vdt_input][i] = cap.card;
close(fd);
}
}
#endif /* __linux__ */
/* TODO: Add OSX implementation for listing input video devices */
size[output] = 0;
size[vdt_output] = 0;
/* TODO: List output video devices */
// Start poll thread
if (pthread_mutex_init(&mutex, NULL) != 0)
if (pthread_mutex_init(&video_mutex, NULL) != 0)
return vde_InternalError;
pthread_t thread_id;
@ -155,12 +168,26 @@ VideoDeviceError terminate_video_devices()
video_thread_running = false;
usleep(20000);
if (pthread_mutex_destroy(&mutex) != 0)
if (pthread_mutex_destroy(&video_mutex) != 0)
return (VideoDeviceError) vde_InternalError;
return (VideoDeviceError) vde_None;
}
VideoDeviceError register_video_device_callback(int32_t friend_number, uint32_t device_idx, VideoDataHandleCallback callback, void* data)
{
if (size[vdt_input] <= device_idx || !video_devices_running[vdt_input][device_idx] || video_devices_running[vdt_input][device_idx]->fd == NULL)
return vde_InvalidSelection;
lock;
video_devices_running[vdt_input][device_idx]->cb = callback;
video_devices_running[vdt_input][device_idx]->cb_data = data;
video_devices_running[vdt_input][device_idx]->friend_number = friend_number;
unlock;
return vde_None;
}
VideoDeviceError set_primary_video_device(VideoDeviceType type, int32_t selection)
{
if (size[type] <= selection || selection < 0) return vde_InvalidSelection;
@ -212,19 +239,14 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
}
if (type == vdt_input) {
#ifdef __linux__
char device_address[] = "/dev/videoXX";
snprintf(device_address + 10 , sizeof(device_address) - 10, "%i", selection);
device->fd = open(device_address, O_RDWR);
if ( device->fd == -1 )
return vde_FailedStart;
}
else {
}
if (type == vdt_input) {
#ifdef __linux__
/* Obtain video device capabilities */
struct v4l2_capability cap;
if (-1 == xioctl(device->fd, VIDIOC_QUERYCAP, &cap)) {
@ -236,7 +258,7 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
memset(&(fmt), 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
if(-1 == xioctl(device->fd, VIDIOC_G_FMT, &fmt)) {
return vde_UnsupportedMode;
}
@ -308,6 +330,34 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
/*TODO: Add OSX implementation of opening video devices */
/* Initialize X11 window associated to device */
if ((device->x_display = XOpenDisplay(NULL)) == NULL) {
return vde_FailedStart;
}
int screen = DefaultScreen(device->x_display);
if ((device->x_window = XCreateSimpleWindow(device->x_display, RootWindow(device->x_display, screen), 0, 0,
device->video_width, device->video_height, 0, BlackPixel(device->x_display, screen), WhitePixel(device->x_display, screen))) == NULL) {
return vde_FailedStart;
}
XStoreName(device->x_display, device->x_window, "Video Preview");
XSelectInput(device->x_display, device->x_window, ExposureMask|ButtonPressMask|KeyPressMask);
if ((device->x_gc = DefaultGC(device->x_display, screen)) == NULL) {
return vde_FailedStart;
}
XMapWindow(device->x_display, device->x_window);
//XClearWindow(device->x_display, device->x_window);
XMapRaised(device->x_display, device->x_window);
XFlush(device->x_display);
vpx_img_alloc(&device->input, VPX_IMG_FMT_I420, device->video_width, device->video_height, 1);
video_thread_paused = false;
}
@ -344,15 +394,42 @@ void* video_thread_poll (void* arg) // TODO: maybe use thread for every input so
if (-1 == ioctl(device->fd, VIDIOC_DQBUF, &buf)) {
unlock;
continue;
}
}
void *data = (void*)device->buffers[buf.index].start;
uint8_t *y;
uint8_t *u;
uint8_t *v;
yuv422to420(y, u, v, data, device->video_width, device->video_width);
uint16_t video_width = device->video_width;
uint16_t video_height = device->video_height;
int screen = DefaultScreen(device->x_display);
if ( device->cb ) device->cb(device->video_width, device->video_height, y, u, v, device->cb_data);
yuv422to420(device->input.planes[0], device->input.planes[1], device->input.planes[2], data, video_width, video_height);
uint8_t *img_data = malloc(video_width * video_height * 4);
yuv420tobgr(video_width, video_height, device->input.planes[0], device->input.planes[1], device->input.planes[2], video_width, video_width / 2, video_width / 2, img_data);
XImage image = {
.width = video_width,
.height = video_height,
.depth = 24,
.bits_per_pixel = 32,
.format = ZPixmap,
.byte_order = LSBFirst,
.bitmap_unit = 8,
.bitmap_bit_order = LSBFirst,
.bytes_per_line = video_width * 4,
.red_mask = 0xFF0000,
.green_mask = 0xFF00,
.blue_mask = 0xFF,
.data = (char*)img_data
};
Pixmap pixmap = XCreatePixmap(device->x_display, device->x_window, video_width, video_height, 24);
XPutImage(device->x_display, pixmap, device->x_gc, &image, 0, 0, 0, 0, video_width, video_height);
XCopyArea(device->x_display, pixmap, device->x_window, device->x_gc, 0, 0, video_width, video_height, 0, 0);
XFreePixmap(device->x_display, pixmap);
free(img_data);
//XFlush(device->x_display);
//
//if ( device->cb ) device->cb(device->video_width, device->video_height, device->y, device->u, device->v, device->cb_data);
if (-1 == xioctl(device->fd, VIDIOC_QBUF, &buf)) {
unlock;
@ -361,7 +438,8 @@ void* video_thread_poll (void* arg) // TODO: maybe use thread for every input so
}
unlock;
}
usleep(5000);
usleep(1000 * 1000 / 24);
//usleep(5000);
}
}
@ -392,6 +470,10 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx)
}
close(device->fd);
vpx_img_free(&device->input);
XFreeGC(device->x_display, device->x_gc);
XDestroyWindow(device->x_display, device->x_window);
XCloseDisplay(device->x_display);
}
else {
@ -405,6 +487,29 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx)
return rc;
}
void yuv420tobgr(uint16_t width, uint16_t height, const uint8_t *y, const uint8_t *u, const uint8_t *v, unsigned int ystride, unsigned int ustride, unsigned int vstride, uint8_t *out)
{
unsigned long int i, j;
for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
uint8_t *point = out + 4 * ((i * width) + j);
int t_y = y[((i * ystride) + j)];
int t_u = u[(((i / 2) * ustride) + (j / 2))];
int t_v = v[(((i / 2) * vstride) + (j / 2))];
t_y = t_y < 16 ? 16 : t_y;
int r = (298 * (t_y - 16) + 409 * (t_v - 128) + 128) >> 8;
int g = (298 * (t_y - 16) - 100 * (t_u - 128) - 208 * (t_v - 128) + 128) >> 8;
int b = (298 * (t_y - 16) + 516 * (t_u - 128) + 128) >> 8;
point[2] = r>255? 255 : r<0 ? 0 : r;
point[1] = g>255? 255 : g<0 ? 0 : g;
point[0] = b>255? 255 : b<0 ? 0 : b;
point[3] = ~0;
}
}
}
void yuv422to420(uint8_t *plane_y, uint8_t *plane_u, uint8_t *plane_v, uint8_t *input, uint16_t width, uint16_t height)
{
uint8_t *end = input + width * height * 2;
@ -424,7 +529,6 @@ void yuv422to420(uint8_t *plane_y, uint8_t *plane_u, uint8_t *plane_v, uint8_t *
*plane_y++ = *input++;
input++;//v
}
}
}