1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 20:53:01 +01:00

Fixed YUV stride issues

This commit is contained in:
cnhenry 2015-08-07 11:40:35 -05:00
parent 54e2fe8d6f
commit d359ba6a54
5 changed files with 85 additions and 109 deletions

View File

@ -308,32 +308,6 @@ void callstate_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_
callback_call_started(av, friend_number, &CallContrl); callback_call_started(av, friend_number, &CallContrl);
} }
} }
#ifdef VIDEO
Call* this_call = &CallContrl.calls[friend_number];
VideoDeviceError error;
if ( state & TOXAV_FRIEND_CALL_STATE_RECEIVING_V ) {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Receiving video frames");
error = open_primary_video_device(vdt_output, &this_call->out_idx);
} else {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "No longer receiving video frames");
error = close_video_device(vdt_output, &this_call->out_idx);
}
if ( error == vde_FailedStart)
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to start input video device");
if ( error == vde_InternalError )
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Internal error with opening input video device");
if ( error != vde_None )
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open output video device!");
if ( state & TOXAV_FRIEND_CALL_STATE_SENDING_V ) {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending video frames");
}
#endif /* VIDEO */
} }
void receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, void receive_audio_frame_cb(ToxAV *av, uint32_t friend_number,
@ -411,6 +385,10 @@ void callback_recv_ending ( void* av, uint32_t friend_number, void* arg )
} }
stop_transmission(&CallContrl.calls[friend_number], friend_number); stop_transmission(&CallContrl.calls[friend_number], friend_number);
#ifdef VIDEO
callback_video_ending(av, friend_number, &CallContrl);
#endif /* VIDEO */
} }
void callback_call_started ( void* av, uint32_t friend_number, void* arg ) void callback_call_started ( void* av, uint32_t friend_number, void* arg )
{ {
@ -441,6 +419,9 @@ void callback_call_canceled ( void* av, uint32_t friend_number, void* arg )
/* In case call is active */ /* In case call is active */
stop_transmission(&CallContrl.calls[friend_number], friend_number); stop_transmission(&CallContrl.calls[friend_number], friend_number);
#ifdef VIDEO
callback_video_ending(av, friend_number, &CallContrl);
#endif /* VIDEO */
} }
void callback_call_rejected ( void* av, uint32_t friend_number, void* arg ) void callback_call_rejected ( void* av, uint32_t friend_number, void* arg )
{ {
@ -469,6 +450,9 @@ void callback_call_ended ( void* av, uint32_t friend_number, void* arg )
} }
stop_transmission(&CallContrl.calls[friend_number], friend_number); stop_transmission(&CallContrl.calls[friend_number], friend_number);
#ifdef VIDEO
callback_video_ending(av, friend_number, &CallContrl);
#endif /* VIDEO */
} }
void callback_requ_timeout ( void* av, uint32_t friend_number, void* arg ) void callback_requ_timeout ( void* av, uint32_t friend_number, void* arg )

View File

@ -51,6 +51,7 @@ typedef struct CallControl {
Call calls[MAX_CALLS]; Call calls[MAX_CALLS];
bool pending_call; bool pending_call;
bool video_call;
uint32_t call_state; uint32_t call_state;
bool audio_enabled; bool audio_enabled;

View File

@ -11,12 +11,13 @@
void receive_video_frame_cb( ToxAV *av, uint32_t friend_number, void receive_video_frame_cb( ToxAV *av, uint32_t friend_number,
uint16_t width, uint16_t height, uint16_t width, uint16_t height,
uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, uint8_t const *y, uint8_t const *u, uint8_t const *v,
int32_t ystride, int32_t ustride, int32_t vstride, int32_t astride, int32_t ystride, int32_t ustride, int32_t vstride,
void *user_data ); void *user_data );
void video_bit_rate_status_cb( ToxAV *av, uint32_t friend_number, void video_bit_rate_status_cb( ToxAV *av, uint32_t friend_number,
bool stable, uint32_t bit_rate, void *user_data); bool stable, uint32_t bit_rate, void *user_data);
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_starting ( void* av, uint32_t friend_number, void *arg );
void callback_video_ending ( void* av, uint32_t friend_number, void *arg ); void callback_video_ending ( void* av, uint32_t friend_number, void *arg );
@ -69,17 +70,21 @@ void read_video_device_callback(int16_t width, int16_t height, const uint8_t* y,
} else if ( error == TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND ) { } else if ( error == TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND ) {
line_info_add(CallContrl.window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error friend not found"); line_info_add(CallContrl.window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error friend not found");
} else if ( error = TOXAV_ERR_SEND_FRAME_INVALID ) { } else if ( error = TOXAV_ERR_SEND_FRAME_INVALID ) {
line_info_add(CallContrl.window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error invalid video frame %i, %i, %i", width, height, friend_number); line_info_add(CallContrl.window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error invalid video frame");
} }
} }
} }
void write_video_device_callback(uint16_t width, uint16_t height, void write_video_device_callback(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 *y, uint8_t const *u, uint8_t const *v,
int32_t ystride, int32_t ustride, int32_t vstride, int32_t ystride, int32_t ustride, int32_t vstride,
void *user_data) void *user_data)
{ {
write_video_out(width, height, y, u, v, ystride, ustride, vstride, user_data); CallControl* cc = (CallControl*)user_data;
Call* this_call = &cc->calls[friend_number];
if(write_video_out(width, height, y, u, v, ystride, ustride, vstride, user_data) == vde_DeviceNotActive)
callback_recv_video_starting(cc->av, friend_number, cc);
} }
int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call) int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call)
@ -89,17 +94,10 @@ int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call)
return -1; return -1;
} }
VideoDeviceError error = open_primary_video_device(vdt_input, &call->in_idx); if ( open_primary_video_device(vdt_input, &call->in_idx) != vde_None ) {
if ( error == vde_FailedStart)
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to start input video device");
if ( error == vde_InternalError )
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Internal error with opening input video device");
if ( error != vde_None )
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input video device!"); 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, if ( register_video_device_callback(self->num, call->in_idx,
read_video_device_callback, &self->num) != vde_None) read_video_device_callback, &self->num) != vde_None)
@ -110,13 +108,11 @@ int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call)
int stop_video_transmission(Call *call, int friend_number) int stop_video_transmission(Call *call, int friend_number)
{ {
if ( call->ttas ) { if ( call->in_idx != -1 )
if ( call->in_idx != -1 ) close_video_device(vdt_input, call->in_idx);
close_video_device(vdt_input, call->in_idx); if ( call->out_idx != -1 )
return 0; close_video_device(vdt_output, call->out_idx);
} return 0;
return -1;
} }
/* /*
@ -130,65 +126,54 @@ int stop_video_transmission(Call *call, int friend_number)
/* /*
* Callbacks * Callbacks
*/ */
void receive_video_frame_cb(ToxAV *av, uint32_t friend_number, void receive_video_frame_cb(ToxAV *av, uint32_t friend_number,
uint16_t width, uint16_t height, uint16_t width, uint16_t height,
uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, uint8_t const *y, uint8_t const *u, uint8_t const *v,
int32_t ystride, int32_t ustride, int32_t vstride, int32_t astride, int32_t ystride, int32_t ustride, int32_t vstride,
void *user_data) void *user_data)
{ {
CallControl* cc = (CallControl*)user_data; CallControl* cc = (CallControl*)user_data;
ToxWindow* window = CallContrl.window;
Call* this_call = &CallContrl.calls[friend_number]; write_video_device_callback(friend_number, width, height, y, u, v, ystride, ustride, vstride, user_data);
VideoDeviceError error;
if ( CallContrl.call_state & TOXAV_FRIEND_CALL_STATE_SENDING_V ) {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Receiving video frames");
error = open_primary_video_device(vdt_output, &this_call->out_idx);
} else {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "No longer receiving video frames");
error = close_video_device(vdt_output, &this_call->out_idx);
}
if ( error == vde_FailedStart)
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to start input video device");
if ( error == vde_InternalError )
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Internal error with opening input video device");
if ( error != vde_None )
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open output video device!");
write_video_device_callback(width, height, y, u, v, ystride, ustride, vstride, user_data);
} }
void video_bit_rate_status_cb(ToxAV *av, uint32_t friend_number, void video_bit_rate_status_cb(ToxAV *av, uint32_t friend_number,
bool stable, uint32_t bit_rate, void *user_data) bool stable, uint32_t bit_rate, void *user_data)
{ {
CallControl* cc = user_data; CallControl* cc = (CallControl*)user_data;
if ( stable && CallContrl.call_state & TOXAV_FRIEND_CALL_STATE_SENDING_V ) { if ( stable ) {
cc->video_bit_rate = bit_rate; cc->video_bit_rate = bit_rate;
toxav_video_bit_rate_set(CallContrl.av, friend_number, CallContrl.video_bit_rate, false, NULL); toxav_video_bit_rate_set(CallContrl.av, friend_number, CallContrl.video_bit_rate, false, NULL);
} }
} }
void callback_recv_video_starting(void* av, uint32_t friend_number, void *arg)
{
CallControl *cc = (CallControl*)arg;
ToxWindow* windows = cc->window;
Call* this_call = &cc->calls[friend_number];
open_primary_video_device(vdt_output, &this_call->out_idx);
CallContrl.video_call = true;
}
void callback_video_starting(void* av, uint32_t friend_number, void *arg) void callback_video_starting(void* av, uint32_t friend_number, void *arg)
{ {
CallControl *cc = (CallControl*)arg; CallControl *cc = (CallControl*)arg;
ToxWindow* windows = cc->window; ToxWindow* windows = cc->window;
Call* this_call = &cc->calls[friend_number];
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++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], av, &cc->calls[friend_number])) { if(0 != start_video_transmission(&windows[i], av, this_call)) {
line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!"); line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
return; return;
} }
windows[i].is_video = true;
line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video starting"); line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting.");
cc->video_call = true;
} }
} }
} }
@ -196,22 +181,25 @@ void callback_video_ending(void* av, uint32_t friend_number, void *arg)
{ {
CallControl *cc = (CallControl*)arg; CallControl *cc = (CallControl*)arg;
ToxWindow* windows = cc->window; ToxWindow* windows = cc->window;
Call* this_call = &cc->calls[friend_number];
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++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) {
line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video ending"); line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Video operations ending.");
windows[i].is_video = false;
} }
} }
stop_video_transmission(&cc->calls[friend_number], friend_number); cc->video_call = false;
terminate_video();
} }
/* /*
* End of Callbacks * End of Callbacks
*/ */
/* /*
* Commands from chat_commands.h * Commands from chat_commands.h
*/ */
@ -239,8 +227,8 @@ void cmd_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
goto on_error; goto on_error;
} }
if ( self->is_video ) { if ( CallContrl.video_call ) {
error_str = "Video is already running in this call."; error_str = "Video is already sending in this call.";
goto on_error; goto on_error;
} }
@ -270,12 +258,7 @@ void cmd_end_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
goto on_error; goto on_error;
} }
if ( !self->is_call ) { if ( !CallContrl.video_call ) {
error_str = "Not in call!";
goto on_error;
}
if ( !self->is_video ) {
error_str = "Video is not running in this call."; error_str = "Video is not running in this call.";
goto on_error; goto on_error;
} }
@ -415,7 +398,7 @@ void cmd_ccur_video_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, ch
} }
else { else {
/* TODO: check for failure */ /* TODO: check for failure */
close_video_device(input, this_call->in_idx); close_video_device(input, &this_call->in_idx);
open_video_device(input, selection, &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); register_video_device_callback(self->num, this_call->in_idx, read_video_device_callback, &self->num);
} }

View File

@ -123,21 +123,27 @@ VideoDeviceError init_video_devices()
size[vdt_input] = 0; size[vdt_input] = 0;
#ifdef __linux__ #ifdef __linux__
for(int i = 0; i <= MAX_DEVICES; ++i) { for(; size[vdt_input] <= MAX_DEVICES; ++size[vdt_input]) {
size[vdt_input] = i; //size[vdt_input] = i;
int fd; int fd;
struct v4l2_capability cap;
char device_address[] = "/dev/videoXX"; char device_address[] = "/dev/videoXX";
snprintf(device_address + 10, sizeof(char) * strlen(device_address) - 10, "%i", size[vdt_input]);
snprintf(device_address + 10, sizeof(device_address) - 10, "%i", i);
fd = open(device_address, O_RDWR | O_NONBLOCK, 0); fd = open(device_address, O_RDWR | O_NONBLOCK, 0);
if (fd == -1) { if (fd == -1) {
break; break;
} else { } else {
video_devices_names[vdt_input][i] = cap.card; struct v4l2_capability cap;
char* video_input_name;
/* Query V4L for capture capabilities */
if(ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
video_input_name = cap.card;
} else {
video_input_name = device_address;
}
video_devices_names[vdt_input][size[vdt_input]] = video_input_name;
close(fd); close(fd);
} }
} }
@ -145,6 +151,8 @@ VideoDeviceError init_video_devices()
/* TODO: Add OSX implementation for listing input video devices */ /* TODO: Add OSX implementation for listing input video devices */
size[vdt_output] = 1; size[vdt_output] = 1;
char* video_output_name = "Video Receiver";
video_devices_names[vdt_output][0] = video_output_name;
// Start poll thread // Start poll thread
if (pthread_mutex_init(&video_mutex, NULL) != 0) if (pthread_mutex_init(&video_mutex, NULL) != 0)
@ -244,8 +252,9 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
snprintf(device_address + 10 , sizeof(device_address) - 10, "%i", selection); snprintf(device_address + 10 , sizeof(device_address) - 10, "%i", selection);
device->fd = open(device_address, O_RDWR); device->fd = open(device_address, O_RDWR);
if ( device->fd == -1 ) if ( device->fd == -1 ) {
return vde_FailedStart; return vde_FailedStart;
}
/* Obtain video device capabilities */ /* Obtain video device capabilities */
struct v4l2_capability cap; struct v4l2_capability cap;
@ -275,6 +284,7 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
req.memory = V4L2_MEMORY_MMAP; req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(device->fd, VIDIOC_REQBUFS, &req)) { if (-1 == xioctl(device->fd, VIDIOC_REQBUFS, &req)) {
assert(0);
return vde_UnsupportedMode; return vde_UnsupportedMode;
} }
@ -371,7 +381,7 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
int screen = DefaultScreen(device->x_display); int screen = DefaultScreen(device->x_display);
if ((device->x_window = XCreateSimpleWindow(device->x_display, RootWindow(device->x_display, screen), if ((device->x_window = XCreateSimpleWindow(device->x_display, RootWindow(device->x_display, screen),
0, 0, 800, 400, 0, 0, 0, 100, 100, 0,
BlackPixel(device->x_display, screen), BlackPixel(device->x_display, screen),
WhitePixel(device->x_display, screen))) == NULL) { WhitePixel(device->x_display, screen))) == NULL) {
return vde_FailedStart; return vde_FailedStart;
@ -386,8 +396,8 @@ VideoDeviceError open_video_device(VideoDeviceType type, int32_t selection, uint
XMapWindow(device->x_display, device->x_window); XMapWindow(device->x_display, device->x_window);
//XClearWindow(device->x_display, device->x_window); XClearWindow(device->x_display, device->x_window);
//XMapRaised(device->x_display, device->x_window); XMapRaised(device->x_display, device->x_window);
XFlush(device->x_display); XFlush(device->x_display);
vpx_img_alloc(&device->input, VPX_IMG_FMT_I420, device->video_width, device->video_height, 1); vpx_img_alloc(&device->input, VPX_IMG_FMT_I420, device->video_width, device->video_height, 1);
@ -423,10 +433,10 @@ __inline VideoDeviceError write_video_out(uint16_t width, uint16_t height,
ystride = abs(ystride); ystride = abs(ystride);
ustride = abs(ustride); ustride = abs(ustride);
vstride = abs(vstride); vstride = abs(vstride);
line_info_add(CallContrl.window, NULL, NULL, NULL, SYS_MSG, 0, 0, "ystride: %i ustride: %i vstride: %i", ystride, ustride, vstride);
uint16_t *img_data = malloc(width * height * 6); uint8_t *img_data = malloc(width * height * 4);
yuv420tobgr(width, height, y, u, v, yuv420tobgr(width, height, y, u, v, ystride, ustride, vstride, img_data);
ystride, ustride, vstride, img_data);
XImage image = { XImage image = {
.width = width, .width = width,
@ -526,14 +536,13 @@ void* video_thread_poll (void* arg) // TODO: maybe use thread for every input so
XCopyArea(device->x_display, pixmap, device->x_window, device->x_gc, 0, 0, video_width, video_height, 0, 0); XCopyArea(device->x_display, pixmap, device->x_window, device->x_gc, 0, 0, video_width, video_height, 0, 0);
XFreePixmap(device->x_display, pixmap); XFreePixmap(device->x_display, pixmap);
XFlush(device->x_display); XFlush(device->x_display);
free(img_data);
if (-1 == xioctl(device->fd, VIDIOC_QBUF, &buf)) { if (-1 == xioctl(device->fd, VIDIOC_QBUF, &buf)) {
unlock; unlock;
continue; continue;
} }
free(img_data);
} }
unlock; unlock;
} }
@ -648,7 +657,7 @@ void print_video_devices(ToxWindow* self, VideoDeviceType type)
int i; int i;
for (i = 0; i < size[type]; ++i) for (i = 0; i < size[type]; ++i)
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i, &video_devices_names[type][i]); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i, video_devices_names[type][i]);
return; return;
} }

View File

@ -154,7 +154,6 @@ struct ToxWindow {
#ifdef VIDEO #ifdef VIDEO
int video_device_selection[2]; /* -1 if not set, if set uses these selections instead of primary video device */ int video_device_selection[2]; /* -1 if not set, if set uses these selections instead of primary video device */
bool is_video;
#endif /* VIDEO */ #endif /* VIDEO */
#endif /* AUDIO */ #endif /* AUDIO */