Tests works and some fixes

This commit is contained in:
mannol 2014-05-24 16:02:01 +02:00
parent 9db41e7582
commit 565e95301f
10 changed files with 164 additions and 107 deletions

View File

@ -1,9 +1,7 @@
if BUILD_TESTS if BUILD_TESTS
TESTS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test TESTS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test
#toxav_basic_test toxav_many_test
check_PROGRAMS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test check_PROGRAMS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test
#toxav_basic_test toxav_many_test
AUTOTEST_CFLAGS = \ AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \ $(LIBSODIUM_CFLAGS) \
@ -20,11 +18,11 @@ AUTOTEST_LDADD = \
$(CHECK_LIBS) $(CHECK_LIBS)
#Tests don't work atm
if BUILD_AV if BUILD_AV
#TESTS += toxav_basic_test TESTS += toxav_basic_test toxav_many_test
#check_PROGRAMS += toxav_basic_test check_PROGRAMS += toxav_basic_test toxav_many_test
#AUTOTEST_LDADD += libtoxav.la AUTOTEST_LDADD += libtoxav.la
endif endif
messenger_autotest_SOURCES = ../auto_tests/messenger_test.c messenger_autotest_SOURCES = ../auto_tests/messenger_test.c
@ -76,20 +74,20 @@ tox_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_test_LDADD = $(AUTOTEST_LDADD) tox_test_LDADD = $(AUTOTEST_LDADD)
#Tests don't work atm
if BUILD_AV if BUILD_AV
#toxav_basic_test_SOURCES = ../auto_tests/toxav_basic_test.c toxav_basic_test_SOURCES = ../auto_tests/toxav_basic_test.c
#toxav_basic_test_CFLAGS = $(AUTOTEST_CFLAGS) toxav_basic_test_CFLAGS = $(AUTOTEST_CFLAGS)
#toxav_basic_test_LDADD = $(AUTOTEST_LDADD) toxav_basic_test_LDADD = $(AUTOTEST_LDADD)
#toxav_many_test_SOURCES = ../auto_tests/toxav_many_test.c toxav_many_test_SOURCES = ../auto_tests/toxav_many_test.c
#toxav_many_test_CFLAGS = $(AUTOTEST_CFLAGS) toxav_many_test_CFLAGS = $(AUTOTEST_CFLAGS)
#toxav_many_test_LDADD = $(AUTOTEST_LDADD) toxav_many_test_LDADD = $(AUTOTEST_LDADD)
endif endif
endif endif

View File

@ -13,6 +13,7 @@
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/logger.h" #include "../toxcore/logger.h"
#include "../toxcore/crypto_core.h"
#include "../toxav/toxav.h" #include "../toxav/toxav.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@ -159,6 +160,7 @@ case 3: /* Wait for Both to have status ended */\
if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n"); if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n");
START_TEST(test_AV_flows) START_TEST(test_AV_flows)
// int test_AV_flows()
{ {
long long unsigned int cur_time = time(NULL); long long unsigned int cur_time = time(NULL);
Tox *bootstrap_node = tox_new(0); Tox *bootstrap_node = tox_new(0);
@ -200,11 +202,10 @@ START_TEST(test_AV_flows)
muhcaps.video_height = muhcaps.video_width = 128; muhcaps.video_height = muhcaps.video_width = 128;
Status status_control = { Status status_control = {
{none, toxav_new(Alice, 1), NULL}, {none, toxav_new(Alice, 1), NULL, -1},
{none, toxav_new(Bob, 1), NULL}, {none, toxav_new(Bob, 1), NULL, -1},
}; };
ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances"); ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances");
@ -222,9 +223,11 @@ START_TEST(test_AV_flows)
toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control); toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control);
const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
int16_t sample_payload[frame_size];
randombytes_salsa20_random_buf(sample_payload, sizeof(int16_t) * frame_size);
int16_t sample_payload[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
uint8_t prepared_payload[1000];
int payload_size; int payload_size;
vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1); vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1);
@ -241,30 +244,34 @@ START_TEST(test_AV_flows)
/* /*
* Call with audio only on both sides. Alice calls Bob. * Call with audio only on both sides. Alice calls Bob.
*/ */
CALL_AND_START_LOOP(TypeAudio, TypeAudio) { CALL_AND_START_LOOP(TypeAudio, TypeAudio) {
/* Both send */ /* Both send */
int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120); payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
if (!( payload_size > 0 )) { /* FIXME: this will always fail */ if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" ); ck_assert_msg ( 0, "Failed to encode payload" );
} }
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
/* Both receive */ /* Both receive */
int16_t storage[10]; int16_t storage[frame_size];
int recved; int recved;
/* Payload from Bob */ /* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
memset(storage, 0, 10);
} }
/* Payload from Alice */ recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@ -287,27 +294,29 @@ START_TEST(test_AV_flows)
*/ */
CALL_AND_START_LOOP(TypeAudio, TypeVideo) { CALL_AND_START_LOOP(TypeAudio, TypeVideo) {
/* Both send */ /* Both send */
int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120); payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
if (!( payload_size > 0 )) { /* FIXME: this will always fail */ if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" ); ck_assert_msg ( 0, "Failed to encode payload" );
} }
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image); // toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
/* Both receive */ /* Both receive */
int16_t storage[10]; int16_t storage[frame_size];
vpx_image_t *video_storage; vpx_image_t *video_storage;
int recved; int recved;
/* Payload from Bob */ /* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
memset(storage, 0, 10);
} }
/* Video payload */ /* Video payload */
@ -324,7 +333,7 @@ START_TEST(test_AV_flows)
/* Payload from Alice */ /* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage); recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@ -347,28 +356,32 @@ START_TEST(test_AV_flows)
*/ */
CALL_AND_START_LOOP(TypeVideo, TypeVideo) { CALL_AND_START_LOOP(TypeVideo, TypeVideo) {
/* Both send */ /* Both send */
int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120);
if (!( payload_size > 0 )) { /* FIXME: this will always fail */ payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" ); ck_assert_msg ( 0, "Failed to encode payload" );
} }
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image);
// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image); // toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
/* Both receive */ /* Both receive */
int16_t storage[10]; int16_t storage[frame_size];
vpx_image_t *video_storage; vpx_image_t *video_storage;
int recved; int recved;
/* Payload from Bob */ /* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
memset(storage, 0, 10);
} }
/* Video payload */ /* Video payload */
@ -385,7 +398,7 @@ START_TEST(test_AV_flows)
/* Payload from Alice */ /* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage); recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@ -437,7 +450,6 @@ START_TEST(test_AV_flows)
toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10); toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10);
step++; step++;
break; break;
\
case 1: /* Bob */ case 1: /* Bob */
if (status_control.Bob.status == Ringing) { if (status_control.Bob.status == Ringing) {
@ -491,7 +503,7 @@ START_TEST(test_AV_flows)
break; break;
case 2: /* Wait for Both to have status ended */ case 2: /* Wait for Both to have status ended */
if (status_control.Alice.status == Ended && status_control.Bob.status == Cancel) running = 0; if (status_control.Bob.status == Cancel) running = 0;
break; break;
} }
@ -521,7 +533,7 @@ Suite *tox_suite(void)
TCase *tc_av_flows = tcase_create("AV_flows"); TCase *tc_av_flows = tcase_create("AV_flows");
tcase_add_test(tc_av_flows, test_AV_flows); tcase_add_test(tc_av_flows, test_AV_flows);
tcase_set_timeout(tc_av_flows, 100); /* Timeout on 100 too much? */ tcase_set_timeout(tc_av_flows, 200);
suite_add_tcase(s, tc_av_flows); suite_add_tcase(s, tc_av_flows);
return s; return s;
@ -539,4 +551,6 @@ int main(int argc, char *argv[])
srunner_free(test_runner); srunner_free(test_runner);
return number_failed; return number_failed;
// return test_AV_flows();
} }

View File

@ -13,6 +13,7 @@
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/logger.h" #include "../toxcore/logger.h"
#include "../toxcore/crypto_core.h"
#include "../toxav/toxav.h" #include "../toxav/toxav.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@ -131,9 +132,11 @@ void* in_thread_call (void* arg)
int step = 0,running = 1; int step = 0,running = 1;
int call_idx; int call_idx;
const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
int16_t sample_payload[frame_size];
randombytes_salsa20_random_buf(sample_payload, sizeof(int16_t) * frame_size);
int16_t sample_payload[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
uint8_t prepared_payload[1000];
/* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */ /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */
@ -162,37 +165,37 @@ void* in_thread_call (void* arg)
toxav_prepare_transmission(this_call->Callee.av, 0, &cast, 1); toxav_prepare_transmission(this_call->Callee.av, 0, &cast, 1);
toxav_prepare_transmission(this_call->Caller.av, call_idx, &cast, 1); toxav_prepare_transmission(this_call->Caller.av, call_idx, &cast, 1);
int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, 1000, sample_payload, 120); int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, RTP_PAYLOAD_SIZE, sample_payload, frame_size);
if (!( payload_size > 0 )) { if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" ); ck_assert_msg ( 0, "Failed to encode payload" );
} }
while (time(NULL) - start < 10) { /* 10 seconds */ while (time(NULL) - start < 10) { /* 10 seconds */
/* Both send */ /* Both send */
toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size); toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size);
toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size); toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size);
call_print(time(NULL) - start, "Blaaah");
/* Both receive */ /* Both receive */
int16_t storage[1000]; int16_t storage[RTP_PAYLOAD_SIZE];
int recved; int recved;
/* Payload from CALLER */ /* Payload from CALLER */
recved = toxav_recv_audio(this_call->Callee.av, 0, 120, storage); recved = toxav_recv_audio(this_call->Callee.av, 0, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLER is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLER is invalid");*/
} }
/* Payload from CALLEE */ /* Payload from CALLEE */
recved = toxav_recv_audio(this_call->Caller.av, call_idx, 120, storage); recved = toxav_recv_audio(this_call->Caller.av, call_idx, frame_size, storage);
if ( recved ) { if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLEE is invalid");*/ /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLEE is invalid");*/
} }
//c_sleep(20); c_sleep(20);
} }
step++; /* This terminates the loop */ step++; /* This terminates the loop */

View File

@ -792,8 +792,10 @@ MSICall* find_call ( MSISession* session, uint8_t* call_id )
uint32_t i = 0; uint32_t i = 0;
for (; i < session->max_calls; i ++ ) for (; i < session->max_calls; i ++ )
if ( session->calls[i] && memcmp(session->calls[i]->id, call_id, CALL_ID_LEN) == 0 ) if ( session->calls[i] && memcmp(session->calls[i]->id, call_id, CALL_ID_LEN) == 0 ) {
LOGGER_DEBUG("Found call id: %s", session->calls[i]->id);
return session->calls[i]; return session->calls[i];
}
return NULL; return NULL;
} }
@ -843,7 +845,8 @@ int has_call_error ( MSISession *session, MSICall* call, MSIMessage *msg )
return handle_error ( session, call, error_no_callid, msg->friend_id ); return handle_error ( session, call, error_no_callid, msg->friend_id );
} else if ( !call ) { } else if ( !call ) {
return handle_error ( session, call, error_no_call, msg->friend_id ); LOGGER_WARNING("Handling message while no call!");
return 0;
} else if ( memcmp ( call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) { } else if ( memcmp ( call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) {
return handle_error ( session, call, error_id_mismatch, msg->friend_id ); return handle_error ( session, call, error_id_mismatch, msg->friend_id );
@ -958,6 +961,9 @@ int terminate_call ( MSISession *session, MSICall *call )
return -1; return -1;
} }
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 /* Check event loop and cancel timed events if there are any
* NOTE: This has to be done before possibly * NOTE: This has to be done before possibly
* locking the mutex the second time * locking the mutex the second time
@ -982,6 +988,9 @@ int terminate_call ( MSISession *session, MSICall *call )
free ( call ); free ( call );
if ( rc != EBUSY ) /* Unlock if locked by this call */
pthread_mutex_unlock(&session->mutex);
return 0; return 0;
} }
@ -1159,11 +1168,11 @@ int handle_recv_end ( MSISession *session, MSICall* call, MSIMessage *msg )
/********** Response handlers **********/ /********** Response handlers **********/
int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg ) int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg )
{ {
LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %s", session, call->id );
if ( has_call_error ( session, call, msg ) == 0 ) if ( has_call_error ( session, call, msg ) == 0 )
return 0; 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 ); call->ringing_timer_id = event.timer_alloc ( handle_timeout, call, call->ringing_tout_ms );
invoke_callback(call->call_idx, MSI_OnRinging); invoke_callback(call->call_idx, MSI_OnRinging);
@ -1172,11 +1181,12 @@ int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg )
} }
int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg ) int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg )
{ {
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
if ( has_call_error ( session, call, msg ) == 0 ) if ( has_call_error ( session, call, msg ) == 0 )
return 0; return 0;
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
if ( !msg->cryptokey.header_value ) { if ( !msg->cryptokey.header_value ) {
return handle_error ( session, call, error_no_crypto_key, msg->friend_id ); return handle_error ( session, call, error_no_crypto_key, msg->friend_id );
} }
@ -1213,11 +1223,11 @@ int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg )
} }
int handle_recv_ending ( MSISession *session, MSICall* call, MSIMessage *msg ) int handle_recv_ending ( MSISession *session, MSICall* call, MSIMessage *msg )
{ {
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
if ( has_call_error ( session, call, msg ) == 0 ) if ( has_call_error ( session, call, msg ) == 0 )
return 0; return 0;
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
/* Stop timer */ /* Stop timer */
event.timer_release ( call->request_timer_id ); event.timer_release ( call->request_timer_id );
@ -1308,9 +1318,13 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
_msg->friend_id = source; _msg->friend_id = source;
pthread_mutex_lock(&_session->mutex);
/* Find what call */ /* Find what call */
MSICall* _call = _msg->callid.header_value ? find_call(_session, _msg->callid.header_value ) : NULL; MSICall* _call = _msg->callid.header_value ? find_call(_session, _msg->callid.header_value ) : NULL;
/* Now handle message */ /* Now handle message */
if ( _msg->request.header_value ) { /* Handle request */ if ( _msg->request.header_value ) { /* Handle request */
@ -1381,7 +1395,9 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
LOGGER_WARNING("Invalid message: no resp nor requ headers"); LOGGER_WARNING("Invalid message: no resp nor requ headers");
} }
free_end:free_message ( _msg ); free_end:
free_message ( _msg );
pthread_mutex_unlock(&_session->mutex);
} }
@ -1438,6 +1454,8 @@ MSISession *msi_init_session ( Messenger* messenger, int32_t max_calls )
/* 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);
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;
} }
@ -1456,6 +1474,10 @@ int msi_terminate_session ( MSISession *session )
return -1; return -1;
} }
pthread_mutex_lock(&session->mutex);
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
pthread_mutex_unlock(&session->mutex);
int _status = 0; int _status = 0;
/* If have calls, cancel them */ /* If have calls, cancel them */
@ -1467,7 +1489,8 @@ int msi_terminate_session ( MSISession *session )
msi_cancel ( session, idx, session->calls[idx]->peers [_it], "MSI session terminated!" ); msi_cancel ( session, idx, session->calls[idx]->peers [_it], "MSI session terminated!" );
} }
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
pthread_mutex_destroy(&session->mutex);
LOGGER_DEBUG("Terminated session: %p", session); LOGGER_DEBUG("Terminated session: %p", session);
free ( session ); free ( session );
@ -1486,12 +1509,17 @@ 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);
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
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 ) return -1; /* Cannot handle more calls */ if ( !_call ) {
pthread_mutex_unlock(&session->mutex);
return -1; /* Cannot handle more calls */
}
*call_index = _call->call_idx; *call_index = _call->call_idx;
@ -1517,6 +1545,8 @@ 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);
return 0; return 0;
} }
@ -1532,15 +1562,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);
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);
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);
return -1; return -1;
} }
@ -1557,6 +1590,7 @@ int msi_hangup ( MSISession* session, int32_t call_index )
session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );
pthread_mutex_unlock(&session->mutex);
return 0; return 0;
} }
@ -1571,10 +1605,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);
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);
return -1; return -1;
} }
@ -1606,6 +1642,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);
return 0; return 0;
} }
@ -1620,10 +1657,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);
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);
return -1; return -1;
} }
@ -1634,7 +1673,9 @@ int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const c
send_message ( session, session->calls[call_index], _msg_cancel, peer ); send_message ( session, session->calls[call_index], _msg_cancel, peer );
free_message ( _msg_cancel ); free_message ( _msg_cancel );
session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); /*session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
terminate_call ( session, session->calls[call_index] );
pthread_mutex_unlock(&session->mutex);
return 0; return 0;
} }
@ -1649,10 +1690,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);
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);
return -1; return -1;
} }
@ -1665,6 +1708,7 @@ int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason
session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );
pthread_mutex_unlock(&session->mutex);
return 0; return 0;
} }
@ -1678,14 +1722,18 @@ 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);
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);
return -1; return -1;
}
/* just terminate it */ /* just terminate it */
terminate_call ( session, session->calls[call_index] ); terminate_call ( session, session->calls[call_index] );
pthread_mutex_unlock(&session->mutex);
return 0; return 0;
} }

View File

@ -84,9 +84,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; /* It's to be assumed that call will have pthread_mutex_t mutex; /* */
* separate thread so add mutex
*/
uint32_t *peers; uint32_t *peers;
uint16_t peer_count; uint16_t peer_count;
@ -113,7 +111,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;
} MSISession; } MSISession;

View File

@ -778,13 +778,12 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/ int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
(uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 ); (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
int full_length = encrypted_length + 3;
_send_data[1] = msg->data[0]; _send_data[1] = msg->data[0];
_send_data[2] = msg->data[1]; _send_data[2] = msg->data[1];
if ( full_length != send_custom_user_packet(messenger, session->dest, _send_data, full_length) ) { if ( -1 == send_custom_user_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno)); LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
rtp_free_msg ( session, msg ); rtp_free_msg ( session, msg );
return -1; return -1;
@ -800,6 +799,7 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
} }
rtp_free_msg ( session, msg ); rtp_free_msg ( session, msg );
return 0; return 0;
} }

View File

@ -96,9 +96,10 @@ ToxAv *toxav_new( Tox* messenger, int32_t max_calls)
{ {
ToxAv *av = calloc ( sizeof(ToxAv), 1); ToxAv *av = calloc ( sizeof(ToxAv), 1);
if (av == NULL) if (av == NULL) {
LOGGER_WARNING("Allocation failed!");
return NULL; return NULL;
}
av->messenger = (Messenger *)messenger; av->messenger = (Messenger *)messenger;
av->msi_session = msi_init_session(av->messenger, max_calls); av->msi_session = msi_init_session(av->messenger, max_calls);

View File

@ -67,7 +67,7 @@ typedef enum {
* @brief Call type identifier. * @brief Call type identifier.
*/ */
typedef enum { typedef enum {
TypeAudio = 70, TypeAudio = 192,
TypeVideo TypeVideo
} ToxAvCallType; } ToxAvCallType;

View File

@ -25,7 +25,6 @@
#include "config.h" #include "config.h"
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
#include "logger.h" #include "logger.h"
#ifdef LOGGING #ifdef LOGGING
@ -96,7 +95,7 @@ int logger_init(const char* file_name, LoggerLevel level)
sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid()); sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid());
if ( logger.log_file ) { if ( logger.log_file ) {
fprintf(stderr, "Error opening logger name: %s with level %d: %s!\n", final_l, level, strerror(errno)); fprintf(stderr, "Error opening logger name: %s with level %d: file already opened!\n", final_l, level);
free (final_l); free (final_l);
return -1; return -1;
} }
@ -104,11 +103,7 @@ int logger_init(const char* file_name, LoggerLevel level)
logger.log_file = fopen(final_l, "ab"); logger.log_file = fopen(final_l, "ab");
if ( logger.log_file == NULL ) { if ( logger.log_file == NULL ) {
char error[1000]; fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, strerror(errno));
if ( strerror_r(errno, error, 1000) == 0 )
fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, error);
else
fprintf(stderr, "Error opening logger file: %s\n", final_l);
free (final_l); free (final_l);
return -1; return -1;
@ -116,7 +111,7 @@ int logger_init(const char* file_name, LoggerLevel level)
logger.level = level; logger.level = level;
logger.start_time = current_time(); logger.start_time = current_time_monotonic();
time_t tim = time(NULL); time_t tim = time(NULL);
@ -147,10 +142,10 @@ void logger_write (LoggerLevel level, const char* format, ...)
fflush(logger.log_file); fflush(logger.log_file);
} }
char* logger_timestr(char* dest) char* logger_timestr(char* dest, size_t max_size)
{ {
uint64_t diff = (current_time() - logger.start_time) / 1000; /* ms */ uint64_t diff = (current_time_monotonic() - logger.start_time); /* ms */
sprintf(dest, "%"PRIu64"", diff); snprintf(dest, max_size, "%"PRIu64"", diff);
return dest; return dest;
} }

View File

@ -45,7 +45,7 @@ int logger_init(const char* file_name, LoggerLevel level);
const char* logger_stringify_level(LoggerLevel level); const char* logger_stringify_level(LoggerLevel level);
unsigned logger_get_pid(); unsigned logger_get_pid();
void logger_write (LoggerLevel level, const char* format, ...); void logger_write (LoggerLevel level, const char* format, ...);
char* logger_timestr (char* dest); char* logger_timestr (char* dest, size_t max_size);
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
@ -53,16 +53,16 @@ char* logger_timestr (char* dest);
#define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif #endif
#define WRITE_FORMAT(__LEVEL__, format) char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "\n[%u] [%s] [%s] [%s:%d %s()] %s", \ #define WRITE_FORMAT(__LEVEL__, format) char __time__[20]; char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "\n[%u] [%s] [%s] [%s:%d %s()] %s", \
logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__), _SFILE, __LINE__, __func__, format) logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__, 20), _SFILE, __LINE__, __func__, format)
/* Use these macros */ /* Use these macros */
#define LOGGER_INIT(name, level) logger_init(name, level); #define LOGGER_INIT(name, level) logger_init(name, level);
#define LOGGER_INFO(format, ...) do { char __time__[20]; WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) #define LOGGER_INFO(format, ...) do { WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); free(the_str); } while (0)
#define LOGGER_DEBUG(format, ...) do { char __time__[20]; WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) #define LOGGER_DEBUG(format, ...) do { WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); free(the_str); } while (0)
#define LOGGER_WARNING(format, ...) do { char __time__[20]; WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) #define LOGGER_WARNING(format, ...) do { WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); free(the_str); } while (0)
#define LOGGER_ERROR(format, ...) do { char __time__[20]; WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) #define LOGGER_ERROR(format, ...) do { WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0)
/* To do some checks or similar only when logging use this */ /* To do some checks or similar only when logging use this */
#define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) #define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0)