gpt4 book ai didi

c++ - 解码音频后调用阻塞 snd_pcm_writei 会导致奇怪的播放吗?

转载 作者:行者123 更新时间:2023-11-30 03:43:19 27 4
gpt4 key购买 nike

如果我的问题标题很糟糕,请原谅我。我的妻子总是告诉我,我不擅长措辞。

我编写了一些代码来读取由另一个线程填充的缓冲区。缓冲区中充满了由 opus 编解码器编码的音频数据。每次 20 毫秒从远程端接收 VoIP 数据。为了尽可能快地播放音频,在一个循环中,我一次从缓冲区中取出 20 毫秒的数据,然后对其进行解码,然后直接将其发送到 snd_pcm_writei 上播放。

我在 Google 上四处寻找有关将 snd_pcm_writei 与先前编码的音频一起使用的一些示例,以了解其他人是如何做到的。我运气不太好。

我的想法是,如果我在等待互斥量并等待编码,我无法在逻辑上看到音频“流畅”。我想在每 20 毫秒的帧之间会有时间间隔,没有音频被发送到扬声器。我的怀疑是否正确,这可能会产生不完美的音频?

我的相关代码:

while( true )
{
// We need a positive lock
if( !buffer_lock )
buffer_lock.lock();

LOG_DEBUG( *logger_ ) << "After the mutex lock.";
LOG_DEBUG( *logger_ ) << "Buffer size: " << current_audio->buffer_size_;
LOG_DEBUG( *logger_ ) << "Read pointer: " << current_audio->read_pointer_;

opus_int32 payload_size;

LOG_DEBUG( *logger_ ) << "calling audioCanDecodeChunk()";

// Now fisticuffs do we have enouffs?
if( audioCanDecodeChunk( current_audio, payload_size ) )
{
LOG_DEBUG( *logger_ ) << "We have enough current_audio buffer.";

// Are we dank?
if( payload_size<0 or payload_size>MAX_PACKET )
{
LOG_ERROR( *logger_ ) << "Decoding error, payload size (" << payload_size << ") is outsize range.";
break; // Terminal
}

// We have enough!
// Advance the read pointer
current_audio->read_pointer_+= 4;

// Copy it out
memcpy( payload_buffer, current_audio->buffer_+current_audio->read_pointer_, payload_size );

// Release it
buffer_lock.unlock();

// Now thingify it
int samples_decoded = opus_decode( opus_decoder_,
(const unsigned char *)payload_buffer,
payload_size,
(opus_int16 *)pcm_buffer,
MAX_FRAME_SIZE,
0 );

// How did we do?
if( samples_decoded<0 )
{
// What hap?
LOG_ERROR( *logger_ ) << "Error decoding samples: " << opus_strerror( samples_decoded );
break;
}
else
{
// Now we have our PCM!
int bytes_decoded = current_audio->recording_.channels*sizeof( opus_int16 )*samples_decoded;

LOG_DEBUG( *logger_ ) << "We have decoded " << bytes_decoded << " bytes payload: " << payload_size;

// Now write
if( (error = snd_pcm_writei( playback_handle_, pcm_buffer, samples_decoded ))!=samples_decoded )
{
LOG_ERROR( *logger_ ) << "snd_pcm_writei error: " << snd_strerror( error );
}
}

// Advance pointer
current_audio->read_pointer_+= payload_size;

} // If we don't have enough let it slide and unlock
else if( current_audio->done_ ) // Were we issued a flush?
{
LOG_DEBUG( *logger_ ) << "We are done.";

// We are done with this loop
break;
}
else
{
// Wait for it (an update)
LOG_DEBUG( *logger_ ) << "Before wait_buffer wait. Done: " << ( current_audio->done_ ? "true" : "false" ) <<
"Size: " << current_audio->buffer_size_
<< ", Read: " << current_audio->read_pointer_;
current_audio->wait_buffer_.wait( buffer_lock );
LOG_DEBUG( *logger_ ) << "After wait_buffer wait";
}

} // End while( true )

最佳答案

如果写入 20 毫秒 block 之间的时间正好是 20 毫秒,那么当您写入新 block 时设备的缓冲区将为空。即使是最小的延迟也会导致欠载。

为防止欠载,您必须使缓冲区尽可能满。这意味着在开始时,您必须在不等待 block 之间的情况下填充它。

当发送方的时钟运行速度快于设备时钟时,流最终会欠载。这可以通过测量时钟差异、更改发送方的传输速率或动态重新采样数据来避免。

关于c++ - 解码音频后调用阻塞 snd_pcm_writei 会导致奇怪的播放吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com