int32_t S3_HLS_Add_To_Queue()

in S3_HLS_Queue.c [62:109]


int32_t S3_HLS_Add_To_Queue(S3_HLS_QUEUE_CTX* ctx, uint8_t* first_part, uint32_t first_length, uint8_t* second_part, uint32_t second_length, time_t timestamp) {
    if(NULL == ctx) {
        QUEUE_DEBUG("[Add]Invalid Queue Context!\n");
        return S3_HLS_INVALID_PARAMETER;
    }
    
    QUEUE_DEBUG("[Add]Locking queue! %lu\n", pthread_self());
    int32_t ret = pthread_mutex_lock(&ctx->s3_hls_queue_lock);
    if(0 != ret) {
        QUEUE_DEBUG("[Add]Failed to lock queue lock! %d\n", ret);
        return ret;
    }
    
    QUEUE_DEBUG("Current queue length: %d\n", ctx->queue_length);
    if(ctx->queue_length == S3_HLS_MAX_PARTS_IN_BUFFER) {
        QUEUE_DEBUG("[Add]Queue is full!\n");
        ret = pthread_mutex_unlock(&ctx->s3_hls_queue_lock);
        if(0 != ret) {
            QUEUE_DEBUG("[Add]Unlock queue failed! %d\n", ret);
        }
        return S3_HLS_QUEUE_FULL; // queue is full
    }

    uint8_t current_pos = ctx->queue_pos + ctx->queue_length;
    if(current_pos >= S3_HLS_MAX_PARTS_IN_BUFFER)
        current_pos -= S3_HLS_MAX_PARTS_IN_BUFFER;

    ctx->queue[current_pos].first_part_start = first_part;
    ctx->queue[current_pos].first_part_length = first_length;
    ctx->queue[current_pos].second_part_start = second_part;
    ctx->queue[current_pos].second_part_length = second_length;
    ctx->queue[current_pos].timestamp = timestamp;

    QUEUE_DEBUG("Before increase length: %d\n", ctx->queue_length);

    ctx->queue_length++;
    
    QUEUE_DEBUG("Added to queue! length: %d\n", ctx->queue_length);
    
    QUEUE_DEBUG("[Add]Unlocking queue!%lu\n", pthread_self());
    ret = pthread_mutex_unlock(&ctx->s3_hls_queue_lock);
    if(0 != ret) {
        QUEUE_DEBUG("[Add]Failed to unlock queue lock! %d\n", ret);
        return ret;
    }
    
    return S3_HLS_OK;
}