Make rtp & codec actions thread-safe

This commit is contained in:
mannol 2014-07-05 15:11:25 +02:00
parent a8fa360547
commit 8b9d3992a4
6 changed files with 177 additions and 155 deletions

View File

@ -80,8 +80,12 @@ JitterBuffer *create_queue(int capacity)
void terminate_queue(JitterBuffer *q)
{
if (!q) return;
empty_queue(q);
free(q->queue);
LOGGER_DEBUG("Terminated jitter buffer: %p", q);
free(q);
}
@ -314,6 +318,8 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
void codec_terminate_session ( CodecState *cs )
{
if (!cs) return;
if ( cs->audio_encoder )
opus_encoder_destroy(cs->audio_encoder);
@ -325,6 +331,9 @@ void codec_terminate_session ( CodecState *cs )
if ( cs->capabilities & v_encoding )
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)

View File

@ -1043,12 +1043,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
return NULL;
}
int32_t _call_idx = 0;
int32_t call_idx = 0;
for (; _call_idx < session->max_calls; _call_idx ++) {
if ( !session->calls[_call_idx] ) {
for (; call_idx < session->max_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!");
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!");
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!");
free(_call);
free(call);
return NULL;
}
_call->session = session;
call->session = session;
/*_call->_participant_count = _peers;*/
_call->request_timer_id = 0;
_call->ringing_timer_id = 0;
call->request_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);
return _call;
LOGGER_DEBUG("Started new call with index: %u", call_idx);
return call;
}
@ -1104,7 +1104,7 @@ int terminate_call ( MSISession *session, MSICall *call )
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);
/* 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 );
/* Get a handle */
pthread_mutex_lock ( &call->mutex );
pthread_mutex_lock ( call->mutex );
session->calls[call->call_idx] = NULL;
@ -1123,14 +1123,14 @@ int terminate_call ( MSISession *session, MSICall *call )
free ( call->peers);
/* Release handle */
pthread_mutex_unlock ( &call->mutex );
pthread_mutex_unlock ( call->mutex );
pthread_mutex_destroy ( &call->mutex );
pthread_mutex_destroy ( call->mutex );
free ( call );
if ( rc != EBUSY ) /* Unlock if locked by this call */
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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");
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
if ( call ) {
@ -1194,27 +1194,27 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
call = init_call ( session, 1, 0 );
if ( !call ) {
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
LOGGER_ERROR("Starting call");
return 0;
}
} else {
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
return 0; /* Wait for ringing from peer */
}
} else {
send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
terminate_call(session, call);
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
return 0;
}
} else {
call = init_call ( session, 1, 0 );
if ( !call ) {
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
LOGGER_ERROR("Starting call");
return 0;
}
@ -1223,7 +1223,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
if ( !msg->callid.header_value ) {
send_error ( session, call, error_no_callid, msg->friend_id );
terminate_call(session, call);
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 );
free_message ( _msg_ringing );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 );
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
call->state = call_active;
flush_peer_type ( call, msg, 0 );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnStart);
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);
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
send_message ( session, call, _msg_ending, msg->friend_id );
free_message ( _msg_ending );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 );
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
/* Act as end message */
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnCancel);
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 );
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
send_message ( session, call, _msg_ending, msg->friend_id );
free_message ( _msg_ending );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnEnd);
@ -1337,11 +1337,11 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
return 0;
}
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
if ( call->ringing_timer_id ) {
LOGGER_WARNING("Call already ringing");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnRinging);
return 1;
@ -1361,7 +1361,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
return 0;
}
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
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 );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnStarting);
return 1;
@ -1387,14 +1387,14 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
return 0;
}
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
/* Stop timer */
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);
@ -1405,11 +1405,11 @@ int handle_recv_ending ( 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 ) {
LOGGER_WARNING("Handling 'error' on non-existing call!");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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);
}
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
invoke_callback(call->call_idx, MSI_OnEnding);
@ -1603,38 +1603,38 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
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!");
return NULL;
}
_retu->messenger_handle = messenger;
_retu->agent_handler = NULL;
_retu->timer_handler = handler;
retu->messenger_handle = messenger;
retu->agent_handler = NULL;
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!");
free(_retu);
free(retu);
return NULL;
}
_retu->max_calls = max_calls;
retu->max_calls = max_calls;
_retu->frequ = 10000; /* default value? */
_retu->call_timeout = 30000; /* default value? */
retu->frequ = 10000; /* 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 */
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);
return _retu;
LOGGER_DEBUG("New msi session: %p max calls: %u", retu, max_calls);
return retu;
}
@ -1651,9 +1651,9 @@ int msi_terminate_session ( MSISession *session )
return -1;
}
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
int _status = 0;
@ -1671,7 +1671,7 @@ int msi_terminate_session ( MSISession *session )
timer_terminate_session(session->timer_handler);
pthread_mutex_destroy(&session->mutex);
pthread_mutex_destroy(session->mutex);
LOGGER_DEBUG("Terminated session: %p", 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 )
{
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
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 */
if ( !_call ) {
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
LOGGER_ERROR("Cannot handle more calls");
return -1;
}
@ -1729,7 +1729,7 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
LOGGER_DEBUG("Invite sent");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 )
{
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
LOGGER_ERROR("Invalid call index!");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
return -1;
}
if ( !session->calls[call_index] || session->calls[call_index]->state != call_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;
}
@ -1776,7 +1776,7 @@ int msi_hangup ( MSISession *session, int32_t call_index )
session->calls[call_index]->request_timer_id =
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;
}
@ -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 )
{
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
LOGGER_ERROR("Invalid call index!");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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;
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 )
{
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
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] ) {
LOGGER_ERROR("Invalid call index!");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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]->request_timer_id = timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
terminate_call ( session, session->calls[call_index] );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 )
{
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");
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
LOGGER_ERROR("Invalid call index!");
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
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 =
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;
}
@ -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 )
{
pthread_mutex_lock(&session->mutex);
pthread_mutex_lock(session->mutex);
LOGGER_DEBUG("Session: %p Stopping call index: %u", session, 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;
}
@ -1915,6 +1915,6 @@ int msi_stopcall ( MSISession *session, int32_t call_index )
terminate_call ( session, session->calls[call_index] );
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(session->mutex);
return 0;
}

View File

@ -77,7 +77,7 @@ typedef struct _MSICall { /* Call info structure */
int ringing_timer_id; /* Timer id for ringing timeout */
pthread_mutex_t mutex; /* */
pthread_mutex_t mutex[1]; /* */
uint32_t *peers;
uint16_t peer_count;
@ -104,7 +104,7 @@ typedef struct _MSISession {
uint32_t frequ;
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;
} MSISession;

View File

@ -727,12 +727,9 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
void rtp_terminate_session ( RTPSession *session, Messenger *messenger )
{
if ( !session ) {
LOGGER_WARNING("No session!");
return -1;
}
if ( !session ) return;
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);
LOGGER_DEBUG("Terminated RTP session: %p", session);
/* And finally free session */
free ( session );
return 0;
}

View File

@ -195,7 +195,7 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_terminate_session ( RTPSession *session, Messenger *messenger );
void rtp_terminate_session ( RTPSession *session, Messenger *messenger );

View File

@ -68,6 +68,7 @@ typedef struct _CallSpecific {
void *frame_buf; /* buffer for split video payloads */
_Bool call_active;
pthread_mutex_t mutex[1];
} CallSpecific;
@ -299,7 +300,7 @@ int toxav_stop_call ( ToxAv *av, int32_t call_index )
int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettings *codec_settings, int support_video )
{
if ( !av->msi_session || cii(call_index, av->msi_session) ||
!av->msi_session->calls[call_index] || av->calls[call_index].call_active) {
!av->msi_session->calls[call_index] || av->calls[call_index].call_active) {
LOGGER_ERROR("Error while starting RTP session: invalid call!\n");
return ErrorInternal;
}
@ -312,7 +313,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
if ( !call->crtps[audio_index] ) {
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] ) {
LOGGER_ERROR("Error while starting video RTP session!\n");
rtp_terminate_session(call->crtps[audio_index], av->messenger);
return ErrorStartingVideoRtp;
goto error;
}
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);
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!");
return ErrorInternal;
goto error;
}
}
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!");
return ErrorInternal;
goto error;
}
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_height,
codec_settings->video_bitrate) )) {
if ( pthread_mutex_init(call->mutex, NULL) != 0 ) goto error;
call->call_active = 1;
return ErrorNone;
}
error:
rtp_terminate_session(call->crtps[audio_index], av->messenger);
rtp_terminate_session(call->crtps[video_index], av->messenger);
free(call->frame_buf);
terminate_queue(call->j_buf);
codec_terminate_session(call->cs);
return ErrorInternal;
}
@ -388,30 +388,17 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
CallSpecific *call = &av->calls[call_index];
pthread_mutex_lock(call->mutex);
call->call_active = 0;
if ( call->crtps[audio_index] && -1 == rtp_terminate_session(call->crtps[audio_index], av->messenger) ) {
LOGGER_ERROR("Error while terminating audio RTP session!\n");
/*return ErrorTerminatingAudioRtp;*/
} else call->crtps[audio_index] = 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");
rtp_terminate_session(call->crtps[audio_index], av->messenger); call->crtps[audio_index] = NULL;
rtp_terminate_session(call->crtps[video_index], av->messenger); call->crtps[video_index] = NULL;
terminate_queue(call->j_buf); call->j_buf = NULL;
codec_terminate_session(call->cs); call->cs = NULL;
pthread_mutex_unlock(call->mutex);
pthread_mutex_destroy(call->mutex);
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,
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) {
return send(payload, length);
return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, payload, length);
} else {
if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
LOGGER_ERROR("Invalid video frame size: %u\n", length);
return -1;
return ErrorInternal;
}
/* number of pieces - 1*/
uint8_t numparts = (length - 1) / 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;
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);
payload += VIDEOFRAME_PIECE_SIZE;
if (send(load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
return -1;
if (rtp_send_msg(call->crtps[type - TypeAudio], av->messenger,
load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
return ErrorInternal;
}
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 */
length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
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 {
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];
pthread_mutex_lock(call->mutex);
uint8_t packet [RTP_PAYLOAD_SIZE];
int recved_size;
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);
*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];
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);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
pthread_mutex_unlock(call->mutex);
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)) ) {
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);
copied += pkt->data.frame.sz;
}
}
pthread_mutex_unlock(call->mutex);
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];
pthread_mutex_lock(call->mutex);
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 ) {
int dec_size = opus_decode(call->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
pthread_mutex_unlock(call->mutex);
if ( dec_size < 0 ) {
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
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 ) {
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 ) {
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
return ErrorInternal;
} else return dec_size;
} else {
pthread_mutex_unlock(call->mutex);
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);
pthread_mutex_unlock(av->calls[call_index].mutex);
if (rc < 0) {
LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
return ErrorInternal;