ffmpeg7.0 + sdl3.0播放音频

ffmpeg7.0 + sdl3 播放音频文件

核心流程:ffmpeg负责解码 sdl负责播放pcm

// playAudio.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
extern "C" {
#include "SDL3/SDL.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"
#include "libavcodec/avcodec.h"
#include "libswresample/swresample.h"
#include "libavformat/avformat.h"
}

int main()
{
    // 1 初始化sdl
    if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
        SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    // 2 ffmpeg 打开解码器 初始化重采样对象
    std::string filename = "10. SDL Install.mp4";
    
    AVFormatContext* ic = nullptr;
    int ret = avformat_open_input(&ic, filename.c_str(), nullptr, nullptr);
    if (ret < 0) {
        std::cout << " avformat_open_input failed " << std::endl;
        return -1;
    }
    ret = avformat_find_stream_info(ic, nullptr);
    if (ret < 0) {
        std::cout << " avformat_find_stream_info failed " << std::endl;
        return -1;
    }

    int audioIndex = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
    if (audioIndex < 0) {
        std::cout << " av_find_best_stream audio failed " << std::endl;
        return -1;
    }
    AVCodecContext* avctx = avcodec_alloc_context3(nullptr);
    if (!avctx) {
        std::cout << " avcodec_alloc_context3 failed " << std::endl;
        return -1;
    }

    ret = avcodec_parameters_to_context(avctx, ic->streams[audioIndex]->codecpar);
    if (ret < 0) {
        std::cout << " avcodec_parameters_to_context failed " << std::endl;
        return -1;
    }
    avctx->pkt_timebase = ic->streams[audioIndex]->time_base;
   
    const AVCodec* codec = avcodec_find_decoder(avctx->codec_id);
    if (!codec) {
        std::cout << " avcodec_find_decoder failed " << std::endl;
        return -1;
    }

    ret = avcodec_open2(avctx, codec, nullptr);
    if (ret < 0) {
        std::cout << " avcodec_open2 failed " << std::endl;
        return -1;
    }

    AVPacket* pkt = av_packet_alloc();
    if (!pkt) {
        std::cout << " av_packet_alloc failed " << std::endl;
        return -1;
    }

    AVFrame* frame = av_frame_alloc();
    if (!frame) {
        std::cout << " av_frame_alloc failed " << std::endl;
        return -1;
    }
    struct SwrContext* swr_ctx = nullptr;
    ret = swr_alloc_set_opts2(&swr_ctx,
        &avctx->ch_layout, AV_SAMPLE_FMT_S16, 44100,
        &avctx->ch_layout, avctx->sample_fmt, avctx->sample_rate,0, nullptr);
    if (ret < 0 || swr_init(swr_ctx) < 0) {
        std::cout << " swr_alloc_set_opts2 or  failed swr_init " << std::endl;
        return -1;
    }
       
    // 3 打开对应声卡设备
    uint8_t* sampleData = nullptr;
    int sampleSize = 0;

    SDL_AudioSpec spec;
    spec.freq = 44100;
    spec.channels = avctx->ch_layout.nb_channels;
    spec.format = SDL_AUDIO_S16LE;

    SDL_AudioStream* stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, nullptr, nullptr);
    if (!stream) {
        SDL_Log("Couldn't create audio stream: %s", SDL_GetError());
        return -1;
    }

    /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */
    SDL_ResumeAudioStreamDevice(stream);


    // 4 获取解码数据 喂给声卡进行播放
    while (1) {
        ret = av_read_frame(ic, pkt);
        if (ret < 0)
            break;
        if (pkt->stream_index != audioIndex) {
            av_packet_unref(pkt);
            continue;
        }
        ret = avcodec_send_packet(avctx, pkt);
        if (ret < 0) {
            av_packet_unref(pkt);
            break;
        }
        while (ret >= 0) {
            ret = avcodec_receive_frame(avctx, frame);
            if (ret >= 0) {
                if (sampleData == nullptr) {
                    // avctx->frame_size 这个有可能为0 所以用frame->nb_samples
                    sampleSize = av_samples_get_buffer_size(nullptr, avctx->ch_layout.nb_channels, frame->nb_samples, AV_SAMPLE_FMT_S16, 1);
                    if (sampleSize < 0)
                        break;
                    sampleData = new uint8_t[sampleSize * 2];
                }
                int resampleSize = swr_convert(swr_ctx, &sampleData, sampleSize, frame->data, frame->nb_samples);
                // AV_SAMPLE_FMT_S16 16位 2个字节所以要乘以2
                int64_t pcmDataLen = resampleSize * avctx->ch_layout.nb_channels * 2;
          
                int remaininglen = SDL_GetAudioStreamQueued(stream);
                while (remaininglen > pcmDataLen * frame->nb_samples) {
                    /* feed more data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */
                    remaininglen = SDL_GetAudioStreamQueued(stream);
                    SDL_Delay(1);
                }
                //SDL_PutAudioStreamData 一直调用内存一直涨
                SDL_PutAudioStreamData(stream, sampleData, pcmDataLen);
            }
        }
        av_packet_unref(pkt);

        // 这里可以直接用 AVSampleFormat 对应的 SDL_AudioFormat  可以不用重采样
        //data_size = av_get_bytes_per_sample(avctx->sample_fmt);
        //if (data_size < 0) {
        //    /* This should not occur, checking just for paranoia */
        //    fprintf(stderr, "Failed to calculate data size\n");
        //    exit(1);
        //}
        //for (i = 0; i < frame->nb_samples; i++)
        //    for (ch = 0; ch < dec_ctx->ch_layout.nb_channels; ch++)
        //        fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile);
    }

    // 用于结束播放
    getchar();

    SDL_ClearAudioStream(stream);
    // 5 释放初始化的上下文
    if (pkt)
        av_packet_free(&pkt);
    if (frame)
        av_frame_free(&frame);

    if (avctx) 
        avcodec_free_context(&avctx);
  
    if (ic)
        avformat_close_input(&ic);
    if (sampleData)
        delete[] sampleData;
    if (swr_ctx)
        swr_free(&swr_ctx);
    SDL_Quit();

}


工程链接: https://download.csdn.net/download/lg15273112290/90634581

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值