Make toxav thread safe

This commit is contained in:
mannol 2015-03-01 18:45:04 +01:00
parent 9e65cd5337
commit 45e8807c1e
6 changed files with 339 additions and 250 deletions

View File

@ -16,7 +16,7 @@
typedef struct {
bool incoming;
TOXAV_CALL_STATE state;
uint32_t state;
} CallControl;
const char* stringify_state(TOXAV_CALL_STATE s)
@ -44,9 +44,9 @@ void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool
printf("Handling CALL callback\n");
((CallControl*)user_data)->incoming = true;
}
void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data)
void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data)
{
printf("Handling CALL STATE callback: %s\n", stringify_state(state));
printf("Handling CALL STATE callback: %d\n", state);
((CallControl*)user_data)->state = state;
}
@ -211,13 +211,13 @@ int main (int argc, char** argv)
}
printf("\nTrying regular call (Audio and Video)...\n");
// REGULAR_CALL_FLOW(48, 4000);
REGULAR_CALL_FLOW(48, 4000);
printf("\nTrying regular call (Audio only)...\n");
// REGULAR_CALL_FLOW(48, 0);
REGULAR_CALL_FLOW(48, 0);
printf("\nTrying regular call (Video only)...\n");
// REGULAR_CALL_FLOW(0, 4000);
REGULAR_CALL_FLOW(0, 4000);
#undef REGULAR_CALL_FLOW
@ -318,10 +318,8 @@ int main (int argc, char** argv)
/* At first try all stuff while in invalid state */
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_PAUSE, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_RESUME, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_VIDEO, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_VIDEO, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO, NULL));
assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_VIDEO, NULL));
{
TOXAV_ERR_ANSWER rc;
@ -342,36 +340,36 @@ int main (int argc, char** argv)
assert(BobCC.state == TOXAV_CALL_STATE_PAUSED);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_RESUME, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_STATE_SENDING_AV);
assert(BobCC.state & (TOXAV_CALL_STATE_SENDING_A | TOXAV_CALL_STATE_SENDING_V));
/* Mute/Unmute single */
printf("Mute/Unmute single\n");
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_CONTROL_MUTE_AUDIO);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
assert(BobCC.state ^ TOXAV_CALL_STATE_RECEIVING_A);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_CONTROL_UNMUTE_AUDIO);
assert(BobCC.state & TOXAV_CALL_STATE_RECEIVING_A);
/* Mute/Unmute both */
printf("Mute/Unmute both\n");
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_STATE_SENDING_V);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_VIDEO, NULL));
assert(BobCC.state ^ TOXAV_CALL_STATE_RECEIVING_A);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_VIDEO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_STATE_NOT_SENDING);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
assert(BobCC.state ^ TOXAV_CALL_STATE_RECEIVING_V);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_STATE_SENDING_A);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_VIDEO, NULL));
assert(BobCC.state & TOXAV_CALL_STATE_RECEIVING_A);
assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_TOGGLE_MUTE_VIDEO, NULL));
iterate(Bsn, AliceAV, BobAV);
assert(BobCC.state == TOXAV_CALL_STATE_SENDING_AV);
assert(BobCC.state & TOXAV_CALL_STATE_RECEIVING_V);
{
TOXAV_ERR_CALL_CONTROL rc;
toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
printf("toxav_call_control failed: %d\n", rc);
exit(1);
@ -384,6 +382,13 @@ int main (int argc, char** argv)
printf("Success!\n");
}
toxav_kill(BobAV);
toxav_kill(AliceAV);
tox_kill(Bob);
tox_kill(Alice);
tox_kill(Bsn);
printf("\nTest successful!\n");
return 0;
}

View File

@ -343,21 +343,17 @@ CSSession *cs_new(uint32_t peer_video_frame_piece_size)
return NULL;
}
if (create_recursive_mutex(cs->queue_mutex) != 0) {
LOGGER_WARNING("Failed to create recursive mutex!");
free(cs);
return NULL;
}
cs->peer_video_frame_piece_size = peer_video_frame_piece_size;
return cs;
FAILURE:
LOGGER_WARNING("Error initializing codec session! Application might misbehave!");
cs_disable_audio_sending(cs);
cs_disable_audio_receiving(cs);
cs_disable_video_sending(cs);
cs_disable_video_receiving(cs);
free(cs);
return NULL;
}
void cs_kill(CSSession *cs)
@ -374,6 +370,8 @@ void cs_kill(CSSession *cs)
cs_disable_video_sending(cs);
cs_disable_video_receiving(cs);
pthread_mutex_destroy(cs->queue_mutex);
LOGGER_DEBUG("Terminated codec state: %p", cs);
free(cs);
}
@ -536,19 +534,12 @@ int cs_enable_video_receiving(CSSession* cs)
{
if (cs->v_decoding)
return 0;
if (create_recursive_mutex(cs->queue_mutex) != 0) {
LOGGER_WARNING("Failed to create recursive mutex!");
return -1;
}
int rc = vpx_codec_dec_init_ver(cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE,
NULL, 0, VPX_DECODER_ABI_VERSION);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
pthread_mutex_destroy(cs->queue_mutex);
return -1;
}
@ -591,7 +582,6 @@ void cs_disable_video_receiving(CSSession* cs)
cs->frame_buf = NULL;
vpx_codec_destroy(cs->v_decoder);
pthread_mutex_destroy(cs->queue_mutex);
}
}

View File

@ -99,7 +99,9 @@ void handle_msi_packet ( Messenger *m, int friend_id, const uint8_t *data, uint1
*/
void msi_register_callback ( MSISession *session, MSICallbackType callback, MSICallbackID id)
{
pthread_mutex_lock(session->mutex);
session->callbacks[id] = callback;
pthread_mutex_unlock(session->mutex);
}
MSISession *msi_new ( Messenger *messenger )
{
@ -163,15 +165,19 @@ int msi_invite ( MSISession *session, MSICall **call, uint32_t friend_id, uint8_
{
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
pthread_mutex_lock(session->mutex);
if (get_call(session, friend_id) != NULL) {
LOGGER_ERROR("Already in a call");
pthread_mutex_unlock(session->mutex);
return -1;
}
(*call) = new_call ( session, friend_id );
if ( *call == NULL )
if ( *call == NULL ) {
pthread_mutex_unlock(session->mutex);
return -1;
}
(*call)->self_capabilities = capabilities;
@ -180,7 +186,7 @@ int msi_invite ( MSISession *session, MSICall **call, uint32_t friend_id, uint8_
msg.capabilities.exists = true;
msg.capabilities.value = capabilities;
msg.vfpsz.exists = true;
msg.vfpsz.value = htons(VIDEOFRAME_PIECE_SIZE);
@ -189,28 +195,37 @@ int msi_invite ( MSISession *session, MSICall **call, uint32_t friend_id, uint8_
(*call)->state = msi_CallRequesting;
LOGGER_DEBUG("Invite sent");
pthread_mutex_unlock(session->mutex);
return 0;
}
int msi_hangup ( MSICall* call )
{
LOGGER_DEBUG("Session: %p Hanging up call with friend: %u", call->session, call->friend_id);
MSISession* session = call->session;
pthread_mutex_lock(session->mutex);
MSIMessage msg;
msg_init(&msg, requ_pop);
send_message ( call->session->messenger, call->friend_id, &msg );
send_message ( session->messenger, call->friend_id, &msg );
kill_call(call);
pthread_mutex_unlock(session->mutex);
return 0;
}
int msi_answer ( MSICall* call, uint8_t capabilities )
{
LOGGER_DEBUG("Session: %p Answering call from: %u", call->session, call->friend_id);
MSISession* session = call->session;
pthread_mutex_lock(session->mutex);
if ( call->state != msi_CallRequested ) {
/* Though sending in invalid state will not cause anything wierd
* Its better to not do it like a maniac */
LOGGER_ERROR("Call is in invalid state!");
pthread_mutex_unlock(session->mutex);
return -1;
}
@ -225,9 +240,10 @@ int msi_answer ( MSICall* call, uint8_t capabilities )
msg.vfpsz.exists = true;
msg.vfpsz.value = htons(VIDEOFRAME_PIECE_SIZE);
send_message ( call->session->messenger, call->friend_id, &msg );
send_message ( session->messenger, call->friend_id, &msg );
call->state = msi_CallActive;
pthread_mutex_unlock(session->mutex);
return 0;
}
@ -235,6 +251,9 @@ int msi_change_capabilities( MSICall* call, uint8_t capabilities )
{
LOGGER_DEBUG("Session: %p Trying to change capabilities to friend %u", call->session, call->friend_id);
MSISession* session = call->session;
pthread_mutex_lock(session->mutex);
if ( call->state != msi_CallActive ) {
/* Sending capabilities change can cause error on other side if
* the call is not active since we don't send header 'vfpsz'.
@ -244,6 +263,7 @@ int msi_change_capabilities( MSICall* call, uint8_t capabilities )
* like new. TODO: explain this better
*/
LOGGER_ERROR("Call is in invalid state!");
pthread_mutex_unlock(session->mutex);
return -1;
}
@ -257,6 +277,7 @@ int msi_change_capabilities( MSICall* call, uint8_t capabilities )
send_message ( call->session->messenger, call->friend_id, &msg );
pthread_mutex_unlock(session->mutex);
return 0;
}
@ -316,7 +337,7 @@ int msg_parse_in ( MSIMessage *dest, const uint8_t *data, uint16_t length )
case IDError:
CHECK_SIZE(it, size_constraint, 1);
CHECK_ENUM_HIGH(it, msi_ErrUndisclosed);
CHECK_ENUM_HIGH(it, msi_EUndisclosed);
SET_UINT8(it, dest->error);
break;
@ -440,7 +461,7 @@ int invoke_callback(MSICall* call, MSICallbackID cb)
if ( call->session->callbacks[cb] ) {
LOGGER_DEBUG("Invoking callback function: %d", cb);
if ( call->session->callbacks[cb] ( call->session->av, call ) != 0 ) {
LOGGER_WARNING("Callback handling failed, sending error");
LOGGER_WARNING("Callback state handling failed, sending error");
goto FAILURE;
}
@ -449,10 +470,11 @@ int invoke_callback(MSICall* call, MSICallbackID cb)
FAILURE:
/* If no callback present or error happened while handling,
* an error message will be send to friend
* an error message will be sent to friend
*/
call->error = msi_HandleError;
if (call->error == msi_ENone)
call->error = msi_EHandle;
return -1;
}
static MSICall *get_call ( MSISession *session, uint32_t friend_id )
@ -517,9 +539,12 @@ MSICall *new_call ( MSISession *session, uint32_t friend_id )
}
void kill_call ( MSICall *call )
{
/* Assume that session mutex is locked */
if ( call == NULL )
return;
LOGGER_DEBUG("Killing call: %p", call);
MSISession* session = call->session;
MSICall* prev = call->prev;
@ -529,23 +554,23 @@ void kill_call ( MSICall *call )
prev->next = next;
else if (next)
session->calls_head = next->friend_id;
else goto CLEAR;
else goto CLEAR_CONTAINER;
if (next)
next->prev = prev;
else if (prev)
session->calls_tail = prev->friend_id;
else goto CLEAR;
else goto CLEAR_CONTAINER;
session->calls[call->friend_id] = NULL;
free(call);
return;
CLEAR:
CLEAR_CONTAINER:
session->calls_head = session->calls_tail = 0;
free(session->calls);
session->calls = NULL;
free(call);
session->calls = NULL;
}
void on_peer_status(Messenger *m, int friend_id, uint8_t status, void *data)
{
@ -556,13 +581,17 @@ void on_peer_status(Messenger *m, int friend_id, uint8_t status, void *data)
case 0: { /* Friend is now offline */
LOGGER_DEBUG("Friend %d is now offline", friend_id);
pthread_mutex_lock(session->mutex);
MSICall* call = get_call(session, friend_id);
if (call == NULL)
if (call == NULL) {
pthread_mutex_unlock(session->mutex);
return;
}
invoke_callback(call, msi_OnPeerTimeout); /* Failure is ignored */
kill_call(call);
pthread_mutex_unlock(session->mutex);
}
break;
@ -580,18 +609,17 @@ void handle_push ( MSICall *call, const MSIMessage *msg )
if (!msg->capabilities.exists) {
LOGGER_WARNING("Session: %p Invalid capabilities on 'push'");
call->error = msi_InvalidMessage;
call->error = msi_EInvalidMessage;
goto FAILURE;
}
if (call->state != msi_CallActive) {
if (!msg->vfpsz.exists) {
LOGGER_WARNING("Session: %p Invalid vfpsz on 'push'");
call->error = msi_InvalidMessage;
call->error = msi_EInvalidMessage;
goto FAILURE;
}
/* Sending video frame piece size is ignored when call is active */
call->peer_vfpsz = ntohs(msg->vfpsz.value);
}
@ -610,6 +638,38 @@ void handle_push ( MSICall *call, const MSIMessage *msg )
} break;
case msi_CallActive: {
if (msg->vfpsz.exists) {
/* If peer sended video frame piece size
* while the call is already active it's probable
* that he is trying to re-call us while the call
* is not terminated on our side. We can assume that
* in this case we can automatically answer the re-call.
*/
if (call->peer_vfpsz != ntohs(msg->vfpsz.value)) {
LOGGER_WARNING("Friend sent invalid parameters for re-call");
call->error = msi_EInvalidParam;
invoke_callback(call, msi_OnError);
goto FAILURE;
}
LOGGER_INFO("Friend is recalling us");
MSIMessage msg;
msg_init(&msg, requ_push);
msg.capabilities.exists = true;
msg.capabilities.value = call->self_capabilities;
msg.vfpsz.exists = true;
msg.vfpsz.value = htons(VIDEOFRAME_PIECE_SIZE);
send_message ( call->session->messenger, call->friend_id, &msg );
/* If peer changed capabilities during re-call they will
* be handled accordingly during the next step
*/
}
/* Only act if capabilities changed */
if ( call->peer_capabilities != msg->capabilities.value) {
LOGGER_INFO("Friend is changing capabilities");
@ -629,6 +689,7 @@ void handle_push ( MSICall *call, const MSIMessage *msg )
if ( invoke_callback(call, msi_OnStart) == -1 )
goto FAILURE;
} break;
case msi_CallRequested: {
@ -646,7 +707,7 @@ FAILURE:
void handle_pop ( MSICall *call, const MSIMessage *msg )
{
assert(call);
LOGGER_DEBUG("Session: %p Handling 'pop', friend id: %d", call->session, call->friend_id);
/* callback errors are ignored */
@ -682,8 +743,6 @@ void handle_pop ( MSICall *call, const MSIMessage *msg )
}
kill_call ( call );
return;
}
void handle_msi_packet ( Messenger *m, int friend_id, const uint8_t *data, uint16_t length, void *object )
{
@ -694,30 +753,34 @@ void handle_msi_packet ( Messenger *m, int friend_id, const uint8_t *data, uint1
if ( msg_parse_in ( &msg, data, length ) == -1 ) {
LOGGER_WARNING("Error parsing message");
send_error(m, friend_id, msi_InvalidMessage);
send_error(m, friend_id, msi_EInvalidMessage);
return;
} else {
LOGGER_DEBUG("Successfully parsed message");
}
pthread_mutex_lock(session->mutex);
MSICall *call = get_call(session, friend_id);
if (call == NULL) {
if (msg.request.value != requ_push) {
send_error(m, friend_id, msi_StrayMessage);
send_error(m, friend_id, msi_EStrayMessage);
pthread_mutex_unlock(session->mutex);
return;
}
call = new_call(session, friend_id);
if (call == NULL) {
send_error(m, friend_id, msi_SystemError);
send_error(m, friend_id, msi_ESystem);
pthread_mutex_unlock(session->mutex);
return;
}
}
if (msg.request.value == requ_push)
if (msg.request.value == requ_push)
handle_push(call, &msg);
else
handle_pop(call, &msg); /* always kills the call */
pthread_mutex_unlock(session->mutex);
}

View File

@ -35,14 +35,14 @@
* Error codes.
*/
typedef enum {
msi_ErrorNone,
msi_InvalidMessage,
msi_InvalidParam,
msi_InvalidState,
msi_StrayMessage,
msi_SystemError,
msi_HandleError,
msi_ErrUndisclosed, /* NOTE: must be last enum otherwise parsing wont work */
msi_ENone,
msi_EInvalidMessage,
msi_EInvalidParam,
msi_EInvalidState,
msi_EStrayMessage,
msi_ESystem,
msi_EHandle,
msi_EUndisclosed, /* NOTE: must be last enum otherwise parsing wont work */
} MSIError;
/**
@ -118,6 +118,9 @@ typedef struct MSISession_s {
void *av;
Messenger *messenger;
/* The mutex controls async access from control
* thread(s) and core thread.
*/
pthread_mutex_t mutex[1];
MSICallbackType callbacks[7];
} MSISession;

View File

@ -40,15 +40,16 @@ enum {
video_index,
};
typedef struct ToxAVCall_s
{
typedef struct ToxAVCall_s {
ToxAV* av;
pthread_mutex_t mutex_control[1];
pthread_mutex_t mutex_encoding_audio[1];
pthread_mutex_t mutex_encoding_video[1];
pthread_mutex_t mutex_do[1];
RTPSession *rtps[2]; /* Audio is first and video is second */
CSSession *cs;
pthread_mutex_t mutex_audio_sending[1];
pthread_mutex_t mutex_video_sending[1];
/* Only audio or video can be decoded at one time */
pthread_mutex_t mutex_decoding[1];
bool active;
MSICall* msi_call;
uint32_t friend_id;
@ -56,14 +57,14 @@ typedef struct ToxAVCall_s
uint32_t s_audio_b; /* Sending audio bitrate */
uint32_t s_video_b; /* Sending video bitrate */
uint8_t last_capabilities;
uint8_t last_self_capabilities;
uint8_t last_peer_capabilities;
struct ToxAVCall_s *prev;
struct ToxAVCall_s *next;
} ToxAVCall;
struct toxAV
{
struct toxAV {
Messenger* m;
MSISession* msi;
@ -71,11 +72,14 @@ struct toxAV
ToxAVCall** calls;
uint32_t calls_tail;
uint32_t calls_head;
pthread_mutex_t mutex[1];
PAIR(toxav_call_cb *, void*) ccb; /* Call callback */
PAIR(toxav_call_state_cb *, void *) scb; /* Call state callback */
PAIR(toxav_receive_audio_frame_cb *, void *) acb; /* Audio frame receive callback */
PAIR(toxav_receive_video_frame_cb *, void *) vcb; /* Video frame receive callback */
PAIR(toxav_request_video_frame_cb *, void *) rvcb; /* Request video callback */
PAIR(toxav_request_audio_frame_cb *, void *) racb; /* Request video callback */
/** Decode time measures */
int32_t dmssc; /** Measure count */
@ -92,7 +96,6 @@ int callback_end(void* toxav_inst, MSICall* call);
int callback_error(void* toxav_inst, MSICall* call);
int callback_capabilites(void* toxav_inst, MSICall* call);
TOXAV_CALL_STATE capabilities_to_state(uint8_t capabilities);
ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error);
ToxAVCall* call_get(ToxAV* av, uint32_t friend_number);
void call_remove(ToxAVCall* call);
@ -118,7 +121,7 @@ ToxAV* toxav_new(Tox* tox, TOXAV_ERR_NEW* error)
goto FAILURE;
}
av = calloc ( sizeof(ToxAV), 1);
av = calloc (sizeof(ToxAV), 1);
if (av == NULL) {
LOGGER_WARNING("Allocation failed!");
@ -126,10 +129,17 @@ ToxAV* toxav_new(Tox* tox, TOXAV_ERR_NEW* error)
goto FAILURE;
}
if (create_recursive_mutex(av->mutex) == -1) {
LOGGER_WARNING("Mutex creation failed!");
rc = TOXAV_ERR_NEW_MALLOC;
goto FAILURE;
}
av->m = (Messenger *)tox;
av->msi = msi_new(av->m);
if (av->msi == NULL) {
pthread_mutex_destroy(av->mutex);
rc = TOXAV_ERR_NEW_MALLOC;
goto FAILURE;
}
@ -163,6 +173,7 @@ void toxav_kill(ToxAV* av)
{
if (av == NULL)
return;
pthread_mutex_lock(av->mutex);
msi_kill(av->msi);
@ -175,6 +186,8 @@ void toxav_kill(ToxAV* av)
}
}
pthread_mutex_unlock(av->mutex);
pthread_mutex_destroy(av->mutex);
free(av);
}
@ -185,6 +198,7 @@ Tox* toxav_get_tox(ToxAV* av)
uint32_t toxav_iteration_interval(const ToxAV* av)
{
/* If no call is active interval is 200 */
return av->calls ? av->interval : 200;
}
@ -194,14 +208,28 @@ void toxav_iteration(ToxAV* av)
return;
uint64_t start = current_time_monotonic();
uint32_t rc = 200 + av->dmssa; /* If no call is active interval is 200 */
uint32_t rc = 0;
pthread_mutex_lock(av->mutex);
ToxAVCall* i = av->calls[av->calls_head];
for (; i; i = i->next) {
if (i->active) {
pthread_mutex_lock(i->mutex_decoding);
/* TODO make AV synchronisation */
if (av->racb.first)
av->racb.first(av, i->friend_id, av->racb.second);
if (av->rvcb.first)
av->rvcb.first(av, i->friend_id, av->rvcb.second);
pthread_mutex_unlock(av->mutex);
cs_do(i->cs);
rc = MIN(i->cs->last_packet_frame_duration, rc);
}
pthread_mutex_unlock(i->mutex_decoding);
} else
continue;
pthread_mutex_lock(av->mutex);
}
av->interval = rc < av->dmssa ? 0 : rc - av->dmssa;
@ -216,38 +244,47 @@ void toxav_iteration(ToxAV* av)
bool toxav_call(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL* error)
{
pthread_mutex_lock(av->mutex);
ToxAVCall* call = call_new(av, friend_number, error);
if (call == NULL)
if (call == NULL) {
pthread_mutex_unlock(av->mutex);
return false;
}
call->s_audio_b = audio_bit_rate;
call->s_video_b = video_bit_rate;
call->last_capabilities = msi_CapRAudio | msi_CapRVideo;
call->last_self_capabilities = msi_CapRAudio | msi_CapRVideo;
call->last_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
call->last_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
call->last_self_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
call->last_self_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
if (msi_invite(av->msi, &call->msi_call, friend_number, call->last_capabilities) != 0) {
if (msi_invite(av->msi, &call->msi_call, friend_number, call->last_self_capabilities) != 0) {
call_remove(call);
if (error)
*error = TOXAV_ERR_CALL_MALLOC;
pthread_mutex_unlock(av->mutex);
return false;
}
call->msi_call->av_call = call;
pthread_mutex_unlock(av->mutex);
return true;
}
void toxav_callback_call(ToxAV* av, toxav_call_cb* function, void* user_data)
{
pthread_mutex_lock(av->mutex);
av->ccb.first = function;
av->ccb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
bool toxav_answer(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER* error)
{
pthread_mutex_lock(av->mutex);
TOXAV_ERR_ANSWER rc = TOXAV_ERR_ANSWER_OK;
if (m_friend_exists(av->m, friend_number) == 0) {
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
@ -270,16 +307,18 @@ bool toxav_answer(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, ui
call->s_audio_b = audio_bit_rate;
call->s_video_b = video_bit_rate;
call->last_capabilities = msi_CapRAudio | msi_CapRVideo;
call->last_self_capabilities = msi_CapRAudio | msi_CapRVideo;
call->last_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
call->last_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
call->last_self_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
call->last_self_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
if (msi_answer(call->msi_call, call->last_capabilities) != 0)
if (msi_answer(call->msi_call, call->last_self_capabilities) != 0)
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING; /* the only reason for msi_answer to fail */
END:
pthread_mutex_unlock(av->mutex);
if (error)
*error = rc;
@ -288,12 +327,15 @@ END:
void toxav_callback_call_state(ToxAV* av, toxav_call_state_cb* function, void* user_data)
{
pthread_mutex_lock(av->mutex);
av->scb.first = function;
av->scb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL* error)
{
pthread_mutex_lock(av->mutex);
TOXAV_ERR_CALL_CONTROL rc = TOXAV_ERR_CALL_CONTROL_OK;
if (m_friend_exists(av->m, friend_number) == 0) {
@ -308,9 +350,7 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
goto END;
}
/* TODO rest of these */
switch (control)
{
switch (control) {
case TOXAV_CALL_CONTROL_RESUME: {
if (!call->active) {
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
@ -319,10 +359,10 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
/* Only act if paused and had media transfer active before */
if (call->msi_call->self_capabilities == 0 &&
call->last_capabilities ) {
call->last_self_capabilities ) {
if (msi_change_capabilities(call->msi_call,
call->last_capabilities) == -1) {
call->last_self_capabilities) == -1) {
/* The only reason for this function to fail is invalid state
* ( not active ) */
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
@ -342,7 +382,7 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
/* Only act if not already paused */
if (call->msi_call->self_capabilities) {
call->last_capabilities = call->msi_call->self_capabilities;
call->last_self_capabilities = call->msi_call->self_capabilities;
if (msi_change_capabilities(call->msi_call, 0) == -1 ) {
/* The only reason for this function to fail is invalid state
@ -365,7 +405,7 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
call_remove(call);
} break;
case TOXAV_CALL_CONTROL_MUTE_AUDIO: {
case TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO: {
if (!call->active) {
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
@ -381,10 +421,23 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
}
rtp_stop_receiving(call->rtps[audio_index]);
} else {
/* This call was already muted so notify the friend that he can
* start sending audio again
*/
if (msi_change_capabilities(call->msi_call, call->
msi_call->self_capabilities | msi_CapRAudio) == -1) {
/* The only reason for this function to fail is invalid state
* ( not active ) */
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
}
rtp_start_receiving(call->rtps[audio_index]);
}
} break;
case TOXAV_CALL_CONTROL_MUTE_VIDEO: {
case TOXAV_CALL_CONTROL_TOGGLE_MUTE_VIDEO: {
if (!call->active) {
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
@ -400,35 +453,10 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
}
rtp_stop_receiving(call->rtps[video_index]);
}
} break;
case TOXAV_CALL_CONTROL_UNMUTE_AUDIO: {
if (!call->active) {
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
}
if (call->msi_call->self_capabilities & ~msi_CapRAudio) {
if (msi_change_capabilities(call->msi_call, call->
msi_call->self_capabilities | msi_CapRAudio) == -1) {
/* The only reason for this function to fail is invalid state
* ( not active ) */
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
}
rtp_start_receiving(call->rtps[audio_index]);
}
} break;
case TOXAV_CALL_CONTROL_UNMUTE_VIDEO: {
if (!call->active) {
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
goto END;
}
if (call->msi_call->self_capabilities & ~msi_CapRVideo) {
} else {
/* This call was already muted so notify the friend that he can
* start sending video again
*/
if (msi_change_capabilities(call->msi_call, call->
msi_call->self_capabilities | msi_CapRVideo) == -1) {
/* The only reason for this function to fail is invalid state
@ -443,6 +471,8 @@ bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL co
}
END:
pthread_mutex_unlock(av->mutex);
if (error)
*error = rc;
@ -461,7 +491,10 @@ bool toxav_set_video_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t video_
void toxav_callback_request_video_frame(ToxAV* av, toxav_request_video_frame_cb* function, void* user_data)
{
/* TODO */
pthread_mutex_lock(av->mutex);
av->rvcb.first = function;
av->rvcb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t* y, const uint8_t* u, const uint8_t* v, TOXAV_ERR_SEND_FRAME* error)
@ -474,24 +507,32 @@ bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, u
goto END;
}
pthread_mutex_lock(av->mutex);
call = call_get(av, friend_number);
if (call == NULL) {
if (call == NULL || !call->active) {
pthread_mutex_unlock(av->mutex);
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
goto END;
}
pthread_mutex_lock(call->mutex_video_sending);
pthread_mutex_unlock(av->mutex);
if (call->msi_call->state != msi_CallActive) {
/* TODO */
pthread_mutex_unlock(call->mutex_video_sending);
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
goto END;
}
if ( y == NULL || u == NULL || v == NULL ) {
pthread_mutex_unlock(call->mutex_video_sending);
rc = TOXAV_ERR_SEND_FRAME_NULL;
goto END;
}
if ( cs_set_sending_video_resolution(call->cs, width, height) != 0 ) {
pthread_mutex_unlock(call->mutex_video_sending);
rc = TOXAV_ERR_SEND_FRAME_INVALID;
goto END;
}
@ -513,6 +554,7 @@ bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, u
vpx_img_free(&img); /* FIXME don't free? */
if ( vrc != VPX_CODEC_OK) {
pthread_mutex_unlock(call->mutex_video_sending);
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(vrc));
rc = TOXAV_ERR_SEND_FRAME_INVALID;
goto END;
@ -542,13 +584,18 @@ bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, u
for (i = 0; i < parts; i++) {
iter = cs_iterate_split_video_frame(call->cs, &part_size);
if (rtp_send_msg(call->rtps[video_index], iter, part_size) < 0)
if (rtp_send_msg(call->rtps[video_index], iter, part_size) < 0) {
pthread_mutex_unlock(call->mutex_video_sending);
LOGGER_WARNING("Could not send video frame: %s\n", strerror(errno));
goto END;
}
}
}
}
}
pthread_mutex_unlock(call->mutex_video_sending);
END:
if (error)
*error = rc;
@ -558,7 +605,10 @@ END:
void toxav_callback_request_audio_frame(ToxAV* av, toxav_request_audio_frame_cb* function, void* user_data)
{
/* TODO */
pthread_mutex_lock(av->mutex);
av->racb.first = function;
av->racb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, TOXAV_ERR_SEND_FRAME* error)
@ -571,24 +621,32 @@ bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pc
goto END;
}
pthread_mutex_lock(av->mutex);
call = call_get(av, friend_number);
if (call == NULL) {
if (call == NULL || !call->active) {
pthread_mutex_unlock(av->mutex);
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
goto END;
}
pthread_mutex_lock(call->mutex_audio_sending);
pthread_mutex_unlock(av->mutex);
if (call->msi_call->state != msi_CallActive) {
/* TODO */
pthread_mutex_unlock(call->mutex_audio_sending);
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
goto END;
}
if ( pcm == NULL ) {
pthread_mutex_unlock(call->mutex_audio_sending);
rc = TOXAV_ERR_SEND_FRAME_NULL;
goto END;
}
if ( channels != 1 || channels != 2 ) {
pthread_mutex_unlock(call->mutex_audio_sending);
rc = TOXAV_ERR_SEND_FRAME_INVALID;
goto END;
}
@ -604,6 +662,7 @@ bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pc
if (vrc < 0) {
LOGGER_WARNING("Failed to encode frame");
rc = TOXAV_ERR_SEND_FRAME_INVALID;
pthread_mutex_unlock(call->mutex_audio_sending);
goto END;
}
@ -611,6 +670,8 @@ bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pc
/* TODO check for error? */
}
pthread_mutex_unlock(call->mutex_audio_sending);
END:
if (error)
*error = rc;
@ -620,14 +681,18 @@ END:
void toxav_callback_receive_video_frame(ToxAV* av, toxav_receive_video_frame_cb* function, void* user_data)
{
pthread_mutex_lock(av->mutex);
av->vcb.first = function;
av->vcb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
void toxav_callback_receive_audio_frame(ToxAV* av, toxav_receive_audio_frame_cb* function, void* user_data)
{
pthread_mutex_lock(av->mutex);
av->acb.first = function;
av->acb.second = user_data;
pthread_mutex_unlock(av->mutex);
}
@ -639,10 +704,12 @@ void toxav_callback_receive_audio_frame(ToxAV* av, toxav_receive_audio_frame_cb*
int callback_invite(void* toxav_inst, MSICall* call)
{
ToxAV* toxav = toxav_inst;
pthread_mutex_lock(toxav->mutex);
ToxAVCall* av_call = call_new(toxav, call->friend_id, NULL);
if (av_call == NULL) {
LOGGER_WARNING("Failed to initialize call...");
pthread_mutex_unlock(toxav->mutex);
return -1;
}
@ -653,36 +720,41 @@ int callback_invite(void* toxav_inst, MSICall* call)
toxav->ccb.first(toxav, call->friend_id, call->peer_capabilities & msi_CapSAudio,
call->peer_capabilities & msi_CapSVideo, toxav->ccb.second);
pthread_mutex_unlock(toxav->mutex);
return 0;
}
int callback_start(void* toxav_inst, MSICall* call)
{
ToxAV* toxav = toxav_inst;
pthread_mutex_lock(toxav->mutex);
ToxAVCall* av_call = call_get(toxav, call->friend_id);
if (av_call == NULL) {
/* Should this ever happen? */
pthread_mutex_unlock(toxav->mutex);
return -1;
}
if (!call_prepare_transmission(av_call)) {
callback_error(toxav_inst, call);
call_remove(av_call);
pthread_mutex_unlock(toxav->mutex);
return -1;
}
if (toxav->scb.first)
toxav->scb.first(toxav, call->friend_id,
capabilities_to_state(call->peer_capabilities), toxav->scb.second);
toxav->scb.first(toxav, call->friend_id, call->peer_capabilities, toxav->scb.second);
pthread_mutex_unlock(toxav->mutex);
return 0;
}
int callback_end(void* toxav_inst, MSICall* call)
{
ToxAV* toxav = toxav_inst;
pthread_mutex_lock(toxav->mutex);
if (toxav->scb.first)
toxav->scb.first(toxav, call->friend_id, TOXAV_CALL_STATE_END, toxav->scb.second);
@ -690,43 +762,39 @@ int callback_end(void* toxav_inst, MSICall* call)
call_kill_transmission(call->av_call);
call_remove(call->av_call);
pthread_mutex_unlock(toxav->mutex);
return 0;
}
int callback_error(void* toxav_inst, MSICall* call)
{
ToxAV* toxav = toxav_inst;
pthread_mutex_lock(toxav->mutex);
if (toxav->scb.first)
toxav->scb.first(toxav, call->friend_id, TOXAV_CALL_STATE_ERROR, toxav->scb.second);
call_kill_transmission(call->av_call);
call_remove(call->av_call);
pthread_mutex_unlock(toxav->mutex);
return 0;
}
int callback_capabilites(void* toxav_inst, MSICall* call)
{
ToxAV* toxav = toxav_inst;
pthread_mutex_lock(toxav->mutex);
/* TODO modify cs? */
if (toxav->scb.first)
toxav->scb.first(toxav, call->friend_id,
capabilities_to_state(call->peer_capabilities), toxav->scb.second);
toxav->scb.first(toxav, call->friend_id, call->peer_capabilities, toxav->scb.second);
pthread_mutex_unlock(toxav->mutex);
return 0;
}
TOXAV_CALL_STATE capabilities_to_state(uint8_t capabilities)
{
if (capabilities == 0)
return TOXAV_CALL_STATE_PAUSED;
else if ((capabilities & msi_CapSAudio) && (capabilities & msi_CapSVideo))
return TOXAV_CALL_STATE_SENDING_AV;
else if (capabilities & msi_CapSAudio)
return TOXAV_CALL_STATE_SENDING_A;
else if (capabilities & msi_CapSVideo)
return TOXAV_CALL_STATE_SENDING_V;
return TOXAV_CALL_STATE_NOT_SENDING;
}
bool audio_bitrate_invalid(uint32_t bitrate)
{
/* Opus RFC 6716 section-2.1.1 dictates the following:
@ -743,6 +811,7 @@ bool video_bitrate_invalid(uint32_t bitrate)
ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error)
{
/* Assumes mutex locked */
TOXAV_ERR_CALL rc = TOXAV_ERR_CALL_OK;
ToxAVCall* call = NULL;
@ -772,28 +841,10 @@ ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error)
call->av = av;
call->friend_id = friend_number;
if (create_recursive_mutex(call->mutex_control) != 0) {
free(call);
call = NULL;
rc = TOXAV_ERR_CALL_MALLOC;
goto END;
}
if (create_recursive_mutex(call->mutex_do) != 0) {
pthread_mutex_destroy(call->mutex_control);
free(call);
call = NULL;
rc = TOXAV_ERR_CALL_MALLOC;
goto END;
}
if (av->calls == NULL) { /* Creating */
av->calls = calloc (sizeof(ToxAVCall*), friend_number + 1);
if (av->calls == NULL) {
pthread_mutex_destroy(call->mutex_control);
pthread_mutex_destroy(call->mutex_do);
free(call);
call = NULL;
rc = TOXAV_ERR_CALL_MALLOC;
@ -806,8 +857,6 @@ ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error)
void* tmp = realloc(av->calls, sizeof(ToxAVCall*) * friend_number + 1);
if (tmp == NULL) {
pthread_mutex_destroy(call->mutex_control);
pthread_mutex_destroy(call->mutex_do);
free(call);
call = NULL;
rc = TOXAV_ERR_CALL_MALLOC;
@ -843,6 +892,7 @@ END:
ToxAVCall* call_get(ToxAV* av, uint32_t friend_number)
{
/* Assumes mutex locked */
if (av->calls == NULL || av->calls_tail < friend_number)
return NULL;
@ -851,6 +901,8 @@ ToxAVCall* call_get(ToxAV* av, uint32_t friend_number)
bool call_prepare_transmission(ToxAVCall* call)
{
/* Assumes mutex locked */
if (call == NULL)
return false;
@ -860,25 +912,22 @@ bool call_prepare_transmission(ToxAVCall* call)
/* It makes no sense to have CSession without callbacks */
return false;
pthread_mutex_lock(call->mutex_control);
if (call->active) {
pthread_mutex_unlock(call->mutex_control);
LOGGER_WARNING("Call already active!\n");
return true;
}
if (pthread_mutex_init(call->mutex_encoding_audio, NULL) != 0)
if (pthread_mutex_init(call->mutex_audio_sending, NULL) != 0)
goto MUTEX_INIT_ERROR;
if (pthread_mutex_init(call->mutex_encoding_video, NULL) != 0) {
pthread_mutex_destroy(call->mutex_encoding_audio);
if (pthread_mutex_init(call->mutex_video_sending, NULL) != 0) {
pthread_mutex_destroy(call->mutex_audio_sending);
goto MUTEX_INIT_ERROR;
}
if (pthread_mutex_init(call->mutex_do, NULL) != 0) {
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
if (pthread_mutex_init(call->mutex_decoding, NULL) != 0) {
pthread_mutex_destroy(call->mutex_audio_sending);
pthread_mutex_destroy(call->mutex_video_sending);
goto MUTEX_INIT_ERROR;
}
@ -945,7 +994,6 @@ bool call_prepare_transmission(ToxAVCall* call)
}
call->active = 1;
pthread_mutex_unlock(call->mutex_control);
return true;
FAILURE:
@ -955,16 +1003,12 @@ FAILURE:
call->rtps[video_index] = NULL;
cs_kill(call->cs);
call->cs = NULL;
call->active = 0;
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
pthread_mutex_destroy(call->mutex_do);
pthread_mutex_unlock(call->mutex_control);
pthread_mutex_destroy(call->mutex_audio_sending);
pthread_mutex_destroy(call->mutex_video_sending);
pthread_mutex_destroy(call->mutex_decoding);
return false;
MUTEX_INIT_ERROR:
pthread_mutex_unlock(call->mutex_control);
LOGGER_ERROR("Mutex initialization failed!\n");
return false;
}
@ -974,29 +1018,26 @@ void call_kill_transmission(ToxAVCall* call)
if (call == NULL || call->active == 0)
return;
pthread_mutex_lock(call->mutex_control);
call->active = 0;
pthread_mutex_lock(call->mutex_encoding_audio);
pthread_mutex_unlock(call->mutex_encoding_audio);
pthread_mutex_lock(call->mutex_encoding_video);
pthread_mutex_unlock(call->mutex_encoding_video);
pthread_mutex_lock(call->mutex_do);
pthread_mutex_unlock(call->mutex_do);
pthread_mutex_lock(call->mutex_audio_sending);
pthread_mutex_unlock(call->mutex_audio_sending);
pthread_mutex_lock(call->mutex_video_sending);
pthread_mutex_unlock(call->mutex_video_sending);
pthread_mutex_lock(call->mutex_decoding);
pthread_mutex_unlock(call->mutex_decoding);
rtp_kill(call->rtps[audio_index]);
call->rtps[audio_index] = NULL;
rtp_kill(call->rtps[video_index]);
call->rtps[video_index] = NULL;
cs_kill(call->cs);
call->cs = NULL;
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
pthread_mutex_destroy(call->mutex_do);
pthread_mutex_unlock(call->mutex_control);
pthread_mutex_destroy(call->mutex_audio_sending);
pthread_mutex_destroy(call->mutex_video_sending);
pthread_mutex_destroy(call->mutex_decoding);
}
void call_remove(ToxAVCall* call)
@ -1010,9 +1051,6 @@ void call_remove(ToxAVCall* call)
ToxAVCall* prev = call->prev;
ToxAVCall* next = call->next;
pthread_mutex_destroy(call->mutex_control);
pthread_mutex_destroy(call->mutex_do);
free(call);
if (prev)

View File

@ -187,43 +187,41 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui
******************************************************************************/
typedef enum TOXAV_CALL_STATE {
/**
* Not sending anything. Either the friend requested that this client stops
* sending anything, or the client turned off both audio and video by setting
* the respective bit rates to 0.
*
* If both sides are in this state, the call is effectively on hold, but not
* in the PAUSED state.
* Not sending nor receiving anything, meaning, one of the sides requested pause.
* The call will be resumed once the side that initiated pause resumes it.
*/
TOXAV_CALL_STATE_NOT_SENDING,
TOXAV_CALL_STATE_PAUSED = 0,
/**
* Sending audio only. Either the friend requested that this client stops
* sending video, or the client turned off video by setting the video bit rate
* to 0.
* The flag that marks that friend is sending audio.
*/
TOXAV_CALL_STATE_SENDING_A,
TOXAV_CALL_STATE_SENDING_A = 1,
/**
* Sending video only. Either the friend requested that this client stops
* sending audio (muted), or the client turned off audio by setting the audio
* bit rate to 0.
* The flag that marks that friend is sending video.
*/
TOXAV_CALL_STATE_SENDING_V,
TOXAV_CALL_STATE_SENDING_V = 2,
/**
* Sending both audio and video.
* The flag that marks that friend is receiving audio.
*/
TOXAV_CALL_STATE_SENDING_AV,
TOXAV_CALL_STATE_RECEIVING_A = 4,
/**
* The call is on hold. Both sides stop sending and receiving.
* The flag that marks that friend is receiving video.
*/
TOXAV_CALL_STATE_PAUSED,
TOXAV_CALL_STATE_RECEIVING_V = 8,
/**
* The core will never set TOXAV_CALL_STATE_END or TOXAV_CALL_STATE_ERROR
* together with other states.
*/
/**
* The call has finished. This is the final state after which no more state
* transitions can occur for the call.
*/
TOXAV_CALL_STATE_END,
TOXAV_CALL_STATE_END = 16,
/**
* Sent by the AV core if an error occurred on the remote end.
*/
TOXAV_CALL_STATE_ERROR
TOXAV_CALL_STATE_ERROR = 32
} TOXAV_CALL_STATE;
/**
* The function type for the `call_state` callback.
@ -231,7 +229,7 @@ typedef enum TOXAV_CALL_STATE {
* @param friend_number The friend number for which the call state changed.
* @param state The new call state.
*/
typedef void toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data);
typedef void toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data);
/**
* Set the callback for the `call_state` event. Pass NULL to unset.
*
@ -261,27 +259,19 @@ typedef enum TOXAV_CALL_CONTROL {
/**
* Request that the friend stops sending audio. Regardless of the friend's
* compliance, this will cause the `receive_audio_frame` event to stop being
* triggered on receiving an audio frame from the friend.
* triggered on receiving an audio frame from the friend. If the audio was
* already muted, calling this control will notify client to start sending
* audio again.
*/
TOXAV_CALL_CONTROL_MUTE_AUDIO,
TOXAV_CALL_CONTROL_TOGGLE_MUTE_AUDIO,
/**
* Request that the friend stops sending video. Regardless of the friend's
* compliance, this will cause the `receive_video_frame` event to stop being
* triggered on receiving an video frame from the friend.
* triggered on receiving an video frame from the friend. If the video was
* already muted, calling this control will notify client to start sending
* video again.
*/
TOXAV_CALL_CONTROL_MUTE_VIDEO,
/**
* Notify the friend that we are AGAIN ready to handle incoming audio.
* This control will not work if the call is not started with audio
* initiated.
*/
TOXAV_CALL_CONTROL_UNMUTE_AUDIO,
/**
* Notify the friend that we are AGAIN ready to handle incoming video.
* This control will not work if the call is not started with video
* initiated.
*/
TOXAV_CALL_CONTROL_UNMUTE_VIDEO
TOXAV_CALL_CONTROL_TOGGLE_MUTE_VIDEO,
} TOXAV_CALL_CONTROL;
typedef enum TOXAV_ERR_CALL_CONTROL {
TOXAV_ERR_CALL_CONTROL_OK,