2015-07-11 03:54:06 +02:00
|
|
|
/* video_device.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "video_device.h"
|
|
|
|
#include "video_call.h"
|
2015-07-30 08:49:27 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
#include <sys/ioctl.h>
|
2015-07-30 08:49:27 +02:00
|
|
|
|
|
|
|
#include <vpx/vpx_image.h>
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2017-11-15 04:46:47 +01:00
|
|
|
#import "osx_video.h"
|
|
|
|
#else
|
2020-03-30 14:35:34 +02:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/Xos.h>
|
2015-07-19 16:51:35 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
2018-10-02 23:31:14 +02:00
|
|
|
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
2017-11-11 20:03:28 +01:00
|
|
|
#include <sys/videoio.h>
|
2018-10-02 23:31:14 +02:00
|
|
|
#else
|
|
|
|
#include <linux/videodev2.h>
|
2018-10-08 19:39:04 +02:00
|
|
|
#endif /* defined(__OpenBSD__) || defined(__NetBSD__) */
|
2020-03-30 14:35:34 +02:00
|
|
|
#endif /* __OSX__ || __APPLE__ */
|
2015-07-11 04:55:49 +02:00
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
#include "line_info.h"
|
|
|
|
#include "settings.h"
|
2020-03-15 19:57:00 +01:00
|
|
|
#include "misc_tools.h"
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-07-06 17:45:35 +02:00
|
|
|
#ifdef VIDEO
|
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
#define inline__ inline __attribute__((always_inline))
|
|
|
|
|
|
|
|
extern struct user_settings *user_settings;
|
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
struct VideoBuffer {
|
|
|
|
void *start;
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
typedef struct VideoDevice {
|
2015-07-30 08:49:27 +02:00
|
|
|
VideoDataHandleCallback cb; /* Use this to handle data from input device usually */
|
2016-09-25 03:07:04 +02:00
|
|
|
void *cb_data; /* Data to be passed to callback */
|
2015-07-30 08:49:27 +02:00
|
|
|
int32_t friend_number; /* ToxAV friend number */
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if !(defined(__OSX__) || defined(__APPLE__))
|
2015-08-20 13:39:47 +02:00
|
|
|
int fd; /* File descriptor of video device selected/opened */
|
2015-07-19 16:51:35 +02:00
|
|
|
struct v4l2_format fmt;
|
|
|
|
struct VideoBuffer *buffers;
|
2015-08-16 09:20:16 +02:00
|
|
|
uint32_t n_buffers;
|
2015-11-04 05:04:05 +01:00
|
|
|
#endif
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
uint32_t ref_count;
|
|
|
|
int32_t selection;
|
|
|
|
pthread_mutex_t mutex[1];
|
2015-07-19 16:51:35 +02:00
|
|
|
uint16_t video_width;
|
|
|
|
uint16_t video_height;
|
2015-07-30 08:49:27 +02:00
|
|
|
|
|
|
|
vpx_image_t input;
|
|
|
|
|
|
|
|
Display *x_display;
|
|
|
|
Window x_window;
|
|
|
|
GC x_gc;
|
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
} VideoDevice;
|
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
const char *dvideo_device_names[2]; /* Default device */
|
|
|
|
const char *video_devices_names[2][MAX_DEVICES]; /* Container of available devices */
|
2015-07-11 03:54:06 +02:00
|
|
|
static int size[2]; /* Size of above containers */
|
2015-07-22 20:06:14 +02:00
|
|
|
VideoDevice *video_devices_running[2][MAX_DEVICES] = {{NULL}}; /* Running devices */
|
|
|
|
uint32_t primary_video_device[2]; /* Primary device */
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
static ToxAV *av = NULL;
|
2015-07-11 03:54:06 +02:00
|
|
|
|
|
|
|
/* q_mutex */
|
2020-04-23 02:22:18 +02:00
|
|
|
#define lock pthread_mutex_lock(&video_mutex)
|
|
|
|
#define unlock pthread_mutex_unlock(&video_mutex)
|
2015-07-30 08:49:27 +02:00
|
|
|
pthread_mutex_t video_mutex;
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
bool video_thread_running = true,
|
2016-09-25 03:07:04 +02:00
|
|
|
video_thread_paused = true; /* Thread control */
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
void *video_thread_poll(void *);
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2015-11-04 05:04:05 +01:00
|
|
|
static void yuv420tobgr(uint16_t width, uint16_t height, const uint8_t *y,
|
2016-09-25 03:07:04 +02:00
|
|
|
const uint8_t *u, const uint8_t *v, unsigned int ystride,
|
|
|
|
unsigned int ustride, unsigned int vstride, uint8_t *out)
|
2015-08-09 04:21:23 +02:00
|
|
|
{
|
|
|
|
unsigned long int i, j;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-09 04:21:23 +02:00
|
|
|
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;
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
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;
|
2015-08-09 04:21:23 +02:00
|
|
|
point[3] = ~0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if !(defined(__OSX__) || defined(__APPLE__))
|
2015-11-04 05:04:05 +01:00
|
|
|
static void yuv422to420(uint8_t *plane_y, uint8_t *plane_u, uint8_t *plane_v,
|
2016-09-25 03:07:04 +02:00
|
|
|
uint8_t *input, uint16_t width, uint16_t height)
|
2015-08-09 04:21:23 +02:00
|
|
|
{
|
|
|
|
uint8_t *end = input + width * height * 2;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
while (input != end) {
|
2015-08-09 04:21:23 +02:00
|
|
|
uint8_t *line_end = input + width * 2;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
while (input != line_end) {
|
2015-08-09 04:21:23 +02:00
|
|
|
*plane_y++ = *input++;
|
|
|
|
*plane_u++ = *input++;
|
|
|
|
*plane_y++ = *input++;
|
|
|
|
*plane_v++ = *input++;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_end = input + width * 2;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
while (input != line_end) {
|
2015-08-09 04:21:23 +02:00
|
|
|
*plane_y++ = *input++;
|
|
|
|
input++;//u
|
|
|
|
*plane_y++ = *input++;
|
|
|
|
input++;//v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:23:01 +02:00
|
|
|
static int xioctl(int fh, unsigned long request, void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
do {
|
|
|
|
r = ioctl(fh, request, arg);
|
|
|
|
} while (-1 == r && EINTR == errno);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-11-15 21:02:10 +01:00
|
|
|
#endif
|
2015-08-20 21:23:01 +02:00
|
|
|
|
2015-07-11 03:54:06 +02:00
|
|
|
/* Meet devices */
|
2015-07-19 16:51:35 +02:00
|
|
|
#ifdef VIDEO
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDeviceError init_video_devices(ToxAV *av_)
|
2015-07-11 03:54:06 +02:00
|
|
|
#else
|
2015-07-19 16:51:35 +02:00
|
|
|
VideoDeviceError init_video_devices()
|
2015-07-11 03:54:06 +02:00
|
|
|
#endif /* VIDEO */
|
|
|
|
{
|
2015-07-24 20:10:13 +02:00
|
|
|
size[vdt_input] = 0;
|
2015-07-11 11:06:12 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (osx_video_init((char **)video_devices_names[vdt_input], &size[vdt_input]) != 0) {
|
2017-11-15 21:02:10 +01:00
|
|
|
return vde_InternalError;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#else /* not __OSX__ || __APPLE__ */
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
for (; size[vdt_input] <= MAX_DEVICES; ++size[vdt_input]) {
|
2015-07-19 16:51:35 +02:00
|
|
|
int fd;
|
2015-07-30 08:49:27 +02:00
|
|
|
char device_address[] = "/dev/videoXX";
|
2015-08-07 18:40:35 +02:00
|
|
|
snprintf(device_address + 10, sizeof(char) * strlen(device_address) - 10, "%i", size[vdt_input]);
|
2015-07-11 11:06:12 +02:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
fd = open(device_address, O_RDWR | O_NONBLOCK, 0);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (fd == -1) {
|
2015-07-11 11:06:12 +02:00
|
|
|
break;
|
2015-07-30 08:49:27 +02:00
|
|
|
} else {
|
2015-08-07 18:40:35 +02:00
|
|
|
struct v4l2_capability cap;
|
2016-09-25 03:07:04 +02:00
|
|
|
char *video_input_name;
|
2015-08-07 18:40:35 +02:00
|
|
|
|
|
|
|
/* Query V4L for capture capabilities */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 != ioctl(fd, VIDIOC_QUERYCAP, &cap)) {
|
2016-09-25 03:07:04 +02:00
|
|
|
video_input_name = (char *)malloc(strlen((const char *)cap.card) + strlen(device_address) + 4);
|
|
|
|
strcpy(video_input_name, (char *)cap.card);
|
2015-08-14 03:01:05 +02:00
|
|
|
strcat(video_input_name, " (");
|
2016-09-25 03:07:04 +02:00
|
|
|
strcat(video_input_name, (char *)device_address);
|
2015-08-14 03:01:05 +02:00
|
|
|
strcat(video_input_name, ")");
|
2015-08-07 18:40:35 +02:00
|
|
|
} else {
|
2016-09-25 03:07:04 +02:00
|
|
|
video_input_name = (char *)malloc(strlen(device_address) + 3);
|
2015-08-14 03:01:05 +02:00
|
|
|
strcpy(video_input_name, "(");
|
|
|
|
strcat(video_input_name, device_address);
|
|
|
|
strcat(video_input_name, ")");
|
2015-08-07 18:40:35 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-07 18:40:35 +02:00
|
|
|
video_devices_names[vdt_input][size[vdt_input]] = video_input_name;
|
|
|
|
|
2015-07-30 08:49:27 +02:00
|
|
|
close(fd);
|
2015-07-11 11:06:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 13:39:47 +02:00
|
|
|
|
2015-08-12 15:24:43 +02:00
|
|
|
#endif
|
|
|
|
|
2015-08-06 06:26:45 +02:00
|
|
|
size[vdt_output] = 1;
|
2016-09-25 03:07:04 +02:00
|
|
|
char *video_output_name = "Toxic Video Receiver";
|
2015-08-07 18:40:35 +02:00
|
|
|
video_devices_names[vdt_output][0] = video_output_name;
|
2015-07-11 11:06:12 +02:00
|
|
|
|
|
|
|
// Start poll thread
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_mutex_init(&video_mutex, NULL) != 0) {
|
2015-07-11 11:06:12 +02:00
|
|
|
return vde_InternalError;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-11 11:06:12 +02:00
|
|
|
|
|
|
|
pthread_t thread_id;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_create(&thread_id, NULL, video_thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) {
|
2015-07-11 11:06:12 +02:00
|
|
|
return vde_InternalError;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-11 03:54:06 +02:00
|
|
|
|
|
|
|
#ifdef VIDEO
|
2015-07-11 11:06:12 +02:00
|
|
|
av = av_;
|
2015-07-11 03:54:06 +02:00
|
|
|
#endif /* VIDEO */
|
|
|
|
|
2015-07-11 11:06:12 +02:00
|
|
|
return (VideoDeviceError) vde_None;
|
2015-07-11 03:54:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 19:23:07 +02:00
|
|
|
VideoDeviceError terminate_video_devices(void)
|
2015-07-11 03:54:06 +02:00
|
|
|
{
|
2015-07-11 11:06:12 +02:00
|
|
|
/* Cleanup if needed */
|
2016-09-23 00:00:14 +02:00
|
|
|
lock;
|
2015-07-22 20:06:14 +02:00
|
|
|
video_thread_running = false;
|
2016-09-23 00:00:14 +02:00
|
|
|
unlock;
|
|
|
|
|
2015-07-11 11:06:12 +02:00
|
|
|
usleep(20000);
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2015-08-14 03:01:05 +02:00
|
|
|
int i;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-14 03:01:05 +02:00
|
|
|
for (i = 0; i < size[vdt_input]; ++i) {
|
2016-09-25 03:07:04 +02:00
|
|
|
free((void *)video_devices_names[vdt_input][i]);
|
2015-08-14 03:01:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_mutex_destroy(&video_mutex) != 0) {
|
2015-07-11 11:06:12 +02:00
|
|
|
return (VideoDeviceError) vde_InternalError;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-11 03:54:06 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2015-08-20 21:23:01 +02:00
|
|
|
osx_video_release();
|
2020-03-30 14:35:34 +02:00
|
|
|
#endif /* __OSX__ || __APPLE__ */
|
2015-08-20 13:39:47 +02:00
|
|
|
|
2015-07-11 11:06:12 +02:00
|
|
|
return (VideoDeviceError) vde_None;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 05:04:05 +01:00
|
|
|
VideoDeviceError register_video_device_callback(int32_t friend_number, uint32_t device_idx,
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDataHandleCallback callback, void *data)
|
2015-07-30 08:49:27 +02:00
|
|
|
{
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (size[vdt_input] <= device_idx || !video_devices_running[vdt_input][device_idx]) {
|
2015-07-30 08:49:27 +02:00
|
|
|
return vde_InvalidSelection;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#else /* not __OSX__ || __APPLE__ */
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (size[vdt_input] <= device_idx || !video_devices_running[vdt_input][device_idx]
|
|
|
|
|| !video_devices_running[vdt_input][device_idx]->fd) {
|
2015-08-20 13:39:47 +02:00
|
|
|
return vde_InvalidSelection;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-20 13:39:47 +02:00
|
|
|
#endif
|
2015-07-30 08:49:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
VideoDeviceError set_primary_video_device(VideoDeviceType type, int32_t selection)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (size[type] <= selection || selection < 0) {
|
|
|
|
return vde_InvalidSelection;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
primary_video_device[type] = selection;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
return vde_None;
|
|
|
|
}
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDeviceError open_primary_video_device(VideoDeviceType type, uint32_t *device_idx)
|
2015-07-19 16:51:35 +02:00
|
|
|
{
|
2015-07-22 20:06:14 +02:00
|
|
|
return open_video_device(type, primary_video_device[type], device_idx);
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
void get_primary_video_device_name(VideoDeviceType type, char *buf, int size)
|
2015-07-19 16:51:35 +02:00
|
|
|
{
|
2015-07-22 20:06:14 +02:00
|
|
|
memcpy(buf, dvideo_device_names[type], size);
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint32_t *device_idx)
|
2015-07-19 16:51:35 +02:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (size[type] <= selection || selection < 0) {
|
|
|
|
return vde_InvalidSelection;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
lock;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-11-12 11:01:28 +01:00
|
|
|
uint32_t i, temp_idx = -1;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-11-12 11:01:28 +01:00
|
|
|
for (i = 0; i < MAX_DEVICES; ++i) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!video_devices_running[type][i]) {
|
2015-11-12 11:01:28 +01:00
|
|
|
temp_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_idx == -1) {
|
|
|
|
unlock;
|
|
|
|
return vde_AllDevicesBusy;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
for (i = 0; i < MAX_DEVICES; i ++) { /* Check if any device has the same selection */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (video_devices_running[type][i] && video_devices_running[type][i]->selection == selection) {
|
2015-08-10 13:14:38 +02:00
|
|
|
|
2015-11-12 11:01:28 +01:00
|
|
|
video_devices_running[type][temp_idx] = video_devices_running[type][i];
|
|
|
|
video_devices_running[type][i]->ref_count++;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
return vde_None;
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDevice *device = video_devices_running[type][temp_idx] = calloc(1, sizeof(VideoDevice));
|
2015-07-19 16:51:35 +02:00
|
|
|
device->selection = selection;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_mutex_init(device->mutex, NULL) != 0) {
|
2015-07-19 16:51:35 +02:00
|
|
|
free(device);
|
|
|
|
unlock;
|
|
|
|
return vde_InternalError;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (type == vdt_input) {
|
2015-08-09 04:21:23 +02:00
|
|
|
video_thread_paused = true;
|
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (osx_video_open_device(selection, &device->video_width, &device->video_height) != 0) {
|
2017-11-15 21:02:10 +01:00
|
|
|
free(device);
|
|
|
|
unlock;
|
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#else /* not __OSX__ || __APPLE__ */
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Open selected device */
|
2015-07-19 16:51:35 +02:00
|
|
|
char device_address[] = "/dev/videoXX";
|
2017-06-30 00:26:06 +02:00
|
|
|
snprintf(device_address + 10, sizeof(device_address) - 10, "%i", selection);
|
2015-07-19 16:51:35 +02:00
|
|
|
|
|
|
|
device->fd = open(device_address, O_RDWR);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (device->fd == -1) {
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-07-19 16:51:35 +02:00
|
|
|
return vde_FailedStart;
|
2015-08-07 18:40:35 +02:00
|
|
|
}
|
2015-07-19 16:51:35 +02:00
|
|
|
|
|
|
|
/* Obtain video device capabilities */
|
|
|
|
struct v4l2_capability cap;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_QUERYCAP, &cap)) {
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup video format */
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
memset(&(fmt), 0, sizeof(fmt));
|
|
|
|
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2015-07-30 08:49:27 +02:00
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_S_FMT, &fmt)) {
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
device->video_width = fmt.fmt.pix.width;
|
|
|
|
device->video_height = fmt.fmt.pix.height;
|
|
|
|
|
|
|
|
/* Request buffers */
|
|
|
|
struct v4l2_requestbuffers req;
|
|
|
|
memset(&(req), 0, sizeof(req));
|
|
|
|
req.count = 4;
|
|
|
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
req.memory = V4L2_MEMORY_MMAP;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_REQBUFS, &req)) {
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (req.count < 2) {
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-09 04:21:23 +02:00
|
|
|
device->buffers = calloc(req.count, sizeof(struct VideoBuffer));
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
for (i = 0; i < req.count; ++i) {
|
2015-07-19 16:51:35 +02:00
|
|
|
struct v4l2_buffer buf;
|
|
|
|
memset(&(buf), 0, sizeof(buf));
|
|
|
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
2015-08-09 04:21:23 +02:00
|
|
|
buf.index = i;
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_QUERYBUF, &buf)) {
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
device->buffers[i].length = buf.length;
|
|
|
|
device->buffers[i].start = mmap(NULL /* start anywhere */,
|
2016-09-25 03:07:04 +02:00
|
|
|
buf.length,
|
|
|
|
PROT_READ | PROT_WRITE /* required */,
|
|
|
|
MAP_SHARED /* recommended */,
|
|
|
|
device->fd, buf.m.offset);
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (MAP_FAILED == device->buffers[i].start) {
|
|
|
|
for (i = 0; i < buf.index; ++i) {
|
2015-08-11 13:11:09 +02:00
|
|
|
munmap(device->buffers[i].start, device->buffers[i].length);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-08-09 04:21:23 +02:00
|
|
|
return vde_FailedStart;
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-09 04:21:23 +02:00
|
|
|
device->n_buffers = i;
|
2015-07-19 16:51:35 +02:00
|
|
|
|
|
|
|
enum v4l2_buf_type type;
|
|
|
|
|
|
|
|
for (i = 0; i < device->n_buffers; ++i) {
|
|
|
|
struct v4l2_buffer buf;
|
2015-08-09 04:21:23 +02:00
|
|
|
memset(&(buf), 0, sizeof(buf));
|
2015-07-19 16:51:35 +02:00
|
|
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
buf.index = i;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_QBUF, &buf)) {
|
|
|
|
for (i = 0; i < device->n_buffers; ++i) {
|
2015-08-11 13:11:09 +02:00
|
|
|
munmap(device->buffers[i].start, device->buffers[i].length);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-11 13:11:09 +02:00
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
2015-11-12 11:01:28 +01:00
|
|
|
unlock;
|
2015-07-19 16:51:35 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 13:14:38 +02:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2015-08-10 13:14:38 +02:00
|
|
|
|
|
|
|
/* Turn on video stream */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_STREAMON, &type)) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_input, temp_idx);
|
|
|
|
unlock;
|
2015-07-19 16:51:35 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
#endif
|
2015-07-30 08:49:27 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Create X11 window associated to device */
|
2018-07-18 17:33:16 +02:00
|
|
|
if ((device->x_display = XOpenDisplay(NULL)) == NULL) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_input, temp_idx);
|
|
|
|
unlock;
|
2015-07-30 08:49:27 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
int screen = DefaultScreen(device->x_display);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
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),
|
|
|
|
BlackPixel(device->x_display, screen)))) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_input, temp_idx);
|
|
|
|
unlock;
|
2015-07-30 08:49:27 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
XStoreName(device->x_display, device->x_window, "Video Preview");
|
2016-09-25 03:07:04 +02:00
|
|
|
XSelectInput(device->x_display, device->x_window, ExposureMask | ButtonPressMask | KeyPressMask);
|
2015-07-30 08:49:27 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if ((device->x_gc = DefaultGC(device->x_display, screen)) == NULL) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_input, temp_idx);
|
|
|
|
unlock;
|
2015-07-30 08:49:27 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
2015-08-14 06:51:39 +02:00
|
|
|
/* Disable user from manually closing the X11 window */
|
|
|
|
Atom wm_delete_window = XInternAtom(device->x_display, "WM_DELETE_WINDOW", false);
|
|
|
|
XSetWMProtocols(device->x_display, device->x_window, &wm_delete_window, 1);
|
|
|
|
|
2015-07-30 08:49:27 +02:00
|
|
|
XMapWindow(device->x_display, device->x_window);
|
2015-08-06 06:26:45 +02:00
|
|
|
XClearWindow(device->x_display, device->x_window);
|
2015-07-30 08:49:27 +02:00
|
|
|
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);
|
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
video_thread_paused = false;
|
2015-08-10 13:14:38 +02:00
|
|
|
} else { /* vdt_output */
|
|
|
|
|
|
|
|
/* Create X11 window associated to device */
|
2018-07-18 17:33:16 +02:00
|
|
|
if ((device->x_display = XOpenDisplay(NULL)) == NULL) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_output, temp_idx);
|
|
|
|
unlock;
|
2015-08-06 06:26:45 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
int screen = DefaultScreen(device->x_display);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!(device->x_window = XCreateSimpleWindow(device->x_display, RootWindow(device->x_display, screen), 0, 0,
|
|
|
|
100, 100, 0, BlackPixel(device->x_display, screen), BlackPixel(device->x_display, screen)))) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_output, temp_idx);
|
|
|
|
unlock;
|
2015-08-06 06:26:45 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
XStoreName(device->x_display, device->x_window, "Video Receive");
|
2016-09-25 03:07:04 +02:00
|
|
|
XSelectInput(device->x_display, device->x_window, ExposureMask | ButtonPressMask | KeyPressMask);
|
2015-08-06 06:26:45 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if ((device->x_gc = DefaultGC(device->x_display, screen)) == NULL) {
|
2015-11-12 11:01:28 +01:00
|
|
|
close_video_device(vdt_output, temp_idx);
|
|
|
|
unlock;
|
2015-08-06 06:26:45 +02:00
|
|
|
return vde_FailedStart;
|
|
|
|
}
|
|
|
|
|
2015-08-14 06:51:39 +02:00
|
|
|
/* Disable user from manually closing the X11 window */
|
|
|
|
Atom wm_delete_window = XInternAtom(device->x_display, "WM_DELETE_WINDOW", false);
|
|
|
|
XSetWMProtocols(device->x_display, device->x_window, &wm_delete_window, 1);
|
|
|
|
|
2015-08-06 06:26:45 +02:00
|
|
|
XMapWindow(device->x_display, device->x_window);
|
2015-08-07 18:40:35 +02:00
|
|
|
XClearWindow(device->x_display, device->x_window);
|
|
|
|
XMapRaised(device->x_display, device->x_window);
|
2015-08-06 06:26:45 +02:00
|
|
|
XFlush(device->x_display);
|
|
|
|
|
|
|
|
vpx_img_alloc(&device->input, VPX_IMG_FMT_I420, device->video_width, device->video_height, 1);
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-11-12 11:01:28 +01:00
|
|
|
*device_idx = temp_idx;
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
2015-11-12 11:01:28 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
return vde_None;
|
|
|
|
}
|
|
|
|
|
2018-09-14 01:47:47 +02:00
|
|
|
VideoDeviceError write_video_out(uint16_t width, uint16_t height,
|
2018-10-10 05:06:57 +02:00
|
|
|
uint8_t const *y, uint8_t const *u, uint8_t const *v,
|
|
|
|
int32_t ystride, int32_t ustride, int32_t vstride,
|
|
|
|
void *user_data)
|
2015-07-30 09:37:31 +02:00
|
|
|
{
|
2020-03-15 19:57:00 +01:00
|
|
|
UNUSED_VAR(user_data);
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDevice *device = video_devices_running[vdt_output][0];
|
2015-07-30 09:37:31 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!device) {
|
|
|
|
return vde_DeviceNotActive;
|
|
|
|
}
|
2015-08-06 06:26:45 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!device->x_window) {
|
|
|
|
return vde_DeviceNotActive;
|
|
|
|
}
|
2015-08-11 13:11:09 +02:00
|
|
|
|
2015-08-06 06:26:45 +02:00
|
|
|
pthread_mutex_lock(device->mutex);
|
|
|
|
|
|
|
|
/* Resize X11 window to correct size */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (device->video_width != width || device->video_height != height) {
|
2015-08-06 06:26:45 +02:00
|
|
|
device->video_width = width;
|
|
|
|
device->video_height = height;
|
|
|
|
XResizeWindow(device->x_display, device->x_window, width, height);
|
2015-08-10 13:14:38 +02:00
|
|
|
|
2015-08-06 06:26:45 +02:00
|
|
|
vpx_img_free(&device->input);
|
|
|
|
vpx_img_alloc(&device->input, VPX_IMG_FMT_I420, width, height, 1);
|
|
|
|
}
|
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Convert YUV420 data to BGR */
|
2015-08-06 06:26:45 +02:00
|
|
|
ystride = abs(ystride);
|
|
|
|
ustride = abs(ustride);
|
|
|
|
vstride = abs(vstride);
|
2015-08-07 18:40:35 +02:00
|
|
|
uint8_t *img_data = malloc(width * height * 4);
|
|
|
|
yuv420tobgr(width, height, y, u, v, ystride, ustride, vstride, img_data);
|
2015-08-06 06:26:45 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Allocate image data in X11 */
|
2015-08-06 06:26:45 +02:00
|
|
|
XImage image = {
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.depth = 24,
|
|
|
|
.bits_per_pixel = 32,
|
|
|
|
.format = ZPixmap,
|
|
|
|
.byte_order = LSBFirst,
|
|
|
|
.bitmap_unit = 8,
|
|
|
|
.bitmap_bit_order = LSBFirst,
|
|
|
|
.bytes_per_line = width * 4,
|
|
|
|
.red_mask = 0xFF0000,
|
|
|
|
.green_mask = 0xFF00,
|
|
|
|
.blue_mask = 0xFF,
|
2016-09-25 03:07:04 +02:00
|
|
|
.data = (char *)img_data
|
2015-08-06 06:26:45 +02:00
|
|
|
};
|
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Render image data */
|
2015-08-06 06:26:45 +02:00
|
|
|
Pixmap pixmap = XCreatePixmap(device->x_display, device->x_window, width, height, 24);
|
|
|
|
XPutImage(device->x_display, pixmap, device->x_gc, &image, 0, 0, 0, 0, width, height);
|
|
|
|
XCopyArea(device->x_display, pixmap, device->x_window, device->x_gc, 0, 0, width, height, 0, 0);
|
|
|
|
XFreePixmap(device->x_display, pixmap);
|
|
|
|
XFlush(device->x_display);
|
|
|
|
free(img_data);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(device->mutex);
|
|
|
|
return vde_None;
|
2015-07-30 09:37:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
void *video_thread_poll(void *arg) // TODO: maybe use thread for every input source
|
2015-07-19 16:51:35 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NOTE: We only need to poll input devices for data.
|
|
|
|
*/
|
2020-03-30 18:56:42 +02:00
|
|
|
UNUSED_VAR(arg);
|
2015-07-19 16:51:35 +02:00
|
|
|
uint32_t i;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2016-09-23 00:00:14 +02:00
|
|
|
while (1) {
|
|
|
|
lock;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-09-23 00:00:14 +02:00
|
|
|
if (!video_thread_running) {
|
|
|
|
unlock;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2016-09-23 00:00:14 +02:00
|
|
|
unlock;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (video_thread_paused) {
|
|
|
|
usleep(10000); /* Wait for unpause. */
|
|
|
|
} else {
|
2016-09-23 00:00:14 +02:00
|
|
|
for (i = 0; i < size[vdt_input]; ++i) {
|
2015-07-19 16:51:35 +02:00
|
|
|
lock;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (video_devices_running[vdt_input][i] != NULL) {
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Obtain frame image data from device buffers */
|
2016-09-25 03:07:04 +02:00
|
|
|
VideoDevice *device = video_devices_running[vdt_input][i];
|
2015-08-20 21:23:01 +02:00
|
|
|
uint16_t video_width = device->video_width;
|
|
|
|
uint16_t video_height = device->video_height;
|
|
|
|
uint8_t *y = device->input.planes[0];
|
|
|
|
uint8_t *u = device->input.planes[1];
|
|
|
|
uint8_t *v = device->input.planes[2];
|
2015-08-12 11:33:59 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (osx_video_read_device(y, u, v, &video_width, &video_height) != 0) {
|
2017-11-15 21:02:10 +01:00
|
|
|
unlock;
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-04 05:54:12 +01:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#else /* not __OSX__ || __APPLE__ */
|
2015-07-19 16:51:35 +02:00
|
|
|
struct v4l2_buffer buf;
|
|
|
|
memset(&(buf), 0, sizeof(buf));
|
|
|
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == ioctl(device->fd, VIDIOC_DQBUF, &buf)) {
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
continue;
|
2015-11-04 05:04:05 +01:00
|
|
|
}
|
2015-07-19 16:51:35 +02:00
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
void *data = (void *)device->buffers[buf.index].start;
|
2015-07-30 08:49:27 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Convert frame image data to YUV420 for ToxAV */
|
2015-08-06 06:26:45 +02:00
|
|
|
yuv422to420(y, u, v, data, video_width, video_height);
|
|
|
|
|
2015-08-20 21:23:01 +02:00
|
|
|
#endif
|
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Send frame data to friend through ToxAV */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (device->cb) {
|
2015-08-06 06:26:45 +02:00
|
|
|
device->cb(video_width, video_height, y, u, v, device->cb_data);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-30 09:37:31 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Convert YUV420 data to BGR */
|
2015-07-30 08:49:27 +02:00
|
|
|
uint8_t *img_data = malloc(video_width * video_height * 4);
|
2015-11-04 05:04:05 +01:00
|
|
|
yuv420tobgr(video_width, video_height, y, u, v,
|
2016-09-25 03:07:04 +02:00
|
|
|
video_width, video_width / 2, video_width / 2, img_data);
|
2015-08-10 13:14:38 +02:00
|
|
|
|
|
|
|
/* Allocate image data in X11 */
|
2015-07-30 08:49:27 +02:00
|
|
|
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,
|
2016-09-25 03:07:04 +02:00
|
|
|
.data = (char *)img_data
|
2015-07-30 08:49:27 +02:00
|
|
|
};
|
2015-08-06 06:26:45 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
/* Render image data */
|
2015-07-30 08:49:27 +02:00
|
|
|
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);
|
2015-08-06 06:26:45 +02:00
|
|
|
XFlush(device->x_display);
|
2015-08-07 18:40:35 +02:00
|
|
|
free(img_data);
|
2015-07-30 08:49:27 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if !(defined(__OSX__) || defined(__APPLE__))
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_QBUF, &buf)) {
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2017-11-16 22:59:51 +01:00
|
|
|
#endif
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-07-30 08:49:27 +02:00
|
|
|
usleep(1000 * 1000 / 24);
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (device_idx >= MAX_DEVICES) {
|
|
|
|
return vde_InvalidSelection;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
lock;
|
2015-08-09 04:21:23 +02:00
|
|
|
VideoDevice *device = video_devices_running[type][device_idx];
|
2015-07-19 16:51:35 +02:00
|
|
|
VideoDeviceError rc = vde_None;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!device) {
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
return vde_DeviceNotActive;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-22 20:06:14 +02:00
|
|
|
video_devices_running[type][device_idx] = NULL;
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!device->ref_count) {
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (type == vdt_input) {
|
2020-03-30 14:35:34 +02:00
|
|
|
#if defined(__OSX__) || defined(__APPLE__)
|
2017-11-15 21:02:10 +01:00
|
|
|
|
|
|
|
osx_video_close_device(device_idx);
|
2020-03-30 14:35:34 +02:00
|
|
|
#else /* not __OSX__ || __APPLE__ */
|
2015-08-06 06:26:45 +02:00
|
|
|
enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == xioctl(device->fd, VIDIOC_STREAMOFF, &buf_type)) {}
|
2015-08-06 06:26:45 +02:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
int i;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-10 13:14:38 +02:00
|
|
|
for (i = 0; i < device->n_buffers; ++i) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (-1 == munmap(device->buffers[i].start, device->buffers[i].length)) {
|
2015-08-09 04:21:23 +02:00
|
|
|
}
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-20 13:39:47 +02:00
|
|
|
close(device->fd);
|
|
|
|
|
2015-08-16 09:20:16 +02:00
|
|
|
#endif
|
2015-08-20 21:23:01 +02:00
|
|
|
vpx_img_free(&device->input);
|
2015-07-30 08:49:27 +02:00
|
|
|
XDestroyWindow(device->x_display, device->x_window);
|
2015-08-06 06:26:45 +02:00
|
|
|
XFlush(device->x_display);
|
2015-07-30 08:49:27 +02:00
|
|
|
XCloseDisplay(device->x_display);
|
2015-08-06 06:26:45 +02:00
|
|
|
pthread_mutex_destroy(device->mutex);
|
2015-08-20 13:39:47 +02:00
|
|
|
|
2020-03-30 14:35:34 +02:00
|
|
|
#if !(defined(__OSX__) || defined(__APPLE__))
|
2015-08-06 06:26:45 +02:00
|
|
|
free(device->buffers);
|
2020-03-30 14:35:34 +02:00
|
|
|
#endif /* not __OSX__ || __APPLE__ */
|
2015-08-20 21:23:01 +02:00
|
|
|
|
2015-08-06 06:26:45 +02:00
|
|
|
free(device);
|
2015-11-04 05:04:05 +01:00
|
|
|
} else {
|
2015-08-06 06:26:45 +02:00
|
|
|
vpx_img_free(&device->input);
|
|
|
|
XDestroyWindow(device->x_display, device->x_window);
|
|
|
|
XFlush(device->x_display);
|
|
|
|
XCloseDisplay(device->x_display);
|
|
|
|
pthread_mutex_destroy(device->mutex);
|
2015-11-04 05:04:05 +01:00
|
|
|
free(device);
|
2015-07-19 16:51:35 +02:00
|
|
|
}
|
2015-07-30 09:37:31 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
|
|
|
device->ref_count--;
|
|
|
|
}
|
2015-11-04 05:04:05 +01:00
|
|
|
|
2015-07-19 16:51:35 +02:00
|
|
|
unlock;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
void print_video_devices(ToxWindow *self, VideoDeviceType type)
|
2015-07-24 20:10:13 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (i = 0; i < size[type]; ++i) {
|
2015-08-07 18:40:35 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i, video_devices_names[type][i]);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-07-24 20:10:13 +02:00
|
|
|
|
|
|
|
return;
|
2015-08-10 13:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoDeviceError video_selection_valid(VideoDeviceType type, int32_t selection)
|
|
|
|
{
|
|
|
|
return (size[type] <= selection || selection < 0) ? vde_InvalidSelection : vde_None;
|
2015-11-04 05:04:05 +01:00
|
|
|
}
|
2018-07-06 17:45:35 +02:00
|
|
|
|
|
|
|
#endif /* VIDEO */
|