From 3035a2cca0719ace1f3b968714fe519d1ef62c64 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Tue, 12 Sep 2023 20:11:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DAudioPlayerAlsaImpl=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E5=B8=A7=E9=9F=B3=E9=A2=91=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E4=B8=8D=E5=87=BA=E6=9D=A5=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp b/src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp index c2ef1d4..fa3ceeb 100644 --- a/src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp +++ b/src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp @@ -167,7 +167,7 @@ void *AudioPlayerAlsaImpl::decodeFunc(void *arg) { player->m_formatCtx = avformat_alloc_context(); player->m_formatCtx->interrupt_callback.callback = interruptCallback; player->m_formatCtx->interrupt_callback.opaque = player; - open_ret = avformat_open_input(&player->m_formatCtx, player->m_url.c_str(), NULL, NULL); + open_ret = avformat_open_input(&player->m_formatCtx, player->m_url.c_str(), NULL, NULL); if (open_ret == 0) { l_open_success_flag = true; break; @@ -262,9 +262,7 @@ void *AudioPlayerAlsaImpl::decodeFunc(void *arg) { int64_t in_channel_layout = av_get_default_channel_layout(player->m_codecCtx->channels); struct SwrContext *convert_ctx = swr_alloc(); - convert_ctx = - swr_alloc_set_opts(convert_ctx, out_channel_layout, out_sample_fmt, OUTPUT_SAMPLE_RATE, in_channel_layout, - player->m_codecCtx->sample_fmt, player->m_codecCtx->sample_rate, 0, NULL); + convert_ctx = swr_alloc_set_opts(convert_ctx, out_channel_layout, out_sample_fmt, OUTPUT_SAMPLE_RATE, in_channel_layout, player->m_codecCtx->sample_fmt, player->m_codecCtx->sample_rate, 0, NULL); swr_init(convert_ctx); if (player->m_seekable && player->m_progressMs != 0) { // seek to position @@ -306,9 +304,9 @@ void *AudioPlayerAlsaImpl::decodeFunc(void *arg) { if (error_code == 0) { uint8_t *target_ptr = player->m_pcmBuffer + pos; memset(player->m_frameBuffer, 0, RESAMPLE_OUTPUT_BUFFER_SIZE); - int len = swr_convert(convert_ctx, &player->m_frameBuffer, RESAMPLE_OUTPUT_BUFFER_SIZE, - (const uint8_t **)frame->data, frame->nb_samples); + int len = swr_convert(convert_ctx, &player->m_frameBuffer, RESAMPLE_OUTPUT_BUFFER_SIZE, (const uint8_t **)frame->data, frame->nb_samples); unsigned int resample_buffer_size = len * out_channels * bytes_per_sample; + if (pos + resample_buffer_size >= PCM_PLAYBUFFER_SIZE) { int offset_ms = (int)(frame->pts * av_q2d(a_stream->time_base) * 1000); pcm_pool->pushPcmChunk(player->m_pcmBuffer, pos, offset_ms); @@ -321,6 +319,7 @@ void *AudioPlayerAlsaImpl::decodeFunc(void *arg) { memcpy(target_ptr, player->m_frameBuffer, resample_buffer_size); pos += resample_buffer_size; } + } else { if (error_code == AVERROR_EOF) { av_frame_unref(frame); @@ -355,6 +354,8 @@ void *AudioPlayerAlsaImpl::decodeFunc(void *arg) { break; } } + int offset_ms = (int)(frame->pts * av_q2d(a_stream->time_base) * 1000); + pcm_pool->pushPcmChunk(player->m_pcmBuffer, pos, offset_ms); logger->info("==decode finish."); break; } else {