From 2369b5e9e289193aac3d691c0f2ac5cfadbb5c8f Mon Sep 17 00:00:00 2001 From: cnhenry Date: Wed, 22 Jul 2015 13:06:14 -0500 Subject: [PATCH] Modified header files structuring --- src/audio_call.c | 82 +++++++++++++--------------------------------- src/audio_call.h | 25 ++++++++++++++ src/video_call.c | 43 ++++++++++++++++++++++++ src/video_call.h | 3 +- src/video_device.c | 54 +++++++++++++++--------------- 5 files changed, 120 insertions(+), 87 deletions(-) diff --git a/src/audio_call.c b/src/audio_call.c index 10769d2..d6e6cec 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -51,8 +51,6 @@ #define cbend pthread_exit(NULL) -#define MAX_CALLS 10 - #define frame_size (CallContrl.audio_sample_rate * CallContrl.audio_frame_duration / 1000) static int set_call(Call* call, bool start) @@ -75,29 +73,6 @@ static int set_call(Call* call, bool start) return 0; } -typedef struct CallControl { - AudioError errors; - - ToxAV *av; - ToxWindow *window; - - Call calls[MAX_CALLS]; - bool pending_call; - uint32_t call_state; - - bool audio_enabled; - bool video_enabled; - uint32_t audio_bit_rate; - uint32_t video_bit_rate; - uint32_t audio_sample_rate; - uint32_t video_sample_rate; - int32_t audio_frame_duration; - int32_t video_frame_duration; - - uint8_t audio_channels; - -} CallControl; - CallControl CallContrl; void call_cb( ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data ); @@ -140,22 +115,30 @@ ToxAV *init_audio(ToxWindow *self, Tox *tox) CallContrl.errors = ae_None; CallContrl.window = self; - CallContrl.audio_enabled = true; - CallContrl.video_enabled = false; - CallContrl.audio_bit_rate = 48; - CallContrl.video_bit_rate = 0; - CallContrl.audio_sample_rate = 48000; - CallContrl.video_sample_rate = 0; - CallContrl.audio_frame_duration = 10; - CallContrl.video_frame_duration = 0; - CallContrl.audio_channels = 1; - - memset(CallContrl.calls, 0, sizeof(CallContrl.calls)); - - /* Streaming stuff from core */ - CallContrl.av = toxav_new(tox, &error); + CallContrl.audio_enabled = true; + CallContrl.audio_bit_rate = 48; + CallContrl.audio_sample_rate = 48000; + CallContrl.audio_frame_duration = 10; + CallContrl.audio_channels = 1; + +#ifdef VIDEO + if ( !init_video(self, tox, CallContrl.av, &CallContrl) ) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to init video"); + return NULL; + } + if (CallContrl.video_enabled == true) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video enabled"); + } +#else + CallContrl.video_enabled = false; + CallContrl.video_bit_rate = 0; + CallContrl.video_sample_rate = 0; + CallContrl.video_frame_duration = 0; +#endif /* VIDEO */ + + memset(CallContrl.calls, 0, sizeof(CallContrl.calls)); if ( !CallContrl.av ) { CallContrl.errors |= ae_StartingCoreAudio; @@ -191,7 +174,7 @@ void terminate_audio() terminate_devices(); } -void read_device_callback (const int16_t* captured, uint32_t size, void* data) +void read_device_callback(const int16_t* captured, uint32_t size, void* data) { TOXAV_ERR_SEND_FRAME error; int32_t friend_number = *((int32_t*)data); /* TODO: Or pass an array of call_idx's */ @@ -343,25 +326,6 @@ void audio_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, cc->audio_bit_rate = bit_rate; } -void receive_video_frame_cb(ToxAV *av, uint32_t friend_number, - uint16_t width, uint16_t height, - uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, - int32_t ystride, int32_t ustride, int32_t vstride, int32_t astride, - void *user_data) -{} - -void video_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, - bool stable, uint32_t bit_rate, void *user_data) -{ - CallControl* cc = user_data; - - if ( stable ) - cc->video_bit_rate = bit_rate; -} - - - - #define CB_BODY(friend_number, Arg, onFunc) do { ToxWindow* windows = (Arg); int i;\ for (i = 0; i < MAX_WINDOWS_NUM; ++i) if (windows[i].onFunc != NULL) windows[i].onFunc(&windows[i], CallContrl.av, friend_number); } while (0) diff --git a/src/audio_call.h b/src/audio_call.h index 7f3b70a..5d95314 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -26,6 +26,8 @@ #include #include "audio_device.h" + +#define MAX_CALLS 10 typedef enum _AudioError { ae_None = 0, @@ -41,6 +43,29 @@ typedef struct Call { pthread_mutex_t mutex; } Call; +typedef struct CallControl { + AudioError errors; + + ToxAV *av; + ToxWindow *window; + + Call calls[MAX_CALLS]; + bool pending_call; + uint32_t call_state; + + bool audio_enabled; + bool video_enabled; + uint32_t audio_bit_rate; + uint32_t video_bit_rate; + uint32_t audio_sample_rate; + uint32_t video_sample_rate; + int32_t audio_frame_duration; + int32_t video_frame_duration; + + uint8_t audio_channels; + +} CallControl; + /* You will have to pass pointer to first member of 'windows' declared in windows.c */ ToxAV *init_audio(ToxWindow *self, Tox *tox); void terminate_audio(); diff --git a/src/video_call.c b/src/video_call.c index e69de29..e288950 100644 --- a/src/video_call.c +++ b/src/video_call.c @@ -0,0 +1,43 @@ +#include "toxic.h" +#include "windows.h" +#include "video_call.h" +#include "chat_commands.h" +#include "global_commands.h" +#include "line_info.h" +#include "notify.h" + +#include + +ToxAV *init_video(ToxWindow *self, Tox *tox, ToxAV *av, CallControl *user_data) +{ + Display *display; + if ((display = XOpenDisplay(NULL)) == NULL) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open X11 display"); + return NULL; + } + + user_data->video_enabled = true; + + //toxav_callback_video_receive_frame(CallContrl.av, receive_video_frame_cb, &CallContrl); + //toxav_callback_video_bit_rate_status(CallContrl.av, video_bit_rate_status_cb, &CallContrl); + + return av; +} + + + +void receive_video_frame_cb(ToxAV *av, uint32_t friend_number, + uint16_t width, uint16_t height, + uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, + int32_t ystride, int32_t ustride, int32_t vstride, int32_t astride, + void *user_data) +{} + +void video_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, + bool stable, uint32_t bit_rate, void *user_data) +{ + CallControl* cc = user_data; + + if ( stable ) + cc->video_bit_rate = bit_rate; +} \ No newline at end of file diff --git a/src/video_call.h b/src/video_call.h index c2b0e54..d3c9ce0 100644 --- a/src/video_call.h +++ b/src/video_call.h @@ -25,6 +25,7 @@ #include +#include "audio_call.h" #include "video_device.h" typedef enum _VideoError { @@ -35,7 +36,7 @@ typedef enum _VideoError { } VideoError; /* You will have to pass pointer to first member of 'windows' declared in windows.c */ -ToxAV *init_video(ToxWindow *self, Tox *tox); +ToxAV *init_video(ToxWindow *self, Tox *tox, ToxAV *av, CallControl *user_data); void terminate_video(); //int start_video_transmission(ToxWindow *self, Call *call); //int stop_video_transmission(Call *call, int friend_number); diff --git a/src/video_device.c b/src/video_device.c index 5e435e9..9fa7570 100644 --- a/src/video_device.c +++ b/src/video_device.c @@ -72,11 +72,11 @@ typedef struct VideoDevice { uint16_t video_height; } VideoDevice; -const char *ddevice_names[2]; /* Default device */ -const char *devices_names[2][MAX_DEVICES]; /* Container of available devices */ +const char *dvideo_device_names[2]; /* Default device */ +const char *video_devices_names[2][MAX_DEVICES]; /* Container of available devices */ static int size[2]; /* Size of above containers */ -VideoDevice *running[2][MAX_DEVICES] = {{NULL}}; /* Running devices */ -uint32_t primary_device[2]; /* Primary device */ +VideoDevice *video_devices_running[2][MAX_DEVICES] = {{NULL}}; /* Running devices */ +uint32_t primary_video_device[2]; /* Primary device */ #ifdef VIDEO static ToxAV* av = NULL; @@ -87,10 +87,10 @@ static ToxAV* av = NULL; #define unlock pthread_mutex_unlock(&mutex); pthread_mutex_t mutex; -bool thread_running = true, - thread_paused = true; /* Thread control */ +bool video_thread_running = true, + video_thread_paused = true; /* Thread control */ -void* thread_poll(void*); +void* video_thread_poll(void*); static int xioctl(int fh, unsigned long request, void *arg) { @@ -122,7 +122,7 @@ VideoDeviceError init_video_devices() if (fd == -1) break; else { - devices_names[input][i] = cap.card; + video_devices_names[input][i] = cap.card; } close(fd); @@ -139,7 +139,7 @@ VideoDeviceError init_video_devices() return vde_InternalError; pthread_t thread_id; - if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) + if ( pthread_create(&thread_id, NULL, video_thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) return vde_InternalError; #ifdef VIDEO @@ -152,7 +152,7 @@ VideoDeviceError init_video_devices() VideoDeviceError terminate_video_devices() { /* Cleanup if needed */ - thread_running = false; + video_thread_running = false; usleep(20000); if (pthread_mutex_destroy(&mutex) != 0) @@ -164,19 +164,19 @@ VideoDeviceError terminate_video_devices() VideoDeviceError set_primary_video_device(VideoDeviceType type, int32_t selection) { if (size[type] <= selection || selection < 0) return vde_InvalidSelection; - primary_device[type] = selection; + primary_video_device[type] = selection; return vde_None; } VideoDeviceError open_primary_video_device(VideoDeviceType type, uint32_t* device_idx) { - return open_video_device(type, primary_device[type], device_idx); + return open_video_device(type, primary_video_device[type], device_idx); } -void get_primary_device_name(VideoDeviceType type, char *buf, int size) +void get_primary_video_device_name(VideoDeviceType type, char *buf, int size) { - memcpy(buf, ddevice_names[type], size); + memcpy(buf, dvideo_device_names[type], size); } VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint32_t* device_idx) @@ -186,23 +186,23 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint lock; uint32_t i; - for (i = 0; i < MAX_DEVICES && running[type][i] != NULL; ++i); + for (i = 0; i < MAX_DEVICES && video_devices_running[type][i] != NULL; ++i); if (i == MAX_DEVICES) { unlock; return vde_AllDevicesBusy; } else *device_idx = i; for (i = 0; i < MAX_DEVICES; i ++) { /* Check if any device has the same selection */ - if ( running[type][i] && running[type][i]->selection == selection ) { + if ( video_devices_running[type][i] && video_devices_running[type][i]->selection == selection ) { - running[type][*device_idx] = running[type][i]; - running[type][i]->ref_count ++; + video_devices_running[type][*device_idx] = video_devices_running[type][i]; + video_devices_running[type][i]->ref_count ++; unlock; return vde_None; } } - VideoDevice* device = running[type][*device_idx] = calloc(1, sizeof(VideoDevice)); + VideoDevice* device = video_devices_running[type][*device_idx] = calloc(1, sizeof(VideoDevice)); device->selection = selection; if (pthread_mutex_init(device->mutex, NULL) != 0) { @@ -308,14 +308,14 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint /*TODO: Add OSX implementation of opening video devices */ - thread_paused = false; + video_thread_paused = false; } unlock; return vde_None; } -void* thread_poll (void* arg) // TODO: maybe use thread for every input source +void* video_thread_poll (void* arg) // TODO: maybe use thread for every input source { /* * NOTE: We only need to poll input devices for data. @@ -324,17 +324,17 @@ void* thread_poll (void* arg) // TODO: maybe use thread for every input source uint32_t i; - while (thread_running) + while (video_thread_running) { - if (thread_paused) usleep(10000); /* Wait for unpause. */ + if (video_thread_paused) usleep(10000); /* Wait for unpause. */ else { for (i = 0; i < size[input]; ++i) { lock; - if (running[input][i] != NULL) + if (video_devices_running[input][i] != NULL) { - VideoDevice* device = running[input][i]; + VideoDevice* device = video_devices_running[input][i]; struct v4l2_buffer buf; memset(&(buf), 0, sizeof(buf)); @@ -373,7 +373,7 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx) if (device_idx >= MAX_DEVICES) return vde_InvalidSelection; lock; - VideoDevice* device = running[type][device_idx]; + VideoDevice* device = video_devices_running[type][device_idx]; VideoDeviceError rc = vde_None; if (!device) { @@ -381,7 +381,7 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx) return vde_DeviceNotActive; } - running[type][device_idx] = NULL; + video_devices_running[type][device_idx] = NULL; if ( !device->ref_count ) {