Browse Source

修复AudioPlayerAlsaImpl最后一帧音频播放不出来的bug

master
zhaohe 2 years ago
parent
commit
3035a2cca0
  1. 13
      src/iflytop/components/alsaplayer/AudioPlayerAlsaImpl.cpp

13
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 {

Loading…
Cancel
Save