1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 17:57:45 +02:00
toxic/src/audio_call.c

765 lines
21 KiB
C
Raw Normal View History

/* audio_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 <http://www.gnu.org/licenses/>.
*
2014-02-23 00:00:34 +01:00
*/
#include "toxic.h"
#include "windows.h"
#include "audio_call.h"
2014-06-21 01:58:00 +02:00
#include "device.h"
2014-03-07 03:14:04 +01:00
#include "chat_commands.h"
2014-03-08 01:12:51 +01:00
#include "global_commands.h"
#include "line_info.h"
2014-03-12 00:25:13 +01:00
2014-03-07 03:14:04 +01:00
#include <curses.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
2014-05-16 20:00:01 +02:00
#include <assert.h>
2014-03-07 03:14:04 +01:00
2014-06-12 01:40:43 +02:00
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
2014-03-07 03:14:04 +01:00
#define _cbend pthread_exit(NULL)
2014-05-16 20:00:01 +02:00
#define MAX_CALLS 10
2014-06-21 01:58:00 +02:00
#define frame_size (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000)
2014-05-16 20:00:01 +02:00
typedef struct _Call {
pthread_t ttid; /* Transmission thread id */
2014-06-24 00:20:44 +02:00
_Bool ttas, has_output; /* Transmission thread active status (0 - stopped, 1- running) */
2014-06-21 01:58:00 +02:00
int in_idx, out_idx;
pthread_mutex_t mutex;
2014-05-16 20:00:01 +02:00
} Call;
2014-06-24 01:00:11 +02:00
void set_call(Call* call, _Bool start)
2014-06-21 01:58:00 +02:00
{
call->in_idx = -1;
call->out_idx = -1;
if ( start ) {
call->ttas = _True;
pthread_mutex_init(&call->mutex, NULL);
}
else {
call->ttid = 0;
pthread_mutex_destroy(&call->mutex);
}
}
2014-06-24 01:00:11 +02:00
struct _ASettings {
2014-03-07 03:14:04 +01:00
AudioError errors;
ToxAv *av;
2014-05-16 20:00:01 +02:00
ToxAvCodecSettings cs;
Call calls[MAX_CALLS];
2014-03-07 03:14:04 +01:00
} ASettins;
2014-06-21 01:58:00 +02:00
void callback_recv_invite ( int32_t call_index, void *arg );
void callback_recv_ringing ( int32_t call_index, void *arg );
2014-05-16 20:00:01 +02:00
void callback_recv_starting ( int32_t call_index, void *arg );
2014-06-21 01:58:00 +02:00
void callback_recv_ending ( int32_t call_index, void *arg );
void callback_recv_error ( int32_t call_index, void *arg );
void callback_call_started ( int32_t call_index, void *arg );
2014-05-16 20:00:01 +02:00
void callback_call_canceled ( int32_t call_index, void *arg );
void callback_call_rejected ( int32_t call_index, void *arg );
2014-06-21 01:58:00 +02:00
void callback_call_ended ( int32_t call_index, void *arg );
void callback_requ_timeout ( int32_t call_index, void *arg );
void callback_peer_timeout ( int32_t call_index, void *arg );
2014-03-07 03:14:04 +01:00
2014-03-26 03:10:05 +01:00
static void print_err (ToxWindow *self, uint8_t *error_str)
{
line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0);
}
ToxAv *init_audio(ToxWindow *self, Tox *tox)
2014-05-16 20:00:01 +02:00
{
ASettins.cs = av_DefaultSettings;
ASettins.cs.video_height = ASettins.cs.video_width = 0;
2014-06-21 01:58:00 +02:00
ASettins.errors = ae_None;
2014-05-16 20:00:01 +02:00
memset(ASettins.calls, 0, sizeof(Call) * 10);
2014-06-21 01:58:00 +02:00
/* Streaming stuff from core */
2014-06-21 01:58:00 +02:00
ASettins.av = toxav_new(tox, MAX_CALLS);
2014-06-21 01:58:00 +02:00
if ( !ASettins.av ) {
ASettins.errors |= ae_StartingCoreAudio;
return NULL;
2014-03-07 03:14:04 +01:00
}
2014-06-21 01:58:00 +02:00
if ( init_devices(ASettins.av) == de_InternalError ) {
line_info_add(self, NULL, NULL, NULL, "Failed to init devices", SYS_MSG, 0, 0);
toxav_kill(ASettins.av);
return ASettins.av = NULL;
2014-03-07 03:14:04 +01:00
}
2014-06-21 01:58:00 +02:00
toxav_register_callstate_callback(callback_call_started, av_OnStart, self);
toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, self);
toxav_register_callstate_callback(callback_call_rejected, av_OnReject, self);
toxav_register_callstate_callback(callback_call_ended, av_OnEnd, self);
toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, self);
2014-06-21 01:58:00 +02:00
toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, self);
toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, self);
toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, self);
2014-06-21 01:58:00 +02:00
toxav_register_callstate_callback(callback_recv_error, av_OnError, self);
toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, self);
toxav_register_callstate_callback(callback_peer_timeout, av_OnPeerTimeout, self);
2014-03-07 03:14:04 +01:00
return ASettins.av;
}
void terminate_audio()
{
2014-05-16 20:00:01 +02:00
int i;
for (i = 0; i < MAX_CALLS; i ++)
stop_transmission(i);
2014-03-14 23:08:08 +01:00
if ( ASettins.av )
toxav_kill(ASettins.av);
2014-05-16 20:00:01 +02:00
2014-06-21 01:58:00 +02:00
terminate_devices();
2014-03-07 03:14:04 +01:00
}
2014-06-21 01:58:00 +02:00
void read_device_callback (const int16_t* captured, uint32_t size, void* data)
2014-03-07 03:14:04 +01:00
{
2014-06-21 01:58:00 +02:00
int32_t call_index = *((int32_t*)data); /* TODO: Or pass an array of call_idx's */
uint8_t encoded_payload[RTP_PAYLOAD_SIZE];
int32_t payload_size = toxav_prepare_audio_frame(ASettins.av, call_index, encoded_payload, RTP_PAYLOAD_SIZE, captured, size);
if ( payload_size <= 0 || toxav_send_audio(ASettins.av, call_index, encoded_payload, payload_size) < 0 ) {
/*fprintf(stderr, "Could not encode audio packet\n");*/
}
2014-03-07 03:14:04 +01:00
}
/*
* Transmission
*/
void *transmission(void *arg)
2014-06-21 01:58:00 +02:00
{
#define lock pthread_mutex_lock(&this_call->mutex)
#define unlock pthread_mutex_unlock(&this_call->mutex)
2014-05-16 20:00:01 +02:00
ToxWindow* self = arg;
2014-06-21 01:58:00 +02:00
int32_t call_index = self->call_idx;
2014-03-14 23:08:08 +01:00
/* Missing audio support */
if ( !ASettins.av ) _cbend;
2014-05-16 20:00:01 +02:00
Call* this_call = &ASettins.calls[call_index];
2014-03-07 03:14:04 +01:00
int32_t dec_frame_len;
2014-05-16 20:00:01 +02:00
int16_t PCM[frame_size];
2014-06-24 00:20:44 +02:00
this_call->has_output = 1;
2014-05-16 20:00:01 +02:00
if ( open_primary_device(input, &this_call->in_idx) != de_None )
line_info_add(self, NULL, NULL, NULL, "Failed to open input device!", SYS_MSG, 0, 0);
if ( register_device_callback(call_index, this_call->in_idx, read_device_callback, &call_index, _True) != de_None)
2014-06-21 01:58:00 +02:00
/* Set VAD as true for all; TODO: Make it more dynamic */
line_info_add(self, NULL, NULL, NULL, "Failed to register input handler!", SYS_MSG, 0, 0);
2014-06-21 01:58:00 +02:00
if ( open_primary_device(output, &this_call->out_idx) != de_None ) {
line_info_add(self, NULL, NULL, NULL, "Failed to open output device!", SYS_MSG, 0, 0);
2014-06-24 00:20:44 +02:00
this_call->has_output = 0;
}
2014-03-07 03:14:04 +01:00
/* Start transmission */
2014-05-16 20:00:01 +02:00
while (this_call->ttas) {
2014-06-21 01:58:00 +02:00
lock;
2014-06-24 00:20:44 +02:00
if ( this_call->has_output ) {
if (playback_device_ready(this_call->out_idx) == de_Busy) {
unlock;
continue;
}
dec_frame_len = toxav_recv_audio(ASettins.av, call_index, frame_size, PCM);
/* Play the packet */
if (dec_frame_len > 0) {
write_out(this_call->out_idx, PCM, dec_frame_len, av_DefaultSettings.audio_channels);
}
else if (dec_frame_len != 0) {
/* >implying it'll ever get an error */
}
2014-05-16 20:00:01 +02:00
}
2014-06-21 01:58:00 +02:00
unlock;
2014-03-07 03:14:04 +01:00
usleep(1000);
}
2014-03-07 03:14:04 +01:00
cleanup:
2014-06-21 01:58:00 +02:00
if ( this_call->in_idx != -1 ) if ( close_device(input, this_call->in_idx) != de_None )
line_info_add(self, NULL, NULL, NULL, "Failed to close input device!", SYS_MSG, 0, 0);
if ( this_call->out_idx != -1 ) if ( close_device(output, this_call->out_idx) != de_None )
line_info_add(self, NULL, NULL, NULL, "Failed to close output device!", SYS_MSG, 0, 0);
2014-06-24 01:00:11 +02:00
set_call(this_call, _False);
2014-06-21 01:58:00 +02:00
2014-03-07 03:14:04 +01:00
_cbend;
}
int start_transmission(ToxWindow *self)
2014-03-07 03:14:04 +01:00
{
2014-06-21 01:58:00 +02:00
if ( !ASettins.av || self->call_idx == -1 ) return -1;
2014-05-16 20:00:01 +02:00
2014-03-07 03:14:04 +01:00
/* Don't provide support for video */
2014-06-21 01:58:00 +02:00
if ( 0 != toxav_prepare_transmission(ASettins.av, self->call_idx, &ASettins.cs, 0) ) {
2014-05-16 20:00:01 +02:00
line_info_add(self, NULL, NULL, NULL, "Could not prepare transmission", SYS_MSG, 0, 0);
}
2014-06-21 01:58:00 +02:00
if ( !toxav_capability_supported(ASettins.av, self->call_idx, AudioDecoding) ||
!toxav_capability_supported(ASettins.av, self->call_idx, AudioEncoding) )
2014-05-16 20:00:01 +02:00
return -1;
2014-06-24 01:00:11 +02:00
set_call(&ASettins.calls[self->call_idx], _True);
2014-06-21 01:58:00 +02:00
if ( 0 != pthread_create(&ASettins.calls[self->call_idx].ttid, NULL, transmission, self ) &&
0 != pthread_detach(ASettins.calls[self->call_idx].ttid) ) {
2014-03-07 03:14:04 +01:00
return -1;
2014-06-24 00:54:23 +02:00
}
return 0;
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
int stop_transmission(int call_index)
{
2014-06-21 01:58:00 +02:00
if ( ASettins.calls[call_index].ttas ) {
toxav_kill_transmission(ASettins.av, call_index);
ASettins.calls[call_index].ttas = _False;
return 0;
2014-06-21 01:58:00 +02:00
}
return -1;
2014-03-07 03:14:04 +01:00
}
/*
* End of transmission
*/
/*
* Callbacks
*/
2014-05-16 20:00:01 +02:00
#define CB_BODY(call_idx, 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], ASettins.av, call_idx); } while (0)
2014-03-07 03:14:04 +01:00
2014-05-16 20:00:01 +02:00
void callback_recv_invite ( int32_t call_index, void* arg )
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onInvite);
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_recv_ringing ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onRinging);
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_recv_starting ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
ToxWindow* windows = arg;
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i)
2014-06-21 01:58:00 +02:00
if (windows[i].onStarting != NULL && windows[i].call_idx == call_index) {
2014-05-16 20:00:01 +02:00
windows[i].onStarting(&windows[i], ASettins.av, call_index);
if ( 0 != start_transmission(&windows[i]) ) {/* YEAH! */
line_info_add(&windows[i], NULL, NULL, NULL, "Error starting transmission!", SYS_MSG, 0, 0);
return;
}
}
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_recv_ending ( int32_t call_index, void* arg )
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onEnding);
stop_transmission(call_index);
((ToxWindow*)arg)->call_idx = -1;
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_recv_error ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onError);
stop_transmission(call_index);
2014-06-24 00:30:11 +02:00
((ToxWindow*)arg)->call_idx = -1;
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_call_started ( int32_t call_index, void* arg )
{
ToxWindow* windows = arg;
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i)
2014-06-21 01:58:00 +02:00
if (windows[i].onStart != NULL && windows[i].call_idx == call_index) {
2014-05-16 20:00:01 +02:00
windows[i].onStart(&windows[i], ASettins.av, call_index);
if ( 0 != start_transmission(&windows[i]) ) {/* YEAH! */
line_info_add(&windows[i], NULL, NULL, NULL, "Error starting transmission!", SYS_MSG, 0, 0);
return;
}
}
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_call_canceled ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onCancel);
2014-03-14 23:08:08 +01:00
/* In case call is active */
2014-05-16 20:00:01 +02:00
stop_transmission(call_index);
((ToxWindow*)arg)->call_idx = -1;
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_call_rejected ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onReject);
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_call_ended ( int32_t call_index, void* arg )
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onEnd);
stop_transmission(call_index);
((ToxWindow*)arg)->call_idx = -1;
2014-03-07 03:14:04 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_requ_timeout ( int32_t call_index, void* arg )
2014-03-07 03:14:04 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onRequestTimeout);
2014-06-24 00:30:11 +02:00
((ToxWindow*)arg)->call_idx = -1;
2014-03-14 23:08:08 +01:00
}
2014-05-16 20:00:01 +02:00
void callback_peer_timeout ( int32_t call_index, void* arg )
2014-03-14 23:08:08 +01:00
{
2014-05-16 20:00:01 +02:00
CB_BODY(call_index, arg, onPeerTimeout);
stop_transmission(call_index);
2014-03-14 23:08:08 +01:00
/* Call is stopped manually since there might be some other
* actions that one can possibly take on timeout
*/
2014-05-16 20:00:01 +02:00
toxav_stop_call(ASettins.av, call_index);
((ToxWindow*)arg)->call_idx = -1;
2014-03-07 03:14:04 +01:00
}
/*
* End of Callbacks
2014-03-07 03:14:04 +01:00
*/
/*
* Commands from chat_commands.h
*/
void cmd_call(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *error_str;
if (argc != 0) {
error_str = "Invalid syntax!";
goto on_error;
}
if ( !ASettins.av ) {
error_str = "Audio not supported!";
goto on_error;
}
2014-06-24 04:02:22 +02:00
if (!self->stb->is_online) {
error_str = "Friend is offline.";
goto on_error;
}
2014-06-21 01:58:00 +02:00
ToxAvError error = toxav_call(ASettins.av, &self->call_idx, self->num, TypeAudio, 30);
2014-03-07 03:14:04 +01:00
if ( error != ErrorNone ) {
if ( error == ErrorAlreadyInCall ) error_str = "Already in a call!";
else error_str = "Internal error!";
2014-03-07 03:14:04 +01:00
goto on_error;
}
2014-06-21 01:58:00 +02:00
snprintf(msg, sizeof(msg), "Calling... idx: %d", self->call_idx);
2014-05-16 20:00:01 +02:00
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-03-07 03:14:04 +01:00
return;
on_error:
2014-03-26 03:02:48 +01:00
snprintf(msg, sizeof(msg), "%s", error_str);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-03-07 03:14:04 +01:00
}
void cmd_answer(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t *error_str;
if (argc != 0) {
error_str = "Invalid syntax!";
goto on_error;
}
if ( !ASettins.av ) {
error_str = "Audio not supported!";
goto on_error;
}
2014-06-21 01:58:00 +02:00
ToxAvError error = toxav_answer(ASettins.av, self->call_idx, TypeAudio);
2014-03-07 03:14:04 +01:00
if ( error != ErrorNone ) {
if ( error == ErrorInvalidState ) error_str = "Cannot answer in invalid state!";
else if ( error == ErrorNoCall ) error_str = "No incoming call!";
2014-03-07 03:14:04 +01:00
else error_str = "Internal error!";
2014-03-07 03:14:04 +01:00
goto on_error;
}
2014-03-07 03:14:04 +01:00
/* Callback will print status... */
2014-03-07 03:14:04 +01:00
return;
on_error:
print_err (self, error_str);
2014-03-07 03:14:04 +01:00
}
2014-03-23 22:54:56 +01:00
void cmd_reject(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t *error_str;
if (argc != 0) {
error_str = "Invalid syntax!";
goto on_error;
}
if ( !ASettins.av ) {
error_str = "Audio not supported!";
goto on_error;
}
2014-06-21 01:58:00 +02:00
ToxAvError error = toxav_reject(ASettins.av, self->call_idx, "Why not?");
2014-03-23 22:54:56 +01:00
if ( error != ErrorNone ) {
if ( error == ErrorInvalidState ) error_str = "Cannot reject in invalid state!";
else if ( error == ErrorNoCall ) error_str = "No incoming call!";
2014-03-23 22:54:56 +01:00
else error_str = "Internal error!";
2014-03-23 22:54:56 +01:00
goto on_error;
}
2014-03-23 22:54:56 +01:00
/* Callback will print status... */
2014-03-23 22:54:56 +01:00
return;
on_error:
2014-03-26 03:02:48 +01:00
print_err (self, error_str);
2014-03-23 22:54:56 +01:00
}
2014-03-07 03:14:04 +01:00
void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t *error_str;
if (argc != 0) {
error_str = "Invalid syntax!";
goto on_error;
}
if ( !ASettins.av ) {
error_str = "Audio not supported!";
goto on_error;
}
2014-06-21 01:58:00 +02:00
ToxAvError error = toxav_hangup(ASettins.av, self->call_idx);
2014-03-07 03:14:04 +01:00
if ( error != ErrorNone ) {
if ( error == ErrorInvalidState ) error_str = "Cannot hangup in invalid state!";
else if ( error == ErrorNoCall ) error_str = "No call!";
else error_str = "Internal error!";
2014-03-07 03:14:04 +01:00
goto on_error;
}
2014-03-07 03:14:04 +01:00
return;
on_error:
print_err (self, error_str);
2014-03-07 03:14:04 +01:00
}
void cmd_cancel(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t *error_str;
if (argc != 0) {
error_str = "Invalid syntax!";
goto on_error;
}
if ( !ASettins.av ) {
error_str = "Audio not supported!";
goto on_error;
}
2014-06-21 01:58:00 +02:00
ToxAvError error = toxav_cancel(ASettins.av, self->call_idx, self->num,
2014-05-16 20:00:01 +02:00
"Only those who appreciate small things know the beauty that is life");
2014-03-07 03:14:04 +01:00
if ( error != ErrorNone ) {
if ( error == ErrorNoCall ) error_str = "No call!";
2014-06-24 01:00:11 +02:00
else if ( error == ErrorInvalidState ) error_str = "Cannot cancel in invalid state!";
2014-03-07 03:14:04 +01:00
else error_str = "Internal error!";
2014-03-07 03:14:04 +01:00
goto on_error;
}
2014-03-07 03:14:04 +01:00
/* Callback will print status... */
2014-03-07 03:14:04 +01:00
return;
on_error:
print_err (self, error_str);
2014-03-07 03:14:04 +01:00
}
2014-03-08 01:12:51 +01:00
void cmd_list_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *error_str;
2014-03-08 01:12:51 +01:00
if ( argc != 1 ) {
if ( argc < 1 ) error_str = "Type must be specified!";
2014-03-08 01:12:51 +01:00
else error_str = "Only one argument allowed!";
2014-03-08 01:12:51 +01:00
goto on_error;
}
2014-06-21 01:58:00 +02:00
DeviceType type;
2014-03-08 01:12:51 +01:00
if ( strcmp(argv[1], "in") == 0 ) /* Input devices */
type = input;
2014-03-08 01:12:51 +01:00
else if ( strcmp(argv[1], "out") == 0 ) /* Output devices */
type = output;
2014-03-08 01:12:51 +01:00
else {
snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-03-08 01:12:51 +01:00
return;
}
2014-06-21 01:58:00 +02:00
print_devices(self, type);
2014-03-08 01:12:51 +01:00
return;
on_error:
print_err (self, error_str);
2014-03-08 01:12:51 +01:00
}
2014-03-07 03:14:04 +01:00
2014-06-21 01:58:00 +02:00
/* This changes primary device only */
2014-03-08 01:12:51 +01:00
void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *error_str;
2014-03-08 01:12:51 +01:00
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!";
2014-03-08 01:12:51 +01:00
goto on_error;
}
2014-06-21 01:58:00 +02:00
DeviceType type;
2014-03-08 01:12:51 +01:00
if ( strcmp(argv[1], "in") == 0 ) /* Input devices */
type = input;
2014-03-08 01:12:51 +01:00
else if ( strcmp(argv[1], "out") == 0 ) /* Output devices */
type = output;
2014-03-08 01:12:51 +01:00
else {
snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-03-08 01:12:51 +01:00
return;
}
2014-03-08 01:12:51 +01:00
char *end;
long int selection = strtol(argv[2], &end, 10);
2014-03-08 01:12:51 +01:00
if ( *end ) {
error_str = "Invalid input";
goto on_error;
}
2014-05-16 20:00:01 +02:00
2014-06-21 01:58:00 +02:00
if ( set_primary_device(type, selection) == de_InvalidSelection ) {
error_str="Invalid selection!";
2014-05-16 20:00:01 +02:00
goto on_error;
}
2014-06-21 01:58:00 +02:00
return;
on_error:
print_err (self, error_str);
}
void cmd_ccur_device(WINDOW * window, ToxWindow * self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
2014-06-21 01:58:00 +02:00
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *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;
2014-05-16 20:00:01 +02:00
}
2014-06-21 01:58:00 +02:00
DeviceType type;
if ( strcmp(argv[1], "in") == 0 ) /* Input devices */
type = input;
else if ( strcmp(argv[1], "out") == 0 ) /* Output devices */
type = output;
else {
snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]);
2014-04-08 23:20:21 +02:00
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
2014-06-21 01:58:00 +02:00
return;
2014-03-08 01:12:51 +01:00
}
2014-05-16 20:00:01 +02:00
2014-06-21 01:58:00 +02:00
char *end;
long int selection = strtol(argv[2], &end, 10);
if ( *end ) {
error_str = "Invalid input";
goto on_error;
}
if ( selection_valid(type, selection) == de_InvalidSelection ) {
error_str="Invalid selection!";
goto on_error;
}
/* If call is active, change device */
if ( self->call_idx > -1) {
Call* this_call = &ASettins.calls[self->call_idx];
if (this_call->ttas) {
if (type == output) {
pthread_mutex_lock(&this_call->mutex);
close_device(output, this_call->out_idx);
2014-06-24 00:20:44 +02:00
this_call->has_output = open_device(output, selection, &this_call->out_idx)
== de_None ? 1 : 0;
2014-06-21 01:58:00 +02:00
pthread_mutex_unlock(&this_call->mutex);
}
else {
/* TODO: check for failure */
close_device(input, this_call->in_idx);
open_device(input, selection, &this_call->in_idx);
/* Set VAD as true for all; TODO: Make it more dynamic */
register_device_callback(self->call_idx, this_call->in_idx, read_device_callback, &self->call_idx, _True);
2014-06-21 01:58:00 +02:00
}
}
}
self->device_selection[type] = selection;
2014-03-08 01:12:51 +01:00
return;
on_error:
print_err (self, error_str);
}
void cmd_mute(WINDOW * window, ToxWindow * self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *error_str;
if ( argc != 1 ) {
if ( argc < 1 ) error_str = "Type must be specified!";
else error_str = "Only two arguments allowed!";
goto on_error;
}
DeviceType type;
if ( strcmp(argv[1], "in") == 0 ) /* Input devices */
type = input;
else if ( strcmp(argv[1], "out") == 0 ) /* Output devices */
type = output;
else {
snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
return;
}
/* If call is active, use this_call values */
if ( self->call_idx > -1) {
Call* this_call = &ASettins.calls[self->call_idx];
2014-06-24 00:54:23 +02:00
pthread_mutex_lock(&this_call->mutex);
if (type == input) {
device_mute(type, this_call->in_idx);
self->chatwin->infobox.in_is_muted ^= 1;
} else {
device_mute(type, this_call->out_idx);
2014-06-24 01:00:11 +02:00
self->chatwin->infobox.out_is_muted ^= 1;
2014-06-24 00:54:23 +02:00
}
pthread_mutex_unlock(&this_call->mutex);
}
return;
on_error:
print_err (self, error_str);
}
void cmd_sense(WINDOW * window, ToxWindow * self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
uint8_t msg[MAX_STR_SIZE];
uint8_t *error_str;
if ( argc != 1 ) {
if ( argc < 1 ) error_str = "Must have value!";
else error_str = "Only two arguments allowed!";
goto on_error;
}
char *end;
float value = strtof(argv[1], &end);
if ( *end ) {
error_str = "Invalid input";
goto on_error;
}
/* Call must be active */
2014-06-24 00:54:23 +02:00
if ( self->call_idx > -1) {
device_set_VAD_treshold(ASettins.calls[self->call_idx].in_idx, value);
self->chatwin->infobox.vad_lvl = value;
}
return;
on_error:
print_err (self, error_str);
}