From 9863dfc2aefc1d4ce1dfdcd363d138a9b9e02fb8 Mon Sep 17 00:00:00 2001 From: cnhenry Date: Fri, 10 Jul 2015 20:54:06 -0500 Subject: [PATCH] Began implementing video_device.* --- cfg/checks/av.mk | 2 +- src/video_device.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ src/video_device.h | 58 ++++++++++++++++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/video_device.c diff --git a/cfg/checks/av.mk b/cfg/checks/av.mk index bad5ba0..2e4031e 100644 --- a/cfg/checks/av.mk +++ b/cfg/checks/av.mk @@ -8,7 +8,7 @@ else endif # Variables for video call support -VIDEO_LIBS = libtoxav opencv +VIDEO_LIBS = libtoxav xlib AUDIO_CFLAGS = -DVIDEO ifneq (, $(findstring video_device.o $(OBJ))) VIDEO_OBJ = video_call.o diff --git a/src/video_device.c b/src/video_device.c new file mode 100644 index 0000000..4384cf7 --- /dev/null +++ b/src/video_device.c @@ -0,0 +1,114 @@ +/* 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 . + * + */ + +#include "video_device.h" + +#ifdef VIDEO +#include "video_call.h" +#endif /* VIDEO */ + +#include "line_info.h" +#include "settings.h" + +#include +#include +#include +#include +#include +#include + +#define inline__ inline __attribute__((always_inline)) + +extern struct user_settings *user_settings; + +typedef struct VideoDevice { + //ALCdevice *dhndl; /* Handle of device selected/opened */ + //ALCcontext *ctx; /* Device context */ + DataHandleCallback 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 */ + + uint32_t source, buffers[OPENAL_BUFS]; /* Playback source/buffers */ + uint32_t ref_count; + int32_t selection; + pthread_mutex_t mutex[1]; + uint32_t sample_rate; + uint32_t frame_duration; +} VideoDevice; + +const char *ddevice_names[2]; /* Default device */ +const char *devices_names[2][MAX_DEVICES]; /* Container of available devices */ +static int size[2]; /* Size of above containers */ +Device *running[2][MAX_DEVICES] = {{NULL}}; /* Running devices */ +uint32_t primary_device[2]; /* Primary device */ + +#ifdef VIDEO +static ToxAV* av = NULL; +#endif /* VIDEO */ + +/* q_mutex */ +#define lock pthread_mutex_lock(&mutex); +#define unlock pthread_mutex_unlock(&mutex); +pthread_mutext_t mutex; + +bool thread_running = true, + thread_paused = true; /* Thread control */ + +void* thread_poll(void*); +/* Meet devices */ +#ifndef VIDEO +VideoDeviceError init_video_devices(ToxAV* av_) +#else +#endif /* VIDEO */ +{ + const char *stringed_device_list; + + size[input] = 0; + + size[output] = 0; + + // Start poll thread + if (pthread_mutex_init(&mutex, NULL) != 0) + return vde_InternalError; + + pthread_t thread_id; + if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detatch(thread_id) != 0) + return vde_InternalError; + +#ifdef VIDEO + av = av_; +#endif /* VIDEO */ + + return (VideoDeviceError) vde_None; +} + +VideoDeviceError terminate_video_devices() +{ + /* Cleanup if needed */ + thread_running = false; + usleep(20000); + + if (pthread_mutex_destroy(&mutex) != 0) + return (VideoDeviceError) vde_InternalError; + + return (VideoDeviceError) vde_None; +} \ No newline at end of file diff --git a/src/video_device.h b/src/video_device.h index d062c85..1cd425e 100644 --- a/src/video_device.h +++ b/src/video_device.h @@ -18,4 +18,60 @@ * You should have received a copy of the GNU General Public License * along with Toxic. If not, see . * - */ \ No newline at end of file + */ + +#ifndef VIDEO_DEVICE_H +#define VIDEO_DEVICE_H + +#define MAX_DEVICES 32 +#include +#include "windows.h" + +typedef enum VideoDeviceType { + input, + output, +} VideoDeviceType; + +typedef enum VideoDeviceError { + vde_None, + vde_InternalError = -1, + vde_InvalidSelection = -2, + vde_FailedStart = -3, + vde_Busy = -4, + vde_AllDevicesBusy = -5, + vde_DeviceNotActive = -6, + vde_BufferError = -7, + vde_UnsupportedMode = -8, + vde_CaptureError = -9, +} VideoDeviceError; + +typedef void (*DataHandleCallback) (const int16_t*, uint32_t size, void* data); + + +#ifdef VIDEO +DeviceError init_video_devices(ToxAV* av); +#else +DeviceError init_video_devices(); +#endif /* VIDEO */ + +VideoDeviceError terminate_video_devices(); + +/* Callback handles ready data from INPUT device */ +VideoDeviceError register_video_device_callback(int32_t call_idx, uint32_t device_idx, DataHandleCallback callback, void* data); +void* get_video_device_callback_data(uint32_t device_idx); + +VideoDeviceError set_primary_video_device(VideoDeviceType type, int32_t selection); +VideoDeviceError open_primary_video_device(VideoDeviceType type, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels); +/* Start device */ +VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels); +/* Stop device */ +VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx); + +/* Write data to device */ +VideoDeviceError write_out(uint32_t device_idx, const int16_t* data, uint32_t length, uint8_t channels); + +void print_devices(ToxWindow* self, DeviceType type); +void get_primary_device_name(VideoDeviceType type, char *buf, int size); + +VideoDeviceError selection_valid(VideoDeviceType type, int32_t selection); +#endif /* VIDEO_DEVICE_H */ \ No newline at end of file