使用ffmpeg实现一个播放器的想法虽然不新颖,但在嵌入式linux上通过ffmpeg的代码接口实现,并使用alsa接口输出音频,还是有一定挑战和趣味性的。以下是对原文的伪原创处理:
实现一个嵌入式Linux上的音频播放器,支持mp3、aac和wav格式的文件,是一个有趣的项目。基于ffmpeg的强大功能,我们可以实现这个播放器,主要利用ffmpeg的协议处理和音频解码能力。尽管网上有很多相关代码,但由于版本差异,接口存在变化,实际实现时仍需花费不少时间调试。
总结几点关键经验:
使用的是ffmpeg-4.1.9版本,编译选项如下:
//fdk-aac root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build# vim arm-gcc-cxx11.cmake root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build# cmake -DCMAKE_TOOLCHAIN_FILE=/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build/arm-gcc-cxx11.cmake ../ -- The C compiler identification is GNU 6.4.1 root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app_linux# cat /home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build/arm-gcc-cxx11.cmake # Sample toolchain file for building with gcc compiler # Typical usage: # *) cmake -H. -B_build -DCMAKE_TOOLCHAIN_FILE="${PWD}/toolchains/gcc.cmake" SET(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR arm) # set compiler set(CMAKE_C_COMPILER arm-openwrt-linux-gnueabi-gcc) set(CMAKE_CXX_COMPILER arm-openwrt-linux-gnueabi-g++) set(CONFIGURE_OPTS --enable-static=yes --enable-shared=no --disable-shared) # set c++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) //mp3 /home/lyz/work/broadcast_app/app/thirds_libs_src/lame-3.100 ./configure --host=arm-openwrt-linux-gnueabi --prefix=${PWD}/build/ ./configure --target-os=linux --prefix=/home/lyz/work/broadcast_app/app_linux/thirds_libs_src/ffmpeg-4.1.9/tmp --disable-shared --disable-muxers --enable-pic --enable-static --enable-gpl --enable-nonfree --enable-ffmpeg --disable-debug --disable-filters --disable-encoders --disable-hwaccels --enable-static --enable-libmp3lame --enable-demuxers --enable-parsers --enable-protocols --disable-x86asm --disable-stripping --extra-cflags='-I/home/lyz/work/broadcast_app/app_linux/libs/include/ -I/home/lyz/work/broadcast_app/app_linux/libs/include/lame -Os -fpic ' --extra-ldflags='-ldl -lm -L/home/lyz/work/broadcast_app/app_linux/libs/' --enable-decoder=aac --enable-swresample --enable-decoder=ac3
在cpp文件中引用ffmpeg库时,如果出现链接错误,需要在头文件包含处添加两个前缀:
//.cpp #include <alsa> #ifdef __cplusplus extern "C" { #endif #include "libavutil/time.h" #include "libavformat/avformat.h" #include "libavcodec/avcodec.h" #include "libavdevice/avdevice.h" #include "libswresample/swresample.h" #include "libswscale/swscale.h" #ifdef __cplusplus } #endif
即使修改了头文件包含方式,仍然可能出现链接错误,这与链接库的顺序有关。错误的库链接顺序如下:
LDFLAGS += -L ./libs/ -lavcodec -lavfilter -lavformat -lavutil -lpostproc -lswscale -lswresample -lfdk-aac -lmp3lame
正确的库链接顺序应为:
LDFLAGS += -Wl,-Bstatic -L./libs -lavformat -lavcodec -lswscale -lswresample -lavutil -lavfilter -lavdevice -lpostproc -lfdk-aac -lmp3lame
注意动态链接和静态链接的区别:
LDFLAGS += -Wl,-Bstatic -L./libs -lavformat -lavcodec -lswscale -lswresample -lavutil -lavfilter -lavdevice -lpostproc -lfdk-aac -lmp3lame LDFLAGS += -Wl,-Bdynamic -ldl -lm -lasound -lpthread
在处理内存泄漏问题时,可以使用valgrind工具进行检测:
root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app_linux# valgrind ./bas ./Test1.wav 0
最后,使用ALSA接口完整播放mp3文件声音的代码如下:
//static const char *device = "hw:1,0"; /* playback device "hw:0,0" */ static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ static unsigned int rate = 44100; /* stream rate */ static unsigned int channels = 2; /* count of channels */ static unsigned int buffer_time = 500000; /* ring buffer length in us */ static unsigned int period_time = 100000; /* period time in us */ static int resample = 1; /* enable alsa-lib resampling */ static snd_pcm_sframes_t buffer_size; static snd_pcm_sframes_t period_size; snd_pcm_access_t mode = SND_PCM_ACCESS_RW_INTERLEAVED; static snd_output_t *output = NULL; /*配置参数*/ static int set_hwparams(snd_pcm_t *handle,snd_pcm_hw_params_t *params,snd_pcm_access_t access){ unsigned int rrate; snd_pcm_uframes_t size; int err, dir = 0; /* choose all parameters */ err = snd_pcm_hw_params_any(handle, params); if (err < 0) { printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err)); return err; } /* set hardware resampling */ err = snd_pcm_hw_params_set_rate_resample(handle, params, resample); if (err < 0) { printf("Resampling setup failed for playback: %s\n", snd_strerror(err)); return err; } /* set the interleaved read/write format */ err = snd_pcm_hw_params_set_access(handle, params, access); if (err < 0) { printf("Access type not available for playback: %s\n", snd_strerror(err)); return err; } /* set the sample format */ err = snd_pcm_hw_params_set_format(handle, params, format); if (err < 0) { printf("Sample format not available for playback: %s\n", snd_strerror(err)); return err; } /* set the count of channels */ err = snd_pcm_hw_params_set_channels(handle, params, channels); if (err < 0) { printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err)); return err; } /* set the stream rate */ rrate = rate; err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0); if (err < 0) { printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err)); return err; } if (rrate != rate) { printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); return -EINVAL; } /* set the buffer time */ err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir); if (err < 0) { printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err)); return err; } err = snd_pcm_hw_params_get_buffer_size(params, &buffer_size); if (err < 0) { printf("Unable to get buffer size for playback: %s\n", snd_strerror(err)); return err; } printf("buffer_size = %ld\n", buffer_size); /* set the period time */ err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir); if (err < 0) { printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err)); return err; } err = snd_pcm_hw_params_get_period_size(params, &period_size, &dir); if (err < 0) { printf("Unable to get period size for playback: %s\n", snd_strerror(err)); return err; } printf("period_size = %ld\n", period_size); /* write the parameters to the driver */ err = snd_pcm_hw_params(handle, params); if (err < 0) { printf("Unable to set hw params for playback: %s\n", snd_strerror(err)); return err; } return 0; } int test_play_mp3(int argc, char *argv[]){ int rc; int size; int got_picture; int nb_data; bool pkt_pending = false; int audio_stream_idx; char **hints, **n; char *alsa_device_name; if (argc < 2) { printf("Usage: %s <file>\n", argv[0]); return 1; } AVFormatContext *infmt_ctx = NULL; AVPacket *input_packet = av_packet_alloc(); AVFrame *pframeSRC = av_frame_alloc(); AVFrame *pframePCM = av_frame_alloc(); int ret; if ((ret = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { printf("Playback open error: %s\n", snd_strerror(ret)); return ret; } if ((err = snd_pcm_hw_params_malloc(¶ms)) < 0) { printf("Cannot allocate hardware parameter structure (%s)\n", snd_strerror(err)); return err; } if ((err = set_hwparams(handle, params, mode)) < 0) { printf("Setting of hwparams failed: %s\n", snd_strerror(err)); snd_pcm_hw_params_free(params); snd_pcm_close(handle); return err; } if ((err = snd_pcm_sw_params_malloc(&swparams)) < 0) { printf("Cannot allocate software parameters structure (%s)\n", snd_strerror(err)); return err; } if ((err = set_swparams(handle, swparams)) < 0) { printf("Setting of swparams failed: %s\n", snd_strerror(err)); snd_pcm_sw_params_free(swparams); snd_pcm_hw_params_free(params); snd_pcm_close(handle); return err; } if ((err = snd_pcm_prepare(handle)) < 0) { printf("Cannot prepare audio interface for use (%s)\n", snd_strerror(err)); return err; } printf("Try to open the device for playback - success\r\n"); snd_pcm_close (pcm); pcm = NULL; alsa_device_name = name; break; } printf("found device:%s\r\n", alsa_device_name); //break; }} n++; } printf("Playback device is %s\n", alsa_device_name); /* Install error handler after enumeration, otherwise we'll get many * error messages about invalid card/device ID. */ snd_lib_error_set_handler(alsa_error_handler); err = snd_device_name_free_hint((void**)hints); err = snd_output_stdio_attach(&output, stdout, 0); if (err < 0) { printf("Attach output failed: %s\n", snd_strerror(err)); return err; } if ((ret = avformat_open_input(&infmt_ctx, argv[1], NULL, NULL)) < 0) { fprintf(stderr, "Could not open input file '%s' (error '%s')\n", argv[1], av_err2str(ret)); return ret; } infmt_ctx->max_analyze_duration = 5*AV_TIME_BASE; //读取一部分视音频流并且获得一些相关的信息 ret=avformat_find_stream_info(infmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information\n"); avformat_close_input(&infmt_ctx); return ret; } audio_stream_idx = av_find_best_stream(infmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if (audio_stream_idx < 0) { fprintf(stderr, "Could not find any audio stream in the file\n"); avformat_close_input(&infmt_ctx); return -1; } AVCodecParameters *pCodecParameters = infmt_ctx->streams[audio_stream_idx]->codecpar; if (pCodecParameters == NULL){ printf("pCodecParameters is NULL\n"); avformat_close_input(&infmt_ctx); return -1; } //找到解码器 const AVCodec* decodec = avcodec_find_decoder(pCodecParameters->codec_id); if (!decodec) { printf("not find decoder codec audio_stream_idx:%d codec_id:%d\n", audio_stream_idx, pCodecParameters->codec_id); avformat_close_input(&infmt_ctx); return -1; } AVCodecContext *decodec_ctx = avcodec_alloc_context3(decodec); if (!decodec_ctx) { printf("Can't allocate decoder context\n"); avformat_close_input(&infmt_ctx); return AVERROR(ENOMEM); } if(avcodec_parameters_to_context(decodec_ctx, pCodecParameters) < 0) { printf("Could not copy codec parameters to context\n"); avcodec_free_context(&decodec_ctx); avformat_close_input(&infmt_ctx); return -1; } pkt_timebase = infmt_ctx->streams[audio_stream_idx]->time_base; #if 0 decodec_ctx->sample_rate = pCodecParameters->sample_rate; decodec_ctx->sample_fmt = (AVSampleFormat)pCodecParameters->format ; decodec_ctx->channels = pCodecParameters->channels; decodec_ctx->channel_layout = pCodecParameters->channel_layout; #endif //打开解码器 ret = avcodec_open2(decodec_ctx, decodec, NULL); if (ret < 0) { printf("Could not open codec\n"); avcodec_free_context(&decodec_ctx); avformat_close_input(&infmt_ctx); return ret; } pframePCM->format = AV_SAMPLE_FMT_S16; pframePCM->channel_layout = AV_CH_LAYOUT_STEREO; pframePCM->sample_rate = rate; pframePCM->nb_samples = period_size; pframePCM->channels = channels; av_frame_get_buffer(pframePCM, 0); #else uint8_t *converted_input_samples = NULL; int converted_input_samples_size = av_samples_alloc(&converted_input_samples, NULL, channels , period_size, AV_SAMPLE_FMT_S16, 0); #endif struct SwrContext *pcm_convert_ctx = swr_alloc(); if (!pcm_convert_ctx) { printf("Could not allocate resampler context\n"); free(buffer); return -1; } swr_alloc_set_opts(pcm_convert_ctx, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, pframePCM->sample_rate, av_get_default_channel_layout(decodec_ctx->channels), decodec_ctx->sample_fmt, decodec_ctx->sample_rate, 0, NULL); ret = swr_init(pcm_convert_ctx); if (ret < 0) { printf("Could not open resampler context\n"); swr_free(&pcm_convert_ctx); free(buffer); return -1; } pframeSRC->format = (AVSampleFormat)pCodecParameters->format ; pframeSRC->channel_layout = decodec_ctx->channel_layout; pframeSRC->sample_rate = decodec_ctx->sample_rate; pframeSRC->nb_samples = (20*decodec_ctx->sample_rate * channels * 2) / 8000;; pframeSRC->channels = channels; av_frame_get_buffer(pframeSRC, 0); #endif int finished = 0; int decode_ret = 0; int data_size = av_get_bytes_per_sample(decodec_ctx->sample_fmt); printf("data_size:%d, frame_size:%d, dst_samples:%d\n", data_size, pCodecParameters->frame_size, pframePCM->nb_samples); while (!finished) { ret=av_read_frame(infmt_ctx, input_packet); if (ret != 0) { if (ret == AVERROR_EOF){ finished = 1; break; } printf("fail to read_frame\n"); break; } //avcodec_send_packet/avcodec_receive_frame //解码获取初始音频 ret = avcodec_send_packet(decodec_ctx, input_packet); if (ret == AVERROR(EAGAIN)) { pkt_pending = true; continue; } if (ret < 0) { printf("Error sending a packet for decoding\n"); break; } pkt_pending = false; while (ret >= 0) { ret = avcodec_receive_frame(decodec_ctx, pframeSRC); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { printf("Error during decoding\n"); break; } int source_samples = pframeSRC->nb_samples; int out_samples = source_samples; // uint8_t *write_2_pcm = NULL; if (out_samples != pframePCM->nb_samples){ no_resample = 1; //读取到一帧音频或者视频 //MP3->PCM,ret=swr_convert(pcm_convert_ctx, pframePCM->data, pframePCM->nb_samples,(const uint8_t **)pframeSRC->extended_data, pframeSRC->nb_samples); if (ret < 0) { printf("[1]source_samples:%d, pframeSRC->nb_samples:%d,ret:%d\n", source_samples, pframeSRC->nb_samples, ret); continue; } else { //printf("[2]out_samples:%d, pframeSRC->nb_samples:%d,ret:%d\n", source_samples, pframeSRC->nb_samples, ret); } write_2_pcm = pframePCM->data[0]; nb_data = ret; } else { printf("out_samples:%d, pframeSRC->nb_samples:%d \n", out_samples, pframeSRC->nb_samples ); nb_data = out_samples; write_2_pcm = pframeSRC->data[0]; } //向硬件写入音频数据 rc = snd_pcm_writei(handle, write_2_pcm, out_samples); if (rc == -EPIPE) { printf("underrun occurred\n"); err=snd_pcm_prepare(handle); if(err < 0) { printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err)); break; } } if (rc < 0) { printf("write to audio interface failed (%s)\n", snd_strerror(rc)); break; } if (rc != out_samples) { printf("short write, write %d frames\n", rc); } av_packet_unref(input_packet); } } if (pcm_convert_ctx) { swr_free(&pcm_convert_ctx); } av_packet_free(&input_packet); if (pframeSRC) { av_frame_free(&pframeSRC); } #if 1 if (pframePCM) { av_frame_free(&pframePCM); } #endif if(decodec_ctx != NULL){ avcodec_close(decodec_ctx); avcodec_free_context(&decodec_ctx); } if (infmt_ctx != NULL) { avformat_close_input(&infmt_ctx); avformat_free_context(infmt_ctx); } snd_pcm_drain(handle); snd_pcm_close(handle); //free(converted_input_samples); free(buffer); free(alsa_device_name); return 0; }
参考:https://www.php.cn/link/94a5313663ab243911f0da89ed1096db
下一步计划是实现对rtsp流的请求。
2022/11/28更新:实现rtsp播放器,只需将播放路径直接设置为rtsp地址,操作非常简单!
以上就是Linux下使用ffmpeg播放mp3/aac/wav文件的音乐播放器应用的详细内容,更多请关注php中文网其它相关文章!
potplayer是一款功能全面的视频播放器,支持各种格式的音频文件,内置了非常强大的解码器功能,能够非常流畅的观看,有需要的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号