From 77c7a3e1030daf9cfba9dae4f850a7a92810ec83 Mon Sep 17 00:00:00 2001 From: mannol Date: Sat, 5 Jul 2014 19:27:31 +0200 Subject: Check if call is active after getting mutex handle --- toxav/toxav.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/toxav/toxav.c b/toxav/toxav.c index 36053fd5..19fcd854 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -381,8 +381,8 @@ error: */ int toxav_kill_transmission ( ToxAv *av, int32_t call_index ) { - if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) { - LOGGER_WARNING("Action on inactive call: %d", call_index); + if (cii(call_index, av->msi_session)) { + LOGGER_WARNING("Invalid call index: %d", call_index); return ErrorNoCall; } @@ -390,6 +390,12 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index ) pthread_mutex_lock(&call->mutex); + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } + call->call_active = 0; rtp_terminate_session(call->crtps[audio_index], av->messenger); call->crtps[audio_index] = NULL; @@ -527,15 +533,21 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out { if ( !output ) return ErrorInternal; - if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) { - LOGGER_WARNING("Action on inactive call: %d", call_index); + if (cii(call_index, av->msi_session)) { + LOGGER_WARNING("Invalid call index: %d", call_index); return ErrorNoCall; } - CallSpecific *call = &av->calls[call_index]; pthread_mutex_lock(&call->mutex); + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } + + uint8_t packet [RTP_PAYLOAD_SIZE]; int recved_size; @@ -602,15 +614,23 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out */ inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, int frame_size) { - if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) { - LOGGER_WARNING("Action on inactive call: %d", call_index); + if (cii(call_index, av->msi_session)) { + LOGGER_WARNING("Invalid call index: %d", call_index); return ErrorNoCall; } + CallSpecific* call = &av->calls[call_index]; + pthread_mutex_lock(&call->mutex); + + + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } - pthread_mutex_lock(&av->calls[call_index].mutex); int rc = toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size); - pthread_mutex_unlock(&av->calls[call_index].mutex); + pthread_mutex_unlock(&call->mutex); return rc; } @@ -628,8 +648,8 @@ inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *fr */ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, vpx_image_t *input) { - if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) { - LOGGER_WARNING("Action on inactive call: %d", call_index); + if (cii(call_index, av->msi_session)) { + LOGGER_WARNING("Invalid call index: %d", call_index); return ErrorNoCall; } @@ -637,6 +657,12 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d CallSpecific *call = &av->calls[call_index]; pthread_mutex_lock(&call->mutex); + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } + reconfigure_video_encoder_resolution(call->cs, input->d_w, input->d_h); int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US); @@ -685,14 +711,21 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i { if ( !dest ) return ErrorInternal; - if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) { - LOGGER_WARNING("Action on inactive call: %d", call_index); + if (cii(call_index, av->msi_session)) { + LOGGER_WARNING("Invalid call index: %d", call_index); return ErrorNoCall; } CallSpecific *call = &av->calls[call_index]; pthread_mutex_lock(&call->mutex); + + + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } uint8_t packet [RTP_PAYLOAD_SIZE]; @@ -741,10 +774,18 @@ inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *fr return ErrorNoCall; } + CallSpecific* call = &av->calls[call_index]; + pthread_mutex_lock(&call->mutex); + + + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } - pthread_mutex_lock(&av->calls[call_index].mutex); int rc = toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size); - pthread_mutex_unlock(&av->calls[call_index].mutex); + pthread_mutex_unlock(&call->mutex); return rc; } @@ -769,12 +810,19 @@ inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t return ErrorNoCall; } + CallSpecific* call = &av->calls[call_index]; + pthread_mutex_lock(&call->mutex); - pthread_mutex_lock(&av->calls[call_index].mutex); - int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max); + if (!call->call_active){ + pthread_mutex_unlock(&call->mutex); + LOGGER_WARNING("Action on inactive call: %d", call_index); + return ErrorNoCall; + } + + int32_t rc = opus_encode(call->cs->audio_encoder, frame, frame_size, dest, dest_max); + pthread_mutex_unlock(&call->mutex); - pthread_mutex_unlock(&av->calls[call_index].mutex); if (rc < 0) { LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc)); -- cgit v1.2.3