toxcore/auto_tests/toxav_basic_test.c

513 lines
15 KiB
C
Raw Normal View History

2014-04-05 23:09:44 +08:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <check.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "../toxcore/tox.h"
#include "../toxav/toxav.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#define c_sleep(x) Sleep(1*x)
#else
#include <unistd.h>
#define c_sleep(x) usleep(1000*x)
#endif
2014-04-06 02:47:58 +08:00
typedef enum _CallStatus {
2014-04-05 23:09:44 +08:00
none,
InCall,
Ringing,
Ended,
Rejected,
Cancel
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
} CallStatus;
2014-04-06 02:47:58 +08:00
typedef struct _Party {
2014-04-05 23:09:44 +08:00
CallStatus status;
2014-04-06 02:47:58 +08:00
ToxAv *av;
time_t *CallStarted;
2014-04-05 23:09:44 +08:00
} Party;
typedef struct _Status {
Party Alice;
Party Bob;
} Status;
void accept_friend_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
2014-04-06 02:47:58 +08:00
{
2014-04-05 23:09:44 +08:00
if (length == 15 && memcmp("ILIKESMALLTITS", data, 15) == 0) {
tox_add_friend_norequest(m, public_key);
}
}
/******************************************************************************/
void callback_recv_invite ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
/* Bob always receives invite */
2014-04-06 02:47:58 +08:00
cast->Bob.status = Ringing;
2014-04-05 23:09:44 +08:00
}
void callback_recv_ringing ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
/* Alice always sends invite */
2014-04-06 02:47:58 +08:00
cast->Alice.status = Ringing;
2014-04-05 23:09:44 +08:00
}
void callback_recv_starting ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
/* Alice always sends invite */
printf("Call started on Alice side...\n");
2014-04-06 02:47:58 +08:00
cast->Alice.status = InCall;
2014-04-05 23:09:44 +08:00
toxav_prepare_transmission(cast->Alice.av, 1);
}
void callback_recv_ending ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
if ( cast->Alice.status == Rejected) {
printf ( "Call ended for Bob!\n" );
cast->Bob.status = Ended;
2014-04-06 02:47:58 +08:00
} else {
2014-04-05 23:09:44 +08:00
printf ( "Call ended for Alice!\n" );
cast->Alice.status = Ended;
}
}
void callback_recv_error ( void *_arg )
{
ck_assert_msg(0, "AV internal error");
}
void callback_call_started ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
/* Alice always sends invite */
printf("Call started on Bob side...\n");
2014-04-06 02:47:58 +08:00
cast->Bob.status = InCall;
2014-04-05 23:09:44 +08:00
toxav_prepare_transmission(cast->Bob.av, 1);
}
void callback_call_canceled ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
printf ( "Call Canceled for Bob!\n" );
cast->Bob.status = Cancel;
}
void callback_call_rejected ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
printf ( "Call rejected by Bob!\n"
"Call ended for Alice!\n" );
/* If Bob rejects, call is ended for alice and she sends ending */
cast->Alice.status = Rejected;
}
void callback_call_ended ( void *_arg )
{
2014-04-06 02:47:58 +08:00
Status *cast = _arg;
2014-04-05 23:09:44 +08:00
printf ( "Call ended for Bob!\n" );
cast->Bob.status = Ended;
}
void callback_requ_timeout ( void *_arg )
{
ck_assert_msg(0, "No answer!");
}
/*************************************************************************************************/
/* Alice calls bob and the call starts.
* What happens in the call is defined after. To quit the loop use: step++;
*/
#define CALL_AND_START_LOOP(AliceCallType, BobCallType) \
{ int step = 0, running = 1; while (running) {\
tox_do(bootstrap_node); tox_do(Alice); tox_do(Bob); \
switch ( step ) {\
case 0: /* Alice */ printf("Alice is calling...\n");\
toxav_call(status_control.Alice.av, 0, AliceCallType, 10); step++; break;\
case 1: /* Bob */ if (status_control.Bob.status == Ringing) { printf("Bob answers...\n");\
cur_time = time(NULL); toxav_answer(status_control.Bob.av, BobCallType); step++; } break; \
case 2: /* Rtp transmission */ \
if (status_control.Bob.status == InCall && status_control.Alice.status == InCall)
#define TERMINATE_SCOPE() break;\
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");
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
START_TEST(test_AV)
2014-04-06 02:47:58 +08:00
{
2014-04-05 23:09:44 +08:00
long long unsigned int cur_time = time(NULL);
Tox *bootstrap_node = tox_new(0);
Tox *Alice = tox_new(0);
Tox *Bob = tox_new(0);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances");
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
uint32_t to_compare = 974536;
tox_callback_friend_request(Alice, accept_friend_request, &to_compare);
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(Alice, address);
int test = tox_add_friend(Bob, address, (uint8_t *)"ILIKESMALLTITS", 15);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
uint8_t off = 1;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
while (1) {
tox_do(bootstrap_node);
tox_do(Alice);
tox_do(Bob);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if (tox_isconnected(bootstrap_node) && tox_isconnected(Alice) && tox_isconnected(Bob) && off) {
printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time);
off = 0;
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if (tox_get_friend_connection_status(Alice, 0) == 1 && tox_get_friend_connection_status(Bob, 0) == 1)
break;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
c_sleep(20);
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
printf("All set after %llu seconds! Starting call...\n", time(NULL) - cur_time);
2014-04-06 02:47:58 +08:00
2014-04-07 05:59:18 +08:00
ToxAvCodecSettings muhcaps = av_DefaultSettings;
muhcaps.video_height = muhcaps.video_width = 128;
2014-04-06 02:47:58 +08:00
Status status_control = {
2014-04-07 05:59:18 +08:00
{none, toxav_new(Alice, &muhcaps), NULL},
{none, toxav_new(Bob, &muhcaps), NULL},
2014-04-05 23:09:44 +08:00
};
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances");
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
toxav_register_callstate_callback(callback_call_started, av_OnStart, &status_control);
toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, &status_control);
toxav_register_callstate_callback(callback_call_rejected, av_OnReject, &status_control);
toxav_register_callstate_callback(callback_call_ended, av_OnEnd, &status_control);
toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, &status_control);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, &status_control);
toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, &status_control);
toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, &status_control);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
toxav_register_callstate_callback(callback_recv_error, av_OnError, &status_control);
toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control);
2014-04-06 02:47:58 +08:00
int16_t sample_payload[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1);
2014-04-05 23:09:44 +08:00
memcpy(sample_image->planes[VPX_PLANE_Y], sample_payload, 10);
memcpy(sample_image->planes[VPX_PLANE_U], sample_payload, 10);
memcpy(sample_image->planes[VPX_PLANE_V], sample_payload, 10);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/*************************************************************************************************
* Successful flows (when call starts)
*/
2014-04-06 02:47:58 +08:00
/*
2014-04-05 23:09:44 +08:00
* Call with audio only on both sides. Alice calls Bob.
*/
2014-04-06 02:47:58 +08:00
CALL_AND_START_LOOP(TypeAudio, TypeAudio) {
2014-04-05 23:09:44 +08:00
/* Both send */
toxav_send_audio(status_control.Alice.av, sample_payload, 10);
toxav_send_audio(status_control.Bob.av, sample_payload, 10);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Both receive */
int16_t storage[10];
int recved;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Alice */
recved = toxav_recv_audio(status_control.Alice.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
2014-04-05 23:09:44 +08:00
memset(storage, 0, 10);
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Bob.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av);
toxav_kill_transmission(status_control.Bob.av);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av);
}
}
TERMINATE_SCOPE()
2014-04-06 02:47:58 +08:00
/*
2014-04-05 23:09:44 +08:00
* Call with audio on both sides and video on one side. Alice calls Bob.
*/
2014-04-06 02:47:58 +08:00
CALL_AND_START_LOOP(TypeAudio, TypeVideo) {
2014-04-05 23:09:44 +08:00
/* Both send */
toxav_send_audio(status_control.Alice.av, sample_payload, 10);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
toxav_send_audio(status_control.Bob.av, sample_payload, 10);
toxav_send_video(status_control.Bob.av, sample_image);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Both receive */
int16_t storage[10];
2014-04-06 02:47:58 +08:00
vpx_image_t *video_storage;
2014-04-05 23:09:44 +08:00
int recved;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
2014-04-05 23:09:44 +08:00
memset(storage, 0, 10);
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Video payload */
toxav_recv_video(status_control.Alice.av, &video_storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
2014-04-08 03:52:32 +08:00
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av);
toxav_kill_transmission(status_control.Bob.av);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av);
}
}
TERMINATE_SCOPE()
2014-04-06 02:47:58 +08:00
/*
2014-04-05 23:09:44 +08:00
* Call with audio and video on both sides. Alice calls Bob.
*/
2014-04-06 02:47:58 +08:00
CALL_AND_START_LOOP(TypeVideo, TypeVideo) {
2014-04-05 23:09:44 +08:00
/* Both send */
toxav_send_audio(status_control.Alice.av, sample_payload, 10);
toxav_send_video(status_control.Alice.av, sample_image);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
toxav_send_audio(status_control.Bob.av, sample_payload, 10);
toxav_send_video(status_control.Bob.av, sample_image);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Both receive */
int16_t storage[10];
2014-04-06 02:47:58 +08:00
vpx_image_t *video_storage;
2014-04-05 23:09:44 +08:00
int recved;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
2014-04-05 23:09:44 +08:00
memset(storage, 0, 10);
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Video payload */
toxav_recv_video(status_control.Alice.av, &video_storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
2014-04-08 03:52:32 +08:00
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, 10, storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( recved ) {
2014-04-08 03:52:32 +08:00
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Video payload */
toxav_recv_video(status_control.Bob.av, &video_storage);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if ( video_storage ) {
/*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
2014-04-08 03:52:32 +08:00
memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Alice is invalid");*/
2014-04-05 23:09:44 +08:00
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av);
toxav_kill_transmission(status_control.Bob.av);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av);
}
}
TERMINATE_SCOPE()
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/*************************************************************************************************
* Other flows
*/
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/*
* Call and reject
*/
2014-04-06 02:47:58 +08:00
{
2014-04-05 23:09:44 +08:00
int step = 0;
int running = 1;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
while (running) {
2014-04-06 02:47:58 +08:00
tox_do(bootstrap_node);
tox_do(Alice);
2014-04-05 23:09:44 +08:00
tox_do(Bob);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
switch ( step ) {
2014-04-06 02:47:58 +08:00
case 0: /* Alice */
2014-04-05 23:09:44 +08:00
printf("Alice is calling...\n");
2014-04-06 02:47:58 +08:00
toxav_call(status_control.Alice.av, 0, TypeAudio, 10);
step++;
break;
\
case 1: /* Bob */
if (status_control.Bob.status == Ringing) {
printf("Bob rejects...\n");
2014-04-05 23:09:44 +08:00
toxav_reject(status_control.Bob.av, "Who likes D's anyway?");
2014-04-06 02:47:58 +08:00
step++;
}
break;
2014-04-05 23:09:44 +08:00
case 2: /* Wait for Both to have status ended */
2014-04-06 02:47:58 +08:00
if (status_control.Alice.status == Rejected && status_control.Bob.status == Ended) running = 0;
break;
}
c_sleep(20);
}
2014-04-05 23:09:44 +08:00
printf("\n");
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
/*
* Call and cancel
*/
2014-04-06 02:47:58 +08:00
{
2014-04-05 23:09:44 +08:00
int step = 0;
int running = 1;
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
while (running) {
2014-04-06 02:47:58 +08:00
tox_do(bootstrap_node);
tox_do(Alice);
2014-04-05 23:09:44 +08:00
tox_do(Bob);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
switch ( step ) {
2014-04-06 02:47:58 +08:00
case 0: /* Alice */
2014-04-05 23:09:44 +08:00
printf("Alice is calling...\n");
2014-04-06 02:47:58 +08:00
toxav_call(status_control.Alice.av, 0, TypeAudio, 10);
step++;
break;
\
case 1: /* Alice again */
if (status_control.Bob.status == Ringing) {
printf("Alice cancels...\n");
2014-04-05 23:09:44 +08:00
toxav_cancel(status_control.Alice.av, 0, "Who likes D's anyway?");
2014-04-06 02:47:58 +08:00
step++;
}
break;
2014-04-05 23:09:44 +08:00
case 2: /* Wait for Both to have status ended */
2014-04-06 02:47:58 +08:00
if (status_control.Alice.status == Ended && status_control.Bob.status == Cancel) running = 0;
break;
}
c_sleep(20);
}
2014-04-05 23:09:44 +08:00
printf("\n");
}
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
printf("Calls ended!\n");
}
END_TEST
/*************************************************************************************************/
Suite *tox_suite(void)
{
2014-04-05 23:36:54 +08:00
Suite *s = suite_create("ToxAV");
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
TCase *tc_av = tcase_create("A/V");
tcase_add_test(tc_av, test_AV);
tcase_set_timeout(tc_av, 100); /* Timeout on 100 too much? */
suite_add_tcase(s, tc_av);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
return s;
}
int main(int argc, char *argv[])
{
Suite *tox = tox_suite();
SRunner *test_runner = srunner_create(tox);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
setbuf(stdout, NULL);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
srunner_run_all(test_runner, CK_NORMAL);
int number_failed = srunner_ntests_failed(test_runner);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
srunner_free(test_runner);
2014-04-06 02:47:58 +08:00
2014-04-05 23:09:44 +08:00
return number_failed;
2014-04-09 21:43:19 +08:00
}