From bdbf4a367318d4e6b37975627fe86c7e79449ee4 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 17 Oct 2013 08:09:06 -0400 Subject: Fixed building on windows. --- toxmsi/phone.c | 375 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 199 insertions(+), 176 deletions(-) (limited to 'toxmsi/phone.c') diff --git a/toxmsi/phone.c b/toxmsi/phone.c index 010f26aa..f14d0323 100644 --- a/toxmsi/phone.c +++ b/toxmsi/phone.c @@ -14,13 +14,13 @@ #include #include #include -#include +/* #include Can this be removed? */ #include #include "AV_codec.h" -void INFO (const char* _format, ...) +void INFO (const char *_format, ...) { printf("\r[!] "); va_list _arg; @@ -31,13 +31,13 @@ void INFO (const char* _format, ...) fflush(stdout); } -int rtp_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32_t length ) +int rtp_handlepacket ( void *_object, tox_IP_Port ip_port, uint8_t *data, uint32_t length ) { - phone_t* _phone = _object; - rtp_msg_t* _msg; + phone_t *_phone = _object; + rtp_msg_t *_msg; uint8_t _payload_id; - if ( _phone->_msi->_call && _phone->_msi->_call->_state == call_active ){ + if ( _phone->_msi->_call && _phone->_msi->_call->_state == call_active ) { _msg = rtp_msg_parse ( NULL, data + 1, length - 1 ); /* ignore marker byte */ @@ -55,10 +55,10 @@ int rtp_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32 return SUCCESS; } -int msi_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32_t length ) +int msi_handlepacket ( void *_object, tox_IP_Port ip_port, uint8_t *data, uint32_t length ) { - msi_session_t* _session = _object; - msi_msg_t* _msg; + msi_session_t *_session = _object; + msi_msg_t *_msg; _msg = msi_parse_msg ( data + 1 ); /* ignore marker byte */ @@ -75,9 +75,9 @@ int msi_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32 return SUCCESS; } -void* phone_receivepacket ( void* _phone_p ) +void *phone_receivepacket ( void *_phone_p ) { - phone_t* _phone = _phone_p; + phone_t *_phone = _phone_p; networking_registerhandler(_phone->_networking, MSI_PACKET, msi_handlepacket, _phone->_msi); @@ -94,31 +94,31 @@ void* phone_receivepacket ( void* _phone_p ) /* Media transport callback */ typedef struct hmtc_args_s { - rtp_session_t** _rtp_audio; - rtp_session_t** _rtp_video; - call_type* _local_type_call; - call_state* _this_call; + rtp_session_t **_rtp_audio; + rtp_session_t **_rtp_video; + call_type *_local_type_call; + call_state *_this_call; void *_core_handler; } hmtc_args_t; -void* phone_handle_media_transport_poll ( void* _hmtc_args_p ) +void *phone_handle_media_transport_poll ( void *_hmtc_args_p ) { - rtp_msg_t* _audio_msg, * _video_msg; + rtp_msg_t *_audio_msg, * _video_msg; - hmtc_args_t* _hmtc_args = _hmtc_args_p; + hmtc_args_t *_hmtc_args = _hmtc_args_p; - rtp_session_t* _rtp_audio = *_hmtc_args->_rtp_audio; - rtp_session_t* _rtp_video = *_hmtc_args->_rtp_video; + rtp_session_t *_rtp_audio = *_hmtc_args->_rtp_audio; + rtp_session_t *_rtp_video = *_hmtc_args->_rtp_video; - call_type* _type = _hmtc_args->_local_type_call; - void* _core_handler = _hmtc_args->_core_handler; + call_type *_type = _hmtc_args->_local_type_call; + void *_core_handler = _hmtc_args->_core_handler; - call_state* _this_call = _hmtc_args->_this_call; + call_state *_this_call = _hmtc_args->_this_call; while ( *_this_call == call_active ) { - // THREADLOCK() + // THREADLOCK() _audio_msg = rtp_recv_msg ( _rtp_audio ); _video_msg = rtp_recv_msg ( _rtp_video ); @@ -139,14 +139,15 @@ void* phone_handle_media_transport_poll ( void* _hmtc_args_p ) rtp_free_msg ( _rtp_video, _video_msg ); _video_msg = NULL; } + /* -------------------- */ - _audio_msg = rtp_msg_new ( _rtp_audio, (const uint8_t*)"audio\0", 6 ) ; + _audio_msg = rtp_msg_new ( _rtp_audio, (const uint8_t *)"audio\0", 6 ) ; rtp_send_msg ( _rtp_audio, _audio_msg, _core_handler ); _audio_msg = NULL; - if ( *_type == type_video ){ /* if local call send video */ - _video_msg = rtp_msg_new ( _rtp_video, (const uint8_t*)"video\0", 6 ) ; + if ( *_type == type_video ) { /* if local call send video */ + _video_msg = rtp_msg_new ( _rtp_video, (const uint8_t *)"video\0", 6 ) ; rtp_send_msg ( _rtp_video, _video_msg, _core_handler ); _video_msg = NULL; } @@ -159,7 +160,7 @@ void* phone_handle_media_transport_poll ( void* _hmtc_args_p ) //THREADLOCK() - if ( _audio_msg ){ + if ( _audio_msg ) { rtp_free_msg(_rtp_audio, _audio_msg); } @@ -185,9 +186,9 @@ void* phone_handle_media_transport_poll ( void* _hmtc_args_p ) pthread_exit ( NULL ); } -pthread_t phone_startmedia_loop ( phone_t* _phone ) +pthread_t phone_startmedia_loop ( phone_t *_phone ) { - if ( !_phone ){ + if ( !_phone ) { return 0; } @@ -207,10 +208,10 @@ pthread_t phone_startmedia_loop ( phone_t* _phone ) rtp_set_prefix ( _phone->_rtp_video, &_prefix, 1 ); rtp_add_receiver ( _phone->_rtp_video, &_phone->_msi->_friend_id ); rtp_set_payload_type(_phone->_rtp_video, _PAYLOAD_VP8); - - - hmtc_args_t* rtp_targs = calloc(sizeof(hmtc_args_t),1); + + + hmtc_args_t *rtp_targs = calloc(sizeof(hmtc_args_t), 1); rtp_targs->_rtp_audio = &_phone->_rtp_audio; @@ -220,26 +221,29 @@ pthread_t phone_startmedia_loop ( phone_t* _phone ) rtp_targs->_core_handler = _phone->_networking; codec_state *cs; - cs=_phone->cs; + cs = _phone->cs; //_status = pthread_create ( &_rtp_tid, NULL, phone_handle_media_transport_poll, rtp_targs ); - cs->_rtp_audio=_phone->_rtp_audio; - cs->_rtp_video=_phone->_rtp_video; - cs->_networking=_phone->_networking; - cs->socket=_phone->_tox_sock; + cs->_rtp_audio = _phone->_rtp_audio; + cs->_rtp_video = _phone->_rtp_video; + cs->_networking = _phone->_networking; + cs->socket = _phone->_tox_sock; cs->quit = 0; - - printf("support: %d %d\n",cs->support_send_audio,cs->support_send_video); - - if(cs->support_send_audio&&cs->support_send_video) /* quick fix */ - pthread_create(&_phone->cs->encode_audio_thread, NULL, encode_audio_thread, _phone->cs); - if(cs->support_receive_audio) - pthread_create(&_phone->cs->decode_audio_thread, NULL, decode_audio_thread, _phone->cs); - - if(cs->support_send_video) - pthread_create(&_phone->cs->encode_video_thread, NULL, encode_video_thread, _phone->cs); - if(cs->support_receive_video) - pthread_create(&_phone->cs->decode_video_thread, NULL, decode_video_thread, _phone->cs); -// + + printf("support: %d %d\n", cs->support_send_audio, cs->support_send_video); + + if (cs->support_send_audio && cs->support_send_video) /* quick fix */ + pthread_create(&_phone->cs->encode_audio_thread, NULL, encode_audio_thread, _phone->cs); + + if (cs->support_receive_audio) + pthread_create(&_phone->cs->decode_audio_thread, NULL, decode_audio_thread, _phone->cs); + + if (cs->support_send_video) + pthread_create(&_phone->cs->encode_video_thread, NULL, encode_video_thread, _phone->cs); + + if (cs->support_receive_video) + pthread_create(&_phone->cs->decode_video_thread, NULL, decode_video_thread, _phone->cs); + +// return 1; @@ -252,20 +256,21 @@ pthread_t phone_startmedia_loop ( phone_t* _phone ) MCBTYPE callback_recv_invite ( MCBARGS ) { - const char* _call_type; + const char *_call_type; - msi_session_t* _msi = _arg; + msi_session_t *_msi = _arg; /* Get the last one */ call_type _type = _msi->_call->_type_peer[_msi->_call->_participants - 1]; - switch ( _type ){ - case type_audio: - _call_type = "audio"; - break; - case type_video: - _call_type = "video"; - break; + switch ( _type ) { + case type_audio: + _call_type = "audio"; + break; + + case type_video: + _call_type = "video"; + break; } INFO( "Incoming %s call!", _call_type ); @@ -281,8 +286,9 @@ MCBTYPE callback_recv_ringing ( MCBARGS ) } MCBTYPE callback_recv_starting ( MCBARGS ) { - msi_session_t* _session = _arg; - if ( !phone_startmedia_loop(_session->_agent_handler) ){ + msi_session_t *_session = _arg; + + if ( !phone_startmedia_loop(_session->_agent_handler) ) { INFO("Starting call failed!"); } else { INFO ("Call started! ( press h to hangup )"); @@ -290,34 +296,40 @@ MCBTYPE callback_recv_starting ( MCBARGS ) } MCBTYPE callback_recv_ending ( MCBARGS ) { - msi_session_t* _session = _arg; - phone_t * _phone = _session->_agent_handler; - _phone->cs->quit=1; - if(_phone->cs->encode_video_thread) - pthread_join(_phone->cs->encode_video_thread,NULL); - if(_phone->cs->encode_audio_thread) - pthread_join(_phone->cs->encode_audio_thread,NULL); - if(_phone->cs->decode_audio_thread) - pthread_join(_phone->cs->decode_audio_thread,NULL); - if(_phone->cs->decode_video_thread) - pthread_join(_phone->cs->decode_video_thread,NULL); + msi_session_t *_session = _arg; + phone_t *_phone = _session->_agent_handler; + _phone->cs->quit = 1; + + if (_phone->cs->encode_video_thread) + pthread_join(_phone->cs->encode_video_thread, NULL); + + if (_phone->cs->encode_audio_thread) + pthread_join(_phone->cs->encode_audio_thread, NULL); + + if (_phone->cs->decode_audio_thread) + pthread_join(_phone->cs->decode_audio_thread, NULL); + + if (_phone->cs->decode_video_thread) + pthread_join(_phone->cs->decode_video_thread, NULL); + SDL_Quit(); printf("all A/V threads successfully shut down\n"); - + INFO ( "Call ended!" ); } MCBTYPE callback_recv_error ( MCBARGS ) { - msi_session_t* _session = _arg; + msi_session_t *_session = _arg; INFO( "Error: %s", _session->_last_error_str ); } MCBTYPE callback_call_started ( MCBARGS ) { - msi_session_t* _session = _arg; - if ( !phone_startmedia_loop(_session->_agent_handler) ){ + msi_session_t *_session = _arg; + + if ( !phone_startmedia_loop(_session->_agent_handler) ) { INFO("Starting call failed!"); } else { INFO ("Call started! ( press h to hangup )"); @@ -334,21 +346,26 @@ MCBTYPE callback_call_rejected ( MCBARGS ) } MCBTYPE callback_call_ended ( MCBARGS ) { - - msi_session_t* _session = _arg; - phone_t * _phone = _session->_agent_handler; - _phone->cs->quit=1; - if(_phone->cs->encode_video_thread) - pthread_join(_phone->cs->encode_video_thread,NULL); - if(_phone->cs->encode_audio_thread) - pthread_join(_phone->cs->encode_audio_thread,NULL); - if(_phone->cs->decode_audio_thread) - pthread_join(_phone->cs->decode_audio_thread,NULL); - if(_phone->cs->decode_video_thread) - pthread_join(_phone->cs->decode_video_thread,NULL); + + msi_session_t *_session = _arg; + phone_t *_phone = _session->_agent_handler; + _phone->cs->quit = 1; + + if (_phone->cs->encode_video_thread) + pthread_join(_phone->cs->encode_video_thread, NULL); + + if (_phone->cs->encode_audio_thread) + pthread_join(_phone->cs->encode_audio_thread, NULL); + + if (_phone->cs->decode_audio_thread) + pthread_join(_phone->cs->decode_audio_thread, NULL); + + if (_phone->cs->decode_video_thread) + pthread_join(_phone->cs->decode_video_thread, NULL); + SDL_Quit(); printf("all A/V threads successfully shut down\n"); - + INFO ( "Call ended!" ); } @@ -358,10 +375,10 @@ MCBTYPE callback_requ_timeout ( MCBARGS ) } -phone_t* initPhone(uint16_t _listen_port, uint16_t _send_port) +phone_t *initPhone(uint16_t _listen_port, uint16_t _send_port) { - phone_t* _retu = calloc(sizeof(phone_t),1); - _retu->cs = av_calloc(sizeof(codec_state),1); + phone_t *_retu = calloc(sizeof(phone_t), 1); + _retu->cs = av_calloc(sizeof(codec_state), 1); /* Initialize our mutex */ pthread_mutex_init ( &_mutex, NULL ); @@ -388,13 +405,13 @@ phone_t* initPhone(uint16_t _listen_port, uint16_t _send_port) /* Initialize msi */ - _retu->_msi = msi_init_session ( _retu->_networking, (const uint8_t*)_USERAGENT ); + _retu->_msi = msi_init_session ( _retu->_networking, (const uint8_t *)_USERAGENT ); if ( !_retu->_msi ) { fprintf ( stderr, "msi_init_session() failed\n" ); return NULL; } - + /* Initiate codecs */ init_encoder(_retu->cs); init_decoder(_retu->cs); @@ -425,7 +442,7 @@ phone_t* initPhone(uint16_t _listen_port, uint16_t _send_port) return _retu; } -pthread_t phone_startmain_loop(phone_t* _phone) +pthread_t phone_startmain_loop(phone_t *_phone) { int _status; /* Start receive thread */ @@ -461,9 +478,9 @@ pthread_t phone_startmain_loop(phone_t* _phone) return _phone_loop_thread; } -void* phone_poll ( void* _p_phone ) +void *phone_poll ( void *_p_phone ) { - phone_t* _phone = _p_phone; + phone_t *_phone = _p_phone; int _status = SUCCESS; @@ -482,121 +499,127 @@ void* phone_poll ( void* _p_phone ) "r (reject incoming call)\n" "q (quit)\n" "================================================================================" - ); + ); - while ( 1 ) - { + while ( 1 ) { fgets(_line, sizeof(_line), stdin); - int i; + int i; + for (i = 0; i < 100; i++) { if (_line[i] == '\n') { _line[i] = '\0'; } } + _len = strlen(_line); - - if ( !_len ){ - printf(" >> "); fflush(stdout); + + if ( !_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 'c': - { - if ( _phone->_msi->_call ){ - INFO("Already in a call"); - break; - } + case 'c': { + if ( _phone->_msi->_call ) { + INFO("Already in a call"); + break; + } - call_type _ctype; - if ( _len < 11 ){ - INFO("Invalid input; usage: c a/v 0.0.0.0"); - break; - } - else if ( _line[2] == 'a' || _line[2] != 'v' ){ /* default and audio */ - _ctype = type_audio; - } - else { /* video */ - _ctype = type_video; - } + call_type _ctype; - strcpy(_dest, _line + 4 ); - _status = t_setipport(_dest, _phone->_send_port, &(_phone->_msi->_friend_id)); + if ( _len < 11 ) { + INFO("Invalid input; usage: c a/v 0.0.0.0"); + break; + } else if ( _line[2] == 'a' || _line[2] != 'v' ) { /* default and audio */ + _ctype = type_audio; + } else { /* video */ + _ctype = type_video; + } + + strcpy(_dest, _line + 4 ); + _status = t_setipport(_dest, _phone->_send_port, &(_phone->_msi->_friend_id)); + + if ( _status < 0 ) { + INFO("Could not resolve address!"); + } else { + /* Set timeout */ + msi_invite ( _phone->_msi, _ctype, 30 * 1000 ); + INFO("Calling!"); + } + + t_memset((uint8_t *)_dest, '\0', 17); - if ( _status < 0 ){ - INFO("Could not resolve address!"); - } else { - /* Set timeout */ - msi_invite ( _phone->_msi, _ctype, 30 * 1000 ); - INFO("Calling!"); } + break; - t_memset((uint8_t*)_dest, '\0', 17); + case 'h': { + if ( !_phone->_msi->_call ) { + break; + } + + msi_hangup(_phone->_msi); + + INFO("Hung up..."); - } break; - case 'h': - { - if ( !_phone->_msi->_call ){ - break; } + break; - msi_hangup(_phone->_msi); + case 'a': { + if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ) { + break; + } - INFO("Hung up..."); + if ( _len > 1 && _line[2] == 'v' ) + msi_answer(_phone->_msi, type_video); + else + msi_answer(_phone->_msi, type_audio); - } break; - case 'a': - { - if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ) { - break; } + break; + + case 'r': { + if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ) { + break; + } - if ( _len > 1 && _line[2] == 'v' ) - msi_answer(_phone->_msi, type_video); - else - msi_answer(_phone->_msi, type_audio); + msi_reject(_phone->_msi); + + INFO("Call Rejected..."); - } break; - case 'r': - { - if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ){ - break; } + break; - msi_reject(_phone->_msi); + case 'q': { + INFO("Quitting!"); + pthread_exit(NULL); + } - INFO("Call Rejected..."); + default: { + INFO("Invalid command!"); + } + break; - } break; - case 'q': - { - INFO("Quitting!"); - pthread_exit(NULL); } - default: - { - INFO("Invalid command!"); - } break; - } - usleep(1000); + usleep(1000); } pthread_exit(NULL); } -int quitPhone(phone_t* _phone) +int quitPhone(phone_t *_phone) { - if ( _phone->_msi->_call ){ + if ( _phone->_msi->_call ) { msi_hangup(_phone->_msi); /* Hangup the phone first */ } - + msi_terminate_session(_phone->_msi); pthread_mutex_destroy ( &_mutex ); @@ -606,17 +629,17 @@ int quitPhone(phone_t* _phone) /* ---------------------- */ -int print_help ( const char* _name ) +int print_help ( const char *_name ) { printf ( "Usage: %s -m (mode) -r/s ( for setting the ports on test version )\n", _name ); return FAILURE; } -int main ( int argc, char* argv [] ) +int main ( int argc, char *argv [] ) { - arg_t* _args = parse_args ( argc, argv ); + arg_t *_args = parse_args ( argc, argv ); - const char* _mode = find_arg_duble ( _args, "-m" ); + const char *_mode = find_arg_duble ( _args, "-m" ); uint16_t _listen_port; uint16_t _send_port; @@ -631,9 +654,9 @@ int main ( int argc, char* argv [] ) _listen_port = 31000; } else return print_help ( argv[0] ); - phone_t* _phone = initPhone(_listen_port, _send_port); + phone_t *_phone = initPhone(_listen_port, _send_port); - if ( _phone ){ + if ( _phone ) { phone_startmain_loop(_phone); quitPhone(_phone); -- cgit v1.2.3