mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Make rtp & codec actions thread-safe
This commit is contained in:
parent
a8fa360547
commit
8b9d3992a4
|
@ -80,8 +80,12 @@ JitterBuffer *create_queue(int capacity)
|
||||||
|
|
||||||
void terminate_queue(JitterBuffer *q)
|
void terminate_queue(JitterBuffer *q)
|
||||||
{
|
{
|
||||||
|
if (!q) return;
|
||||||
|
|
||||||
empty_queue(q);
|
empty_queue(q);
|
||||||
free(q->queue);
|
free(q->queue);
|
||||||
|
|
||||||
|
LOGGER_DEBUG("Terminated jitter buffer: %p", q);
|
||||||
free(q);
|
free(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,6 +318,8 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
|
||||||
|
|
||||||
void codec_terminate_session ( CodecState *cs )
|
void codec_terminate_session ( CodecState *cs )
|
||||||
{
|
{
|
||||||
|
if (!cs) return;
|
||||||
|
|
||||||
if ( cs->audio_encoder )
|
if ( cs->audio_encoder )
|
||||||
opus_encoder_destroy(cs->audio_encoder);
|
opus_encoder_destroy(cs->audio_encoder);
|
||||||
|
|
||||||
|
@ -325,6 +331,9 @@ void codec_terminate_session ( CodecState *cs )
|
||||||
|
|
||||||
if ( cs->capabilities & v_encoding )
|
if ( cs->capabilities & v_encoding )
|
||||||
vpx_codec_destroy(&cs->v_encoder);
|
vpx_codec_destroy(&cs->v_encoder);
|
||||||
|
|
||||||
|
LOGGER_DEBUG("Terminated codec state: %p", cs);
|
||||||
|
free(cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float calculate_sum_sq (int16_t *n, uint16_t k)
|
inline float calculate_sum_sq (int16_t *n, uint16_t k)
|
||||||
|
|
166
toxav/msi.c
166
toxav/msi.c
|
@ -1043,12 +1043,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t _call_idx = 0;
|
int32_t call_idx = 0;
|
||||||
|
|
||||||
for (; _call_idx < session->max_calls; _call_idx ++) {
|
for (; call_idx < session->max_calls; call_idx ++) {
|
||||||
if ( !session->calls[_call_idx] ) {
|
if ( !session->calls[call_idx] ) {
|
||||||
|
|
||||||
if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
|
if (!(session->calls[call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
|
||||||
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1057,35 +1057,35 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _call_idx == session->max_calls ) {
|
if ( call_idx == session->max_calls ) {
|
||||||
LOGGER_WARNING("Reached maximum amount of calls!");
|
LOGGER_WARNING("Reached maximum amount of calls!");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MSICall *_call = session->calls[_call_idx];
|
MSICall *call = session->calls[call_idx];
|
||||||
|
|
||||||
_call->call_idx = _call_idx;
|
call->call_idx = call_idx;
|
||||||
|
|
||||||
if ( !(_call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
|
if ( !(call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
|
||||||
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
||||||
free(_call);
|
free(call);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
_call->session = session;
|
call->session = session;
|
||||||
|
|
||||||
/*_call->_participant_count = _peers;*/
|
/*_call->_participant_count = _peers;*/
|
||||||
|
|
||||||
_call->request_timer_id = 0;
|
call->request_timer_id = 0;
|
||||||
_call->ringing_timer_id = 0;
|
call->ringing_timer_id = 0;
|
||||||
|
|
||||||
_call->ringing_tout_ms = ringing_timeout;
|
call->ringing_tout_ms = ringing_timeout;
|
||||||
|
|
||||||
pthread_mutex_init ( &_call->mutex, NULL );
|
pthread_mutex_init ( call->mutex, NULL );
|
||||||
|
|
||||||
LOGGER_DEBUG("Started new call with index: %u", _call_idx);
|
LOGGER_DEBUG("Started new call with index: %u", call_idx);
|
||||||
return _call;
|
return call;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1104,7 +1104,7 @@ int terminate_call ( MSISession *session, MSICall *call )
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rc = pthread_mutex_trylock(&session->mutex); /* Lock if not locked */
|
int rc = pthread_mutex_trylock(session->mutex); /* Lock if not locked */
|
||||||
|
|
||||||
LOGGER_DEBUG("Terminated call id: %d", call->call_idx);
|
LOGGER_DEBUG("Terminated call id: %d", call->call_idx);
|
||||||
/* Check event loop and cancel timed events if there are any
|
/* Check event loop and cancel timed events if there are any
|
||||||
|
@ -1115,7 +1115,7 @@ int terminate_call ( MSISession *session, MSICall *call )
|
||||||
timer_release ( session->timer_handler, call->ringing_timer_id );
|
timer_release ( session->timer_handler, call->ringing_timer_id );
|
||||||
|
|
||||||
/* Get a handle */
|
/* Get a handle */
|
||||||
pthread_mutex_lock ( &call->mutex );
|
pthread_mutex_lock ( call->mutex );
|
||||||
|
|
||||||
session->calls[call->call_idx] = NULL;
|
session->calls[call->call_idx] = NULL;
|
||||||
|
|
||||||
|
@ -1123,14 +1123,14 @@ int terminate_call ( MSISession *session, MSICall *call )
|
||||||
free ( call->peers);
|
free ( call->peers);
|
||||||
|
|
||||||
/* Release handle */
|
/* Release handle */
|
||||||
pthread_mutex_unlock ( &call->mutex );
|
pthread_mutex_unlock ( call->mutex );
|
||||||
|
|
||||||
pthread_mutex_destroy ( &call->mutex );
|
pthread_mutex_destroy ( call->mutex );
|
||||||
|
|
||||||
free ( call );
|
free ( call );
|
||||||
|
|
||||||
if ( rc != EBUSY ) /* Unlock if locked by this call */
|
if ( rc != EBUSY ) /* Unlock if locked by this call */
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1174,7 +1174,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("Session: %p Handling 'invite' on call: %s", session, call ? (char *)call->id : "making new");
|
LOGGER_DEBUG("Session: %p Handling 'invite' on call: %s", session, call ? (char *)call->id : "making new");
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
|
|
||||||
if ( call ) {
|
if ( call ) {
|
||||||
|
@ -1194,27 +1194,27 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
call = init_call ( session, 1, 0 );
|
call = init_call ( session, 1, 0 );
|
||||||
|
|
||||||
if ( !call ) {
|
if ( !call ) {
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
LOGGER_ERROR("Starting call");
|
LOGGER_ERROR("Starting call");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0; /* Wait for ringing from peer */
|
return 0; /* Wait for ringing from peer */
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
|
send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
|
||||||
terminate_call(session, call);
|
terminate_call(session, call);
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
call = init_call ( session, 1, 0 );
|
call = init_call ( session, 1, 0 );
|
||||||
|
|
||||||
if ( !call ) {
|
if ( !call ) {
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
LOGGER_ERROR("Starting call");
|
LOGGER_ERROR("Starting call");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1223,7 +1223,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
if ( !msg->callid.header_value ) {
|
if ( !msg->callid.header_value ) {
|
||||||
send_error ( session, call, error_no_callid, msg->friend_id );
|
send_error ( session, call, error_no_callid, msg->friend_id );
|
||||||
terminate_call(session, call);
|
terminate_call(session, call);
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1238,7 +1238,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
send_message ( session, call, _msg_ringing, msg->friend_id );
|
send_message ( session, call, _msg_ringing, msg->friend_id );
|
||||||
free_message ( _msg_ringing );
|
free_message ( _msg_ringing );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnInvite);
|
invoke_callback(call->call_idx, MSI_OnInvite);
|
||||||
|
|
||||||
|
@ -1253,13 +1253,13 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id );
|
LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id );
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
call->state = call_active;
|
call->state = call_active;
|
||||||
|
|
||||||
flush_peer_type ( call, msg, 0 );
|
flush_peer_type ( call, msg, 0 );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnStart);
|
invoke_callback(call->call_idx, MSI_OnStart);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1273,14 +1273,14 @@ int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->id);
|
LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->id);
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
|
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
|
||||||
send_message ( session, call, _msg_ending, msg->friend_id );
|
send_message ( session, call, _msg_ending, msg->friend_id );
|
||||||
free_message ( _msg_ending );
|
free_message ( _msg_ending );
|
||||||
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnReject);
|
invoke_callback(call->call_idx, MSI_OnReject);
|
||||||
|
|
||||||
|
@ -1296,11 +1296,11 @@ int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->id );
|
LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->id );
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
/* Act as end message */
|
/* Act as end message */
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
invoke_callback(call->call_idx, MSI_OnCancel);
|
invoke_callback(call->call_idx, MSI_OnCancel);
|
||||||
|
|
||||||
terminate_call ( session, call );
|
terminate_call ( session, call );
|
||||||
|
@ -1315,13 +1315,13 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'end' on call: %s", session, call->id );
|
LOGGER_DEBUG("Session: %p Handling 'end' on call: %s", session, call->id );
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
|
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
|
||||||
send_message ( session, call, _msg_ending, msg->friend_id );
|
send_message ( session, call, _msg_ending, msg->friend_id );
|
||||||
free_message ( _msg_ending );
|
free_message ( _msg_ending );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnEnd);
|
invoke_callback(call->call_idx, MSI_OnEnd);
|
||||||
|
|
||||||
|
@ -1337,11 +1337,11 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
if ( call->ringing_timer_id ) {
|
if ( call->ringing_timer_id ) {
|
||||||
LOGGER_WARNING("Call already ringing");
|
LOGGER_WARNING("Call already ringing");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1349,7 +1349,7 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
call->ringing_timer_id = timer_alloc ( session->timer_handler, handle_timeout, call, call->ringing_tout_ms );
|
call->ringing_timer_id = timer_alloc ( session->timer_handler, handle_timeout, call, call->ringing_tout_ms );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnRinging);
|
invoke_callback(call->call_idx, MSI_OnRinging);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1361,7 +1361,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
|
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
|
||||||
|
|
||||||
|
@ -1375,7 +1375,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
|
|
||||||
|
|
||||||
timer_release ( session->timer_handler, call->ringing_timer_id );
|
timer_release ( session->timer_handler, call->ringing_timer_id );
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnStarting);
|
invoke_callback(call->call_idx, MSI_OnStarting);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1387,14 +1387,14 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
|
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
|
||||||
|
|
||||||
/* Stop timer */
|
/* Stop timer */
|
||||||
timer_release ( session->timer_handler, call->request_timer_id );
|
timer_release ( session->timer_handler, call->request_timer_id );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnEnding);
|
invoke_callback(call->call_idx, MSI_OnEnding);
|
||||||
|
|
||||||
|
@ -1405,11 +1405,11 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
}
|
}
|
||||||
int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
|
int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
if ( !call ) {
|
if ( !call ) {
|
||||||
LOGGER_WARNING("Handling 'error' on non-existing call!");
|
LOGGER_WARNING("Handling 'error' on non-existing call!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1422,7 +1422,7 @@ int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
|
||||||
LOGGER_DEBUG("Error reason: %s", session->last_error_str);
|
LOGGER_DEBUG("Error reason: %s", session->last_error_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
invoke_callback(call->call_idx, MSI_OnEnding);
|
invoke_callback(call->call_idx, MSI_OnEnding);
|
||||||
|
|
||||||
|
@ -1603,38 +1603,38 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
MSISession *_retu = calloc ( sizeof ( MSISession ), 1 );
|
MSISession *retu = calloc ( sizeof ( MSISession ), 1 );
|
||||||
|
|
||||||
if (_retu == NULL) {
|
if (retu == NULL) {
|
||||||
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
_retu->messenger_handle = messenger;
|
retu->messenger_handle = messenger;
|
||||||
_retu->agent_handler = NULL;
|
retu->agent_handler = NULL;
|
||||||
_retu->timer_handler = handler;
|
retu->timer_handler = handler;
|
||||||
|
|
||||||
if (!(_retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
|
if (!(retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
|
||||||
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
||||||
free(_retu);
|
free(retu);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
_retu->max_calls = max_calls;
|
retu->max_calls = max_calls;
|
||||||
|
|
||||||
_retu->frequ = 10000; /* default value? */
|
retu->frequ = 10000; /* default value? */
|
||||||
_retu->call_timeout = 30000; /* default value? */
|
retu->call_timeout = 30000; /* default value? */
|
||||||
|
|
||||||
|
|
||||||
m_callback_msi_packet(messenger, msi_handle_packet, _retu );
|
m_callback_msi_packet(messenger, msi_handle_packet, retu );
|
||||||
|
|
||||||
/* This is called when remote terminates session */
|
/* This is called when remote terminates session */
|
||||||
m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu);
|
m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, retu);
|
||||||
|
|
||||||
pthread_mutex_init(&_retu->mutex, NULL);
|
pthread_mutex_init(retu->mutex, NULL);
|
||||||
|
|
||||||
LOGGER_DEBUG("New msi session: %p max calls: %u", _retu, max_calls);
|
LOGGER_DEBUG("New msi session: %p max calls: %u", retu, max_calls);
|
||||||
return _retu;
|
return retu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1651,9 +1651,9 @@ int msi_terminate_session ( MSISession *session )
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
|
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
int _status = 0;
|
int _status = 0;
|
||||||
|
|
||||||
|
@ -1671,7 +1671,7 @@ int msi_terminate_session ( MSISession *session )
|
||||||
|
|
||||||
timer_terminate_session(session->timer_handler);
|
timer_terminate_session(session->timer_handler);
|
||||||
|
|
||||||
pthread_mutex_destroy(&session->mutex);
|
pthread_mutex_destroy(session->mutex);
|
||||||
|
|
||||||
LOGGER_DEBUG("Terminated session: %p", session);
|
LOGGER_DEBUG("Terminated session: %p", session);
|
||||||
free ( session );
|
free ( session );
|
||||||
|
@ -1690,7 +1690,7 @@ int msi_terminate_session ( MSISession *session )
|
||||||
*/
|
*/
|
||||||
int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
|
int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
|
|
||||||
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
|
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
|
||||||
|
|
||||||
|
@ -1698,7 +1698,7 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
|
||||||
MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */
|
MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */
|
||||||
|
|
||||||
if ( !_call ) {
|
if ( !_call ) {
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
LOGGER_ERROR("Cannot handle more calls");
|
LOGGER_ERROR("Cannot handle more calls");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -1729,7 +1729,7 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
|
||||||
|
|
||||||
LOGGER_DEBUG("Invite sent");
|
LOGGER_DEBUG("Invite sent");
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1746,18 +1746,18 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
|
||||||
*/
|
*/
|
||||||
int msi_hangup ( MSISession *session, int32_t call_index )
|
int msi_hangup ( MSISession *session, int32_t call_index )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
|
LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
|
||||||
|
|
||||||
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
||||||
LOGGER_ERROR("Invalid call index!");
|
LOGGER_ERROR("Invalid call index!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) {
|
if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) {
|
||||||
LOGGER_ERROR("No call with such index or call is not active!");
|
LOGGER_ERROR("No call with such index or call is not active!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1776,7 +1776,7 @@ int msi_hangup ( MSISession *session, int32_t call_index )
|
||||||
session->calls[call_index]->request_timer_id =
|
session->calls[call_index]->request_timer_id =
|
||||||
timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
|
timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1791,12 +1791,12 @@ int msi_hangup ( MSISession *session, int32_t call_index )
|
||||||
*/
|
*/
|
||||||
int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type )
|
int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
|
LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
|
||||||
|
|
||||||
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
||||||
LOGGER_ERROR("Invalid call index!");
|
LOGGER_ERROR("Invalid call index!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1818,7 +1818,7 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
|
||||||
|
|
||||||
session->calls[call_index]->state = call_active;
|
session->calls[call_index]->state = call_active;
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1833,12 +1833,12 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
|
||||||
*/
|
*/
|
||||||
int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason )
|
int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason ? reason : "Unknown");
|
LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason ? reason : "Unknown");
|
||||||
|
|
||||||
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
||||||
LOGGER_ERROR("Invalid call index!");
|
LOGGER_ERROR("Invalid call index!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1852,7 +1852,7 @@ int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const c
|
||||||
/*session->calls[call_index]->state = call_hanged_up;
|
/*session->calls[call_index]->state = call_hanged_up;
|
||||||
session->calls[call_index]->request_timer_id = timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
|
session->calls[call_index]->request_timer_id = timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
|
||||||
terminate_call ( session, session->calls[call_index] );
|
terminate_call ( session, session->calls[call_index] );
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1867,12 +1867,12 @@ int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const c
|
||||||
*/
|
*/
|
||||||
int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason )
|
int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason ? (char *)reason : "Unknown");
|
LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason ? (char *)reason : "Unknown");
|
||||||
|
|
||||||
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
||||||
LOGGER_ERROR("Invalid call index!");
|
LOGGER_ERROR("Invalid call index!");
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1889,7 +1889,7 @@ int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason
|
||||||
session->calls[call_index]->request_timer_id =
|
session->calls[call_index]->request_timer_id =
|
||||||
timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
|
timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1903,11 +1903,11 @@ int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason
|
||||||
*/
|
*/
|
||||||
int msi_stopcall ( MSISession *session, int32_t call_index )
|
int msi_stopcall ( MSISession *session, int32_t call_index )
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&session->mutex);
|
pthread_mutex_lock(session->mutex);
|
||||||
LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index);
|
LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index);
|
||||||
|
|
||||||
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1915,6 +1915,6 @@ int msi_stopcall ( MSISession *session, int32_t call_index )
|
||||||
|
|
||||||
terminate_call ( session, session->calls[call_index] );
|
terminate_call ( session, session->calls[call_index] );
|
||||||
|
|
||||||
pthread_mutex_unlock(&session->mutex);
|
pthread_mutex_unlock(session->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ typedef struct _MSICall { /* Call info structure */
|
||||||
int ringing_timer_id; /* Timer id for ringing timeout */
|
int ringing_timer_id; /* Timer id for ringing timeout */
|
||||||
|
|
||||||
|
|
||||||
pthread_mutex_t mutex; /* */
|
pthread_mutex_t mutex[1]; /* */
|
||||||
uint32_t *peers;
|
uint32_t *peers;
|
||||||
uint16_t peer_count;
|
uint16_t peer_count;
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ typedef struct _MSISession {
|
||||||
uint32_t frequ;
|
uint32_t frequ;
|
||||||
uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
|
uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
|
||||||
|
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex[1];
|
||||||
|
|
||||||
void *timer_handler;
|
void *timer_handler;
|
||||||
} MSISession;
|
} MSISession;
|
||||||
|
|
10
toxav/rtp.c
10
toxav/rtp.c
|
@ -727,12 +727,9 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
|
||||||
* @retval -1 Error occurred.
|
* @retval -1 Error occurred.
|
||||||
* @retval 0 Success.
|
* @retval 0 Success.
|
||||||
*/
|
*/
|
||||||
int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
|
void rtp_terminate_session ( RTPSession *session, Messenger *messenger )
|
||||||
{
|
{
|
||||||
if ( !session ) {
|
if ( !session ) return;
|
||||||
LOGGER_WARNING("No session!");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
|
custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
|
||||||
|
|
||||||
|
@ -747,8 +744,9 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
|
||||||
|
|
||||||
pthread_mutex_destroy(&session->mutex);
|
pthread_mutex_destroy(&session->mutex);
|
||||||
|
|
||||||
|
LOGGER_DEBUG("Terminated RTP session: %p", session);
|
||||||
|
|
||||||
/* And finally free session */
|
/* And finally free session */
|
||||||
free ( session );
|
free ( session );
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
|
@ -195,7 +195,7 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
|
||||||
* @retval -1 Error occurred.
|
* @retval -1 Error occurred.
|
||||||
* @retval 0 Success.
|
* @retval 0 Success.
|
||||||
*/
|
*/
|
||||||
int rtp_terminate_session ( RTPSession *session, Messenger *messenger );
|
void rtp_terminate_session ( RTPSession *session, Messenger *messenger );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
111
toxav/toxav.c
111
toxav/toxav.c
|
@ -68,6 +68,7 @@ typedef struct _CallSpecific {
|
||||||
void *frame_buf; /* buffer for split video payloads */
|
void *frame_buf; /* buffer for split video payloads */
|
||||||
|
|
||||||
_Bool call_active;
|
_Bool call_active;
|
||||||
|
pthread_mutex_t mutex[1];
|
||||||
} CallSpecific;
|
} CallSpecific;
|
||||||
|
|
||||||
|
|
||||||
|
@ -312,7 +313,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
|
||||||
|
|
||||||
if ( !call->crtps[audio_index] ) {
|
if ( !call->crtps[audio_index] ) {
|
||||||
LOGGER_ERROR("Error while starting audio RTP session!\n");
|
LOGGER_ERROR("Error while starting audio RTP session!\n");
|
||||||
return ErrorStartingAudioRtp;
|
return ErrorInternal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -323,9 +324,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
|
||||||
|
|
||||||
if ( !call->crtps[video_index] ) {
|
if ( !call->crtps[video_index] ) {
|
||||||
LOGGER_ERROR("Error while starting video RTP session!\n");
|
LOGGER_ERROR("Error while starting video RTP session!\n");
|
||||||
|
goto error;
|
||||||
rtp_terminate_session(call->crtps[audio_index], av->messenger);
|
|
||||||
return ErrorStartingVideoRtp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
call->frame_limit = 0;
|
call->frame_limit = 0;
|
||||||
|
@ -335,20 +334,15 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
|
||||||
call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1);
|
call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1);
|
||||||
|
|
||||||
if (!call->frame_buf) {
|
if (!call->frame_buf) {
|
||||||
rtp_terminate_session(call->crtps[audio_index], av->messenger);
|
|
||||||
rtp_terminate_session(call->crtps[video_index], av->messenger);
|
|
||||||
LOGGER_WARNING("Frame buffer allocation failed!");
|
LOGGER_WARNING("Frame buffer allocation failed!");
|
||||||
return ErrorInternal;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !(call->j_buf = create_queue(codec_settings->jbuf_capacity)) ) {
|
if ( !(call->j_buf = create_queue(codec_settings->jbuf_capacity)) ) {
|
||||||
rtp_terminate_session(call->crtps[audio_index], av->messenger);
|
|
||||||
rtp_terminate_session(call->crtps[video_index], av->messenger);
|
|
||||||
free(call->frame_buf);
|
|
||||||
LOGGER_WARNING("Jitter buffer creaton failed!");
|
LOGGER_WARNING("Jitter buffer creaton failed!");
|
||||||
return ErrorInternal;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( (call->cs = codec_init_session(codec_settings->audio_bitrate,
|
if ( (call->cs = codec_init_session(codec_settings->audio_bitrate,
|
||||||
|
@ -359,14 +353,20 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
|
||||||
codec_settings->video_width,
|
codec_settings->video_width,
|
||||||
codec_settings->video_height,
|
codec_settings->video_height,
|
||||||
codec_settings->video_bitrate) )) {
|
codec_settings->video_bitrate) )) {
|
||||||
|
|
||||||
|
if ( pthread_mutex_init(call->mutex, NULL) != 0 ) goto error;
|
||||||
|
|
||||||
call->call_active = 1;
|
call->call_active = 1;
|
||||||
|
|
||||||
return ErrorNone;
|
return ErrorNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
error:
|
||||||
rtp_terminate_session(call->crtps[audio_index], av->messenger);
|
rtp_terminate_session(call->crtps[audio_index], av->messenger);
|
||||||
rtp_terminate_session(call->crtps[video_index], av->messenger);
|
rtp_terminate_session(call->crtps[video_index], av->messenger);
|
||||||
free(call->frame_buf);
|
free(call->frame_buf);
|
||||||
terminate_queue(call->j_buf);
|
terminate_queue(call->j_buf);
|
||||||
|
codec_terminate_session(call->cs);
|
||||||
|
|
||||||
return ErrorInternal;
|
return ErrorInternal;
|
||||||
}
|
}
|
||||||
|
@ -388,30 +388,17 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
|
||||||
|
|
||||||
CallSpecific *call = &av->calls[call_index];
|
CallSpecific *call = &av->calls[call_index];
|
||||||
|
|
||||||
|
pthread_mutex_lock(call->mutex);
|
||||||
|
|
||||||
call->call_active = 0;
|
call->call_active = 0;
|
||||||
|
|
||||||
if ( call->crtps[audio_index] && -1 == rtp_terminate_session(call->crtps[audio_index], av->messenger) ) {
|
rtp_terminate_session(call->crtps[audio_index], av->messenger); call->crtps[audio_index] = NULL;
|
||||||
LOGGER_ERROR("Error while terminating audio RTP session!\n");
|
rtp_terminate_session(call->crtps[video_index], av->messenger); call->crtps[video_index] = NULL;
|
||||||
/*return ErrorTerminatingAudioRtp;*/
|
terminate_queue(call->j_buf); call->j_buf = NULL;
|
||||||
} else call->crtps[audio_index] = NULL;
|
codec_terminate_session(call->cs); call->cs = NULL;
|
||||||
|
|
||||||
if ( call->crtps[video_index] && -1 == rtp_terminate_session(call->crtps[video_index], av->messenger) ) {
|
|
||||||
LOGGER_ERROR("Error while terminating video RTP session!\n");
|
|
||||||
/*return ErrorTerminatingVideoRtp;*/
|
|
||||||
} else call->crtps[video_index] = NULL;
|
|
||||||
|
|
||||||
if ( call->j_buf ) {
|
|
||||||
terminate_queue(call->j_buf);
|
|
||||||
call->j_buf = NULL;
|
|
||||||
LOGGER_DEBUG("Terminated j queue");
|
|
||||||
} else LOGGER_DEBUG("No j queue");
|
|
||||||
|
|
||||||
if ( call->cs ) {
|
|
||||||
codec_terminate_session(call->cs);
|
|
||||||
call->cs = NULL;
|
|
||||||
LOGGER_DEBUG("Terminated codec session");
|
|
||||||
} else LOGGER_DEBUG("No codec session");
|
|
||||||
|
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
|
pthread_mutex_destroy(call->mutex);
|
||||||
|
|
||||||
return ErrorNone;
|
return ErrorNone;
|
||||||
}
|
}
|
||||||
|
@ -431,22 +418,22 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
|
||||||
inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
|
inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
|
||||||
unsigned int length )
|
unsigned int length )
|
||||||
{
|
{
|
||||||
#define send(data, len) rtp_send_msg(av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, data, len)
|
CallSpecific* call = &av->calls[call_index];
|
||||||
|
if (call->crtps[type - TypeAudio]) {
|
||||||
|
|
||||||
if (av->calls[call_index].crtps[type - TypeAudio]) {
|
|
||||||
if (type == TypeAudio) {
|
if (type == TypeAudio) {
|
||||||
return send(payload, length);
|
return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, payload, length);
|
||||||
} else {
|
} else {
|
||||||
if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
|
if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
|
||||||
LOGGER_ERROR("Invalid video frame size: %u\n", length);
|
LOGGER_ERROR("Invalid video frame size: %u\n", length);
|
||||||
return -1;
|
return ErrorInternal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* number of pieces - 1*/
|
/* number of pieces - 1*/
|
||||||
uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
|
uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
|
||||||
|
|
||||||
uint8_t load[2 + VIDEOFRAME_PIECE_SIZE];
|
uint8_t load[2 + VIDEOFRAME_PIECE_SIZE];
|
||||||
load[0] = av->calls[call_index].frame_outid++;
|
load[0] = call->frame_outid++;
|
||||||
load[1] = 0;
|
load[1] = 0;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
@ -455,8 +442,10 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
|
||||||
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE);
|
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE);
|
||||||
payload += VIDEOFRAME_PIECE_SIZE;
|
payload += VIDEOFRAME_PIECE_SIZE;
|
||||||
|
|
||||||
if (send(load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
|
if (rtp_send_msg(call->crtps[type - TypeAudio], av->messenger,
|
||||||
return -1;
|
load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
|
||||||
|
|
||||||
|
return ErrorInternal;
|
||||||
}
|
}
|
||||||
|
|
||||||
load[1]++;
|
load[1]++;
|
||||||
|
@ -465,13 +454,12 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
|
||||||
/* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */
|
/* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */
|
||||||
length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
|
length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
|
||||||
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length);
|
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length);
|
||||||
return send(load, VIDEOFRAME_HEADER_SIZE + length);
|
|
||||||
|
return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, load, VIDEOFRAME_HEADER_SIZE + length);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return ErrorNoRtpSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef send
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -545,9 +533,10 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t packet [RTP_PAYLOAD_SIZE];
|
|
||||||
CallSpecific *call = &av->calls[call_index];
|
CallSpecific *call = &av->calls[call_index];
|
||||||
|
pthread_mutex_lock(call->mutex);
|
||||||
|
|
||||||
|
uint8_t packet [RTP_PAYLOAD_SIZE];
|
||||||
int recved_size;
|
int recved_size;
|
||||||
|
|
||||||
while ((recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet)) > 0) {
|
while ((recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet)) > 0) {
|
||||||
|
@ -597,7 +586,9 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
|
||||||
img = vpx_codec_get_frame(&call->cs->v_decoder, &iter);
|
img = vpx_codec_get_frame(&call->cs->v_decoder, &iter);
|
||||||
|
|
||||||
*output = img;
|
*output = img;
|
||||||
return 0;
|
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
|
return ErrorNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -617,7 +608,11 @@ inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *fr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size);
|
pthread_mutex_lock(av->calls[call_index].mutex);
|
||||||
|
int rc = toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size);
|
||||||
|
pthread_mutex_unlock(av->calls[call_index].mutex);
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -640,11 +635,13 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d
|
||||||
|
|
||||||
|
|
||||||
CallSpecific *call = &av->calls[call_index];
|
CallSpecific *call = &av->calls[call_index];
|
||||||
|
pthread_mutex_lock(call->mutex);
|
||||||
|
|
||||||
int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
|
int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
|
||||||
|
|
||||||
if ( rc != VPX_CODEC_OK) {
|
if ( rc != VPX_CODEC_OK) {
|
||||||
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
|
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
return ErrorInternal;
|
return ErrorInternal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -656,13 +653,17 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d
|
||||||
|
|
||||||
while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) {
|
while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) {
|
||||||
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
|
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
|
||||||
if ( copied + pkt->data.frame.sz > dest_max ) return ErrorPacketTooLarge;
|
if ( copied + pkt->data.frame.sz > dest_max ) {
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
|
return ErrorPacketTooLarge;
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz);
|
memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz);
|
||||||
copied += pkt->data.frame.sz;
|
copied += pkt->data.frame.sz;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
return copied;
|
return copied;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,6 +690,7 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
|
||||||
|
|
||||||
|
|
||||||
CallSpecific *call = &av->calls[call_index];
|
CallSpecific *call = &av->calls[call_index];
|
||||||
|
pthread_mutex_lock(call->mutex);
|
||||||
|
|
||||||
uint8_t packet [RTP_PAYLOAD_SIZE];
|
uint8_t packet [RTP_PAYLOAD_SIZE];
|
||||||
|
|
||||||
|
@ -697,6 +699,8 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
|
||||||
if ( recved_size == ErrorAudioPacketLost ) {
|
if ( recved_size == ErrorAudioPacketLost ) {
|
||||||
int dec_size = opus_decode(call->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
|
int dec_size = opus_decode(call->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
|
|
||||||
if ( dec_size < 0 ) {
|
if ( dec_size < 0 ) {
|
||||||
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
|
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
|
||||||
return ErrorInternal;
|
return ErrorInternal;
|
||||||
|
@ -705,11 +709,14 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
|
||||||
} else if ( recved_size ) {
|
} else if ( recved_size ) {
|
||||||
int dec_size = opus_decode(call->cs->audio_decoder, packet, recved_size, dest, frame_size, 0);
|
int dec_size = opus_decode(call->cs->audio_decoder, packet, recved_size, dest, frame_size, 0);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
|
|
||||||
if ( dec_size < 0 ) {
|
if ( dec_size < 0 ) {
|
||||||
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
|
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
|
||||||
return ErrorInternal;
|
return ErrorInternal;
|
||||||
} else return dec_size;
|
} else return dec_size;
|
||||||
} else {
|
} else {
|
||||||
|
pthread_mutex_unlock(call->mutex);
|
||||||
return 0; /* Nothing received */
|
return 0; /* Nothing received */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -733,7 +740,11 @@ inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *fr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size);
|
pthread_mutex_lock(av->calls[call_index].mutex);
|
||||||
|
int rc = toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size);
|
||||||
|
pthread_mutex_unlock(av->calls[call_index].mutex);
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -757,8 +768,12 @@ inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pthread_mutex_lock(av->calls[call_index].mutex);
|
||||||
|
|
||||||
int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max);
|
int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(av->calls[call_index].mutex);
|
||||||
|
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
|
LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
|
||||||
return ErrorInternal;
|
return ErrorInternal;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user