Astyled av code.

This commit is contained in:
irungentoo 2014-02-16 20:01:30 -05:00
parent 9d1eb27717
commit baa4a2f11d
11 changed files with 1565 additions and 1423 deletions

95
toxav/event.c Executable file → Normal file
View File

@ -53,15 +53,15 @@ pthread_create(&_tid, NULL, func, args); assert( pthread_detach(_tid) == 0 ); }
typedef struct _EventContainer {
void* (*func)(void*);
void* func_args;
void *(*func)(void *);
void *func_args;
unsigned timeout;
long long id;
} EventContainer;
typedef struct _EventHandler {
EventContainer* timed_events;
EventContainer *timed_events;
size_t timed_events_count;
int running;
@ -70,14 +70,13 @@ typedef struct _EventHandler {
} EventHandler;
int throw_event( void* (func)(void*), void* arg );
int throw_event( void * (func)(void *), void *arg );
int reset_timer_event ( int id, uint32_t timeout );
int throw_timer_event ( void* (func)(void*), void* arg, unsigned timeout);
int throw_timer_event ( void * (func)(void *), void *arg, unsigned timeout);
int cancel_timer_event ( int id );
int execute_timer_event ( int id );
struct _Event event =
{
struct _Event event = {
throw_event,
/* reset_timer_event */ NULL,
throw_timer_event,
@ -88,7 +87,7 @@ struct _Event event =
/*
* Random functions used by this file
*/
void clear_events (EventContainer** event_container, size_t* counter)
void clear_events (EventContainer **event_container, size_t *counter)
{
free(*event_container );
@ -96,35 +95,41 @@ void clear_events (EventContainer** event_container, size_t* counter)
*counter = 0;
}
int pop_id ( EventContainer** event_container, size_t* counter, int id )
int pop_id ( EventContainer **event_container, size_t *counter, int id )
{
if ( !*event_container || !*counter || !id )
return -1;
EventContainer* _it = *event_container;
EventContainer *_it = *event_container;
int i;
for ( i = *counter; i; -- i ){
for ( i = *counter; i; -- i ) {
if ( _it->id == id ) { /* Hit! */
break;
}
++_it;
}
if ( i ) {
for ( ; i; -- i ){ *_it = *(_it + 1); ++_it; }
for ( ; i; -- i ) {
*_it = *(_it + 1);
++_it;
}
-- (*counter );
if ( !(*counter)) { /* Free and set to NULL */
free(*event_container);
*event_container = NULL;
}
else {
void* _result = realloc(*event_container, sizeof(EventContainer) * (*counter )); /* resize */
} else {
void *_result = realloc(*event_container, sizeof(EventContainer) * (*counter )); /* resize */
if ( _result != NULL ) { *event_container = _result; return 0; }
else {
if ( _result != NULL ) {
*event_container = _result;
return 0;
} else {
/* Not sure what would happen next so abort execution.
*/
fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
@ -139,9 +144,9 @@ int pop_id ( EventContainer** event_container, size_t* counter, int id )
return -1;
}
void push_event ( EventContainer** container, size_t* counter, void* (func)(void*), void* arg )
void push_event ( EventContainer **container, size_t *counter, void * (func)(void *), void *arg )
{
EventContainer* _new = realloc((*container ), sizeof(EventContainer) * ((*counter ) + 1));
EventContainer *_new = realloc((*container ), sizeof(EventContainer) * ((*counter ) + 1));
if ( _new == NULL ) {
/* Not sure what would happen next so abort execution.
@ -162,21 +167,22 @@ void push_event ( EventContainer** container, size_t* counter, void* (func)(void
(*counter )++;
}
void reorder_events ( size_t counter, EventContainer* container, unsigned timeout )
void reorder_events ( size_t counter, EventContainer *container, unsigned timeout )
{
if ( counter > 1 ) {
int i = counter - 1;
/* start from behind excluding last added member */
EventContainer* _it = &container[i - 1];
EventContainer *_it = &container[i - 1];
EventContainer _last_added = container[i];
for ( ; i; --i ) {
if ( _it->timeout > timeout ){
if ( _it->timeout > timeout ) {
*(_it + 1) = *_it;
*_it = _last_added; -- _it;
*_it = _last_added;
-- _it;
}
}
@ -186,16 +192,15 @@ void reorder_events ( size_t counter, EventContainer* container, unsigned timeou
/* ============================================= */
/* main poll for event execution */
void* event_poll( void* arg )
void *event_poll( void *arg )
{
EventHandler* _event_handler = arg;
EventHandler *_event_handler = arg;
while ( _event_handler->running )
{
while ( _event_handler->running ) {
LOCK( _event_handler );
if ( _event_handler->timed_events ){
if ( _event_handler->timed_events ) {
uint32_t _time = ((uint32_t)(current_time() / 1000));
@ -227,7 +232,7 @@ void* event_poll( void* arg )
pthread_exit(NULL);
}
int throw_event( void* (func)(void*), void* arg )
int throw_event( void * (func)(void *), void *arg )
{
pthread_t _tid;
int _rc =
@ -239,7 +244,7 @@ int throw_event( void* (func)(void*), void* arg )
EventHandler event_handler;
/* Place and order array of timers */
int throw_timer_event ( void* (func)(void*), void* arg, unsigned timeout)
int throw_timer_event ( void * (func)(void *), void *arg, unsigned timeout)
{
static int _unique_id = 1;
@ -248,7 +253,8 @@ int throw_timer_event ( void* (func)(void*), void* arg, unsigned timeout)
size_t _counter = event_handler.timed_events_count;
event_handler.timed_events[_counter - 1].timeout = timeout + ((uint32_t)(current_time() / 1000));
event_handler.timed_events[_counter - 1].id = _unique_id; ++_unique_id;
event_handler.timed_events[_counter - 1].id = _unique_id;
++_unique_id;
/* reorder */
@ -263,7 +269,7 @@ int execute_timer_event ( int id )
int _status;
LOCK((&event_handler));
EventContainer* _it = event_handler.timed_events;
EventContainer *_it = event_handler.timed_events;
int _i = event_handler.timed_events_count;
@ -273,25 +279,30 @@ int execute_timer_event ( int id )
RUN_IN_THREAD ( _it->func, _it->func_args );
break;
}
++_it;
}
/* Now remove it from the queue */
if ( _i ) {
for ( ; _i; -- _i ){ *_it = *(_it + 1); ++_it; }
for ( ; _i; -- _i ) {
*_it = *(_it + 1);
++_it;
}
-- event_handler.timed_events_count;
if ( !event_handler.timed_events_count ) { /* Free and set to null */
free(event_handler.timed_events);
event_handler.timed_events = NULL;
}
else {
void* _result = realloc(event_handler.timed_events, sizeof(EventContainer) * event_handler.timed_events_count); /* resize */
} else {
void *_result = realloc(event_handler.timed_events,
sizeof(EventContainer) * event_handler.timed_events_count); /* resize */
if ( _result != NULL ) { event_handler.timed_events = _result; }
else {
if ( _result != NULL ) {
event_handler.timed_events = _result;
} else {
/* Not sure what would happen next so abort execution.
*/
fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
@ -302,8 +313,7 @@ int execute_timer_event ( int id )
_status = 0;
}
else _status = -1;
} else _status = -1;
UNLOCK((&event_handler));
@ -316,7 +326,7 @@ int reset_timer_event ( int id, uint32_t timeout )
LOCK((&event_handler));
EventContainer* _it = event_handler.timed_events;
EventContainer *_it = event_handler.timed_events;
int _i = event_handler.timed_events_count;
@ -326,6 +336,7 @@ int reset_timer_event ( int id, uint32_t timeout )
_it->timeout = timeout + ((uint32_t)(current_time() / 1000));
break;
}
++_it;
}
@ -367,7 +378,7 @@ void __attribute__((destructor)) terminate_event_poll()
event_handler.running = 0;
/* Give it enought time to exit */
usleep(FREQUENCY*2);
usleep(FREQUENCY * 2);
pthread_mutex_destroy( &event_handler.mutex );
}

7
toxav/event.h Executable file → Normal file
View File

@ -38,11 +38,10 @@
* NOTE: timer_reset () and timer_now() are not tested nor usable atm
*
*/
extern struct _Event
{
int (*rise) (void* ( func ) ( void* ), void* arg);
extern struct _Event {
int (*rise) (void * ( func ) ( void * ), void *arg);
int (*timer_reset ) ( int id, unsigned timeout );
int (*timer_alloc) (void* ( func ) ( void* ), void* arg, unsigned timeout);
int (*timer_alloc) (void * ( func ) ( void * ), void *arg, unsigned timeout);
int (*timer_release) (int id);
int (*timer_now) ( int id );
} event;

View File

@ -50,8 +50,8 @@ struct jitter_buffer {
struct jitter_buffer *create_queue(int capacity)
{
struct jitter_buffer *q;
q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer),1);
q->queue = (RTPMessage **)calloc(sizeof(RTPMessage*), capacity);
q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer), 1);
q->queue = (RTPMessage **)calloc(sizeof(RTPMessage *), capacity);
int i = 0;
for (i = 0; i < capacity; ++i) {
@ -200,7 +200,8 @@ int queue(struct jitter_buffer *q, RTPMessage *pk)
int init_video_decoder(CodecState *cs)
{
if (vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION) != VPX_CODEC_OK) {
if (vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0,
VPX_DECODER_ABI_VERSION) != VPX_CODEC_OK) {
fprintf(stderr, "Init video_decoder failed!\n");
return -1;
}
@ -213,7 +214,7 @@ int init_audio_decoder(CodecState *cs, uint32_t audio_channels)
int rc;
cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc );
if ( rc != OPUS_OK ){
if ( rc != OPUS_OK ) {
fprintf(stderr, "Error while starting audio decoder!\n");
return -1;
}
@ -226,7 +227,8 @@ int init_video_encoder(CodecState *cs, uint16_t width, uint16_t height, uint32_t
{
vpx_codec_enc_cfg_t cfg;
int res = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
if(res) {
if (res) {
printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
return -1;
}
@ -234,10 +236,13 @@ int init_video_encoder(CodecState *cs, uint16_t width, uint16_t height, uint32_t
cfg.rc_target_bitrate = video_bitrate;
cfg.g_w = width;
cfg.g_h = height;
if(vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION) != VPX_CODEC_OK) {
if (vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0,
VPX_ENCODER_ABI_VERSION) != VPX_CODEC_OK) {
fprintf(stderr, "Failed to initialize encoder\n");
return -1;
}
return 0;
}
@ -253,7 +258,7 @@ int init_audio_encoder(CodecState *cs, uint32_t audio_channels)
}
CodecState* codec_init_session ( uint32_t audio_bitrate,
CodecState *codec_init_session ( uint32_t audio_bitrate,
uint16_t audio_frame_duration,
uint32_t audio_sample_rate,
uint32_t audio_channels,
@ -261,7 +266,7 @@ CodecState* codec_init_session ( uint32_t audio_bitrate,
uint16_t video_height,
uint32_t video_bitrate )
{
CodecState* _retu = calloc(sizeof(CodecState), 1);
CodecState *_retu = calloc(sizeof(CodecState), 1);
assert(_retu);
_retu->audio_bitrate = audio_bitrate;
@ -291,7 +296,7 @@ CodecState* codec_init_session ( uint32_t audio_bitrate,
return _retu;
}
void codec_terminate_session ( CodecState* cs )
void codec_terminate_session ( CodecState *cs )
{
if ( cs->audio_encoder ) {
opus_encoder_destroy(cs->audio_encoder);

View File

@ -39,7 +39,7 @@
#include <opus/opus.h>
typedef struct _CodecState{
typedef struct _CodecState {
/* video encoding */
vpx_codec_ctx_t v_encoder;
@ -67,7 +67,7 @@ int queue(struct jitter_buffer *q, RTPMessage *pk);
RTPMessage *dequeue(struct jitter_buffer *q, int *success);
CodecState* codec_init_session ( uint32_t audio_bitrate,
CodecState *codec_init_session ( uint32_t audio_bitrate,
uint16_t audio_frame_duration,
uint32_t audio_sample_rate,
uint32_t audio_channels,
@ -75,6 +75,6 @@ CodecState* codec_init_session ( uint32_t audio_bitrate,
uint16_t video_height,
uint32_t video_bitrate );
void codec_terminate_session(CodecState* cs);
void codec_terminate_session(CodecState *cs);
#endif

View File

@ -46,7 +46,7 @@
#define TYPE_REQUEST 1
#define TYPE_RESPONSE 2
unsigned char* VERSION_STRING = (unsigned char*)"0.3.1";
unsigned char *VERSION_STRING = (unsigned char *)"0.3.1";
#define VERSION_STRLEN 5
#define CT_AUDIO_HEADER_VALUE "AUDIO"
@ -110,7 +110,7 @@ typedef struct _MSIMessage {
MSIHeaderCryptoKey cryptokey;
MSIHeaderNonce nonce;
struct _MSIMessage* next;
struct _MSIMessage *next;
int friend_id;
@ -155,13 +155,14 @@ typedef enum {
* @param request The request.
* @return const uint8_t* The string
*/
static inline const uint8_t *stringify_request ( MSIRequest request ) {
static const uint8_t* strings[] = {
( uint8_t* ) "INVITE",
( uint8_t* ) "START",
( uint8_t* ) "CANCEL",
( uint8_t* ) "REJECT",
( uint8_t* ) "END"
static inline const uint8_t *stringify_request ( MSIRequest request )
{
static const uint8_t *strings[] = {
( uint8_t * ) "INVITE",
( uint8_t * ) "START",
( uint8_t * ) "CANCEL",
( uint8_t * ) "REJECT",
( uint8_t * ) "END"
};
return strings[request];
@ -183,12 +184,13 @@ typedef enum {
* @param response The response.
* @return const uint8_t* The string
*/
static inline const uint8_t *stringify_response ( MSIResponse response ) {
static const uint8_t* strings[] = {
( uint8_t* ) "ringing",
( uint8_t* ) "starting",
( uint8_t* ) "ending",
( uint8_t* ) "error"
static inline const uint8_t *stringify_response ( MSIResponse response )
{
static const uint8_t *strings[] = {
( uint8_t * ) "ringing",
( uint8_t * ) "starting",
( uint8_t * ) "ending",
( uint8_t * ) "error"
};
return strings[response];
@ -219,13 +221,14 @@ static inline const uint8_t *stringify_response ( MSIResponse response ) {
* @retval -1 Error occured.
* @retval 0 Success.
*/
int parse_raw_data ( MSIMessage* msg, const uint8_t* data, uint16_t length ) {
int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length )
{
assert ( msg );
if ( data[length - 1] ) /* End byte must have value 0 */
return -1;
const uint8_t* _it = data;
const uint8_t *_it = data;
while ( *_it ) {/* until end_byte is hit */
@ -284,6 +287,7 @@ int parse_raw_data ( MSIMessage* msg, const uint8_t* data, uint16_t length ) {
return -1;
}
} else return -1;
/* If it's anything else return failure as the message is invalid */
}
@ -304,7 +308,8 @@ var.size = t_size;
* @param msg The message.
* @return void
*/
void free_message ( MSIMessage* msg ) {
void free_message ( MSIMessage *msg )
{
assert ( msg );
free ( msg->calltype.header_value );
@ -330,22 +335,23 @@ void free_message ( MSIMessage* msg ) {
* @return MSIMessage* Created message.
* @retval NULL Error occured.
*/
MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
MSIMessage *msi_new_message ( uint8_t type, const uint8_t *type_id )
{
MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
assert ( _retu );
if ( type == TYPE_REQUEST ) {
ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char*)type_id ) )
ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char *)type_id ) )
} else if ( type == TYPE_RESPONSE ) {
ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char*)type_id ) )
ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char *)type_id ) )
} else {
free_message ( _retu );
return NULL;
}
ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char*)VERSION_STRING ) )
ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char *)VERSION_STRING ) )
return _retu;
}
@ -358,10 +364,11 @@ MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
* @return MSIMessage* Parsed message.
* @retval NULL Error occured.
*/
MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
{
assert ( data );
MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
assert ( _retu );
memset ( _retu, 0, sizeof ( MSIMessage ) );
@ -394,45 +401,53 @@ MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
* @param length Pointer to container length.
* @return uint8_t* Iterated container.
*/
uint8_t* append_header_to_string (
uint8_t* dest,
const uint8_t* header_field,
const uint8_t* header_value,
uint8_t *append_header_to_string (
uint8_t *dest,
const uint8_t *header_field,
const uint8_t *header_value,
uint16_t value_len,
uint16_t* length )
uint16_t *length )
{
assert ( dest );
assert ( header_value );
assert ( header_field );
const uint8_t* _hvit = header_value;
const uint8_t *_hvit = header_value;
uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/
*dest = field_byte; /* Set the first byte */
uint8_t* _getback_byte = dest + 1; /* remeber the byte we were on */
uint8_t *_getback_byte = dest + 1; /* remeber the byte we were on */
dest += 3; /* swith to 4th byte where field value starts */
/* Now set the field value and calculate it's length */
uint16_t _i = 0;
for ( ; header_field[_i]; ++_i ) {
*dest = header_field[_i];
++dest;
};
_total += _i;
/* Now set the length of the field byte */
*_getback_byte = ( uint8_t ) _i >> 8;
_getback_byte++;
*_getback_byte = ( uint8_t ) _i;
/* for value part do it regulary */
*dest = value_byte;
dest++;
*dest = ( uint8_t ) value_len >> 8;
dest++;
*dest = ( uint8_t ) value_len;
dest++;
for ( _i = value_len; _i; --_i ) {
@ -457,11 +472,12 @@ if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)
* @param dest Destination.
* @return uint16_t It's final size.
*/
uint16_t message_to_string ( MSIMessage* msg, uint8_t* dest ) {
uint16_t message_to_string ( MSIMessage *msg, uint8_t *dest )
{
assert ( msg );
assert ( dest );
uint8_t* _iterated = dest;
uint8_t *_iterated = dest;
uint16_t _size = 0;
CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version );
@ -504,7 +520,8 @@ GENERIC_SETTER_DEFINITION ( nonce )
* @param size Size of string.
* @return void
*/
void t_randomstr ( uint8_t* str, size_t size ) {
void t_randomstr ( uint8_t *str, size_t size )
{
assert ( str );
static const uint8_t _bytes[] =
@ -539,15 +556,16 @@ typedef enum {
* @param error_code The code.
* @return const uint8_t* The string.
*/
static inline const uint8_t *stringify_error ( MSICallError error_code ) {
static const uint8_t* strings[] = {
( uint8_t* ) "",
( uint8_t* ) "Using dead call",
( uint8_t* ) "Call id not set to any call",
( uint8_t* ) "Call id not available",
( uint8_t* ) "No active call in session",
( uint8_t* ) "No Crypto-key set",
( uint8_t* ) "Callee busy"
static inline const uint8_t *stringify_error ( MSICallError error_code )
{
static const uint8_t *strings[] = {
( uint8_t * ) "",
( uint8_t * ) "Using dead call",
( uint8_t * ) "Call id not set to any call",
( uint8_t * ) "Call id not available",
( uint8_t * ) "No active call in session",
( uint8_t * ) "No Crypto-key set",
( uint8_t * ) "Callee busy"
};
return strings[error_code];
@ -560,15 +578,16 @@ static inline const uint8_t *stringify_error ( MSICallError error_code ) {
* @param error_code The code.
* @return const uint8_t* The string.
*/
static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
static const uint8_t* strings[] = {
( uint8_t* ) "",
( uint8_t* ) "1",
( uint8_t* ) "2",
( uint8_t* ) "3",
( uint8_t* ) "4",
( uint8_t* ) "5",
( uint8_t* ) "6"
static inline const uint8_t *stringify_error_code ( MSICallError error_code )
{
static const uint8_t *strings[] = {
( uint8_t * ) "",
( uint8_t * ) "1",
( uint8_t * ) "2",
( uint8_t * ) "3",
( uint8_t * ) "4",
( uint8_t * ) "5",
( uint8_t * ) "6"
};
return strings[error_code];
@ -585,7 +604,7 @@ static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
* @retval -1 Error occured.
* @retval 0 Success.
*/
int send_message ( MSISession* session, MSIMessage* msg, uint32_t to )
int send_message ( MSISession *session, MSIMessage *msg, uint32_t to )
{
msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN );
@ -604,36 +623,38 @@ int send_message ( MSISession* session, MSIMessage* msg, uint32_t to )
* @param peer_id The peer.
* @return void
*/
void flush_peer_type ( MSISession* session, MSIMessage* msg, int peer_id ) {
void flush_peer_type ( MSISession *session, MSIMessage *msg, int peer_id )
{
if ( msg->calltype.header_value ) {
if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
session->call->type_peer[peer_id] = type_audio;
} else if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
} else if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
session->call->type_peer[peer_id] = type_video;
} else {} /* Error */
} else {} /* Error */
}
void handle_remote_connection_change(Messenger* messenger, int friend_num, uint8_t status, void* session_p)
void handle_remote_connection_change(Messenger *messenger, int friend_num, uint8_t status, void *session_p)
{
MSISession* session = session_p;
MSISession *session = session_p;
switch ( status )
{
case 0: /* Went offline */
{
switch ( status ) {
case 0: { /* Went offline */
if ( session->call ) {
int i = 0;
for ( ; i < session->call->peer_count; i ++ )
if ( session->call->peers[i] == friend_num ) {
msi_stopcall(session); /* Stop the call for now */
return;
}
}
} break;
}
break;
default: break;
default:
break;
}
}
@ -646,12 +667,13 @@ void handle_remote_connection_change(Messenger* messenger, int friend_num, uint8
* @return int
* @retval 0 It's always success.
*/
int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
MSIMessage* _msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
int handle_error ( MSISession *session, MSICallError errid, uint32_t to )
{
MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
const uint8_t* _error_code_str = stringify_error_code ( errid );
const uint8_t *_error_code_str = stringify_error_code ( errid );
msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char* ) _error_code_str ) );
msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char * ) _error_code_str ) );
send_message ( session, _msg_error, to );
free_message ( _msg_error );
@ -673,7 +695,8 @@ int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
* @retval -1 No error.
* @retval 0 Error occured and response sent.
*/
int has_call_error ( MSISession* session, MSIMessage* msg ) {
int has_call_error ( MSISession *session, MSIMessage *msg )
{
if ( !msg->callid.header_value ) {
return handle_error ( session, error_no_callid, msg->friend_id );
@ -695,21 +718,22 @@ int has_call_error ( MSISession* session, MSIMessage* msg ) {
* @param arg Control session
* @return void*
*/
void* handle_timeout ( void* arg )
void *handle_timeout ( void *arg )
{
/* Send hangup either way */
MSISession* _session = arg;
MSISession *_session = arg;
if ( _session && _session->call ) {
uint32_t* _peers = _session->call->peers;
uint32_t *_peers = _session->call->peers;
uint16_t _peer_count = _session->call->peer_count;
/* Cancel all? */
uint16_t _it = 0;
for ( ; _it < _peer_count; _it++ )
msi_cancel ( arg, _peers[_it], (const uint8_t*)"Timeout" );
msi_cancel ( arg, _peers[_it], (const uint8_t *)"Timeout" );
}
@ -727,12 +751,12 @@ void* handle_timeout ( void* arg )
* @param peer_id Its id.
* @return void
*/
void add_peer( MSICall* call, int peer_id )
void add_peer( MSICall *call, int peer_id )
{
if ( !call->peers ) {
call->peers = calloc(sizeof(int), 1);
call->peer_count = 1;
} else{
} else {
call->peer_count ++;
call->peers = realloc( call->peers, sizeof(int) * call->peer_count);
}
@ -749,11 +773,12 @@ void add_peer( MSICall* call, int peer_id )
* @param ringing_timeout Ringing timeout.
* @return MSICall* The created call.
*/
MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) {
MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
{
assert ( session );
assert ( peers );
MSICall* _call = calloc ( sizeof ( MSICall ), 1 );
MSICall *_call = calloc ( sizeof ( MSICall ), 1 );
_call->type_peer = calloc ( sizeof ( MSICallType ), peers );
assert ( _call );
@ -785,7 +810,8 @@ MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) {
* @retval -1 Error occured.
* @retval 0 Success.
*/
int terminate_call ( MSISession* session ) {
int terminate_call ( MSISession *session )
{
assert ( session );
if ( !session->call )
@ -802,7 +828,7 @@ int terminate_call ( MSISession* session ) {
/* Get a handle */
pthread_mutex_lock ( &session->call->mutex );
MSICall* _call = session->call;
MSICall *_call = session->call;
session->call = NULL;
free ( _call->type_peer );
@ -822,13 +848,15 @@ int terminate_call ( MSISession* session ) {
/********** Request handlers **********/
int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
int handle_recv_invite ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( session->call ) {
handle_error ( session, error_busy, msg->friend_id );
return 0;
}
if ( !msg->callid.header_value ) {
handle_error ( session, error_no_callid, msg->friend_id );
return 0;
@ -842,7 +870,7 @@ int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
flush_peer_type ( session, msg, 0 );
MSIMessage* _msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
send_message ( session, _msg_ringing, msg->friend_id );
free_message ( _msg_ringing );
@ -850,7 +878,8 @@ int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
int handle_recv_start ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
@ -873,14 +902,15 @@ int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
int handle_recv_reject ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
return 0;
MSIMessage* _msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
MSIMessage *_msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
send_message ( session, _msg_end, msg->friend_id );
free_message ( _msg_end );
@ -890,7 +920,8 @@ int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
int handle_recv_cancel ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
@ -903,14 +934,15 @@ int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
int handle_recv_end ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
return 0;
MSIMessage* _msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
send_message ( session, _msg_ending, msg->friend_id );
free_message ( _msg_ending );
@ -922,7 +954,8 @@ int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
}
/********** Response handlers **********/
int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
int handle_recv_ringing ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
@ -933,7 +966,8 @@ int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
int handle_recv_starting ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
@ -959,7 +993,7 @@ int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
session->call->state = call_active;
MSIMessage* _msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES );
msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES );
send_message ( session, _msg_start, msg->friend_id );
@ -972,7 +1006,8 @@ int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
int handle_recv_ending ( MSISession *session, MSIMessage *msg )
{
assert ( session );
if ( has_call_error ( session, msg ) == 0 )
@ -985,13 +1020,14 @@ int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
return 1;
}
int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
int handle_recv_error ( MSISession *session, MSIMessage *msg )
{
assert ( session );
assert ( session->call );
/* Handle error accordingly */
if ( msg->reason.header_value ) {
session->last_error_id = atoi ( ( const char* ) msg->reason.header_value );
session->last_error_id = atoi ( ( const char * ) msg->reason.header_value );
session->last_error_str = stringify_error ( session->last_error_id );
}
@ -1035,13 +1071,13 @@ int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
*
*
*/
void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16_t length, void* object )
void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16_t length, void *object )
{
/* Unused */
(void)messenger;
MSISession* _session = object;
MSIMessage* _msg;
MSISession *_session = object;
MSIMessage *_msg;
if ( !length ) return;
@ -1056,7 +1092,7 @@ void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16
if ( _msg->request.header_value ) { /* Handle request */
const uint8_t* _request_value = _msg->request.header_value;
const uint8_t *_request_value = _msg->request.header_value;
if ( same ( _request_value, stringify_request ( invite ) ) ) {
handle_recv_invite ( _session, _msg );
@ -1081,7 +1117,7 @@ void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16
} else if ( _msg->response.header_value ) { /* Handle response */
const uint8_t* _response_value = _msg->response.header_value;
const uint8_t *_response_value = _msg->response.header_value;
if ( same ( _response_value, stringify_response ( ringing ) ) ) {
handle_recv_ringing ( _session, _msg );
@ -1155,10 +1191,11 @@ void msi_register_callback ( MSICallback callback, MSICallbackID id )
* @return MSISession* The created session.
* @retval NULL Error occured.
*/
MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name ) {
MSISession *msi_init_session ( Messenger *messenger, const uint8_t *ua_name )
{
assert ( messenger );
MSISession* _retu = calloc ( sizeof ( MSISession ), 1 );
MSISession *_retu = calloc ( sizeof ( MSISession ), 1 );
assert ( _retu );
_retu->ua_name = ua_name;
@ -1186,13 +1223,14 @@ MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name ) {
* @param session The session
* @return int
*/
int msi_terminate_session ( MSISession* session ) {
int msi_terminate_session ( MSISession *session )
{
assert ( session );
int _status = 0;
terminate_call ( session );
m_callback_msi_packet((struct Messenger*) session->messenger_handle, NULL, NULL);
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
/* TODO: Clean it up more? */
@ -1211,10 +1249,11 @@ int msi_terminate_session ( MSISession* session ) {
* @param friend_id The friend.
* @return int
*/
int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) {
int msi_invite ( MSISession *session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
{
assert ( session );
MSIMessage* _msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
session->call = init_call ( session, 1, rngsec ); /* Just one for now */
t_randomstr ( session->call->id, CALL_ID_LEN );
@ -1226,10 +1265,10 @@ int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, ui
if ( call_type == type_audio ) {
msi_msg_set_calltype
( _msg_invite, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
( _msg_invite, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
} else {
msi_msg_set_calltype
( _msg_invite, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
( _msg_invite, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
}
send_message ( session, _msg_invite, friend_id );
@ -1251,16 +1290,18 @@ int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, ui
* @retval -1 Error occured.
* @retval 0 Success.
*/
int msi_hangup ( MSISession* session ) {
int msi_hangup ( MSISession *session )
{
assert ( session );
if ( !session->call || session->call->state != call_active )
return -1;
MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
MSIMessage *_msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
/* hangup for each peer */
int _it = 0;
for ( ; _it < session->call->peer_count; _it ++ )
send_message ( session, _msg_ending, session->call->peers[_it] );
@ -1280,18 +1321,19 @@ int msi_hangup ( MSISession* session ) {
* @param call_type Answer with Audio or Video(both).
* @return int
*/
int msi_answer ( MSISession* session, MSICallType call_type ) {
int msi_answer ( MSISession *session, MSICallType call_type )
{
assert ( session );
MSIMessage* _msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) );
MSIMessage *_msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) );
session->call->type_local = call_type;
if ( call_type == type_audio ) {
msi_msg_set_calltype
( _msg_starting, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
( _msg_starting, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
} else {
msi_msg_set_calltype
( _msg_starting, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
( _msg_starting, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
}
/* Now set the local encryption key and pass it with STARTING message */
@ -1321,12 +1363,13 @@ int msi_answer ( MSISession* session, MSICallType call_type ) {
* @param reason Set optional reason header. Pass NULL if none.
* @return int
*/
int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason ) {
int msi_cancel ( MSISession *session, uint32_t peer, const uint8_t *reason )
{
assert ( session );
MSIMessage* _msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) );
MSIMessage *_msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) );
if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char*)reason));
if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char *)reason));
send_message ( session, _msg_cancel, peer );
free_message ( _msg_cancel );
@ -1343,12 +1386,13 @@ int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason ) {
* @param session Control session.
* @return int
*/
int msi_reject ( MSISession* session, const uint8_t* reason ) {
int msi_reject ( MSISession *session, const uint8_t *reason )
{
assert ( session );
MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
MSIMessage *_msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char*)reason) + 1);
if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char *)reason) + 1);
send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] );
free_message ( _msg_reject );
@ -1365,7 +1409,8 @@ int msi_reject ( MSISession* session, const uint8_t* reason ) {
* @param session Control session.
* @return int
*/
int msi_stopcall ( MSISession* session ) {
int msi_stopcall ( MSISession *session )
{
assert ( session );
if ( !session->call )

View File

@ -33,7 +33,7 @@
#define CALL_ID_LEN 12
typedef void* ( *MSICallback ) ( void* arg );
typedef void *( *MSICallback ) ( void *arg );
/**
@ -66,15 +66,15 @@ typedef struct _MSICall { /* Call info structure */
MSICallState state;
MSICallType type_local; /* Type of payload user is ending */
MSICallType* type_peer; /* Type of payload others are sending */
MSICallType *type_peer; /* Type of payload others are sending */
uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */
uint8_t* key_local; /* The key for encryption */
uint8_t* key_peer; /* The key for decryption */
uint8_t *key_local; /* The key for encryption */
uint8_t *key_peer; /* The key for decryption */
uint8_t* nonce_local; /* Local nonce */
uint8_t* nonce_peer; /* Peer nonce */
uint8_t *nonce_local; /* Local nonce */
uint8_t *nonce_peer; /* Peer nonce */
int ringing_tout_ms; /* Ringing timeout in ms */
@ -84,7 +84,7 @@ typedef struct _MSICall { /* Call info structure */
pthread_mutex_t mutex; /* It's to be assumed that call will have
* seperate thread so add mutex
*/
uint32_t* peers;
uint32_t *peers;
uint16_t peer_count;
@ -98,15 +98,15 @@ typedef struct _MSICall { /* Call info structure */
typedef struct _MSISession {
/* Call handler */
struct _MSICall* call;
struct _MSICall *call;
int last_error_id; /* Determine the last error */
const uint8_t* last_error_str;
const uint8_t *last_error_str;
const uint8_t* ua_name;
const uint8_t *ua_name;
void* agent_handler; /* Pointer to an object that is handling msi */
Messenger* messenger_handle;
void *agent_handler; /* Pointer to an object that is handling msi */
Messenger *messenger_handle;
uint32_t frequ;
uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
@ -156,7 +156,7 @@ void msi_register_callback(MSICallback callback, MSICallbackID id);
* @return MSISession* The created session.
* @retval NULL Error occured.
*/
MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name );
MSISession *msi_init_session ( Messenger *messenger, const uint8_t *ua_name );
/**
@ -165,7 +165,7 @@ MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name );
* @param session The session
* @return int
*/
int msi_terminate_session ( MSISession* session );
int msi_terminate_session ( MSISession *session );
/**
@ -177,7 +177,7 @@ int msi_terminate_session ( MSISession* session );
* @param friend_id The friend.
* @return int
*/
int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id );
int msi_invite ( MSISession *session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id );
/**
@ -188,7 +188,7 @@ int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, ui
* @retval -1 Error occured.
* @retval 0 Success.
*/
int msi_hangup ( MSISession* session );
int msi_hangup ( MSISession *session );
/**
@ -198,7 +198,7 @@ int msi_hangup ( MSISession* session );
* @param call_type Answer with Audio or Video(both).
* @return int
*/
int msi_answer ( MSISession* session, MSICallType call_type );
int msi_answer ( MSISession *session, MSICallType call_type );
/**
@ -209,7 +209,7 @@ int msi_answer ( MSISession* session, MSICallType call_type );
* @param reason Set optional reason header. Pass NULL if none.
* @return int
*/
int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason );
int msi_cancel ( MSISession *session, uint32_t peer, const uint8_t *reason );
/**
@ -219,7 +219,7 @@ int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason );
* @param reason Set optional reason header. Pass NULL if none.
* @return int
*/
int msi_reject ( MSISession* session, const uint8_t* reason );
int msi_reject ( MSISession *session, const uint8_t *reason );
/**
@ -228,6 +228,6 @@ int msi_reject ( MSISession* session, const uint8_t* reason );
* @param session Control session.
* @return int
*/
int msi_stopcall ( MSISession* session );
int msi_stopcall ( MSISession *session );
#endif /* __TOXMSI */

360
toxav/phone.c Executable file → Normal file
View File

@ -97,7 +97,7 @@ typedef struct av_friend_s {
typedef struct av_session_s {
/* Encoding/decoding/capturing/playing */
ToxAv* av;
ToxAv *av;
VideoPicture video_picture;
struct ALCdevice *audio_capture_device;
@ -114,8 +114,8 @@ typedef struct av_session_s {
pthread_mutex_t _mutex;
Tox* _messenger;
av_friend_t* _friends;
Tox *_messenger;
av_friend_t *_friends;
int _friend_cout;
char _my_public_id[200];
#ifdef TOX_FFMPEG
@ -128,14 +128,14 @@ typedef struct av_session_s {
} av_session_t;
void av_allocate_friend(av_session_t* _phone, int _id, int _active)
void av_allocate_friend(av_session_t *_phone, int _id, int _active)
{
static int _new_id = 0;
if ( !_phone->_friends ) {
_phone->_friends = calloc(sizeof(av_friend_t), 1);
_phone->_friend_cout = 1;
} else{
} else {
_phone->_friend_cout ++;
_phone->_friends = realloc(_phone->_friends, sizeof(av_friend_t) * _phone->_friend_cout);
}
@ -147,13 +147,14 @@ void av_allocate_friend(av_session_t* _phone, int _id, int _active)
_phone->_friends->_active = _active;
}
av_friend_t* av_get_friend(av_session_t* _phone, int _id)
av_friend_t *av_get_friend(av_session_t *_phone, int _id)
{
av_friend_t* _friends = _phone->_friends;
av_friend_t *_friends = _phone->_friends;
if ( !_friends ) return NULL;
int _it = 0;
for (; _it < _phone->_friend_cout; _it ++)
if ( _friends[_it]._id == _id )
return _friends + _it;
@ -164,7 +165,7 @@ av_friend_t* av_get_friend(av_session_t* _phone, int _id)
/***************** MISC *****************/
void INFO (const char* _format, ...)
void INFO (const char *_format, ...)
{
printf("\r[!] ");
va_list _arg;
@ -187,7 +188,7 @@ unsigned char *hex_string_to_bin(char hex_string[])
return val;
}
int getinput( char* _buff, size_t _limit, int* _len )
int getinput( char *_buff, size_t _limit, int *_len )
{
if ( fgets(_buff, _limit, stdin) == NULL )
return -1;
@ -200,19 +201,19 @@ int getinput( char* _buff, size_t _limit, int* _len )
return 0;
}
char* trim_spaces ( char* buff )
char *trim_spaces ( char *buff )
{
int _i = 0, _len = strlen(buff);
char* container = calloc(sizeof(char), _len);
char *container = calloc(sizeof(char), _len);
int _ci = 0;
for ( ; _i < _len; _i++ ) {
while ( _i < _len && buff[_i] == ' ' )
_i++;
if ( _i < _len ){
if ( _i < _len ) {
container[_ci] = buff[_i];
_ci ++;
}
@ -279,18 +280,18 @@ int display_received_frame(av_session_t* _phone, vpx_image_t *image)
pict.linesize[1] = _phone->video_picture.bmp->pitches[2];
pict.linesize[2] = _phone->video_picture.bmp->pitches[1];
*/
/* Convert the image into YUV format that SDL uses *//*
sws_scale(_phone->sws_SDL_r_ctx, (uint8_t const * const *)r_video_frame->data, r_video_frame->linesize, 0,
/* Convert the image into YUV format that SDL uses *//*
sws_scale(_phone->sws_SDL_r_ctx, (uint8_t const * const *)r_video_frame->data, r_video_frame->linesize, 0,
cs->video_decoder_ctx->height, pict.data, pict.linesize );
SDL_UnlockYUVOverlay(_phone->video_picture.bmp);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = cs->video_decoder_ctx->width;
rect.h = cs->video_decoder_ctx->height;
SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);
return 1;
SDL_UnlockYUVOverlay(_phone->video_picture.bmp);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = cs->video_decoder_ctx->width;
rect.h = cs->video_decoder_ctx->height;
SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);
return 1;
}
*/
#ifdef TOX_FFMPEG
@ -298,7 +299,7 @@ void *encode_video_thread(void *arg)
{
INFO("Started encode video thread!");
av_session_t* _phone = arg;
av_session_t *_phone = arg;
_phone->running_encvid = 1;
//CodecState *cs = get_cs_temp(_phone->av);
@ -316,11 +317,12 @@ void *encode_video_thread(void *arg)
int numBytes;
/* Determine required buffer size and allocate buffer */
numBytes = avpicture_get_size(PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height);
buffer = (uint8_t *)av_calloc(numBytes * sizeof(uint8_t),1);
buffer = (uint8_t *)av_calloc(numBytes * sizeof(uint8_t), 1);
avpicture_fill((AVPicture *)s_video_frame, buffer, PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width,
_phone->webcam_decoder_ctx->height);
_phone->sws_ctx = sws_getContext(_phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height,
_phone->webcam_decoder_ctx->pix_fmt, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, PIX_FMT_YUV420P,
_phone->webcam_decoder_ctx->pix_fmt, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height,
PIX_FMT_YUV420P,
SWS_BILINEAR, NULL, NULL, NULL);
@ -362,9 +364,12 @@ void *encode_video_thread(void *arg)
}*/
if (video_frame_finished) {
memcpy(image->planes[VPX_PLANE_Y], s_video_frame->data[0], s_video_frame->linesize[0] * _phone->webcam_decoder_ctx->height);
memcpy(image->planes[VPX_PLANE_U], s_video_frame->data[1], s_video_frame->linesize[1] * _phone->webcam_decoder_ctx->height / 2);
memcpy(image->planes[VPX_PLANE_V], s_video_frame->data[2], s_video_frame->linesize[2] * _phone->webcam_decoder_ctx->height / 2);
memcpy(image->planes[VPX_PLANE_Y], s_video_frame->data[0],
s_video_frame->linesize[0] * _phone->webcam_decoder_ctx->height);
memcpy(image->planes[VPX_PLANE_U], s_video_frame->data[1],
s_video_frame->linesize[1] * _phone->webcam_decoder_ctx->height / 2);
memcpy(image->planes[VPX_PLANE_V], s_video_frame->data[2],
s_video_frame->linesize[2] * _phone->webcam_decoder_ctx->height / 2);
toxav_send_video (_phone->av, image);
//if (avcodec_encode_video2(cs->video_encoder_ctx, &enc_video_packet, s_video_frame, &got_packet) < 0) {
/*if (vpx_codec_encode(&cs->v_encoder, image, frame_counter, 1, 0, 0) != VPX_CODEC_OK) {
@ -415,20 +420,20 @@ void *encode_video_thread(void *arg)
void *encode_audio_thread(void *arg)
{
INFO("Started encode audio thread!");
av_session_t* _phone = arg;
av_session_t *_phone = arg;
_phone->running_encaud = 1;
int ret = 0;
int16_t frame[4096];
int frame_size = AUDIO_FRAME_SIZE;
ALint sample = 0;
alcCaptureStart((ALCdevice*)_phone->audio_capture_device);
alcCaptureStart((ALCdevice *)_phone->audio_capture_device);
while (_phone->running_encaud) {
alcGetIntegerv((ALCdevice*)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
alcGetIntegerv((ALCdevice *)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
if (sample >= frame_size) {
alcCaptureSamples((ALCdevice*)_phone->audio_capture_device, frame, frame_size);
alcCaptureSamples((ALCdevice *)_phone->audio_capture_device, frame, frame_size);
ret = toxav_send_audio(_phone->av, frame, frame_size);
@ -452,15 +457,16 @@ void *encode_audio_thread(void *arg)
void convert_to_rgb(vpx_image_t *img, unsigned char *out)
{
const int w = img->d_w;
const int w2 = w/2;
const int pstride = w*3;
const int w2 = w / 2;
const int pstride = w * 3;
const int h = img->d_h;
const int h2 = h/2;
const int h2 = h / 2;
const int strideY = img->stride[0];
const int strideU = img->stride[1];
const int strideV = img->stride[2];
int posy, posx;
for (posy = 0; posy < h2; posy++) {
unsigned char *dst = out + pstride * (posy * 2);
unsigned char *dst2 = out + pstride * (posy * 2 + 1);
@ -470,34 +476,59 @@ void convert_to_rgb(vpx_image_t *img, unsigned char *out)
const unsigned char *srcV = img->planes[2] + strideV * posy;
for (posx = 0; posx < w2; posx++) {
unsigned char Y,U,V;
short R,G,B;
short iR,iG,iB;
unsigned char Y, U, V;
short R, G, B;
short iR, iG, iB;
U = *(srcU++); V = *(srcV++);
iR = (351 * (V-128)) / 256;
iG = - (179 * (V-128)) / 256 - (86 * (U-128)) / 256;
iB = (444 * (U-128)) / 256;
U = *(srcU++);
V = *(srcV++);
iR = (351 * (V - 128)) / 256;
iG = - (179 * (V - 128)) / 256 - (86 * (U - 128)) / 256;
iB = (444 * (U - 128)) / 256;
Y = *(srcY++);
R = Y + iR ; G = Y + iG ; B = Y + iB ;
R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
*(dst++) = R; *(dst++) = G; *(dst++) = B;
R = Y + iR ;
G = Y + iG ;
B = Y + iB ;
R = (R < 0 ? 0 : (R > 255 ? 255 : R));
G = (G < 0 ? 0 : (G > 255 ? 255 : G));
B = (B < 0 ? 0 : (B > 255 ? 255 : B));
*(dst++) = R;
*(dst++) = G;
*(dst++) = B;
Y = *(srcY2++);
R = Y + iR ; G = Y + iG ; B = Y + iB ;
R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
*(dst2++) = R; *(dst2++) = G; *(dst2++) = B;
R = Y + iR ;
G = Y + iG ;
B = Y + iB ;
R = (R < 0 ? 0 : (R > 255 ? 255 : R));
G = (G < 0 ? 0 : (G > 255 ? 255 : G));
B = (B < 0 ? 0 : (B > 255 ? 255 : B));
*(dst2++) = R;
*(dst2++) = G;
*(dst2++) = B;
Y = *(srcY++) ;
R = Y + iR ; G = Y + iG ; B = Y + iB ;
R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
*(dst++) = R; *(dst++) = G; *(dst++) = B;
R = Y + iR ;
G = Y + iG ;
B = Y + iB ;
R = (R < 0 ? 0 : (R > 255 ? 255 : R));
G = (G < 0 ? 0 : (G > 255 ? 255 : G));
B = (B < 0 ? 0 : (B > 255 ? 255 : B));
*(dst++) = R;
*(dst++) = G;
*(dst++) = B;
Y = *(srcY2++);
R = Y + iR ; G = Y + iG ; B = Y + iB ;
R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
*(dst2++) = R; *(dst2++) = G; *(dst2++) = B;
R = Y + iR ;
G = Y + iG ;
B = Y + iB ;
R = (R < 0 ? 0 : (R > 255 ? 255 : R));
G = (G < 0 ? 0 : (G > 255 ? 255 : G));
B = (B < 0 ? 0 : (B > 255 ? 255 : B));
*(dst2++) = R;
*(dst2++) = G;
*(dst2++) = B;
}
}
}
@ -507,7 +538,7 @@ void convert_to_rgb(vpx_image_t *img, unsigned char *out)
void *decode_video_thread(void *arg)
{
INFO("Started decode video thread!");
av_session_t* _phone = arg;
av_session_t *_phone = arg;
_phone->running_decvid = 1;
//CodecState *cs = get_cs_temp(_phone->av);
@ -528,6 +559,7 @@ void *decode_video_thread(void *arg)
//recved_size = toxav_recv_rtp_payload(_phone->av, TypeVideo, dest);
//if (recved_size) {
vpx_image_t *image;
if (toxav_recv_video(_phone->av, &image) == 0) {
//memcpy(dec_video_packet.data, dest, recved_size);
//dec_video_packet.size = recved_size;
@ -553,11 +585,15 @@ void *decode_video_thread(void *arg)
// _phone->sws_SDL_r_ctx = sws_getContext(width, height, cs->video_decoder_ctx->pix_fmt, width, height, PIX_FMT_YUV420P,
// SWS_BILINEAR, NULL, NULL, NULL);
}
uint8_t *rgb_image = malloc(width*height*3);
uint8_t *rgb_image = malloc(width * height * 3);
convert_to_rgb(image, rgb_image);
SDL_Surface* img_surface = SDL_CreateRGBSurfaceFrom(rgb_image, width, height, 24, width * 3, mask32(0), mask32(1), mask32(2), 0);
if(SDL_BlitSurface(img_surface, NULL, screen, NULL) == 0)
SDL_Surface *img_surface = SDL_CreateRGBSurfaceFrom(rgb_image, width, height, 24, width * 3, mask32(0), mask32(1),
mask32(2), 0);
if (SDL_BlitSurface(img_surface, NULL, screen, NULL) == 0)
SDL_UpdateRect(screen, 0, 0, 0, 0);
/*
SDL_LockYUVOverlay(_phone->video_picture.bmp);
memcpy(_phone->video_picture.bmp->pixels[0], image->planes[VPX_PLANE_Y], _phone->video_picture.bmp->pitches[0] * height);
@ -574,6 +610,7 @@ void *decode_video_thread(void *arg)
//display_received_frame(_phone, image);
} //else {
/* TODO: request the sender to create a new i-frame immediatly */
//printf("Bad video packet\n");
//}
@ -597,7 +634,7 @@ void *decode_video_thread(void *arg)
void *decode_audio_thread(void *arg)
{
INFO("Started decode audio thread!");
av_session_t* _phone = arg;
av_session_t *_phone = arg;
_phone->running_decaud = 1;
//int recved_size;
@ -614,7 +651,7 @@ void *decode_audio_thread(void *arg)
alcMakeContextCurrent(ctx);
int openal_buffers = 5;
buffers = calloc(sizeof(ALuint) * openal_buffers,1);
buffers = calloc(sizeof(ALuint) * openal_buffers, 1);
alGenBuffers(openal_buffers, buffers);
alGenSources((ALuint)1, &source);
alSourcei(source, AL_LOOPING, AL_FALSE);
@ -627,6 +664,7 @@ void *decode_audio_thread(void *arg)
int16_t PCM[frame_size];
int i;
for (i = 0; i < openal_buffers; ++i) {
alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000);
}
@ -644,6 +682,7 @@ void *decode_audio_thread(void *arg)
while (_phone->running_decaud) {
alGetSourcei(source, AL_BUFFERS_PROCESSED, &ready);
if (ready <= 0)
continue;
@ -697,9 +736,9 @@ ending:
int phone_startmedia_loop ( ToxAv* arg )
int phone_startmedia_loop ( ToxAv *arg )
{
if ( !arg ){
if ( !arg ) {
return -1;
}
@ -709,31 +748,30 @@ int phone_startmedia_loop ( ToxAv* arg )
* Rise all threads
*/
#ifdef TOX_FFMPEG
/* Only checks for last peer */
if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo &&
0 > event.rise(encode_video_thread, toxav_get_agent_handler(arg)) )
{
0 > event.rise(encode_video_thread, toxav_get_agent_handler(arg)) ) {
INFO("Error while starting encode_video_thread()");
return -1;
}
#endif
/* Always send audio */
if ( 0 > event.rise(encode_audio_thread, toxav_get_agent_handler(arg)) )
{
if ( 0 > event.rise(encode_audio_thread, toxav_get_agent_handler(arg)) ) {
INFO("Error while starting encode_audio_thread()");
return -1;
}
/* Only checks for last peer */
if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo &&
0 > event.rise(decode_video_thread, toxav_get_agent_handler(arg)) )
{
0 > event.rise(decode_video_thread, toxav_get_agent_handler(arg)) ) {
INFO("Error while starting decode_video_thread()");
return -1;
}
if ( 0 > event.rise(decode_audio_thread, toxav_get_agent_handler(arg)) )
{
if ( 0 > event.rise(decode_audio_thread, toxav_get_agent_handler(arg)) ) {
INFO("Error while starting decode_audio_thread()");
return -1;
}
@ -760,14 +798,15 @@ int phone_startmedia_loop ( ToxAv* arg )
/* Some example callbacks */
void* callback_recv_invite ( void* _arg )
void *callback_recv_invite ( void *_arg )
{
assert(_arg);
switch ( toxav_get_peer_transmission_type(_arg, 0) ){
switch ( toxav_get_peer_transmission_type(_arg, 0) ) {
case TypeAudio:
INFO( "Incoming audio call!");
break;
case TypeVideo:
INFO( "Incoming video call!");
break;
@ -775,23 +814,24 @@ void* callback_recv_invite ( void* _arg )
pthread_exit(NULL);
}
void* callback_recv_ringing ( void* _arg )
void *callback_recv_ringing ( void *_arg )
{
INFO ( "Ringing!" );
pthread_exit(NULL);
}
void* callback_recv_starting ( void* _arg )
void *callback_recv_starting ( void *_arg )
{
if ( 0 != phone_startmedia_loop(_arg) ){
if ( 0 != phone_startmedia_loop(_arg) ) {
INFO("Starting call failed!");
} else {
INFO ("Call started! ( press h to hangup )");
}
pthread_exit(NULL);
}
void* callback_recv_ending ( void* _arg )
void *callback_recv_ending ( void *_arg )
{
av_session_t* _phone = toxav_get_agent_handler(_arg);
av_session_t *_phone = toxav_get_agent_handler(_arg);
_phone->running_encaud = 0;
_phone->running_decaud = 0;
@ -811,7 +851,7 @@ void* callback_recv_ending ( void* _arg )
pthread_exit(NULL);
}
void* callback_recv_error ( void* _arg )
void *callback_recv_error ( void *_arg )
{
/*MSISession* _session = _arg;
@ -819,9 +859,9 @@ void* callback_recv_error ( void* _arg )
pthread_exit(NULL);
}
void* callback_call_started ( void* _arg )
void *callback_call_started ( void *_arg )
{
if ( 0 != phone_startmedia_loop(_arg) ){
if ( 0 != phone_startmedia_loop(_arg) ) {
INFO("Starting call failed!");
} else {
INFO ("Call started! ( press h to hangup )");
@ -829,19 +869,19 @@ void* callback_call_started ( void* _arg )
pthread_exit(NULL);
}
void* callback_call_canceled ( void* _arg )
void *callback_call_canceled ( void *_arg )
{
INFO ( "Call canceled!" );
pthread_exit(NULL);
}
void* callback_call_rejected ( void* _arg )
void *callback_call_rejected ( void *_arg )
{
INFO ( "Call rejected!" );
pthread_exit(NULL);
}
void* callback_call_ended ( void* _arg )
void *callback_call_ended ( void *_arg )
{
av_session_t* _phone = toxav_get_agent_handler(_arg);
av_session_t *_phone = toxav_get_agent_handler(_arg);
_phone->running_encaud = 0;
_phone->running_decaud = 0;
@ -862,15 +902,15 @@ void* callback_call_ended ( void* _arg )
pthread_exit(NULL);
}
void* callback_requ_timeout ( void* _arg )
void *callback_requ_timeout ( void *_arg )
{
INFO( "No answer! " );
pthread_exit(NULL);
}
av_session_t* av_init_session()
av_session_t *av_init_session()
{
av_session_t* _retu = malloc(sizeof(av_session_t));
av_session_t *_retu = malloc(sizeof(av_session_t));
/* Initialize our mutex */
pthread_mutex_init ( &_retu->_mutex, NULL );
@ -902,8 +942,9 @@ av_session_t* av_init_session()
INFO("Enter capture device number");
char dev[2]; char* left;
char* warned_ = fgets(dev, 2, stdin);
char dev[2];
char *left;
char *warned_ = fgets(dev, 2, stdin);
(void)warned_;
long selection = strtol(dev, &left, 10);
@ -911,20 +952,20 @@ av_session_t* av_init_session()
printf("'%s' is not a number!", dev);
fflush(stdout);
exit(EXIT_FAILURE);
}
else {
} else {
INFO("Selected: %d ( %s )", selection, device_names[selection]);
}
_retu->audio_capture_device =
(struct ALCdevice*)alcCaptureOpenDevice(
(struct ALCdevice *)alcCaptureOpenDevice(
device_names[selection], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16, AUDIO_FRAME_SIZE * 4);
if (alcGetError((ALCdevice*)_retu->audio_capture_device) != AL_NO_ERROR) {
printf("Could not start capture device! %d\n", alcGetError((ALCdevice*)_retu->audio_capture_device));
if (alcGetError((ALCdevice *)_retu->audio_capture_device) != AL_NO_ERROR) {
printf("Could not start capture device! %d\n", alcGetError((ALCdevice *)_retu->audio_capture_device));
return 0;
}
uint16_t height = 0, width = 0;
#ifdef TOX_FFMPEG
avdevice_register_all();
@ -932,6 +973,7 @@ av_session_t* av_init_session()
av_register_all();
_retu->video_input_format = av_find_input_format(VIDEO_DRIVER);
if (avformat_open_input(&_retu->video_format_ctx, DEFAULT_WEBCAM, _retu->video_input_format, NULL) != 0) {
fprintf(stderr, "Opening video_input_format failed!\n");
//return -1;
@ -968,6 +1010,7 @@ av_session_t* av_init_session()
//return -1;
return NULL;
}
width = _retu->webcam_decoder_ctx->width;
height = _retu->webcam_decoder_ctx->height;
#endif
@ -998,15 +1041,16 @@ av_session_t* av_init_session()
return _retu;
}
int av_terminate_session(av_session_t* _phone)
int av_terminate_session(av_session_t *_phone)
{
toxav_hangup(_phone->av);
free(_phone->_friends);
pthread_mutex_destroy ( &_phone->_mutex );
Tox* _p = _phone->_messenger;
_phone->_messenger = NULL; usleep(100000); /* Wait for tox_poll to end */
Tox *_p = _phone->_messenger;
_phone->_messenger = NULL;
usleep(100000); /* Wait for tox_poll to end */
tox_kill(_p);
toxav_kill(_phone->av);
@ -1022,7 +1066,7 @@ int av_terminate_session(av_session_t* _phone)
/* Auto accept friend request */
void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata)
{
av_session_t* _phone = _userdata;
av_session_t *_phone = _userdata;
av_allocate_friend (_phone, -1, 0);
INFO("Got friend request with message: %s", _data);
@ -1034,10 +1078,10 @@ void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void
void av_friend_active(Tox *_messenger, int _friendnumber, uint8_t *_string, uint16_t _length, void *_userdata)
{
av_session_t* _phone = _userdata;
av_session_t *_phone = _userdata;
INFO("Friend no. %d is online", _friendnumber);
av_friend_t* _this_friend = av_get_friend(_phone, _friendnumber);
av_friend_t *_this_friend = av_get_friend(_phone, _friendnumber);
if ( !_this_friend ) {
INFO("But it's not registered!");
@ -1047,25 +1091,25 @@ void av_friend_active(Tox *_messenger, int _friendnumber, uint8_t *_string, uint
(*_this_friend)._active = 1;
}
int av_add_friend(av_session_t* _phone, char* _friend_hash)
int av_add_friend(av_session_t *_phone, char *_friend_hash)
{
trim_spaces(_friend_hash);
unsigned char *_bin_string = hex_string_to_bin(_friend_hash);
int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT, sizeof("Tox phone "_USERAGENT));
int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT,
sizeof("Tox phone "_USERAGENT));
free(_bin_string);
if ( _number >= 0) {
INFO("Added friend as %d", _number );
av_allocate_friend(_phone, _number, 0);
}
else
} else
INFO("Unknown error %i", _number );
return _number;
}
int av_connect_to_dht(av_session_t* _phone, char* _dht_key, const char* _dht_addr, unsigned short _dht_port)
int av_connect_to_dht(av_session_t *_phone, char *_dht_key, const char *_dht_addr, unsigned short _dht_port)
{
unsigned char *_binary_string = hex_string_to_bin(_dht_key);
@ -1080,7 +1124,7 @@ int av_connect_to_dht(av_session_t* _phone, char* _dht_key, const char* _dht_add
/*********************************/
void do_phone ( av_session_t* _phone )
void do_phone ( av_session_t *_phone )
{
INFO("Welcome to tox_phone version: " _USERAGENT "\n"
"Usage: \n"
@ -1093,73 +1137,70 @@ void do_phone ( av_session_t* _phone )
"================================================================================"
);
while ( 1 )
{
while ( 1 ) {
char _line [ 1500 ];
int _len;
if ( -1 == getinput(_line, 1500, &_len) ){
if ( -1 == getinput(_line, 1500, &_len) ) {
printf(" >> ");
fflush(stdout);
continue;
}
if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ){
if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ) {
INFO("Invalid input!");
continue;
}
switch (_line[0]){
switch (_line[0]) {
case 'f':
{
case 'f': {
char _id [128];
strncpy(_id, _line + 2, 128);
av_add_friend(_phone, _id);
} break;
case 'c':
{
}
break;
case 'c': {
ToxAvCallType _ctype;
if ( _len < 5 ){
if ( _len < 5 ) {
INFO("Invalid input; usage: c a/v [friend]");
break;
}
else if ( _line[2] == 'a' || _line[2] != 'v' ){ /* default and audio */
} else if ( _line[2] == 'a' || _line[2] != 'v' ) { /* default and audio */
_ctype = TypeAudio;
}
else { /* video */
} else { /* video */
_ctype = TypeVideo;
}
char* _end;
char *_end;
int _friend = strtol(_line + 4, &_end, 10);
if ( *_end ){
if ( *_end ) {
INFO("Friend num has to be numerical value");
break;
}
if ( toxav_call(_phone->av, _friend, _ctype, 30) == ErrorAlreadyInCall ){
if ( toxav_call(_phone->av, _friend, _ctype, 30) == ErrorAlreadyInCall ) {
INFO("Already in a call");
break;
}
else INFO("Calling friend: %d!", _friend);
} else INFO("Calling friend: %d!", _friend);
} break;
case 'h':
{
}
break;
case 'h': {
if ( toxav_hangup(_phone->av) == ErrorNoCall ) {
INFO("No call!");
break;
}
else INFO("Hung up...");
} else INFO("Hung up...");
} break;
case 'a':
{
}
break;
case 'a': {
ToxAvError rc;
if ( _len > 1 && _line[2] == 'v' ) {
@ -1171,24 +1212,26 @@ void do_phone ( av_session_t* _phone )
INFO("No call to answer!");
}
} break;
case 'r':
{
}
break;
case 'r': {
if ( toxav_reject(_phone->av, "User action") == ErrorInvalidState )
INFO("No state to cancel!");
else INFO("Call Rejected...");
} break;
case 'q':
{
}
break;
case 'q': {
INFO("Quitting!");
return;
}
case '\n':
{
case '\n': {
}
default:
{
default: {
} break;
}
@ -1196,10 +1239,11 @@ void do_phone ( av_session_t* _phone )
}
}
void* tox_poll (void* _messenger_p)
void *tox_poll (void *_messenger_p)
{
Tox** _messenger = _messenger_p;
while( *_messenger ) {
Tox **_messenger = _messenger_p;
while ( *_messenger ) {
tox_do(*_messenger);
usleep(10000);
}
@ -1207,17 +1251,16 @@ void* tox_poll (void* _messenger_p)
pthread_exit(NULL);
}
int av_wait_dht(av_session_t* _phone, int _wait_seconds, const char* _ip, char* _key, unsigned short _port)
int av_wait_dht(av_session_t *_phone, int _wait_seconds, const char *_ip, char *_key, unsigned short _port)
{
if ( !_wait_seconds )
return -1;
int _waited = 0;
while( !tox_isconnected(_phone->_messenger) ) {
while ( !tox_isconnected(_phone->_messenger) ) {
if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) )
{
if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) ) {
INFO("Could not connect to: %s", _ip);
av_terminate_session(_phone);
return -1;
@ -1237,27 +1280,27 @@ int av_wait_dht(av_session_t* _phone, int _wait_seconds, const char* _ip, char*
}
/* ---------------------- */
int print_help ( const char* _name )
int print_help ( const char *_name )
{
printf ( "Usage: %s [IP] [PORT] [KEY]\n"
"\t[IP] (DHT ip)\n"
"\t[PORT] (DHT port)\n"
"\t[KEY] (DHT public key)\n"
"P.S. Friends and key are stored in ./tox_phone.conf\n"
,_name );
, _name );
return 1;
}
int main ( int argc, char* argv [] )
int main ( int argc, char *argv [] )
{
if ( argc < 1 || argc < 4 )
return print_help(argv[0]);
char* _convertable;
char *_convertable;
const char* _ip = argv[1];
char* _key = argv[3];
const char *_ip = argv[1];
char *_key = argv[3];
unsigned short _port = strtol(argv[2], &_convertable, 10);
if ( *_convertable ) {
@ -1265,7 +1308,7 @@ int main ( int argc, char* argv [] )
return 1;
}
av_session_t* _phone = av_init_session();
av_session_t *_phone = av_init_session();
tox_callback_friend_request(_phone->_messenger, av_friend_requ, _phone);
tox_callback_status_message(_phone->_messenger, av_friend_active, _phone);
@ -1284,6 +1327,7 @@ int main ( int argc, char* argv [] )
int _r;
int _wait_seconds = 5;
for ( _r = 0; _r == 0; _r = av_wait_dht(_phone, _wait_seconds, _ip, _key, _port) ) _wait_seconds --;

View File

@ -60,7 +60,7 @@
* @param bytes What bytes
* @return void
*/
inline__ void bytes_to_U32(uint32_t* dest, const uint8_t* bytes)
inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes)
{
*dest =
#ifdef WORDS_BIGENDIAN
@ -83,7 +83,7 @@ inline__ void bytes_to_U32(uint32_t* dest, const uint8_t* bytes)
* @param bytes What bytes
* @return void
*/
inline__ void bytes_to_U16(uint16_t* dest, const uint8_t* bytes)
inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes)
{
*dest =
#ifdef WORDS_BIGENDIAN
@ -102,7 +102,7 @@ inline__ void bytes_to_U16(uint16_t* dest, const uint8_t* bytes)
* @param value The value
* @return void
*/
inline__ void U32_to_bytes(uint8_t* dest, uint32_t value)
inline__ void U32_to_bytes(uint8_t *dest, uint32_t value)
{
#ifdef WORDS_BIGENDIAN
*(dest) = ( value );
@ -124,7 +124,7 @@ inline__ void U32_to_bytes(uint8_t* dest, uint32_t value)
* @param value The value
* @return void
*/
inline__ void U16_to_bytes(uint8_t* dest, uint16_t value)
inline__ void U16_to_bytes(uint8_t *dest, uint16_t value)
{
#ifdef WORDS_BIGENDIAN
*(dest) = ( value );
@ -145,7 +145,7 @@ inline__ void U16_to_bytes(uint8_t* dest, uint16_t value)
* @retval -1 The message came in order.
* @retval 0 The message came late.
*/
inline__ int check_late_message (RTPSession* session, RTPMessage* msg)
inline__ int check_late_message (RTPSession *session, RTPMessage *msg)
{
/*
* Check Sequence number. If this new msg has lesser number then the session->rsequnum
@ -163,7 +163,7 @@ inline__ int check_late_message (RTPSession* session, RTPMessage* msg)
* @param target The target
* @return void
*/
inline__ void increase_nonce(uint8_t* nonce, uint16_t target)
inline__ void increase_nonce(uint8_t *nonce, uint16_t target)
{
uint16_t _nonce_counter;
@ -176,12 +176,13 @@ inline__ void increase_nonce(uint8_t* nonce, uint16_t target)
/* Check overflow */
if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */
uint8_t _it = 3;
while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1;
_nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */
} else { /* Increase nonce */
_nonce_counter+= target;
_nonce_counter += target;
}
/* Assign the last bytes */
@ -197,8 +198,7 @@ inline__ void increase_nonce(uint8_t* nonce, uint16_t target)
* @brief Speaks for it self.
*
*/
static const uint32_t payload_table[] =
{
static const uint32_t payload_table[] = {
8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */
44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */
0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */
@ -223,25 +223,26 @@ static const uint32_t payload_table[] =
* @return RTPHeader* Extracted header.
* @retval NULL Error occurred while extracting header.
*/
RTPHeader* extract_header ( const uint8_t* payload, int length )
RTPHeader *extract_header ( const uint8_t *payload, int length )
{
if ( !payload || !length ) {
return NULL;
}
const uint8_t* _it = payload;
const uint8_t *_it = payload;
RTPHeader* _retu = calloc(1, sizeof (RTPHeader));
RTPHeader *_retu = calloc(1, sizeof (RTPHeader));
assert(_retu);
_retu->flags = *_it; ++_it;
_retu->flags = *_it;
++_it;
/* This indicates if the first 2 bits are valid.
* Now it may happen that this is out of order but
* it cuts down chances of parsing some invalid value
*/
if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ){
if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ) {
/* Deallocate */
free(_retu);
return NULL;
@ -271,16 +272,20 @@ RTPHeader* extract_header ( const uint8_t* payload, int length )
}
_retu->marker_payloadt = *_it; ++_it;
_retu->marker_payloadt = *_it;
++_it;
_retu->length = _length;
bytes_to_U32(&_retu->timestamp, _it); _it += 4;
bytes_to_U32(&_retu->timestamp, _it);
_it += 4;
bytes_to_U32(&_retu->ssrc, _it);
uint8_t _x;
for ( _x = 0; _x < _cc; _x++ ) {
_it += 4; bytes_to_U32(&(_retu->csrc[_x]), _it);
_it += 4;
bytes_to_U32(&(_retu->csrc[_x]), _it);
}
return _retu;
@ -294,15 +299,16 @@ RTPHeader* extract_header ( const uint8_t* payload, int length )
* @return RTPExtHeader* Extracted extension header.
* @retval NULL Error occurred while extracting extension header.
*/
RTPExtHeader* extract_ext_header ( const uint8_t* payload, uint16_t length )
RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
{
const uint8_t* _it = payload;
const uint8_t *_it = payload;
RTPExtHeader* _retu = calloc(1, sizeof (RTPExtHeader));
RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader));
assert(_retu);
uint16_t _ext_length;
bytes_to_U16(&_ext_length, _it); _it += 2;
bytes_to_U16(&_ext_length, _it);
_it += 2;
if ( length < ( _ext_length * sizeof(uint32_t) ) ) {
@ -311,14 +317,17 @@ RTPExtHeader* extract_ext_header ( const uint8_t* payload, uint16_t length )
}
_retu->length = _ext_length;
bytes_to_U16(&_retu->type, _it); _it += 2;
bytes_to_U16(&_retu->type, _it);
_it += 2;
_retu->table = calloc(_ext_length, sizeof (uint32_t));
assert(_retu->table);
uint16_t _x;
for ( _x = 0; _x < _ext_length; _x++ ) {
_it += 4; bytes_to_U32(&(_retu->table[_x]), _it);
_it += 4;
bytes_to_U32(&(_retu->table[_x]), _it);
}
return _retu;
@ -331,27 +340,33 @@ RTPExtHeader* extract_ext_header ( const uint8_t* payload, uint16_t length )
* @param payload The payload.
* @return uint8_t* Iterated position.
*/
uint8_t* add_header ( RTPHeader* header, uint8_t* payload )
uint8_t *add_header ( RTPHeader *header, uint8_t *payload )
{
uint8_t _cc = GET_FLAG_CSRCC ( header );
uint8_t* _it = payload;
uint8_t *_it = payload;
/* Add sequence number first */
U16_to_bytes(_it, header->sequnum); _it += 2;
U16_to_bytes(_it, header->sequnum);
_it += 2;
*_it = header->flags; ++_it;
*_it = header->marker_payloadt; ++_it;
*_it = header->flags;
++_it;
*_it = header->marker_payloadt;
++_it;
U32_to_bytes( _it, header->timestamp); _it+=4;
U32_to_bytes( _it, header->timestamp);
_it += 4;
U32_to_bytes( _it, header->ssrc);
if ( header->csrc ) {
uint8_t _x;
for ( _x = 0; _x < _cc; _x++ ) {
_it+=4; U32_to_bytes( _it, header->csrc[_x]);
_it += 4;
U32_to_bytes( _it, header->csrc[_x]);
}
}
@ -365,17 +380,21 @@ uint8_t* add_header ( RTPHeader* header, uint8_t* payload )
* @param payload The payload.
* @return uint8_t* Iterated position.
*/
uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload )
uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload )
{
uint8_t* _it = payload;
uint8_t *_it = payload;
U16_to_bytes(_it, header->length); _it+=2;
U16_to_bytes(_it, header->type); _it-=2; /* Return to 0 position */
U16_to_bytes(_it, header->length);
_it += 2;
U16_to_bytes(_it, header->type);
_it -= 2; /* Return to 0 position */
if ( header->table ) {
uint16_t _x;
for ( _x = 0; _x < header->length; _x++ ) {
_it+=4; U32_to_bytes(_it, header->table[_x]);
_it += 4;
U32_to_bytes(_it, header->table[_x]);
}
}
@ -388,9 +407,9 @@ uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload )
* @param session Control session.
* @return RTPHeader* Created header.
*/
RTPHeader* build_header ( RTPSession* session )
RTPHeader *build_header ( RTPSession *session )
{
RTPHeader* _retu = calloc ( 1, sizeof (RTPHeader) );
RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) );
assert(_retu);
ADD_FLAG_VERSION ( _retu, session->version );
@ -435,16 +454,17 @@ RTPHeader* build_header ( RTPSession* session )
* @return RTPMessage*
* @retval NULL Error occurred.
*/
RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length )
RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
{
RTPMessage* _retu = calloc(1, sizeof (RTPMessage));
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
_retu->header = extract_header ( data, length ); /* It allocates memory and all */
if ( !_retu->header ){
if ( !_retu->header ) {
free(_retu);
return NULL;
}
_retu->header->sequnum = sequnum;
_retu->length = length - _retu->header->length;
@ -454,7 +474,8 @@ RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length )
if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
_retu->ext_header = extract_ext_header ( data + _from_pos, length );
if ( _retu->ext_header ){
if ( _retu->ext_header ) {
_retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
_from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
} else { /* Error */
@ -473,6 +494,7 @@ RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length )
rtp_free_msg(NULL, _retu);
return NULL;
}
_retu->next = NULL;
return _retu;
@ -489,10 +511,10 @@ RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length )
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t length )
int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t length )
{
RTPSession* _session = object;
RTPMessage* _msg;
RTPSession *_session = object;
RTPMessage *_msg;
if ( !_session || length < 13 ) /* 12 is the minimum length for rtp + desc. byte */
return -1;
@ -509,18 +531,18 @@ int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t l
/* Decrypt message */
int _decrypted_length = decrypt_data_symmetric(
(uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
/* This packet is either not encrypted properly or late
*/
if ( -1 == _decrypted_length ){
if ( -1 == _decrypted_length ) {
/* If this is the case, then the packet is most likely late.
* Try with old nonce cycle.
*/
if ( _session->rsequnum < _sequnum ) {
_decrypted_length = decrypt_data_symmetric(
(uint8_t*)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
(uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
if ( !_decrypted_length ) return -1; /* This packet is not encrypted properly */
@ -529,7 +551,7 @@ int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t l
} else {
increase_nonce ( _calculated, MAX_SEQU_NUM );
_decrypted_length = decrypt_data_symmetric(
(uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
if ( !_decrypted_length ) return -1; /* This is just an error */
@ -580,13 +602,13 @@ int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t l
* @return RTPMessage* Created message.
* @retval NULL Error occurred.
*/
RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t length )
RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t length )
{
if ( !session )
return NULL;
uint8_t* _from_pos;
RTPMessage* _retu = calloc(1, sizeof (RTPMessage));
uint8_t *_from_pos;
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
assert(_retu);
/* Sets header values and copies the extension header in _retu */
@ -660,17 +682,17 @@ RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_release_session_recv ( RTPSession* session )
int rtp_release_session_recv ( RTPSession *session )
{
if ( !session ){
if ( !session ) {
return -1;
}
RTPMessage* _tmp,* _it;
RTPMessage *_tmp, * _it;
pthread_mutex_lock(&session->mutex);
for ( _it = session->oldest_msg; _it; _it = _tmp ){
for ( _it = session->oldest_msg; _it; _it = _tmp ) {
_tmp = _it->next;
rtp_free_msg( session, _it);
}
@ -690,12 +712,12 @@ int rtp_release_session_recv ( RTPSession* session )
* @return RTPMessage* The message. You _must_ call rtp_msg_free() to free it.
* @retval NULL No messages in the list, or no list.
*/
RTPMessage* rtp_recv_msg ( RTPSession* session )
RTPMessage *rtp_recv_msg ( RTPSession *session )
{
if ( !session )
return NULL;
RTPMessage* _retu = session->oldest_msg;
RTPMessage *_retu = session->oldest_msg;
pthread_mutex_lock(&session->mutex);
@ -722,9 +744,9 @@ RTPMessage* rtp_recv_msg ( RTPSession* session )
* @retval -1 On error.
* @retval 0 On success.
*/
int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* data, uint16_t length )
int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length )
{
RTPMessage* msg = rtp_new_message (session, data, length);
RTPMessage *msg = rtp_new_message (session, data, length);
if ( !msg ) return -1;
@ -739,7 +761,7 @@ int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* dat
/* Need to skip 2 bytes that are for sequnum */
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;
@ -775,17 +797,19 @@ int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* dat
* @param msg The message.
* @return void
*/
void rtp_free_msg ( RTPSession* session, RTPMessage* msg )
void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
{
if ( !session ){
if ( !session ) {
free ( msg->header->csrc );
if ( msg->ext_header ){
if ( msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
}
} else {
if ( session->csrc != msg->header->csrc )
free ( msg->header->csrc );
if ( msg->ext_header && session->ext_header != msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
@ -811,20 +835,19 @@ void rtp_free_msg ( RTPSession* session, RTPMessage* msg )
* @return RTPSession* Created control session.
* @retval NULL Error occurred.
*/
RTPSession* rtp_init_session ( int payload_type,
Messenger* messenger,
RTPSession *rtp_init_session ( int payload_type,
Messenger *messenger,
int friend_num,
const uint8_t* encrypt_key,
const uint8_t* decrypt_key,
const uint8_t* encrypt_nonce,
const uint8_t* decrypt_nonce )
const uint8_t *encrypt_key,
const uint8_t *decrypt_key,
const uint8_t *encrypt_nonce,
const uint8_t *decrypt_nonce )
{
RTPSession* _retu = calloc(1, sizeof(RTPSession));
RTPSession *_retu = calloc(1, sizeof(RTPSession));
assert(_retu);
/*networking_registerhandler(messenger->net, payload_type, rtp_handle_packet, _retu);*/
if ( -1 == custom_user_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) )
{
if ( -1 == custom_user_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ) {
fprintf(stderr, "Error setting custom register handler for rtp session\n");
free(_retu);
return NULL;
@ -851,9 +874,12 @@ RTPSession* rtp_init_session ( int payload_type,
_retu->decrypt_key = decrypt_key;
/* Need to allocate new memory */
_retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->encrypt_nonce);
_retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->decrypt_nonce);
_retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->nonce_cycle);
_retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->encrypt_nonce);
_retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->decrypt_nonce);
_retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->nonce_cycle);
memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES);
memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES);
@ -886,7 +912,7 @@ RTPSession* rtp_init_session ( int payload_type,
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_terminate_session ( RTPSession* session, Messenger* messenger )
int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
{
if ( !session )
return -1;

View File

@ -47,7 +47,7 @@ typedef struct _RTPHeader {
uint16_t sequnum; /* Sequence Number */
uint32_t timestamp; /* Timestamp */
uint32_t ssrc; /* SSRC */
uint32_t* csrc; /* CSRC's table */
uint32_t *csrc; /* CSRC's table */
uint32_t length; /* Length of the header in payload string. */
} RTPHeader;
@ -60,7 +60,7 @@ typedef struct _RTPHeader {
typedef struct _RTPExtHeader {
uint16_t type; /* Extension profile */
uint16_t length; /* Number of extensions */
uint32_t* table; /* Extension's table */
uint32_t *table; /* Extension's table */
} RTPExtHeader;
@ -70,14 +70,14 @@ typedef struct _RTPExtHeader {
*
*/
typedef struct _RTPMessage {
RTPHeader* header;
RTPExtHeader* ext_header;
RTPHeader *header;
RTPExtHeader *ext_header;
uint8_t data[MAX_RTP_SIZE];
uint32_t length;
IP_Port from;
struct _RTPMessage* next;
struct _RTPMessage *next;
} RTPMessage;
@ -100,13 +100,13 @@ typedef struct _RTPSession {
uint16_t rsequnum; /* Check when recving msg */
uint32_t timestamp;
uint32_t ssrc;
uint32_t* csrc;
uint32_t *csrc;
/* If some additional data must be sent via message
* apply it here. Only by allocating this member you will be
* automatically placing it within a message.
*/
RTPExtHeader* ext_header;
RTPExtHeader *ext_header;
/* External header identifiers */
int resolution;
@ -117,15 +117,15 @@ typedef struct _RTPSession {
* call structure don't allocate or free
*/
const uint8_t* encrypt_key;
const uint8_t* decrypt_key;
uint8_t* encrypt_nonce;
uint8_t* decrypt_nonce;
const uint8_t *encrypt_key;
const uint8_t *decrypt_key;
uint8_t *encrypt_nonce;
uint8_t *decrypt_nonce;
uint8_t* nonce_cycle;
uint8_t *nonce_cycle;
RTPMessage* oldest_msg;
RTPMessage* last_msg; /* tail */
RTPMessage *oldest_msg;
RTPMessage *last_msg; /* tail */
/* Msg prefix for core to know when recving */
uint8_t prefix;
@ -144,7 +144,7 @@ typedef struct _RTPSession {
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_release_session_recv ( RTPSession* session );
int rtp_release_session_recv ( RTPSession *session );
/**
@ -154,7 +154,7 @@ int rtp_release_session_recv ( RTPSession* session );
* @return RTPMessage* The message. You need to call rtp_msg_free() to free it.
* @retval NULL No messages in the list, or no list.
*/
RTPMessage* rtp_recv_msg ( RTPSession* session );
RTPMessage *rtp_recv_msg ( RTPSession *session );
/**
@ -167,7 +167,7 @@ RTPMessage* rtp_recv_msg ( RTPSession* session );
* @retval -1 On error.
* @retval 0 On success.
*/
int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* data, uint16_t length );
int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length );
/**
@ -177,7 +177,7 @@ int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* dat
* @param msg The message.
* @return void
*/
void rtp_free_msg ( RTPSession* session, RTPMessage* msg );
void rtp_free_msg ( RTPSession *session, RTPMessage *msg );
/**
@ -194,13 +194,13 @@ void rtp_free_msg ( RTPSession* session, RTPMessage* msg );
* @return RTPSession* Created control session.
* @retval NULL Error occurred.
*/
RTPSession* rtp_init_session ( int payload_type,
Messenger* messenger,
RTPSession *rtp_init_session ( int payload_type,
Messenger *messenger,
int friend_num,
const uint8_t* encrypt_key,
const uint8_t* decrypt_key,
const uint8_t* encrypt_nonce,
const uint8_t* decrypt_nonce );
const uint8_t *encrypt_key,
const uint8_t *decrypt_key,
const uint8_t *encrypt_nonce,
const uint8_t *decrypt_nonce );
/**
@ -212,7 +212,7 @@ RTPSession* rtp_init_session ( int payload_type,
* @retval -1 Error occurred.
* @retval 0 Success.
*/
int rtp_terminate_session ( RTPSession* session, Messenger* messenger );
int rtp_terminate_session ( RTPSession *session, Messenger *messenger );

View File

@ -47,18 +47,17 @@ typedef enum {
} ThreadState;
typedef struct _ToxAv
{
Messenger* messenger;
typedef struct _ToxAv {
Messenger *messenger;
MSISession* msi_session; /** Main msi session */
MSISession *msi_session; /** Main msi session */
RTPSession* rtp_sessions[2]; /* Audio is first and video is second */
RTPSession *rtp_sessions[2]; /* Audio is first and video is second */
struct jitter_buffer* j_buf;
CodecState* cs;
struct jitter_buffer *j_buf;
CodecState *cs;
void* agent_handler;
void *agent_handler;
} ToxAv;
/**
@ -73,15 +72,16 @@ typedef struct _ToxAv
* @return ToxAv*
* @retval NULL On error.
*/
ToxAv* toxav_new( Tox* messenger, void* useragent, const char* ua_name , uint16_t video_width, uint16_t video_height)
ToxAv *toxav_new( Tox *messenger, void *useragent, const char *ua_name , uint16_t video_width, uint16_t video_height)
{
ToxAv* av = calloc ( sizeof(ToxAv), 1);
ToxAv *av = calloc ( sizeof(ToxAv), 1);
if (av == NULL)
return NULL;
av->messenger = (Messenger *)messenger;
av->msi_session = msi_init_session(av->messenger, (const unsigned char*) ua_name );
av->msi_session = msi_init_session(av->messenger, (const unsigned char *) ua_name );
av->msi_session->agent_handler = av;
av->rtp_sessions[0] = av->rtp_sessions [1] = NULL;
@ -89,7 +89,8 @@ ToxAv* toxav_new( Tox* messenger, void* useragent, const char* ua_name , uint16_
/* NOTE: This should be user defined or? */
av->j_buf = create_queue(20);
av->cs = codec_init_session(AUDIO_BITRATE, AUDIO_FRAME_DURATION, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, video_width, video_height, VIDEO_BITRATE);
av->cs = codec_init_session(AUDIO_BITRATE, AUDIO_FRAME_DURATION, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, video_width,
video_height, VIDEO_BITRATE);
av->agent_handler = useragent;
@ -102,7 +103,7 @@ ToxAv* toxav_new( Tox* messenger, void* useragent, const char* ua_name , uint16_
* @param av Handler.
* @return void
*/
void toxav_kill ( ToxAv* av )
void toxav_kill ( ToxAv *av )
{
msi_terminate_session(av->msi_session);
@ -142,7 +143,7 @@ void toxav_register_callstate_callback ( ToxAVCallback callback, ToxAvCallbackID
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_call (ToxAv* av, int user, ToxAvCallType call_type, int ringing_seconds )
int toxav_call (ToxAv *av, int user, ToxAvCallType call_type, int ringing_seconds )
{
if ( av->msi_session->call ) {
return ErrorAlreadyInCall;
@ -159,7 +160,7 @@ int toxav_call (ToxAv* av, int user, ToxAvCallType call_type, int ringing_second
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_hangup ( ToxAv* av )
int toxav_hangup ( ToxAv *av )
{
if ( !av->msi_session->call ) {
return ErrorNoCall;
@ -181,7 +182,7 @@ int toxav_hangup ( ToxAv* av )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_answer ( ToxAv* av, ToxAvCallType call_type )
int toxav_answer ( ToxAv *av, ToxAvCallType call_type )
{
if ( !av->msi_session->call ) {
return ErrorNoCall;
@ -203,7 +204,7 @@ int toxav_answer ( ToxAv* av, ToxAvCallType call_type )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_reject ( ToxAv* av, const char* reason )
int toxav_reject ( ToxAv *av, const char *reason )
{
if ( !av->msi_session->call ) {
return ErrorNoCall;
@ -213,7 +214,7 @@ int toxav_reject ( ToxAv* av, const char* reason )
return ErrorInvalidState;
}
return msi_reject(av->msi_session, (const uint8_t*) reason);
return msi_reject(av->msi_session, (const uint8_t *) reason);
}
/**
@ -225,13 +226,13 @@ int toxav_reject ( ToxAv* av, const char* reason )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_cancel ( ToxAv* av, const char* reason )
int toxav_cancel ( ToxAv *av, const char *reason )
{
if ( !av->msi_session->call ) {
return ErrorNoCall;
}
return msi_cancel(av->msi_session, 0, (const uint8_t*)reason);
return msi_cancel(av->msi_session, 0, (const uint8_t *)reason);
}
/**
@ -242,7 +243,7 @@ int toxav_cancel ( ToxAv* av, const char* reason )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_stop_call ( ToxAv* av )
int toxav_stop_call ( ToxAv *av )
{
if ( !av->msi_session->call ) {
return ErrorNoCall;
@ -259,9 +260,10 @@ int toxav_stop_call ( ToxAv* av )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_prepare_transmission ( ToxAv* av )
int toxav_prepare_transmission ( ToxAv *av )
{
assert(av->msi_session);
if ( !av->msi_session || !av->msi_session->call ) {
return ErrorNoCall;
}
@ -309,7 +311,7 @@ int toxav_prepare_transmission ( ToxAv* av )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_kill_transmission ( ToxAv* av )
int toxav_kill_transmission ( ToxAv *av )
{
/* Both sessions should be active at any time */
if ( !av->rtp_sessions[0] || !av->rtp_sessions[0] )
@ -341,7 +343,7 @@ int toxav_kill_transmission ( ToxAv* av )
* @retval 0 Success.
* @retval -1 Failure.
*/
inline__ int toxav_send_rtp_payload ( ToxAv* av, ToxAvCallType type, const uint8_t* payload, uint16_t length )
inline__ int toxav_send_rtp_payload ( ToxAv *av, ToxAvCallType type, const uint8_t *payload, uint16_t length )
{
if ( av->rtp_sessions[type - TypeAudio] )
return rtp_send_msg ( av->rtp_sessions[type - TypeAudio], av->msi_session->messenger_handle, payload, length );
@ -358,13 +360,13 @@ inline__ int toxav_send_rtp_payload ( ToxAv* av, ToxAvCallType type, const uint8
* @retval ToxAvError On Error.
* @retval >=0 Size of received payload.
*/
inline__ int toxav_recv_rtp_payload ( ToxAv* av, ToxAvCallType type, uint8_t* dest )
inline__ int toxav_recv_rtp_payload ( ToxAv *av, ToxAvCallType type, uint8_t *dest )
{
if ( !dest ) return ErrorInternal;
if ( !av->rtp_sessions[type - TypeAudio] ) return ErrorNoRtpSession;
RTPMessage* message;
RTPMessage *message;
if ( type == TypeAudio ) {
@ -375,14 +377,13 @@ inline__ int toxav_recv_rtp_payload ( ToxAv* av, ToxAvCallType type, uint8_t* de
/* push the packet into the queue */
queue(av->j_buf, message);
}
} while(message);
} while (message);
int success = 0;
message = dequeue(av->j_buf, &success);
if ( success == 2) return ErrorAudioPacketLost;
}
else {
} else {
message = rtp_recv_msg(av->rtp_sessions[video_index]);
}
@ -408,20 +409,25 @@ inline__ int toxav_recv_rtp_payload ( ToxAv* av, ToxAvCallType type, uint8_t* de
* @retval 0 Success.
* @retval ToxAvError On Error.
*/
inline__ int toxav_recv_video ( ToxAv* av, vpx_image_t **output)
inline__ int toxav_recv_video ( ToxAv *av, vpx_image_t **output)
{
if ( !output ) return ErrorInternal;
uint8_t packet [RTP_PAYLOAD_SIZE];
int recved_size = 0;
do {
recved_size = toxav_recv_rtp_payload(av, TypeVideo, packet);
if (recved_size > 0) {
printf("decode: %s\n", vpx_codec_err_to_string(vpx_codec_decode(&av->cs->v_decoder, packet, recved_size, NULL, 0)));
}
}while (recved_size > 0);
} while (recved_size > 0);
vpx_codec_iter_t iter = NULL;
vpx_image_t *img;
img = vpx_codec_get_frame(&av->cs->v_decoder, &iter);
if (img == NULL)
return ErrorInternal;
@ -438,23 +444,26 @@ inline__ int toxav_recv_video ( ToxAv* av, vpx_image_t **output)
* @retval 0 Success.
* @retval ToxAvError On error.
*/
inline__ int toxav_send_video ( ToxAv* av, vpx_image_t *input)
inline__ int toxav_send_video ( ToxAv *av, vpx_image_t *input)
{
if (vpx_codec_encode(&av->cs->v_encoder, input, av->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US) != VPX_CODEC_OK) {
printf("could not encode video frame\n");
return ErrorInternal;
}
++av->cs->frame_counter;
vpx_codec_iter_t iter = NULL;
const vpx_codec_cx_pkt_t *pkt;
int sent = 0;
while( (pkt = vpx_codec_get_cx_data(&av->cs->v_encoder, &iter)) ) {
while ( (pkt = vpx_codec_get_cx_data(&av->cs->v_encoder, &iter)) ) {
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
if (toxav_send_rtp_payload(av, TypeVideo, pkt->data.frame.buf, pkt->data.frame.sz) != -1)
++sent;
}
}
if (sent > 0)
return 0;
@ -472,9 +481,10 @@ inline__ int toxav_send_video ( ToxAv* av, vpx_image_t *input)
* @retval >=0 Size of received packet.
* @retval ToxAvError On error.
*/
inline__ int toxav_recv_audio ( ToxAv* av, int frame_size, int16_t* dest )
inline__ int toxav_recv_audio ( ToxAv *av, int frame_size, int16_t *dest )
{
if ( !dest ) return ErrorInternal;
uint8_t packet [RTP_PAYLOAD_SIZE];
int recved_size = toxav_recv_rtp_payload(av, TypeAudio, packet);
@ -482,7 +492,7 @@ inline__ int toxav_recv_audio ( ToxAv* av, int frame_size, int16_t* dest )
if ( recved_size == ErrorAudioPacketLost ) {
printf("Lost packet\n");
return opus_decode(av->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
} else if ( recved_size ){
} else if ( recved_size ) {
return opus_decode(av->cs->audio_decoder, packet, recved_size, dest, frame_size, 0);
} else {
return 0; /* Nothing received */
@ -499,10 +509,11 @@ inline__ int toxav_recv_audio ( ToxAv* av, int frame_size, int16_t* dest )
* @retval 0 Success.
* @retval ToxAvError On error.
*/
inline__ int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size)
inline__ int toxav_send_audio ( ToxAv *av, const int16_t *frame, int frame_size)
{
uint8_t temp_data[RTP_PAYLOAD_SIZE];
int32_t ret = opus_encode(av->cs->audio_encoder, frame, frame_size, temp_data, sizeof(temp_data));
if (ret <= 0)
return ErrorInternal;
@ -518,9 +529,10 @@ inline__ int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size)
* @retval ToxAvCallType On success.
* @retval ToxAvError On error.
*/
int toxav_get_peer_transmission_type ( ToxAv* av, int peer )
int toxav_get_peer_transmission_type ( ToxAv *av, int peer )
{
assert(av->msi_session);
if ( peer < 0 || !av->msi_session->call || av->msi_session->call->peer_count <= peer )
return ErrorInternal;
@ -533,7 +545,7 @@ int toxav_get_peer_transmission_type ( ToxAv* av, int peer )
* @param av Handler.
* @return void*
*/
void* toxav_get_agent_handler ( ToxAv* av )
void *toxav_get_agent_handler ( ToxAv *av )
{
return av->agent_handler;
}

View File

@ -29,7 +29,7 @@
/* vpx_image_t */
#include <vpx/vpx_image.h>
typedef void* ( *ToxAVCallback ) ( void* arg );
typedef void *( *ToxAVCallback ) ( void *arg );
typedef struct _ToxAv ToxAv;
#ifndef __TOX_DEFINED__
@ -126,7 +126,7 @@ typedef enum {
* @return ToxAv*
* @retval NULL On error.
*/
ToxAv* toxav_new(Tox* messenger, void* useragent, const char* ua_name, uint16_t video_width, uint16_t video_height);
ToxAv *toxav_new(Tox *messenger, void *useragent, const char *ua_name, uint16_t video_width, uint16_t video_height);
/**
* @brief Remove A/V session.
@ -134,7 +134,7 @@ ToxAv* toxav_new(Tox* messenger, void* useragent, const char* ua_name, uint16_t
* @param av Handler.
* @return void
*/
void toxav_kill(ToxAv* av);
void toxav_kill(ToxAv *av);
/**
* @brief Register callback for call state.
@ -156,7 +156,7 @@ void toxav_register_callstate_callback (ToxAVCallback callback, ToxAvCallbackID
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_call(ToxAv* av, int user, ToxAvCallType call_type, int ringing_seconds);
int toxav_call(ToxAv *av, int user, ToxAvCallType call_type, int ringing_seconds);
/**
* @brief Hangup active call.
@ -166,7 +166,7 @@ int toxav_call(ToxAv* av, int user, ToxAvCallType call_type, int ringing_seconds
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_hangup(ToxAv* av);
int toxav_hangup(ToxAv *av);
/**
* @brief Answer incomming call.
@ -177,7 +177,7 @@ int toxav_hangup(ToxAv* av);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_answer(ToxAv* av, ToxAvCallType call_type );
int toxav_answer(ToxAv *av, ToxAvCallType call_type );
/**
* @brief Reject incomming call.
@ -188,7 +188,7 @@ int toxav_answer(ToxAv* av, ToxAvCallType call_type );
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_reject(ToxAv* av, const char* reason);
int toxav_reject(ToxAv *av, const char *reason);
/**
* @brief Cancel outgoing request.
@ -199,7 +199,7 @@ int toxav_reject(ToxAv* av, const char* reason);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_cancel(ToxAv* av, const char* reason);
int toxav_cancel(ToxAv *av, const char *reason);
/**
* @brief Terminate transmission. Note that transmission will be terminated without informing remote peer.
@ -209,7 +209,7 @@ int toxav_cancel(ToxAv* av, const char* reason);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_stop_call(ToxAv* av);
int toxav_stop_call(ToxAv *av);
/**
* @brief Must be call before any RTP transmission occurs.
@ -219,7 +219,7 @@ int toxav_stop_call(ToxAv* av);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_prepare_transmission(ToxAv* av);
int toxav_prepare_transmission(ToxAv *av);
/**
* @brief Call this at the end of the transmission.
@ -229,7 +229,7 @@ int toxav_prepare_transmission(ToxAv* av);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_kill_transmission(ToxAv* av);
int toxav_kill_transmission(ToxAv *av);
/**
* @brief Receive decoded video packet.
@ -240,7 +240,7 @@ int toxav_kill_transmission(ToxAv* av);
* @retval 0 Success.
* @retval ToxAvError On Error.
*/
int toxav_recv_video ( ToxAv* av, vpx_image_t **output);
int toxav_recv_video ( ToxAv *av, vpx_image_t **output);
/**
* @brief Receive decoded audio frame.
@ -253,7 +253,7 @@ int toxav_recv_video ( ToxAv* av, vpx_image_t **output);
* @retval >=0 Size of received packet.
* @retval ToxAvError On error.
*/
int toxav_recv_audio( ToxAv* av, int frame_size, int16_t* dest );
int toxav_recv_audio( ToxAv *av, int frame_size, int16_t *dest );
/**
* @brief Encode and send video packet.
@ -264,7 +264,7 @@ int toxav_recv_audio( ToxAv* av, int frame_size, int16_t* dest );
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_send_video ( ToxAv* av, vpx_image_t *input);
int toxav_send_video ( ToxAv *av, vpx_image_t *input);
/**
* @brief Encode and send audio frame.
@ -276,7 +276,7 @@ int toxav_send_video ( ToxAv* av, vpx_image_t *input);
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size);
int toxav_send_audio ( ToxAv *av, const int16_t *frame, int frame_size);
/**
* @brief Get peer transmission type. It can either be audio or video.
@ -287,7 +287,7 @@ int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size);
* @retval ToxAvCallType On success.
* @retval ToxAvError On error.
*/
int toxav_get_peer_transmission_type ( ToxAv* av, int peer );
int toxav_get_peer_transmission_type ( ToxAv *av, int peer );
/**
* @brief Get reference to an object that is handling av session.
@ -295,6 +295,6 @@ int toxav_get_peer_transmission_type ( ToxAv* av, int peer );
* @param av Handler.
* @return void*
*/
void* toxav_get_agent_handler ( ToxAv* av );
void *toxav_get_agent_handler ( ToxAv *av );
#endif /* __TOXAV */