summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
Diffstat (limited to 'toxav')
-rw-r--r--toxav/toxav.c49
1 files changed, 31 insertions, 18 deletions
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 1f2718f8..959ef54c 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -46,6 +46,9 @@
46 46
47#define inline__ inline __attribute__((always_inline)) 47#define inline__ inline __attribute__((always_inline))
48 48
49/* call index invalid: true if invalid */
50#define cii(c_idx, session) (c_idx < 0 || c_idx >= session->max_calls)
51
49static const uint8_t audio_index = 0, video_index = 1; 52static const uint8_t audio_index = 0, video_index = 1;
50 53
51typedef struct _CallSpecific { 54typedef struct _CallSpecific {
@@ -181,7 +184,7 @@ int toxav_call (ToxAv *av, int32_t *call_index, int user, ToxAvCallType call_typ
181 */ 184 */
182int toxav_hangup ( ToxAv *av, int32_t call_index ) 185int toxav_hangup ( ToxAv *av, int32_t call_index )
183{ 186{
184 if ( !av->msi_session->calls[call_index] ) { 187 if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
185 return ErrorNoCall; 188 return ErrorNoCall;
186 } 189 }
187 190
@@ -203,7 +206,7 @@ int toxav_hangup ( ToxAv *av, int32_t call_index )
203 */ 206 */
204int toxav_answer ( ToxAv *av, int32_t call_index, ToxAvCallType call_type ) 207int toxav_answer ( ToxAv *av, int32_t call_index, ToxAvCallType call_type )
205{ 208{
206 if ( !av->msi_session->calls[call_index] ) { 209 if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
207 return ErrorNoCall; 210 return ErrorNoCall;
208 } 211 }
209 212
@@ -225,7 +228,7 @@ int toxav_answer ( ToxAv *av, int32_t call_index, ToxAvCallType call_type )
225 */ 228 */
226int toxav_reject ( ToxAv *av, int32_t call_index, const char *reason ) 229int toxav_reject ( ToxAv *av, int32_t call_index, const char *reason )
227{ 230{
228 if ( !av->msi_session->calls[call_index] ) { 231 if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
229 return ErrorNoCall; 232 return ErrorNoCall;
230 } 233 }
231 234
@@ -248,7 +251,7 @@ int toxav_reject ( ToxAv *av, int32_t call_index, const char *reason )
248 */ 251 */
249int toxav_cancel ( ToxAv *av, int32_t call_index, int peer_id, const char *reason ) 252int toxav_cancel ( ToxAv *av, int32_t call_index, int peer_id, const char *reason )
250{ 253{
251 if ( !av->msi_session->calls[call_index] ) { 254 if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
252 return ErrorNoCall; 255 return ErrorNoCall;
253 } 256 }
254 257
@@ -265,7 +268,7 @@ int toxav_cancel ( ToxAv *av, int32_t call_index, int peer_id, const char *reaso
265 */ 268 */
266int toxav_stop_call ( ToxAv *av, int32_t call_index ) 269int toxav_stop_call ( ToxAv *av, int32_t call_index )
267{ 270{
268 if ( !av->msi_session->calls[call_index] ) { 271 if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
269 return ErrorNoCall; 272 return ErrorNoCall;
270 } 273 }
271 274
@@ -282,8 +285,8 @@ int toxav_stop_call ( ToxAv *av, int32_t call_index )
282 */ 285 */
283int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettings *codec_settings, int support_video ) 286int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettings *codec_settings, int support_video )
284{ 287{
285 if ( !av->msi_session || av->msi_session->max_calls <= call_index || !av->msi_session->calls[call_index] ) { 288 if ( !av->msi_session || cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
286 /*fprintf(stderr, "Error while starting audio RTP session: invalid call!\n");*/ 289 LOGGER_ERROR("Error while starting audio RTP session: invalid call!\n");
287 return ErrorInternal; 290 return ErrorInternal;
288 } 291 }
289 292
@@ -301,7 +304,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
301 304
302 305
303 if ( !call->crtps[audio_index] ) { 306 if ( !call->crtps[audio_index] ) {
304 /*fprintf(stderr, "Error while starting audio RTP session!\n");*/ 307 LOGGER_ERROR("Error while starting audio RTP session!\n");
305 return ErrorStartingAudioRtp; 308 return ErrorStartingAudioRtp;
306 } 309 }
307 310
@@ -319,7 +322,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
319 322
320 323
321 if ( !call->crtps[video_index] ) { 324 if ( !call->crtps[video_index] ) {
322 /*fprintf(stderr, "Error while starting video RTP session!\n");*/ 325 LOGGER_ERROR("Error while starting video RTP session!\n");
323 return ErrorStartingVideoRtp; 326 return ErrorStartingVideoRtp;
324 } 327 }
325 } 328 }
@@ -347,15 +350,17 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
347 */ 350 */
348int toxav_kill_transmission ( ToxAv *av, int32_t call_index ) 351int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
349{ 352{
353 if (cii(call_index, av->msi_session)) return ErrorNoCall;
354
350 CallSpecific *call = &av->calls[call_index]; 355 CallSpecific *call = &av->calls[call_index];
351 356
352 if ( call->crtps[audio_index] && -1 == rtp_terminate_session(call->crtps[audio_index], av->messenger) ) { 357 if ( call->crtps[audio_index] && -1 == rtp_terminate_session(call->crtps[audio_index], av->messenger) ) {
353 /*fprintf(stderr, "Error while terminating audio RTP session!\n");*/ 358 LOGGER_ERROR("Error while terminating audio RTP session!\n");
354 return ErrorTerminatingAudioRtp; 359 return ErrorTerminatingAudioRtp;
355 } 360 }
356 361
357 if ( call->crtps[video_index] && -1 == rtp_terminate_session(call->crtps[video_index], av->messenger) ) { 362 if ( call->crtps[video_index] && -1 == rtp_terminate_session(call->crtps[video_index], av->messenger) ) {
358 /*fprintf(stderr, "Error while terminating video RTP session!\n");*/ 363 LOGGER_ERROR("Error while terminating video RTP session!\n");
359 return ErrorTerminatingVideoRtp; 364 return ErrorTerminatingVideoRtp;
360 } 365 }
361 366
@@ -392,6 +397,8 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
392inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload, 397inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
393 uint16_t length ) 398 uint16_t length )
394{ 399{
400 if (cii(call_index, av->msi_session)) return ErrorNoCall;
401
395 if ( av->calls[call_index].crtps[type - TypeAudio] ) 402 if ( av->calls[call_index].crtps[type - TypeAudio] )
396 return rtp_send_msg ( av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, payload, 403 return rtp_send_msg ( av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, payload,
397 length ); 404 length );
@@ -411,6 +418,7 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
411inline__ int toxav_recv_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, uint8_t *dest ) 418inline__ int toxav_recv_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, uint8_t *dest )
412{ 419{
413 if ( !dest ) return ErrorInternal; 420 if ( !dest ) return ErrorInternal;
421 if (cii(call_index, av->msi_session)) return ErrorNoCall;
414 422
415 CallSpecific *call = &av->calls[call_index]; 423 CallSpecific *call = &av->calls[call_index];
416 424
@@ -462,6 +470,7 @@ inline__ int toxav_recv_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
462inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **output) 470inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **output)
463{ 471{
464 if ( !output ) return ErrorInternal; 472 if ( !output ) return ErrorInternal;
473 if (cii(call_index, av->msi_session)) return ErrorNoCall;
465 474
466 uint8_t packet [RTP_PAYLOAD_SIZE]; 475 uint8_t packet [RTP_PAYLOAD_SIZE];
467 int recved_size = 0; 476 int recved_size = 0;
@@ -472,7 +481,7 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
472 recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet); 481 recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet);
473 482
474 if (recved_size > 0 && ( rc = vpx_codec_decode(&call->cs->v_decoder, packet, recved_size, NULL, 0) ) != VPX_CODEC_OK) { 483 if (recved_size > 0 && ( rc = vpx_codec_decode(&call->cs->v_decoder, packet, recved_size, NULL, 0) ) != VPX_CODEC_OK) {
475 /*fprintf(stderr, "Error decoding video: %s\n", vpx_codec_err_to_string(rc));*/ 484 LOGGER_ERROR("Error decoding video: %s\n", vpx_codec_err_to_string(rc));
476 return ErrorInternal; 485 return ErrorInternal;
477 } 486 }
478 487
@@ -497,6 +506,7 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
497 */ 506 */
498inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, int frame_size) 507inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, int frame_size)
499{ 508{
509 if (cii(call_index, av->msi_session)) return ErrorNoCall;
500 return toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size); 510 return toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size);
501} 511}
502 512
@@ -513,12 +523,14 @@ inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *fr
513 */ 523 */
514inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, vpx_image_t *input) 524inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, vpx_image_t *input)
515{ 525{
526 if (cii(call_index, av->msi_session)) return ErrorNoCall;
527
516 CallSpecific *call = &av->calls[call_index]; 528 CallSpecific *call = &av->calls[call_index];
517 529
518 int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US); 530 int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
519 531
520 if ( rc != VPX_CODEC_OK) { 532 if ( rc != VPX_CODEC_OK) {
521 fprintf(stderr, "Could not encode video frame: %s\n", vpx_codec_err_to_string(rc)); 533 LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
522 return ErrorInternal; 534 return ErrorInternal;
523 } 535 }
524 536
@@ -555,6 +567,7 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d
555inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, int16_t *dest ) 567inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, int16_t *dest )
556{ 568{
557 if ( !dest ) return ErrorInternal; 569 if ( !dest ) return ErrorInternal;
570 if (cii(call_index, av->msi_session)) return ErrorNoCall;
558 571
559 CallSpecific *call = &av->calls[call_index]; 572 CallSpecific *call = &av->calls[call_index];
560 573
@@ -595,6 +608,7 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
595 */ 608 */
596inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *frame, int frame_size) 609inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *frame, int frame_size)
597{ 610{
611 if (cii(call_index, av->msi_session)) return ErrorNoCall;
598 return toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size); 612 return toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size);
599} 613}
600 614
@@ -613,10 +627,11 @@ inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *fr
613inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, 627inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max,
614 const int16_t *frame, int frame_size) 628 const int16_t *frame, int frame_size)
615{ 629{
630 if (cii(call_index, av->msi_session)) return ErrorNoCall;
616 int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max); 631 int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max);
617 632
618 if (rc < 0) { 633 if (rc < 0) {
619 fprintf(stderr, "Failed to encode payload: %s\n", opus_strerror(rc)); 634 LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
620 return ErrorInternal; 635 return ErrorInternal;
621 } 636 }
622 637
@@ -634,9 +649,7 @@ inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t
634 */ 649 */
635int toxav_get_peer_transmission_type ( ToxAv *av, int32_t call_index, int peer ) 650int toxav_get_peer_transmission_type ( ToxAv *av, int32_t call_index, int peer )
636{ 651{
637 assert(av->msi_session); 652 if ( peer < 0 || cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] || av->msi_session->calls[call_index]->peer_count <= peer )
638
639 if ( peer < 0 || !av->msi_session->calls[call_index] || av->msi_session->calls[call_index]->peer_count <= peer )
640 return ErrorInternal; 653 return ErrorInternal;
641 654
642 return av->msi_session->calls[call_index]->type_peer[peer]; 655 return av->msi_session->calls[call_index]->type_peer[peer];
@@ -654,7 +667,7 @@ int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer )
654{ 667{
655 assert(av->msi_session); 668 assert(av->msi_session);
656 669
657 if ( peer < 0 || !av->msi_session->calls[call_index] || av->msi_session->calls[call_index]->peer_count <= peer ) 670 if ( peer < 0 || cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] || av->msi_session->calls[call_index]->peer_count <= peer )
658 return ErrorInternal; 671 return ErrorInternal;
659 672
660 return av->msi_session->calls[call_index]->peers[peer]; 673 return av->msi_session->calls[call_index]->peers[peer];