Merge branch 'master' of https://github.com/mannol1/ProjectTox-Core into mannol1-master

This commit is contained in:
irungentoo 2014-05-30 19:11:28 -04:00
commit ab44440f37
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 70 additions and 43 deletions

View File

@ -59,7 +59,10 @@ JitterBuffer *create_queue(int capacity)
if ( !(q = calloc(sizeof(JitterBuffer), 1)) ) return NULL;
if (!(q->queue = calloc(sizeof(RTPMessage *), capacity))) return NULL;
if (!(q->queue = calloc(sizeof(RTPMessage *), capacity))) {
free(q);
return NULL;
}
q->size = 0;
q->capacity = capacity;

View File

@ -328,7 +328,8 @@ var.size = t_size; }
void free_message ( MSIMessage *msg )
{
if ( msg == NULL ) {
LOGGER_ERROR("Tried to free empty message");
LOGGER_WARNING("Tried to free empty message");
return;
}
free ( msg->calltype.header_value );
@ -818,6 +819,11 @@ MSICall *find_call ( MSISession *session, uint8_t *call_id )
*/
int handle_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_t to )
{
if (!call) {
LOGGER_WARNING("Cannot handle error on 'null' call");
return -1;
}
LOGGER_DEBUG("Sending error: %d on call: %s", errid, call->id);
MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
@ -1038,9 +1044,10 @@ void *handle_timeout ( void *arg )
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);
if ( call ) {
if ( call->peers[0] == msg->friend_id ) {
/* The glare case. A calls B when at the same time
@ -1051,7 +1058,18 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
*/
if ( call_id_bigger (call->id, msg->callid.header_value) == 1 ) { /* Peer has advantage */
/* Terminate call; peer will timeout(call) if call initialization (magically) fails */
terminate_call(session, call);
call = init_call ( session, 1, 0 );
if ( !call ) {
pthread_mutex_unlock(&session->mutex);
LOGGER_ERROR("Starting call");
return 0;
}
} else {
pthread_mutex_unlock(&session->mutex);
return 0; /* Wait for ringing from peer */
@ -1062,6 +1080,14 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
pthread_mutex_unlock(&session->mutex);
return 0;
}
} else {
call = init_call ( session, 1, 0 );
if ( !call ) {
pthread_mutex_unlock(&session->mutex);
LOGGER_ERROR("Starting call");
return 0;
}
}
if ( !msg->callid.header_value ) {
@ -1070,47 +1096,40 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
return 0;
}
MSICall *new_call = init_call ( session, 1, 0 );
memcpy ( call->id, msg->callid.header_value, CALL_ID_LEN );
call->state = call_starting;
if ( !new_call ) {
handle_error ( session, call, error_busy, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return 0;
}
add_peer( call, msg->friend_id);
memcpy ( new_call->id, msg->callid.header_value, CALL_ID_LEN );
new_call->state = call_starting;
add_peer( new_call, msg->friend_id);
flush_peer_type ( new_call, msg, 0 );
flush_peer_type ( call, msg, 0 );
MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
send_message ( session, new_call, _msg_ringing, msg->friend_id );
send_message ( session, call, _msg_ringing, msg->friend_id );
free_message ( _msg_ringing );
pthread_mutex_unlock(&session->mutex);
invoke_callback(new_call->call_idx, MSI_OnInvite);
invoke_callback(call->call_idx, MSI_OnInvite);
return 1;
}
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);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return -1;
}
if ( !msg->cryptokey.header_value ) {
int rc = handle_error ( session, call, error_no_crypto_key, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return rc;
}
call->state = call_active;
call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
@ -1122,7 +1141,7 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
flush_peer_type ( call, msg, 0 );
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnStart);
return 1;
}
@ -1131,7 +1150,7 @@ 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);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
@ -1144,7 +1163,7 @@ int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnReject);
/*
event.timer_release ( session->call->request_timer_id );
@ -1159,11 +1178,12 @@ 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);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
/* Act as end message */
/*
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
@ -1181,7 +1201,7 @@ 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);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
@ -1192,7 +1212,7 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
free_message ( _msg_ending );
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnEnd);
terminate_call ( session, call );
@ -1203,29 +1223,30 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
{
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %s", session, call->id );
call->ringing_timer_id = event.timer_alloc ( handle_timeout, call, call->ringing_tout_ms );
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnRinging);
return 1;
}
int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
{
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
@ -1262,7 +1283,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
event.timer_release ( call->ringing_timer_id );
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnStarting);
return 1;
}
@ -1274,13 +1295,14 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
/* Stop timer */
event.timer_release ( call->request_timer_id );
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnEnding);
/* Terminate call */
@ -1291,7 +1313,7 @@ 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);
if ( !call ) {
LOGGER_WARNING("Handling 'error' on non-existing call!");
pthread_mutex_unlock(&session->mutex);
@ -1308,11 +1330,11 @@ int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
}
pthread_mutex_unlock(&session->mutex);
invoke_callback(call->call_idx, MSI_OnEnding);
terminate_call ( session, call );
return 1;
}
@ -1418,6 +1440,10 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
goto free_end;
}
/* Got response so cancel timer */
if ( _call )
event.timer_release ( _call->request_timer_id );
uint8_t _response_value[32];
memcpy(_response_value, _msg->response.header_value, _msg->response.size);
@ -1440,10 +1466,6 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
goto free_end;
}
/* Got response so cancel timer */
if ( _call )
event.timer_release ( _call->request_timer_id );
} else {
LOGGER_WARNING("Invalid message: no resp nor requ headers");
}
@ -1568,13 +1590,13 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */
if ( !_call ) {
pthread_mutex_unlock(&session->mutex);
return -1; /* Cannot handle more calls */
LOGGER_ERROR("Cannot handle more calls");
return -1;
}
*call_index = _call->call_idx;
@ -1585,6 +1607,8 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
_call->type_local = call_type;
MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
/* Do whatever with message */
if ( call_type == type_audio ) {
msi_msg_set_calltype ( _msg_invite, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );