summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
Diffstat (limited to 'toxav')
-rwxr-xr-xtoxav/phone.c66
-rw-r--r--toxav/toxmedia.c5
-rw-r--r--toxav/toxmedia.h4
-rwxr-xr-xtoxav/toxmsi.c31
-rwxr-xr-xtoxav/toxmsi.h23
5 files changed, 65 insertions, 64 deletions
diff --git a/toxav/phone.c b/toxav/phone.c
index d1b62b68..6e746bc5 100755
--- a/toxav/phone.c
+++ b/toxav/phone.c
@@ -354,7 +354,7 @@ void *encode_video_thread(void *arg)
354 cs->webcam_decoder_ctx->pix_fmt, cs->webcam_decoder_ctx->width, cs->webcam_decoder_ctx->height, PIX_FMT_YUV420P, 354 cs->webcam_decoder_ctx->pix_fmt, cs->webcam_decoder_ctx->width, cs->webcam_decoder_ctx->height, PIX_FMT_YUV420P,
355 SWS_BILINEAR, NULL, NULL, NULL); 355 SWS_BILINEAR, NULL, NULL, NULL);
356 356
357 while (!cs->quit && cs->send_video) { 357 while (cs->send_video) {
358 358
359 if (av_read_frame(cs->video_format_ctx, packet) < 0) { 359 if (av_read_frame(cs->video_format_ctx, packet) < 0) {
360 printf("error reading frame\n"); 360 printf("error reading frame\n");
@@ -412,14 +412,14 @@ void *encode_video_thread(void *arg)
412 } 412 }
413 413
414 /* clean up codecs */ 414 /* clean up codecs */
415 pthread_mutex_lock(&cs->avcodec_mutex_lock); 415 pthread_mutex_lock(&cs->ctrl_mutex);
416 av_free(buffer); 416 av_free(buffer);
417 av_free(webcam_frame); 417 av_free(webcam_frame);
418 av_free(s_video_frame); 418 av_free(s_video_frame);
419 sws_freeContext(_phone->sws_ctx); 419 sws_freeContext(_phone->sws_ctx);
420 avcodec_close(cs->webcam_decoder_ctx); 420 avcodec_close(cs->webcam_decoder_ctx);
421 avcodec_close(cs->video_encoder_ctx); 421 avcodec_close(cs->video_encoder_ctx);
422 pthread_mutex_unlock(&cs->avcodec_mutex_lock); 422 pthread_mutex_unlock(&cs->ctrl_mutex);
423 pthread_exit ( NULL ); 423 pthread_exit ( NULL );
424} 424}
425 425
@@ -436,7 +436,7 @@ void *encode_audio_thread(void *arg)
436 ALint sample = 0; 436 ALint sample = 0;
437 alcCaptureStart((ALCdevice*)_phone->audio_capture_device); 437 alcCaptureStart((ALCdevice*)_phone->audio_capture_device);
438 438
439 while (!cs->quit && cs->send_audio) { 439 while (cs->send_audio) {
440 alcGetIntegerv((ALCdevice*)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample); 440 alcGetIntegerv((ALCdevice*)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
441 441
442 if (sample >= frame_size) { 442 if (sample >= frame_size) {
@@ -454,11 +454,10 @@ void *encode_audio_thread(void *arg)
454 } 454 }
455 455
456 /* clean up codecs */ 456 /* clean up codecs */
457 pthread_mutex_lock(&cs->avcodec_mutex_lock); 457 pthread_mutex_lock(&cs->ctrl_mutex);
458 alcCaptureStop((ALCdevice*)_phone->audio_capture_device); 458 alcCaptureStop((ALCdevice*)_phone->audio_capture_device);
459 alcCaptureCloseDevice((ALCdevice*)_phone->audio_capture_device); 459 alcCaptureCloseDevice((ALCdevice*)_phone->audio_capture_device);
460 460 pthread_mutex_unlock(&cs->ctrl_mutex);
461 pthread_mutex_unlock(&cs->avcodec_mutex_lock);
462 pthread_exit ( NULL ); 461 pthread_exit ( NULL );
463} 462}
464 463
@@ -478,7 +477,7 @@ void *decode_video_thread(void *arg)
478 int width = 0; 477 int width = 0;
479 int height = 0; 478 int height = 0;
480 479
481 while (!cs->quit && cs->receive_video) { 480 while (cs->receive_video) {
482 r_msg = rtp_recv_msg ( _phone->_rtp_video ); 481 r_msg = rtp_recv_msg ( _phone->_rtp_video );
483 482
484 if (r_msg) { 483 if (r_msg) {
@@ -508,10 +507,10 @@ void *decode_video_thread(void *arg)
508 507
509 printf("vend\n"); 508 printf("vend\n");
510 /* clean up codecs */ 509 /* clean up codecs */
511 pthread_mutex_lock(&cs->avcodec_mutex_lock); 510 pthread_mutex_lock(&cs->ctrl_mutex);
512 av_free(r_video_frame); 511 av_free(r_video_frame);
513 avcodec_close(cs->video_decoder_ctx); 512 avcodec_close(cs->video_decoder_ctx);
514 pthread_mutex_unlock(&cs->avcodec_mutex_lock); 513 pthread_mutex_unlock(&cs->ctrl_mutex);
515 pthread_exit ( NULL ); 514 pthread_exit ( NULL );
516} 515}
517 516
@@ -544,6 +543,7 @@ void *decode_audio_thread(void *arg)
544 543
545 uint16_t zeros[frame_size]; 544 uint16_t zeros[frame_size];
546 memset(zeros, 0, frame_size); 545 memset(zeros, 0, frame_size);
546 opus_int16 PCM[frame_size];
547 547
548 int i; 548 int i;
549 for (i = 0; i < openal_buffers; ++i) { 549 for (i = 0; i < openal_buffers; ++i) {
@@ -555,7 +555,7 @@ void *decode_audio_thread(void *arg)
555 555
556 if (alGetError() != AL_NO_ERROR) { 556 if (alGetError() != AL_NO_ERROR) {
557 fprintf(stderr, "Error starting audio\n"); 557 fprintf(stderr, "Error starting audio\n");
558 cs->quit = 1; 558 goto ending;
559 } 559 }
560 560
561 struct jitter_buffer *j_buf = NULL; 561 struct jitter_buffer *j_buf = NULL;
@@ -566,9 +566,8 @@ void *decode_audio_thread(void *arg)
566 566
567 int dec_frame_len = 0; 567 int dec_frame_len = 0;
568 568
569 opus_int16 PCM[frame_size];
570 569
571 while (!cs->quit && cs->receive_audio) { 570 while (cs->receive_audio) {
572 571
573 r_msg = rtp_recv_msg ( _phone->_rtp_audio ); 572 r_msg = rtp_recv_msg ( _phone->_rtp_audio );
574 573
@@ -633,16 +632,18 @@ void *decode_audio_thread(void *arg)
633 usleep(1000); 632 usleep(1000);
634 } 633 }
635 634
635
636ending:
636 /* clean up codecs */ 637 /* clean up codecs */
637 pthread_mutex_lock(&cs->avcodec_mutex_lock); 638 pthread_mutex_lock(&cs->ctrl_mutex);
638 639
639 /* clean up openal */
640 alDeleteSources(1, &source); 640 alDeleteSources(1, &source);
641 alDeleteBuffers(openal_buffers, buffers); 641 alDeleteBuffers(openal_buffers, buffers);
642 alcMakeContextCurrent(NULL); 642 alcMakeContextCurrent(NULL);
643 alcDestroyContext(ctx); 643 alcDestroyContext(ctx);
644 alcCloseDevice(dev); 644 alcCloseDevice(dev);
645 pthread_mutex_unlock(&cs->avcodec_mutex_lock); 645
646 pthread_mutex_unlock(&cs->ctrl_mutex);
646 pthread_exit ( NULL ); 647 pthread_exit ( NULL );
647} 648}
648 649
@@ -675,9 +676,7 @@ int phone_startmedia_loop ( av_session_t* _phone )
675 _phone->_msi->call->nonce_peer, 676 _phone->_msi->call->nonce_peer,
676 _phone->_msi->call->nonce_local 677 _phone->_msi->call->nonce_local
677 ); 678 );
678 679
679 _phone->cs->quit = 0;
680
681 init_encoder(_phone->cs); 680 init_encoder(_phone->cs);
682 init_decoder(_phone->cs); 681 init_decoder(_phone->cs);
683 682
@@ -699,6 +698,7 @@ int phone_startmedia_loop ( av_session_t* _phone )
699 return -1; 698 return -1;
700 } 699 }
701 700
701 /* Only checks for last peer */
702 if ( _phone->_msi->call->type_peer[0] == type_video && 0 > event.rise(decode_video_thread, _phone) ) 702 if ( _phone->_msi->call->type_peer[0] == type_video && 0 > event.rise(decode_video_thread, _phone) )
703 { 703 {
704 INFO("Error while starting decode_video_thread()"); 704 INFO("Error while starting decode_video_thread()");
@@ -913,18 +913,18 @@ av_session_t* av_init_session()
913 _retu->_msi->agent_handler = _retu; 913 _retu->_msi->agent_handler = _retu;
914 914
915 /* ------------------ */ 915 /* ------------------ */
916 msi_register_callback(callback_call_started, cb_onstart); 916 msi_register_callback(callback_call_started, MSI_OnStart);
917 msi_register_callback(callback_call_canceled, cb_oncancel); 917 msi_register_callback(callback_call_canceled, MSI_OnCancel);
918 msi_register_callback(callback_call_rejected, cb_onreject); 918 msi_register_callback(callback_call_rejected, MSI_OnReject);
919 msi_register_callback(callback_call_ended, cb_onend); 919 msi_register_callback(callback_call_ended, MSI_OnEnd);
920 msi_register_callback(callback_recv_invite, cb_oninvite); 920 msi_register_callback(callback_recv_invite, MSI_OnInvite);
921 921
922 msi_register_callback(callback_recv_ringing, cb_ringing); 922 msi_register_callback(callback_recv_ringing, MSI_OnRinging);
923 msi_register_callback(callback_recv_starting, cb_starting); 923 msi_register_callback(callback_recv_starting, MSI_OnStarting);
924 msi_register_callback(callback_recv_ending, cb_ending); 924 msi_register_callback(callback_recv_ending, MSI_OnEnding);
925 925
926 msi_register_callback(callback_recv_error, cb_error); 926 msi_register_callback(callback_recv_error, MSI_OnError);
927 msi_register_callback(callback_requ_timeout, cb_timeout); 927 msi_register_callback(callback_requ_timeout, MSI_OnTimeout);
928 /* ------------------ */ 928 /* ------------------ */
929 929
930 return _retu; 930 return _retu;
@@ -1114,7 +1114,7 @@ void do_phone ( av_session_t* _phone )
1114 break; 1114 break;
1115 } 1115 }
1116 1116
1117 msi_reject(_phone->_msi); 1117 msi_reject(_phone->_msi, NULL);
1118 1118
1119 INFO("Call Rejected..."); 1119 INFO("Call Rejected...");
1120 1120
diff --git a/toxav/toxmedia.c b/toxav/toxmedia.c
index 359832d3..00b1ebfc 100644
--- a/toxav/toxmedia.c
+++ b/toxav/toxmedia.c
@@ -345,8 +345,6 @@ int init_send_audio(codec_state *cs)
345 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); 345 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
346 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); 346 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
347 347
348 opus_encoder_init(cs->audio_encoder, AUDIO_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP);
349
350 int nfo; 348 int nfo;
351 err = opus_encoder_ctl(cs->audio_encoder, OPUS_GET_LOOKAHEAD(&nfo)); 349 err = opus_encoder_ctl(cs->audio_encoder, OPUS_GET_LOOKAHEAD(&nfo));
352 /* printf("Encoder lookahead delay : %d\n", nfo); */ 350 /* printf("Encoder lookahead delay : %d\n", nfo); */
@@ -362,8 +360,7 @@ int init_encoder(codec_state *cs)
362 avdevice_register_all(); 360 avdevice_register_all();
363 av_register_all(); 361 av_register_all();
364 362
365 pthread_mutex_init(&cs->rtp_msg_mutex_lock, NULL); 363 pthread_mutex_init(&cs->ctrl_mutex, NULL);
366 pthread_mutex_init(&cs->avcodec_mutex_lock, NULL);
367 364
368 cs->support_send_video = init_send_video(cs); 365 cs->support_send_video = init_send_video(cs);
369 cs->support_send_audio = init_send_audio(cs); 366 cs->support_send_audio = init_send_audio(cs);
diff --git a/toxav/toxmedia.h b/toxav/toxmedia.h
index 927c5ef8..65d1e320 100644
--- a/toxav/toxmedia.h
+++ b/toxav/toxmedia.h
@@ -108,10 +108,8 @@ typedef struct {
108 108
109 uint8_t req_video_refresh; 109 uint8_t req_video_refresh;
110 110
111 pthread_mutex_t rtp_msg_mutex_lock; 111 pthread_mutex_t ctrl_mutex;
112 pthread_mutex_t avcodec_mutex_lock;
113 112
114 uint8_t quit;
115 113
116 uint32_t frame_rate; 114 uint32_t frame_rate;
117 115
diff --git a/toxav/toxmsi.c b/toxav/toxmsi.c
index 4df6bc45..db9ae2d2 100755
--- a/toxav/toxmsi.c
+++ b/toxav/toxmsi.c
@@ -633,7 +633,7 @@ int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
633 session->last_error_id = errid; 633 session->last_error_id = errid;
634 session->last_error_str = stringify_error ( errid ); 634 session->last_error_str = stringify_error ( errid );
635 635
636 event.rise ( callbacks[cb_error], session ); 636 event.rise ( callbacks[MSI_OnError], session );
637 637
638 return 0; 638 return 0;
639} 639}
@@ -688,8 +688,8 @@ void* handle_timeout ( void* arg )
688 688
689 } 689 }
690 690
691 ( *callbacks[cb_timeout] ) ( arg ); 691 ( *callbacks[MSI_OnTimeout] ) ( arg );
692 ( *callbacks[cb_ending ] ) ( arg ); 692 ( *callbacks[MSI_OnEnding ] ) ( arg );
693 693
694 return NULL; 694 return NULL;
695} 695}
@@ -821,7 +821,7 @@ int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
821 send_message ( session, _msg_ringing, msg->friend_id ); 821 send_message ( session, _msg_ringing, msg->friend_id );
822 free_message ( _msg_ringing ); 822 free_message ( _msg_ringing );
823 823
824 event.rise ( callbacks[cb_oninvite], session ); 824 event.rise ( callbacks[MSI_OnInvite], session );
825 825
826 return 1; 826 return 1;
827} 827}
@@ -844,7 +844,7 @@ int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
844 844
845 flush_peer_type ( session, msg, 0 ); 845 flush_peer_type ( session, msg, 0 );
846 846
847 event.rise ( callbacks[cb_onstart], session ); 847 event.rise ( callbacks[MSI_OnStart], session );
848 848
849 return 1; 849 return 1;
850} 850}
@@ -860,7 +860,7 @@ int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
860 free_message ( _msg_end ); 860 free_message ( _msg_end );
861 861
862 event.timer_release ( session->call->request_timer_id ); 862 event.timer_release ( session->call->request_timer_id );
863 event.rise ( callbacks[cb_onreject], session ); 863 event.rise ( callbacks[MSI_OnReject], session );
864 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); 864 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
865 865
866 return 1; 866 return 1;
@@ -874,7 +874,7 @@ int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
874 874
875 terminate_call ( session ); 875 terminate_call ( session );
876 876
877 event.rise ( callbacks[cb_oncancel], session ); 877 event.rise ( callbacks[MSI_OnCancel], session );
878 878
879 return 1; 879 return 1;
880} 880}
@@ -891,7 +891,7 @@ int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
891 891
892 terminate_call ( session ); 892 terminate_call ( session );
893 893
894 event.rise ( callbacks[cb_onend], session ); 894 event.rise ( callbacks[MSI_OnEnd], session );
895 895
896 return 1; 896 return 1;
897} 897}
@@ -904,7 +904,7 @@ int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
904 return 0; 904 return 0;
905 905
906 session->call->ringing_timer_id = event.timer_alloc ( handle_timeout, session, session->call->ringing_tout_ms ); 906 session->call->ringing_timer_id = event.timer_alloc ( handle_timeout, session, session->call->ringing_tout_ms );
907 event.rise ( callbacks[cb_ringing], session ); 907 event.rise ( callbacks[MSI_OnRinging], session );
908 908
909 return 1; 909 return 1;
910} 910}
@@ -942,7 +942,7 @@ int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
942 942
943 flush_peer_type ( session, msg, 0 ); 943 flush_peer_type ( session, msg, 0 );
944 944
945 event.rise ( callbacks[cb_starting], session ); 945 event.rise ( callbacks[MSI_OnStarting], session );
946 event.timer_release ( session->call->ringing_timer_id ); 946 event.timer_release ( session->call->ringing_timer_id );
947 947
948 return 1; 948 return 1;
@@ -956,7 +956,7 @@ int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
956 956
957 terminate_call ( session ); 957 terminate_call ( session );
958 958
959 event.rise ( callbacks[cb_ending], session ); 959 event.rise ( callbacks[MSI_OnEnding], session );
960 960
961 return 1; 961 return 1;
962} 962}
@@ -972,7 +972,7 @@ int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
972 972
973 terminate_call ( session ); 973 terminate_call ( session );
974 974
975 event.rise ( callbacks[cb_ending], session ); 975 event.rise ( callbacks[MSI_OnEnding], session );
976 976
977 return 1; 977 return 1;
978} 978}
@@ -1165,6 +1165,8 @@ int msi_terminate_session ( MSISession* session ) {
1165 int _status = 0; 1165 int _status = 0;
1166 1166
1167 terminate_call ( session ); 1167 terminate_call ( session );
1168 m_callback_msi_packet((struct Messenger*) session->messenger_handle, NULL, NULL);
1169
1168 1170
1169 /* TODO: Clean it up more? */ 1171 /* TODO: Clean it up more? */
1170 1172
@@ -1311,10 +1313,13 @@ int msi_cancel ( MSISession* session, int friend_id ) {
1311 * @param session Control session. 1313 * @param session Control session.
1312 * @return int 1314 * @return int
1313 */ 1315 */
1314int msi_reject ( MSISession* session ) { 1316int msi_reject ( MSISession* session, const uint8_t* reason ) {
1315 assert ( session ); 1317 assert ( session );
1316 1318
1317 MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) ); 1319 MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
1320
1321 if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char*)reason) + 1);
1322
1318 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] ); 1323 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] );
1319 free_message ( _msg_reject ); 1324 free_message ( _msg_reject );
1320 1325
diff --git a/toxav/toxmsi.h b/toxav/toxmsi.h
index 63cff9e5..04987fee 100755
--- a/toxav/toxmsi.h
+++ b/toxav/toxmsi.h
@@ -120,20 +120,20 @@ typedef struct _MSISession {
120 */ 120 */
121typedef enum { 121typedef enum {
122 /* Requests */ 122 /* Requests */
123 cb_oninvite, 123 MSI_OnInvite,
124 cb_onstart, 124 MSI_OnStart,
125 cb_oncancel, 125 MSI_OnCancel,
126 cb_onreject, 126 MSI_OnReject,
127 cb_onend, 127 MSI_OnEnd,
128 128
129 /* Responses */ 129 /* Responses */
130 cb_ringing, 130 MSI_OnRinging,
131 cb_starting, 131 MSI_OnStarting,
132 cb_ending, 132 MSI_OnEnding,
133 133
134 /* Protocol */ 134 /* Protocol */
135 cb_error, 135 MSI_OnError,
136 cb_timeout 136 MSI_OnTimeout
137 137
138} MSICallbackID; 138} MSICallbackID;
139 139
@@ -215,9 +215,10 @@ int msi_cancel ( MSISession* session, int friend_id );
215 * @brief Reject request. 215 * @brief Reject request.
216 * 216 *
217 * @param session Control session. 217 * @param session Control session.
218 * @param reason Set optional reason header. Pass NULL if none.
218 * @return int 219 * @return int
219 */ 220 */
220int msi_reject ( MSISession* session ); 221int msi_reject ( MSISession* session, const uint8_t* reason );
221 222
222 223
223/** 224/**