A little confusion in playing audio by SDL2

when I see ideaudioexamples.html" rel=" nofollow noreferrer "> Audio Example in SDL2, I have some doubts about some of the code. The code is as follows:

// sdl_audio_example.c

-sharpdefine MAX_AUDIO_FRAME_SIZE 192000

// TODO: 1. 
// TODO: 2. audio_chunkaudio_pos = out_buffer??
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;

void audio_callback(void *udata, Uint8 *stream, int len)
{
    if ( audio_len == 0 )
        return;

    len = ( len > audio_len ? audio_len : len );
    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    
    audio_pos += len;
    audio_len -= len;
}

int main(int argc, char *argv[]){
    // do something ...
    
    uint8_t *out_buffer = (uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE * 3 / 2);
    int out_buffer_size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples, AV_SAMPLE_FMT_S16, 1);
    
    for(;;){
        AVFrame *frame = ...;
        
        swr_convert(swr_ctx,
                    &out_buffer,
                    MAX_AUDIO_FRAME_SIZE,
                    (const uint_8 *)frame->data[0],
                    frame->nb_samples);
                    
        audio_chunk = (uint8_t *)out_buffer;
        audio_len = out_buffer_size;
        audio_pos = audio_chunk;
    }

    return 0;
}

question
as shown in the code above:

  1. the meanings represented by the three variables audio_chunk, audio_len and audio_pos
  2. audio_pos says it"s a static buffer, and how does len update the location? Can you draw a picture and describe it?
  3. whether audio_chunk can be omitted, directly audio_pos=out_buffer??

Thank you very much!

Apr.14,2021
Menu