ffmpeg + SDL2播放音频示例

发布时间 2023-07-29 15:20:05作者: Oyking

在网上搜罗了各种各样的样例代码,都存在各种各样的问题,调了好长时间终于能无杂音播放了
由于个人场景需要本样例加了选择扬声器的功能
不过有的可能还会有问题,目前ogg的文件都能播,mp3有的不行
写一下网上的其他代码可能存在的问题和我的修改
注:代码是C++17,如果编不过需要小改一下

测试平台

  • Ubuntu 16.04, ffmpeg version: 4.3.2, SDL version: 2.0.4
  • Windows 10, ffmpeg version: 5.1.2, SDL version: 2.28.1

注意事项

  • include<SDL.h>要注意加宏SDL_MAIN_HANDLED,因为里面有个#define main,比较坑
  • 对部分格式(比如mp3)需要调用 avformat_find_stream_info,不然stream_id、采样率这些东西都会获取不到
  • 由于音频的采样率和输出设备的采样率之类的参数可能不一致,所以需要SwrContext(有些音频采样率是48100),否则会导致播放有杂音
  • 要关注swr_convert的返回值,对于部分音频,单次解码出来的buffer可能没填满,需要像代码中那样计算一下,否则会导致音频播放卡顿
  • 网上部分使用回调的代码,由于上一条,不能每次swr_convert结束后都调一下SDL_Delay,否则可能会出现解码跟不上播放速度导致音频播放卡顿

关于多个扬声器

  • 不需要的话,SDL_OpenAudioDevice第一个参数传空指针就行,像SDL_PauseAudioDevice这种函数可以改成不带Device的
  • 可以通过device id同时控制多个扬声器(不过代码里没有多个,有需要加一下即可)
  • 要停止播放可以调用SDL_ClearQueuedAudio清除播放的缓冲区。暂停的话可以调用SDL_PauseAudioDevice,第二个参数填1。

其他

  • 部分mp3无法播放,部分mp3从memory中打开无法播放,原因不明
  • 似乎无法控制声道,改AV_CH_LAYOUT_STEREO没用

代码

#include <cstdio>
#include <string>
#include <fstream>

#define SDL_MAIN_HANDLED
#include "SDL.h"

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

AVFormatContext *OpenAudioFromFile(const std::string &file_path) {
    AVFormatContext *format_ctx = avformat_alloc_context();
    if (int ret = avformat_open_input(&format_ctx, file_path.c_str(), NULL, NULL);
            ret < 0) {
        printf("avformat_open_input failed, ret: %d, path: %s\n", ret, file_path.c_str());
    }
    return format_ctx;
}

AVFormatContext *OpenAudioFromData(const std::string &data) {
    AVIOContext *avio_ctx = avio_alloc_context(
                (uint8_t *)data.data(), data.size(), 0, NULL, NULL, NULL, NULL);
    if (!avio_ctx) {
        printf("avio_alloc_context failed\n");
        return nullptr;
    }

    AVFormatContext *format_ctx = avformat_alloc_context();
    if (!format_ctx) {
        printf("avformat_alloc_context faield\n");
        return nullptr;
    }
    format_ctx->pb = avio_ctx;

    if (int ret = avformat_open_input(&format_ctx, NULL, NULL, NULL); ret < 0) {
        printf("avformat_open_input failed, ret: %d\n", ret);
        return nullptr;
    }
    return format_ctx;
}

std::string readFile(const std::string &file_path) {
    std::ifstream file(file_path); // 打开文件
    return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // 读取文件内容到string
}

int main(int argc, char *argv[]) {
    printf("ffmpeg version: %s\n", av_version_info());

    SDL_version version;
    SDL_GetVersion(&version);
    printf("SDL version: %d.%d.%d\n", version.major, version.minor, version.patch);

    char *file = "D:/qytx.mp3";
    AVFormatContext *pFormatCtx = NULL; //for opening multi-media file

    int audioStream = -1;

    AVCodecParameters *pCodecParameters = NULL; //codec context
    AVCodecContext *pCodecCtx = NULL;

    const AVCodec *pCodec = NULL; // the codecer
    AVFrame *pFrame = NULL;
    AVPacket *packet;
    uint8_t *out_buffer;

    int64_t in_channel_layout;
    struct SwrContext *au_convert_ctx;

    pFormatCtx = OpenAudioFromFile(file);
    //    std::string data = readFile(file);
    //    pFormatCtx = OpenAudioFromData(data);
    if (!pFormatCtx) {
        return -1;
    }

    if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
        // 处理获取流信息失败的情况
        return -1;
    }

    audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    printf("av_find_best_stream %d\n", audioStream);

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

    // Get a pointer to the codec context for the video stream
    pCodecParameters = pFormatCtx->streams[audioStream]->codecpar;

    // Find the decoder for the video stream
    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 = pCodecCtx->sample_rate;
    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(out_buffer_size * 2); // buffer不够大所以乘2,原因未知

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

    // 获取可用扬声器列表,不需要可以忽略
    int deviceCount = SDL_GetNumAudioDevices(0);
    printf("SDL_GetNumAudioDevices %d\n", deviceCount);
    for (int i = 0; i < deviceCount; i++) {
        printf("Audio Device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0));
    }

    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 = nullptr;

    // 指定扬声器,不需要第一个参数可以填nullptr
    SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(1, 0), false, &spec, nullptr, false);
    printf("device_id %d\n", device_id);
    if (device_id == 0) {
        printf("can't open audio.\n");
        return -1;
    }

    in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);
    au_convert_ctx = swr_alloc_set_opts(nullptr, 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_PauseAudioDevice(device_id, 0);

    fflush(stdout);

    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == audioStream) {
            avcodec_send_packet(pCodecCtx, packet);
            while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
                int ret = swr_convert(au_convert_ctx, &out_buffer, out_buffer_size, (const uint8_t **) pFrame->data,
                                      pFrame->nb_samples); // 转换音频
                if (ret < 0) {
                    printf("swr_convert failed %d\n", ret);
                }
                int out_samples = ret;
                SDL_QueueAudio(device_id, out_buffer, out_samples * spec.channels * av_get_bytes_per_sample(out_sample_fmt));
            }
        }
        av_packet_unref(packet);
    }
    SDL_Delay(200000); //延迟播放
    swr_free(&au_convert_ctx);
    SDL_CloseAudioDevice(device_id);
    SDL_Quit();
}