int32_t S3_HLS_Flush_Buffer()

in S3_HLS_Buffer_Mgr.c [80:129]


int32_t S3_HLS_Flush_Buffer(S3_HLS_BUFFER_CTX* ctx) {
    BUFFER_FLUSH_DEBUG("Flushing buffer!\n");
    if(NULL == ctx)
        return S3_HLS_INVALID_PARAMETER;

    uint8_t* cur_pos = ctx->used_start + ctx->used_length;
    if(NULL == ctx->last_flush) {
        BUFFER_FLUSH_DEBUG("Invalid last flush time!\n");
        return S3_HLS_INVALID_PARAMETER; // valid ctx will contain last_flush
    }
        
    if(0 == ctx->used_length) {
        BUFFER_FLUSH_DEBUG("Buffer is empty!\n");
        time(&ctx->last_flush_timestamp);
        return S3_HLS_OK;
    }
    
    if(ctx->last_flush != cur_pos) { // avoid duplicate flush especially when buffer is full
        if(NULL != ctx->call_back) {
            BUFFER_FLUSH_DEBUG("Calling callback function!\n");
            S3_HLS_BUFFER_PART_CTX part_ctx;
            if(cur_pos >= ctx->buffer_start + ctx->total_length) 
                cur_pos -= ctx->total_length;
                
            if(ctx->last_flush < cur_pos) {
                part_ctx.first_part_start = ctx->last_flush;
                part_ctx.first_part_length = cur_pos - ctx->last_flush;
                
                part_ctx.second_part_start = NULL;
                part_ctx.second_part_length = 0;
            } else { // acrossed ring buffer boundary
                part_ctx.first_part_start = ctx->last_flush;
                part_ctx.first_part_length = ctx->buffer_start + ctx->total_length - ctx->last_flush;
                
                part_ctx.second_part_start = ctx->buffer_start;
                part_ctx.second_part_length = cur_pos - ctx->buffer_start;
            }
            
            part_ctx.timestamp = ctx->last_flush_timestamp;
            
            ctx->call_back(&part_ctx);
            printf("Flush Buffer %p, %p, %d, %p, %d\n", ctx->last_flush, part_ctx.first_part_start, part_ctx.first_part_length, part_ctx.second_part_start, part_ctx.second_part_length);
        }
    
        ctx->last_flush = cur_pos;
        time(&ctx->last_flush_timestamp);
    }

    return S3_HLS_OK;
}