FFmpeg+SDL2實現音頻流播放

SDL2文章列表git

SDL2入門github

SDL2事件處理ide

SDL2紋理渲染函數

SDL2 PCM音頻播放佈局

FFmpeg+SDL2實現視頻流播放ui

以前完成了PCM音頻的播放,此次實現的是FFmpeg+SDL2播聽任意視頻中的音頻流。spa

總體的流程和視頻流播放相似,須要瞭解下的就是 SwrContext 重採樣結構體code

重採樣結構體,就是改變音頻的採樣率、sample format、聲道數等參數,使之按照咱們指望的參數輸出,固然是原有的音頻參數不知足咱們的需求,好比在FFMPEG解碼音頻的時候,不一樣的音源有不一樣的格式,採樣率等,在解碼後的數據中的這些參數也會不一致,若是咱們接下來須要使用解碼後的音頻數據作其餘操做,而這些參數的不一致致使會有不少額外工做,此時直接對其進行重採樣,獲取咱們制定的音頻參數,這樣就會方便不少。orm

經過重採樣,咱們能夠對 sample rate(採樣率)、sample format(採樣格式)、channel layout(通道佈局,能夠經過此參數獲取聲道數)進行調節。視頻

SwrContext經常使用函數

swr_alloc

// 用於申請一個SwrContext結構體
struct SwrContext *swr_alloc(void);
複製代碼

swr_init

// 當設置好相關的參數後,使用此函數來初始化SwrContext結構體
int swr_init(struct SwrContext *s);

複製代碼

swr_alloc_set_opts

//分配SwrContext並設置/重置經常使用的參數。參數包含了輸入輸出參數中sample rate(採樣率)、sample format(採樣格式)、channel layout等參數
函數原型:struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx);

複製代碼

swr_convert

// 將輸入的音頻按照定義的參數進行轉換,並輸出
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count);
複製代碼

swr_free

// 釋放掉SwrContext結構體並將此結構體置爲NULL;
void swr_free(struct SwrContext **s);
複製代碼

示例代碼

//
// Created by 劉偉 on 2019/4/26.
//

#include <stdio.h>
#include <SDL_types.h>
#include "SDL.h"

#include "libswresample/swresample.h"
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>

static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;

#define MAX_AUDIO_FRAME_SIZE 19200


//音頻設備須要更多數據的時候會調用該回調函數
void read_audio_data(void *udata, Uint8 *stream, int len) {

    //首先使用SDL_memset()將stream中的數據設置爲0
    SDL_memset(stream, 0, 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 WinMain(int argc, char *argv[]) {
    char *file = "C:\\Users\\lenovo\\Desktop\\1080p.mov";

    AVFormatContext *pFormatCtx = NULL; 

    int i, audioStream = -1;

    AVCodecParameters *pCodecParameters = NULL;
    AVCodecContext *pCodecCtx = NULL;

    AVCodec *pCodec = NULL; 
    AVFrame *pFrame = NULL;
    AVPacket *packet;
    uint8_t *out_buffer;

    int64_t in_channel_layout;
    
    struct SwrContext *au_convert_ctx;

    if (avformat_open_input(&pFormatCtx, file, NULL, NULL) != 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open video file!");
        return -1; // Couldn't open file
    }

    audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);

    if (audioStream == -1) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Din't find a video stream!");
        return -1;// Didn't find a video stream
    }

    // 音頻流參數
    pCodecParameters = pFormatCtx->streams[audioStream]->codecpar;

    // 獲取解碼器
    pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
    if (pCodec == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported codec!\n");
        return -1; // Codec not found
    }

    // Copy context
    pCodecCtx = avcodec_alloc_context3(pCodec);
    if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't copy codec context");
        return -1;// Error copying codec context
    }

    // Open codec
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open decoder!\n");
        return -1; // Could not open codec
    }
    packet = (AVPacket *) av_malloc(sizeof(AVPacket));
    av_init_packet(packet);
    pFrame = av_frame_alloc();

    uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;//輸出聲道
    int out_nb_samples = 1024;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;//輸出格式S16
    int out_sample_rate = 44100;
    int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);

    int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);
    out_buffer = (uint8_t *) av_malloc(MAX_AUDIO_FRAME_SIZE * 2);


    //Init
    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        printf("Could not initialize SDL - %s\n", SDL_GetError());
        return -1;
    }

    SDL_AudioSpec spec;
    spec.freq = out_sample_rate;
    spec.format = AUDIO_S16SYS;
    spec.channels = out_channels;
    spec.silence = 0;
    spec.samples = out_nb_samples;
    spec.callback = read_audio_data;
    spec.userdata = pCodecCtx;

    if (SDL_OpenAudio(&spec, NULL) < 0) {
        printf("can't open audio.\n");
        return -1;
    }

    in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);
    printf("in_channel_layout --->%d\n", in_channel_layout);
    au_convert_ctx = swr_alloc();
    au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate,in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);
    swr_init(au_convert_ctx);

    SDL_PauseAudio(0);

    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == audioStream) {
            avcodec_send_packet(pCodecCtx, packet);
            while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
                swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **) pFrame->data,pFrame->nb_samples); // 轉換音頻
            }

            audio_chunk = (Uint8 *) out_buffer;
            audio_len = out_buffer_size;
            audio_pos = audio_chunk;

            while (audio_len > 0) {
                SDL_Delay(1);//延遲播放
            }
        }
        av_packet_unref(packet);
    }
    swr_free(&au_convert_ctx);
    SDL_Quit();

    return 0;
}
複製代碼

代碼請查看源碼 GitHub-SimplePlayer-audio_player

相關文章
相關標籤/搜索