diff --git a/src/audio_call.c b/src/audio_call.c index e06e398..6bcafc1 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -61,6 +61,10 @@ static int set_call(Call* call, bool start) { call->in_idx = -1; call->out_idx = -1; +#ifdef VIDEO + call->vin_idx = -1; + call->vin_idx = -1; +#endif /* VIDEO */ if ( start ) { call->ttas = true; @@ -77,18 +81,17 @@ static int set_call(Call* call, bool start) return 0; } -void call_cb( ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data ); -void callstate_cb( ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data ); -void receive_audio_frame_cb( ToxAV *av, uint32_t friend_number, - int16_t const *pcm, size_t sample_count, - uint8_t channels, uint32_t sampling_rate, void *user_data ); +void call_cb ( ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data ); +void callstate_cb ( ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data ); +void receive_audio_frame_cb ( ToxAV *av, uint32_t friend_number, int16_t const *pcm, size_t sample_count, + uint8_t channels, uint32_t sampling_rate, void *user_data ); void audio_bit_rate_status_cb( ToxAV *av, uint32_t friend_number, - bool stable, uint32_t bit_rate, void *user_data ); -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 ); + bool stable, uint32_t bit_rate, void *user_data ); +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); @@ -114,7 +117,7 @@ static void print_err (ToxWindow *self, const char *error_str) ToxAV *init_audio(ToxWindow *self, Tox *tox) { TOXAV_ERR_NEW error; - CallControl.errors = ae_None; + CallControl.audio_errors = ae_None; CallControl.prompt = self; CallControl.pending_call = false; @@ -126,14 +129,16 @@ ToxAV *init_audio(ToxWindow *self, Tox *tox) CallControl.audio_frame_duration = 10; CallControl.audio_channels = 1; +#ifndef VIDEO CallControl.video_enabled = false; CallControl.video_bit_rate = 0; CallControl.video_frame_duration = 0; +#endif /* VIDEO */ memset(CallControl.calls, 0, sizeof(CallControl.calls)); if ( !CallControl.av ) { - CallControl.errors |= ae_StartingCoreAudio; + CallControl.audio_errors |= ae_StartingCoreAudio; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to init ToxAV"); return NULL; @@ -274,6 +279,11 @@ void callstate_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_ switch ( state ) { case ( TOXAV_FRIEND_CALL_STATE_ERROR ): line_info_add(windows, NULL, NULL, NULL, SYS_MSG, 0, 0, "ToxAV callstate error!"); + +#ifdef VIDEO + callback_video_end(friend_number); +#endif /* VIDEO */ + stop_transmission(&CallControl.calls[friend_number], friend_number); callback_call_ended(friend_number); CallControl.pending_call = false; @@ -285,32 +295,39 @@ void callstate_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_ else callback_call_ended(friend_number); +#ifdef VIDEO + callback_video_end(friend_number); +#endif /* VIDEO */ + stop_transmission(&CallControl.calls[friend_number], friend_number); - /* Reset call state after finishing */ + /* Reset stored call state after finishing */ CallControl.call_state = 0; CallControl.pending_call = false; break; - default: /* Start answered call */ - callback_call_started(friend_number); - CallControl.pending_call = false; + default: + if ( CallControl.pending_call ) { + /* Start answered call */ + callback_call_started(friend_number); + CallControl.pending_call = false; + + } else { +#ifdef VIDEO + /* Handle receiving client video call states */ + if ( state & ~TOXAV_FRIEND_CALL_STATE_SENDING_V ) + callback_recv_video_end(friend_number); + + if ( state & ~(TOXAV_FRIEND_CALL_STATE_ACCEPTING_V & TOXAV_FRIEND_CALL_STATE_SENDING_V) + && CallControl.video_call != vs_Send ) + CallControl.video_call = vs_None; + +#endif /* VIDEO */ + } break; } -#ifdef VIDEO - - /* Handle receiving client video call states */ - //if (state & ~TOXAV_FRIEND_CALL_STATE_SENDING_V) { - // callback_recv_video_end(CallControl.av, friend_number, &CallControl); - //} - - //if (state & ~(TOXAV_FRIEND_CALL_STATE_ACCEPTING_V & TOXAV_FRIEND_CALL_STATE_SENDING_V)) { - // CallControl.video_call = false; - //} - -#endif /* VIDEO */ } void receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, @@ -329,17 +346,18 @@ void audio_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, #define CB_BODY(friend_number, onFunc) do { ToxWindow* windows = CallControl.prompt; int i;\ -for (i = 0; i < MAX_WINDOWS_NUM; ++i) if ( windows[i].onFunc != NULL ) windows[i].onFunc(&windows[i], CallControl.av, friend_number, CallControl.call_state); } while (0) +for (i = 0; i < MAX_WINDOWS_NUM; ++i) if ( windows[i].onFunc != NULL && windows[i].num == friend_number )\ +windows[i].onFunc(&windows[i], CallControl.av, friend_number, CallControl.call_state); } while (0) -void callback_recv_invite ( uint32_t friend_number ) +void callback_recv_invite(uint32_t friend_number) { CB_BODY(friend_number, onInvite); } -void callback_recv_ringing ( uint32_t friend_number ) +void callback_recv_ringing(uint32_t friend_number) { CB_BODY(friend_number, onRinging); } -void callback_recv_starting ( uint32_t friend_number ) +void callback_recv_starting(uint32_t friend_number) { ToxWindow* windows = CallControl.prompt; @@ -354,11 +372,11 @@ void callback_recv_starting ( uint32_t friend_number ) } } } -void callback_recv_ending ( uint32_t friend_number ) +void callback_recv_ending(uint32_t friend_number) { CB_BODY(friend_number, onEnding); } -void callback_call_started ( uint32_t friend_number ) +void callback_call_started(uint32_t friend_number) { ToxWindow* windows = CallControl.prompt; @@ -372,26 +390,27 @@ void callback_call_started ( uint32_t friend_number ) } } } -void callback_call_canceled ( uint32_t friend_number ) +void callback_call_canceled(uint32_t friend_number) { CB_BODY(friend_number, onCancel); } -void callback_call_rejected ( uint32_t friend_number ) +void callback_call_rejected(uint32_t friend_number) { CB_BODY(friend_number, onReject); } -void callback_call_ended ( uint32_t friend_number ) +void callback_call_ended(uint32_t friend_number) { CB_BODY(friend_number, onEnd); } -void callback_requ_timeout ( uint32_t friend_number ) +void callback_requ_timeout(uint32_t friend_number) { CB_BODY(friend_number, onRequestTimeout); } -void callback_peer_timeout ( uint32_t friend_number ) +void callback_peer_timeout(uint32_t friend_number) { CB_BODY(friend_number, onPeerTimeout); + callback_video_end(friend_number); stop_transmission(&CallControl.calls[friend_number], friend_number); /* Call is stopped manually since there might be some other * actions that one can possibly take on timeout @@ -542,7 +561,7 @@ void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ } #ifdef VIDEO - callback_video_end(CallControl.av, self->num, &CallControl); + callback_video_end(self->num); #endif /* VIDEO */ diff --git a/src/audio_call.h b/src/audio_call.h index 8203423..e4651e4 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -36,32 +36,59 @@ typedef enum _AudioError { ae_StartingCoreAudio = 1 << 2 } AudioError; +#ifdef VIDEO +typedef enum _VideoError { + ve_None = 0, + ve_StartingCaptureDevice = 1 << 0, + ve_StartingOutputDevice = 1 << 1, + ve_StartingCoreVideo = 1 << 2 +} VideoError; + +typedef enum _VideoState { + vs_None = 0, + vs_Send = 1 << 0, + vs_Receive = 1 << 1, + vs_SendReceive = 1 << 2 +} VideoState; + +#endif /* VIDEO */ + typedef struct Call { pthread_t ttid; /* Transmission thread id */ bool ttas, has_output; /* Transmission thread active status (0 - stopped, 1- running) */ - uint32_t in_idx, out_idx; + uint32_t in_idx, out_idx; /* Audio Index */ +#ifdef VIDEO + uint32_t vin_idx, vout_idx; /* Video Index */ +#endif /* VIDEO */ pthread_mutex_t mutex; } Call; struct CallControl { - AudioError errors; + AudioError audio_errors; +#ifdef VIDEO + VideoError video_errors; +#endif /* VIDEO */ ToxAV *av; ToxWindow *prompt; Call calls[MAX_CALLS]; - bool pending_call; - bool video_call; uint32_t call_state; - + bool pending_call; bool audio_enabled; bool video_enabled; + uint32_t audio_bit_rate; - uint32_t video_bit_rate; int32_t audio_frame_duration; - int32_t video_frame_duration; uint32_t audio_sample_rate; uint8_t audio_channels; + +#ifdef VIDEO + uint32_t video_bit_rate; + int32_t video_frame_duration; + VideoState video_call; + +#endif /* VIDEO */ } CallControl; diff --git a/src/help.c b/src/help.c index 171bc9c..8173082 100644 --- a/src/help.c +++ b/src/help.c @@ -217,7 +217,7 @@ static void help_draw_chat(ToxWindow *self) wprintw(win, "\n Video:\n"); wattroff(win, A_BOLD); wprintw(win, " /video : Send video capture\n"); - wprintw(win, " /endvideo : Close all video windows\n"); + wprintw(win, " /endvideo : End video capture\n"); #endif /* VIDEO */ help_draw_bottom_menu(win); diff --git a/src/toxic.c b/src/toxic.c index c333266..654ca26 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1146,7 +1146,7 @@ int main(int argc, char *argv[]) av = init_audio(prompt, m); #ifdef VIDEO - init_video(prompt, m, av); + init_video(prompt, m); #endif /* VIDEO */ diff --git a/src/video_call.c b/src/video_call.c index d5ead8f..cdf216b 100644 --- a/src/video_call.c +++ b/src/video_call.c @@ -1,3 +1,25 @@ +/* video_call.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 "toxic.h" #include "windows.h" #include "video_call.h" @@ -28,28 +50,31 @@ static void print_err (ToxWindow *self, const char *error_str) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } -ToxAV *init_video(ToxWindow *self, Tox *tox, ToxAV *av) +ToxAV *init_video(ToxWindow *self, Tox *tox) { + CallControl.video_errors = ve_None; + CallControl.video_enabled = true; CallControl.video_bit_rate = 5000; CallControl.video_frame_duration = 10; + CallControl.video_call = vs_None; - if ( !av ) { + if ( !CallControl.av ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video failed to init with ToxAV instance"); return NULL; } - if ( init_video_devices(av) == vde_InternalError ) { + if ( init_video_devices(CallControl.av) == vde_InternalError ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to init video devices"); return NULL; } - toxav_callback_video_receive_frame(av, receive_video_frame_cb, &CallControl); - toxav_callback_video_bit_rate_status(av, video_bit_rate_status_cb, &CallControl); + toxav_callback_video_receive_frame(CallControl.av, receive_video_frame_cb, &CallControl); + toxav_callback_video_bit_rate_status(CallControl.av, video_bit_rate_status_cb, &CallControl); - return av; + return CallControl.av; } void terminate_video() @@ -70,11 +95,9 @@ void read_video_device_callback(int16_t width, int16_t height, const uint8_t* y, line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to send video frame"); if ( error == TOXAV_ERR_SEND_FRAME_NULL ) - line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error NULL video frame"); - else if ( error == TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND ) - line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error friend not found"); + line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to capture video frame"); else if ( error == TOXAV_ERR_SEND_FRAME_INVALID ) - line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error invalid video frame"); + line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video frame"); } } @@ -83,8 +106,8 @@ void write_video_device_callback(uint32_t friend_number, uint16_t width, uint16_ int32_t ystride, int32_t ustride, int32_t vstride, void *user_data) { - if (write_video_out(width, height, y, u, v, ystride, ustride, vstride, user_data) == vde_DeviceNotActive) - callback_recv_video_starting(CallControl.av, friend_number, &CallControl); + if (write_video_out(width, height, y, u, v, ystride, ustride, vstride, user_data) == vde_DeviceNotActive) + callback_recv_video_starting(friend_number); } int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call) @@ -94,17 +117,17 @@ int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call) return -1; } - if (toxav_video_bit_rate_set(CallControl.av, self->num, CallControl.video_bit_rate, false, NULL)) { + if (toxav_video_bit_rate_set(CallControl.av, self->num, CallControl.video_bit_rate, true, NULL) == false) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set video bit rate"); return -1; } - if ( open_primary_video_device(vdt_input, &call->in_idx) != vde_None ) { + if ( open_primary_video_device(vdt_input, &call->vin_idx) != vde_None ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input video device!"); return -1; } - if ( register_video_device_callback(self->num, call->in_idx, read_video_device_callback, &self->num) != vde_None) + if ( register_video_device_callback(self->num, call->vin_idx, read_video_device_callback, &self->num) != vde_None) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to register input video handler!"); return 0; @@ -116,11 +139,11 @@ int stop_video_transmission(Call *call, int friend_number) return -1; } - if ( call->in_idx != -1 ) - close_video_device(vdt_input, call->in_idx); + if ( call->vin_idx != -1 ) + close_video_device(vdt_input, call->vin_idx); - if ( call->out_idx != -1 ) - close_video_device(vdt_output, call->out_idx); + if ( call->vout_idx != -1 ) + close_video_device(vdt_output, call->vout_idx); return 0; } @@ -153,21 +176,36 @@ void video_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, } } -void callback_recv_video_starting(void* av, uint32_t friend_number, void *arg) +void callback_recv_video_starting(uint32_t friend_number) { Call* this_call = &CallControl.calls[friend_number]; - open_primary_video_device(vdt_output, &this_call->out_idx); - CallControl.video_call = true; + if ( this_call->vout_idx == -1 ) + return; + + open_primary_video_device(vdt_output, &this_call->vout_idx); + + if ( CallControl.video_call == vs_Send ) + CallControl.video_call = vs_SendReceive; + else + CallControl.video_call = vs_Receive; + + //line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "%i recv start", CallControl.video_call); } -void callback_recv_video_end(void* av, uint32_t friend_number, void *arg) +void callback_recv_video_end(uint32_t friend_number) { Call* this_call = &CallControl.calls[friend_number]; - if(this_call->out_idx != -1) - close_video_device(vdt_output, this_call->out_idx); + close_video_device(vdt_output, this_call->vout_idx); + + if ( CallControl.video_call == vs_SendReceive ) + CallControl.video_call = vs_Send; + else + CallControl.video_call = vs_None; + + //line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "%i recv end", CallControl.video_call); } -void callback_video_starting(void* av, uint32_t friend_number, void *arg) +void callback_video_starting(uint32_t friend_number) { ToxWindow* windows = CallControl.prompt; Call* this_call = &CallControl.calls[friend_number]; @@ -178,34 +216,45 @@ void callback_video_starting(void* av, uint32_t friend_number, void *arg) if (error == TOXAV_ERR_CALL_CONTROL_OK) { int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].is_call && windows[i].num == friend_number) { + if ( windows[i].is_call && windows[i].num == friend_number ) { if(0 != start_video_transmission(&windows[i], CallControl.av, this_call)) { line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!"); return; } - CallControl.video_call = true; line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting."); + + if ( CallControl.video_call == vs_Receive ) + CallControl.video_call = vs_SendReceive; + else + CallControl.video_call = vs_Send; + + //line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "%i start", CallControl.video_call); } } } } -void callback_video_end(void* av, uint32_t friend_number, void *arg) +void callback_video_end(uint32_t friend_number) { ToxWindow* windows = CallControl.prompt; - if ( CallControl.video_call ) { - toxav_video_bit_rate_set(CallControl.av, friend_number, 0, true, NULL); - + if ( CallControl.video_call != vs_None ) { int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) - if (windows[i].is_call && windows[i].num == friend_number) - line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video call ending."); + if ( windows[i].is_call && windows[i].num == friend_number ) + line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture ending."); + + toxav_video_bit_rate_set(CallControl.av, friend_number, 0, true, NULL); for (i = 0; i < MAX_CALLS; ++i) stop_video_transmission(&CallControl.calls[i], i); - CallControl.video_call = false; + if ( CallControl.video_call == vs_SendReceive ) + CallControl.video_call = vs_Receive; + else + CallControl.video_call = vs_None; + + //line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "%i end", CallControl.video_call); } } /* @@ -241,17 +290,12 @@ void cmd_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M goto on_error; } - if ( CallControl.video_call ) { + if ( CallControl.video_call == vs_Send || CallControl.video_call == vs_SendReceive ) { error_str = "Video is already sending in this call."; goto on_error; } - if ( toxav_video_bit_rate_set(CallControl.av, self->num, CallControl.video_bit_rate, false, NULL) == false ) { - error_str = "ToxAV video bit rate uninitialized."; - goto on_error; - } - - callback_video_starting(CallControl.av, self->num, &CallControl); + callback_video_starting(self->num); return; on_error: @@ -272,12 +316,12 @@ void cmd_end_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg goto on_error; } - if ( !CallControl.video_call ) { + if ( CallControl.video_call == vs_None ) { error_str = "Video is not running in this call."; goto on_error; } - callback_video_end(CallControl.av, self->num, &CallControl); + callback_video_end(self->num); return; on_error: @@ -408,9 +452,9 @@ void cmd_ccur_video_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, ch } else { /* TODO: check for failure */ - close_video_device(input, this_call->in_idx); - open_video_device(input, selection, &this_call->in_idx); - register_video_device_callback(self->num, this_call->in_idx, read_video_device_callback, &self->num); + close_video_device(input, this_call->vin_idx); + open_video_device(input, selection, &this_call->vin_idx); + register_video_device_callback(self->num, this_call->vin_idx, read_video_device_callback, &self->num); } } } diff --git a/src/video_call.h b/src/video_call.h index 4333227..0747287 100644 --- a/src/video_call.h +++ b/src/video_call.h @@ -19,6 +19,27 @@ * along with Toxic. If not, see . * */ +/* video_call.h + * + * + * 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 . + * + */ #ifndef VIDEO_CALL_H #define VIDEO_CALL_H @@ -29,21 +50,15 @@ #include "video_device.h" -typedef enum _VideoError { - ve_None = 0, - ve_StartingCaptureDevice = 1 << 0, - ve_StartingOutputDevice = 1 << 1, - ve_StartingCoreAudio = 1 << 2 -} VideoError; - /* You will have to pass pointer to first member of 'windows' declared in windows.c */ -ToxAV *init_video(ToxWindow *self, Tox *tox, ToxAV *av); +ToxAV *init_video(ToxWindow *self, Tox *tox); void terminate_video(); int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call); int stop_video_transmission(Call *call, int friend_number); -void callback_recv_video_starting(void* av, uint32_t friend_number, void *arg); -void callback_video_starting( void* av, uint32_t friend_number, void *arg ); -void callback_video_end( void* av, uint32_t friend_number, void *arg ); +void callback_recv_video_starting(uint32_t friend_number); +void callback_recv_video_end(uint32_t friend_number); +void callback_video_starting(uint32_t friend_number); +void callback_video_end(uint32_t friend_number); #endif /* VIDEO_CALL_H */ \ No newline at end of file diff --git a/src/video_device.c b/src/video_device.c index 15ed17b..6bc1cd3 100644 --- a/src/video_device.c +++ b/src/video_device.c @@ -186,7 +186,7 @@ VideoDeviceError init_video_devices() /* Query V4L for capture capabilities */ if ( ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1 ) { - video_input_name = cap.card; + video_input_name = &cap.card; } else { video_input_name = device_address; } @@ -311,6 +311,9 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint /* Obtain video device capabilities */ struct v4l2_capability cap; if ( -1 == xioctl(device->fd, VIDIOC_QUERYCAP, &cap) ) { + close(device->fd); + free(device); + return vde_FailedStart; } @@ -321,6 +324,9 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; if( -1 == xioctl(device->fd, VIDIOC_G_FMT, &fmt) ) { + close(device->fd); + free(device); + return vde_FailedStart; } @@ -334,10 +340,16 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; if ( -1 == xioctl(device->fd, VIDIOC_REQBUFS, &req) ) { + close(device->fd); + free(device); + return vde_FailedStart; } if ( req.count < 2 ) { + close(device->fd); + free(device); + return vde_FailedStart; } @@ -352,6 +364,9 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint buf.index = i; if ( -1 == xioctl(device->fd, VIDIOC_QUERYBUF, &buf) ) { + close(device->fd); + free(device); + return vde_FailedStart; } @@ -363,6 +378,11 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint device->fd, buf.m.offset); if ( MAP_FAILED == device->buffers[i].start ) { + for (i = 0; i < buf.index; ++i) + munmap(device->buffers[i].start, device->buffers[i].length); + close(device->fd); + free(device); + return vde_FailedStart; } } @@ -379,6 +399,11 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint buf.index = i; if ( -1 == xioctl(device->fd, VIDIOC_QBUF, &buf) ) { + for (i = 0; i < device->n_buffers; ++i) + munmap(device->buffers[i].start, device->buffers[i].length); + close(device->fd); + free(device); + return vde_FailedStart; } } @@ -387,6 +412,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint /* Turn on video stream */ if ( -1 == xioctl(device->fd, VIDIOC_STREAMON, &type) ) { + close_video_device(vdt_input, *device_idx); + return vde_FailedStart; } @@ -397,6 +424,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint /* Create X11 window associated to device */ if ( (device->x_display = XOpenDisplay(NULL)) == NULL ) { + close_video_device(vdt_input, *device_idx); + return vde_FailedStart; } @@ -405,6 +434,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint 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))) ) { + close_video_device(vdt_input, *device_idx); + return vde_FailedStart; } @@ -412,6 +443,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint XSelectInput(device->x_display, device->x_window, ExposureMask|ButtonPressMask|KeyPressMask); if ( (device->x_gc = DefaultGC(device->x_display, screen)) == NULL ) { + close_video_device(vdt_input, *device_idx); + return vde_FailedStart; } @@ -427,6 +460,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint /* Create X11 window associated to device */ if ( (device->x_display = XOpenDisplay(NULL)) == NULL ) { + close_video_device(vdt_output, *device_idx); + return vde_FailedStart; } @@ -434,6 +469,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint 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))) ) { + close_video_device(vdt_output, *device_idx); + return vde_FailedStart; } @@ -441,6 +478,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint XSelectInput(device->x_display, device->x_window, ExposureMask|ButtonPressMask|KeyPressMask); if ( (device->x_gc = DefaultGC(device->x_display, screen)) == NULL ) { + close_video_device(vdt_output, *device_idx); + return vde_FailedStart; } @@ -465,12 +504,15 @@ __inline VideoDeviceError write_video_out(uint16_t width, uint16_t height, if ( !device ) return vde_DeviceNotActive; + if( !device->x_window ) return vde_DeviceNotActive; + pthread_mutex_lock(device->mutex); - int screen = DefaultScreen(device->x_display); + /* Recreate missing X11 window */ - if ( !device->x_window ) { + /*if ( !device->x_window ) { + int screen = DefaultScreen(device->x_display); 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)); @@ -479,7 +521,7 @@ __inline VideoDeviceError write_video_out(uint16_t width, uint16_t height, XMapRaised(device->x_display, device->x_window); XFlush(device->x_display); - } + }*/ /* Resize X11 window to correct size */ if ( device->video_width != width || device->video_height != height ) { diff --git a/src/video_device.h b/src/video_device.h index c97716e..c32cffc 100644 --- a/src/video_device.h +++ b/src/video_device.h @@ -47,7 +47,6 @@ typedef enum VideoDeviceError { typedef void (*VideoDataHandleCallback) (int16_t width, int16_t height, const uint8_t* y, const uint8_t* u, const uint8_t* v, void* data); - #ifdef VIDEO VideoDeviceError init_video_devices(ToxAV* av); #else