聊天平台源码,聊天平台如何获取到音频流
聊天平台源码,聊天平台如何获取到音频流的相关代码
我们重采样的参数要和SDL的参数是一致的,还要考虑到虽然没有AVPacket了,但是解码器中还有未解码的数据,我们要用avcodec_send_packet(aCodecCtx, NULL)告诉解码器没有数据了,刷新解码器。
下面这段代码的逻辑是如果没有数据要解码了,就跳转到获取解码数据的逻辑。
1 | int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)<br>{<br> <br> static AVPacket pkt;<br> static uint8_t *audio_pkt_data = NULL;<br> static int audio_pkt_size = 0 ;<br> static AVFrame frame;<br> av_frame_get_buffer(&frame, 0 );<br> int len1, data_size = 0 ;<br> int ret = 0 ;<br> int is_no_pkt = 0 ;<br> <br> for (;;)<br> {<br> <br> if (pkt.data)<br> av_packet_unref(&pkt);<br> <br> if (quit)<br> {<br> return - 1 ;<br> }<br> <br> if (packet_queue_get(&audioq, &pkt, 1 ) < 0 )<br> {<br> ret = avcodec_send_packet(aCodecCtx, NULL);<br> if (ret < 0 )<br> {<br> return - 1 ;<br> }<br> is_no_pkt = 1 ;<br> goto __RECEIVE;<br> }<br> <br> ret = avcodec_send_packet(aCodecCtx, &pkt);<br> if (ret < 0 )<br> {<br> ret = - 1 ;<br> printf( "decode error" );<br> av_packet_unref(&pkt);<br> return - 1 ;<br> }<br> __RECEIVE:<br> if (is_no_pkt)<br> {<br> return - 1 ;<br> }<br> else if (pkt.data)<br> {<br> av_packet_unref(&pkt);<br> }<br> ret = avcodec_receive_frame(aCodecCtx, &frame);<br> if (ret < 0 )<br> {<br> continue ;<br> }<br> <br> data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;<br> <br> swr_convert(audio_convert_ctx,<br> &audio_buf,<br> SDL_AUDIO_BUFFER_SIZE,<br> ( const uint8_t **)frame.data,<br> frame.nb_samples);<br> return data_size;<br> }<br>} |
转码后的数据大小=采样大小 * 单通道采样个数 * 通道数
data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;
下面是完整的代码
1 | #include <stdio.h><br>#include < assert .h><br> <br>#include <SDL2/SDL.h><br> <br>#include <libavcodec/avcodec.h><br>#include <libavformat/avformat.h><br>#include <libswscale/swscale.h><br>#include <libswresample/swresample.h><br>#include <libavutil/samplefmt.h><br> <br>#define SDL_AUDIO_BUFFER_SIZE 1024 <br>#define MAX_AUDIO_FRAME_SIZE 192000 <br>typedef struct PacketQueue<br>{<br> AVPacketList *first_pkt, *last_pkt;<br> int nb_packets;<br> int size;<br> SDL_mutex *mutex;<br> SDL_cond *cond;<br>} PacketQueue;<br> <br>PacketQueue audioq;<br> int quit = 0 ;<br>struct SwrContext *audio_convert_ctx = NULL;<br> static Uint8 out_channel = 2 ;<br> static SDL_AudioFormat out_format = AV_SAMPLE_FMT_S16;<br> static int out_nb_samples = 0 ;<br> static int sample_rate = 48000 ;<br> <br> <br> void audio_callback( void *userdata, Uint8 *stream, int len);<br> void packet_queue_init(PacketQueue *q);<br> int packet_queue_put(PacketQueue *q, AVPacket *pkt);<br> int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block);<br> int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size);<br> <br> int main( int argc, char *argv[])<br>{<br> int ret = 0 ;<br> AVFormatContext *fmt_ctx = NULL;<br> char *in_file;<br> int video_stream_index = - 1 ;<br> int audio_stream_index = - 1 ;<br> AVCodec *video_codec = NULL, *audio_codec = NULL;<br> AVCodecParameters *audio_codecpar = NULL, *video_codecpar = NULL;<br> AVCodecContext *audio_codec_ctx = NULL, *video_codec_ctx = NULL;<br> SDL_AudioSpec wanted_spec, src_spec;<br> int64_t in_channel_layout;<br> int64_t out_channel_layout;<br> AVPacket packet;<br> SDL_Event event;<br> av_log_set_level(AV_LOG_INFO);<br> <br> if (argc < 2 )<br> {<br> av_log(NULL, AV_LOG_ERROR, "need infile\n" );<br> return - 1 ;<br> }<br> in_file = argv[ 1 ];<br> //初始化SDL<br> ret = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER);<br> if (ret != 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "sdl init error\n");<br> return ret;<br> }<br> <br> //获取输入文件上下文<br> ret = avformat_open_input(&fmt_ctx,<br> in_file,<br> NULL,<br> NULL);<br> if (!fmt_ctx)<br> {<br> av_log(NULL, AV_LOG_ERROR, "open input file fail\n");<br> goto __FAIL;<br> }<br> <br> //获取视频流索引和音频流索引<br> ret = avformat_find_stream_info(fmt_ctx, NULL);<br> if (ret < 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "find stream fail\n");<br> goto __FAIL;<br> }<br> av_dump_format(fmt_ctx, 0, in_file, 0);<br> video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, -1);<br> audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &audio_codec, -1);<br> if (video_stream_index < 0 || audio_stream_index < 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "find audio or video fail\n");<br> goto __FAIL;<br> }<br> if (!audio_codec || !video_codec)<br> {<br> ret = -1;<br> av_log(NULL, AV_LOG_ERROR, "find audio codec or video codec fail");<br> goto __FAIL;<br> }<br> //获取解码器上下文并打开解码器<br> audio_codecpar = fmt_ctx->streams[audio_stream_index]->codecpar;<br> out_nb_samples = audio_codecpar->frame_size;<br> <br> video_codecpar = fmt_ctx->streams[video_stream_index]->codecpar;<br> <br> audio_codec_ctx = avcodec_alloc_context3(audio_codec);<br> video_codec_ctx = avcodec_alloc_context3(video_codec);<br> <br> avcodec_parameters_to_context(audio_codec_ctx, audio_codecpar);<br> avcodec_parameters_to_context(video_codec_ctx, video_codecpar);<br> <br> ret = avcodec_open2(audio_codec_ctx, audio_codec, NULL);<br> if (ret < 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "open audio codec fail\n");<br> goto __FAIL;<br> }<br> <br> ret = avcodec_open2(video_codec_ctx, video_codec, NULL);<br> if (ret < 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "open video codec fail\n");<br> goto __FAIL;<br> }<br> <br> //设置SDL_音频参数<br> wanted_spec.freq = sample_rate; //采样率<br> wanted_spec.format = AUDIO_S16LSB; //采样大小<br> wanted_spec.channels = out_channel; //声道数<br> wanted_spec.silence = 0;<br> wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; //采样个数<br> wanted_spec.callback = audio_callback;<br> wanted_spec.userdata = audio_codec_ctx;<br> <br> ret = SDL_OpenAudio(&wanted_spec, &src_spec);<br> if (ret < 0)<br> {<br> av_log(NULL, AV_LOG_ERROR, "open audio device fail\n");<br> goto __FAIL;<br> }<br> <br> //设置重采样<br> in_channel_layout = audio_codecpar->channel_layout;<br> out_channel_layout = av_get_default_channel_layout(out_channel);<br> audio_convert_ctx = swr_alloc();<br> if (!audio_convert_ctx)<br> {<br> ret = -1;<br> av_log(NULL, AV_LOG_ERROR, "alloc swr fail\n");<br> goto __FAIL;<br> }<br> <br> swr_alloc_set_opts(audio_convert_ctx,<br> out_channel_layout,<br> out_format,<br> sample_rate,<br> in_channel_layout,<br> audio_codecpar->format,<br> audio_codecpar->sample_rate,<br> 0,<br> NULL);<br> <br> swr_init(audio_convert_ctx);<br> SDL_PauseAudio(0);<br> <br> packet_queue_init(&audioq);<br> while (av_read_frame(fmt_ctx, &packet) >= 0)<br> {<br> if (packet.stream_index == audio_stream_index)<br> {<br> packet_queue_put(&audioq, &packet);<br> }<br> else<br> {<br> av_packet_unref(&packet);<br> }<br> SDL_PollEvent(&event);<br> switch (event.type)<br> {<br> case SDL_QUIT:<br> quit = 1;<br> goto __FAIL;<br> break;<br> default:<br> break;<br> }<br> }<br> while (1)<br> {<br> SDL_WaitEvent(&event);<br> switch (event.type)<br> {<br> case SDL_QUIT:<br> quit = 1;<br> goto __FAIL;<br> break;<br> default:<br> break;<br> }<br> }<br> <br>__FAIL:<br> SDL_CloseAudio();<br> SDL_Quit();<br> if (audio_codecpar)<br> {<br> avcodec_parameters_free(&audio_codecpar);<br> }<br> <br> if (video_codecpar)<br> {<br> avcodec_parameters_free(&video_codecpar);<br> }<br> <br> if (video_codec_ctx)<br> {<br> avcodec_close(video_codec_ctx);<br> avcodec_free_context(&video_codec_ctx);<br> }<br> <br> if (audio_codec_ctx)<br> {<br> avcodec_close(audio_codec_ctx);<br> avcodec_free_context(&audio_codec_ctx);<br> }<br> if (fmt_ctx)<br> {<br> avformat_close_input(&fmt_ctx);<br> avformat_free_context(fmt_ctx);<br> }<br> <br> return ret;<br>}<br> <br>void audio_callback(void *userdata, Uint8 *stream, int len)<br>{<br> <br> AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;<br> int len1, audio_size;<br> <br> static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];<br> static unsigned int audio_buf_size = 0;<br> static unsigned int audio_buf_index = 0;<br> <br> while (len > 0)<br> {<br> if (audio_buf_index >= audio_buf_size)<br> {<br> /* We have already sent all our data; get more */<br> audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));<br> if (audio_size < 0)<br> {<br> /* If error, output silence */<br> audio_buf_size = 1024; // arbitrary?<br> memset(audio_buf, 0, audio_buf_size);<br> }<br> else<br> {<br> audio_buf_size = audio_size;<br> }<br> audio_buf_index = 0;<br> }<br> len1 = audio_buf_size - audio_buf_index;<br> if (len1 > len)<br> len1 = len;<br> fprintf(stderr, "index=%d, len1=%d, len=%d\n",<br> audio_buf_index,<br> len,<br> len1);<br> memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);<br> len -= len1;<br> stream += len1;<br> audio_buf_index += len1;<br> }<br>}<br> <br>void packet_queue_init(PacketQueue *q)<br>{<br> memset(q, 0, sizeof(PacketQueue));<br> q->mutex = SDL_CreateMutex();<br> q->cond = SDL_CreateCond();<br>}<br> <br>int packet_queue_put(PacketQueue *q, AVPacket *pkt)<br>{<br> <br> AVPacketList *pkt1;<br> AVPacket dst_pkt;<br> av_init_packet(&dst_pkt);<br> if (av_packet_ref(&dst_pkt, pkt) < 0)<br> {<br> return -1;<br> }<br> pkt1 = av_malloc(sizeof(AVPacketList));<br> if (!pkt1)<br> return -1;<br> pkt1->pkt = dst_pkt;<br> pkt1->next = NULL;<br> <br> SDL_LockMutex(q->mutex);<br> <br> if (!q->last_pkt)<br> {<br> q->first_pkt = pkt1;<br> }<br> else<br> {<br> q->last_pkt->next = pkt1;<br> }<br> <br> q->last_pkt = pkt1;<br> q->nb_packets++;<br> q->size += pkt1->pkt.size;<br> SDL_CondSignal(q->cond);<br> <br> SDL_UnlockMutex(q->mutex);<br> return 0;<br>}<br> <br>int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)<br>{<br> AVPacketList *pkt1;<br> int ret;<br> <br> SDL_LockMutex(q->mutex);<br> <br> for (;;)<br> {<br> <br> if (quit)<br> {<br> ret = -1;<br> break;<br> }<br> <br> pkt1 = q->first_pkt;<br> if (pkt1)<br> {<br> q->first_pkt = pkt1->next;<br> if (!q->first_pkt)<br> q->last_pkt = NULL;<br> q->nb_packets--;<br> q->size -= pkt1->pkt.size;<br> *pkt = pkt1->pkt;<br> av_free(pkt1);<br> ret = 1;<br> break;<br> }<br> else if (!block)<br> {<br> ret = 0;<br> break;<br> }<br> else<br> {<br> SDL_CondWait(q->cond, q->mutex);<br> }<br> }<br> SDL_UnlockMutex(q->mutex);<br> return ret;<br>}<br> <br>int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)<br>{<br> <br> static AVPacket pkt;<br> static uint8_t *audio_pkt_data = NULL;<br> static int audio_pkt_size = 0;<br> static AVFrame frame;<br> av_frame_get_buffer(&frame, 0);<br> int len1, data_size = 0;<br> int ret = 0;<br> int is_no_pkt = 0;<br> <br> for (;;)<br> {<br> <br> if (pkt.data)<br> av_packet_unref(&pkt);<br> <br> if (quit)<br> {<br> return -1;<br> }<br> <br> if (packet_queue_get(&audioq, &pkt, 1) < 0)<br> {<br> ret = avcodec_send_packet(aCodecCtx, NULL);<br> if (ret < 0)<br> {<br> return -1;<br> }<br> is_no_pkt = 1;<br> goto __RECEIVE;<br> }<br> <br> ret = avcodec_send_packet(aCodecCtx, &pkt);<br> if (ret < 0)<br> {<br> ret = -1;<br> printf("decode error");<br> av_packet_unref(&pkt);<br> return -1;<br> }<br> __RECEIVE:<br> if (is_no_pkt)<br> {<br> return -1;<br> }<br> else if (pkt.data)<br> {<br> av_packet_unref(&pkt);<br> }<br> ret = avcodec_receive_frame(aCodecCtx, &frame);<br> if (ret < 0)<br> {<br> continue;<br> }<br> <br> data_size = av_get_bytes_per_sample(out_format) * out_channel * SDL_AUDIO_BUFFER_SIZE;<br> <br> swr_convert(audio_convert_ctx,<br> &audio_buf,<br> SDL_AUDIO_BUFFER_SIZE,<br> (const uint8_t **)frame.data,<br> frame.nb_samples);<br> return data_size;<br> }<br>} |
以上就是 聊天平台源码,聊天平台如何获取到音频流的相关代码,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现