1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-03 06:16:45 +02:00

Per-call pending status and state handling

* Proper handling of incoming video calls.
* Add command /vcall for initiating video calls
* Add command /res command to set video resolution
This commit is contained in:
zugz (tox)
2020-04-14 00:00:00 +00:00
parent 7dead5ec96
commit 142ce642f0
10 changed files with 547 additions and 502 deletions

View File

@ -40,7 +40,7 @@
#ifdef VIDEO
#define default_video_bit_rate 5000
#define DEFAULT_VIDEO_BIT_RATE 5000
void on_video_receive_frame(ToxAV *av, uint32_t friend_number,
uint16_t width, uint16_t height,
@ -62,7 +62,7 @@ ToxAV *init_video(ToxWindow *self, Tox *tox)
CallControl.video_errors = ve_None;
CallControl.video_enabled = true;
CallControl.video_bit_rate = 0;
CallControl.default_video_bit_rate = 0;
CallControl.video_frame_duration = 10;
if (!CallControl.av) {
@ -92,8 +92,9 @@ void terminate_video(void)
stop_video_transmission(this_call, i);
if (this_call->vout_idx != -1) {
if (this_call->status == cs_Active && this_call->vout_idx != -1) {
close_video_device(vdt_output, this_call->vout_idx);
this_call->vout_idx = -1;
}
}
@ -108,7 +109,7 @@ void read_video_device_callback(int16_t width, int16_t height, const uint8_t *y,
Toxav_Err_Send_Frame error;
/* Drop frame if video sending is disabled */
if (CallControl.video_bit_rate == 0 || this_call->vin_idx == -1) {
if (this_call->video_bit_rate == 0 || this_call->status != cs_Active || this_call->vin_idx == -1) {
line_info_add(CallControl.prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video frame dropped.");
return;
}
@ -137,18 +138,11 @@ void write_video_device_callback(uint32_t friend_number, uint16_t width, uint16_
int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call)
{
if (!self || !av) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare transmission");
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video transmission");
return -1;
}
CallControl.video_bit_rate = default_video_bit_rate;
if (toxav_video_set_bit_rate(CallControl.av, self->num, CallControl.video_bit_rate, 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->vin_idx) != vde_None) {
if (open_primary_video_device(vdt_input, &call->vin_idx, &call->video_width, &call->video_height) != vde_None) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input video device!");
return -1;
}
@ -158,13 +152,22 @@ int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call)
return -1;
}
if (!toxav_video_set_bit_rate(CallControl.av, self->num, call->video_bit_rate, NULL)) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set video bit rate");
return -1;
}
return 0;
}
int stop_video_transmission(Call *call, int friend_number)
{
CallControl.video_bit_rate = 0;
toxav_video_set_bit_rate(CallControl.av, friend_number, CallControl.video_bit_rate, NULL);
if (call->status != cs_Active) {
return -1;
}
call->video_bit_rate = 0;
toxav_video_set_bit_rate(CallControl.av, friend_number, call->video_bit_rate, NULL);
if (call->vin_idx != -1) {
close_video_device(vdt_input, call->vin_idx);
@ -200,24 +203,33 @@ void on_video_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rat
UNUSED_VAR(av);
UNUSED_VAR(user_data);
CallControl.video_bit_rate = video_bit_rate;
toxav_video_set_bit_rate(CallControl.av, friend_number, CallControl.video_bit_rate, NULL);
Call *call = &CallControl.calls[friend_number];
call->video_bit_rate = video_bit_rate;
/* TODO: with current toxav using one-pass VP8, the value of
* video_bit_rate has no effect, except to disable video if it is 0.
* Automatically change resolution instead? */
toxav_video_set_bit_rate(CallControl.av, friend_number, call->video_bit_rate, NULL);
}
void callback_recv_video_starting(uint32_t friend_number)
{
Call *this_call = &CallControl.calls[friend_number];
if (this_call->vout_idx != -1) {
if (this_call->status != cs_Active || this_call->vout_idx != -1) {
return;
}
open_primary_video_device(vdt_output, &this_call->vout_idx);
open_primary_video_device(vdt_output, &this_call->vout_idx, NULL, NULL);
}
void callback_recv_video_end(uint32_t friend_number)
{
Call *this_call = &CallControl.calls[friend_number];
if (this_call->status != cs_Active || this_call->vout_idx == -1) {
return;
}
close_video_device(vdt_output, this_call->vout_idx);
this_call->vout_idx = -1;
}
@ -235,12 +247,9 @@ void callback_video_starting(uint32_t friend_number)
ToxWindow *window = get_window_ptr(i);
if (window != NULL && window->is_call && window->num == friend_number) {
if (0 != start_video_transmission(window, CallControl.av, this_call)) {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
return;
if (start_video_transmission(window, CallControl.av, this_call) == 0) {
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting.");
}
line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting.");
}
}
}
@ -258,44 +267,122 @@ void callback_video_end(uint32_t friend_number)
/*
* Commands from chat_commands.h
*/
void cmd_vcall(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);
UNUSED_VAR(m);
UNUSED_VAR(argv);
if (argc != 0) {
print_err(self, "Unknown arguments.");
return;
}
if (!CallControl.av) {
print_err(self, "ToxAV not supported!");
return;
}
if (!self->stb->connection) {
print_err(self, "Friend is offline.");
return;
}
Call *call = &CallControl.calls[self->num];
if (call->status != cs_None) {
print_err(self, "Already calling.");
return;
}
init_call(call);
call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE;
place_call(self);
}
void cmd_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);
UNUSED_VAR(m);
UNUSED_VAR(argv);
const char *error_str;
Call *this_call = &CallControl.calls[self->num];
if (argc != 0) {
error_str = "Unknown arguments.";
goto on_error;
print_err(self, "Unknown arguments.");
return;
}
if (!CallControl.av) {
error_str = "ToxAV not supported!";
goto on_error;
print_err(self, "ToxAV not supported!");
return;
}
if (!self->stb->connection) {
error_str = "Friend is offline.";
goto on_error;
print_err(self, "Friend is offline.");
return;
}
if (!self->is_call) {
error_str = "Not in call!";
goto on_error;
if (this_call->status != cs_Active) {
print_err(self, "Not in call!");
return;
}
if (this_call->vin_idx == -1) {
this_call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE;
callback_video_starting(self->num);
} else {
callback_video_end(self->num);
}
}
return;
on_error:
print_err(self, error_str);
void cmd_res(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);
UNUSED_VAR(m);
Call *call = &CallControl.calls[self->num];
if (argc == 0) {
if (call->status == cs_Active && call->vin_idx != -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0,
"Resolution of current call: %u x %u",
call->video_width, call->video_height);
} else {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0,
"Initial resolution for video calls: %u x %u",
CallControl.default_video_width, CallControl.default_video_height);
}
return;
}
if (argc != 2) {
print_err(self, "Require 0 or 2 arguments.");
return;
}
char *endw, *endh;
const long int width = strtol(argv[1], &endw, 10);
const long int height = strtol(argv[2], &endh, 10);
if (*endw || *endh || width < 0 || height < 0) {
print_err(self, "Invalid input");
return;
}
if (call->status == cs_Active && call->vin_idx != -1) {
stop_video_transmission(call, self->num);
call->video_width = width;
call->video_height = height;
call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE;
start_video_transmission(self, CallControl.av, call);
} else {
CallControl.default_video_width = width;
CallControl.default_video_height = height;
}
}
void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
@ -303,16 +390,14 @@ void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, c
UNUSED_VAR(window);
UNUSED_VAR(m);
const char *error_str;
if (argc != 1) {
if (argc < 1) {
error_str = "Type must be specified!";
print_err(self, "Type must be specified!");
} else {
error_str = "Only one argument allowed!";
print_err(self, "Only one argument allowed!");
}
goto on_error;
return;
}
VideoDeviceType type;
@ -331,10 +416,6 @@ void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, c
}
print_video_devices(self, type);
return;
on_error:
print_err(self, error_str);
}
/* This changes primary video device only */
@ -343,18 +424,16 @@ void cmd_change_video_device(WINDOW *window, ToxWindow *self, Tox *m, int argc,
UNUSED_VAR(window);
UNUSED_VAR(m);
const char *error_str;
if (argc != 2) {
if (argc < 1) {
error_str = "Type must be specified!";
print_err(self, "Type must be specified!");
} else if (argc < 2) {
error_str = "Must have id!";
print_err(self, "Must have id!");
} else {
error_str = "Only two arguments allowed!";
print_err(self, "Only two arguments allowed!");
}
goto on_error;
return;
}
VideoDeviceType type;
@ -377,96 +456,14 @@ void cmd_change_video_device(WINDOW *window, ToxWindow *self, Tox *m, int argc,
long int selection = strtol(argv[2], &end, 10);
if (*end) {
error_str = "Invalid input";
goto on_error;
print_err(self, "Invalid input");
return;
}
if (set_primary_video_device(type, selection) == vde_InvalidSelection) {
error_str = "Invalid selection!";
goto on_error;
}
return;
on_error:
print_err(self, error_str);
}
void cmd_ccur_video_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);
UNUSED_VAR(m);
const char *error_str;
if (argc != 2) {
if (argc < 1) {
error_str = "Type must be specified!";
} else if (argc < 2) {
error_str = "Must have id!";
} else {
error_str = "Only two arguments allowed!";
}
goto on_error;
}
VideoDeviceType type;
if (strcmp(argv[1], "in") == 0) { /* Input devices */
type = vdt_input;
}
else if (strcmp(argv[1], "out") == 0) { /* Output devices */
type = vdt_output;
}
else {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]);
print_err(self, "Invalid selection!");
return;
}
char *end;
long int selection = strtol(argv[2], &end, 10);
if (*end) {
error_str = "Invalid input";
goto on_error;
}
if (video_selection_valid(type, selection) == vde_InvalidSelection) {
error_str = "Invalid selection!";
goto on_error;
}
/* If call is active, change device */
if (self->is_call) {
Call *this_call = &CallControl.calls[self->num];
if (this_call->ttas) {
if (type == vdt_output) {
} else {
/* TODO: check for failure */
close_video_device(vdt_input, this_call->vin_idx);
open_video_device(vdt_input, selection, &this_call->vin_idx);
register_video_device_callback(self->num, this_call->vin_idx, read_video_device_callback, &self->num);
}
}
}
return;
on_error:
print_err(self, error_str);
}
void stop_video_stream(ToxWindow *self)
{
Call *this_call = &CallControl.calls[self->num];
if (this_call && this_call->vin_idx != -1) {
stop_video_transmission(this_call, self->num);
}
}
#endif /* VIDEO */