如果我的问题标题很糟糕,请原谅我。我的妻子总是告诉我,我不擅长措词。
我编写了一些代码来读取由另一个线程填充的缓冲区。缓冲区填充有由opus编解码器编码的音频数据。一次20ms从远程端接收VoIP数据。为了尽可能快地播放音频,在循环中,我一次从缓冲区中取出20ms的数据,然后对其进行解码,然后直接发送到snd_pcm_writei上播放。
我在谷歌上环顾了一些关于使用snd_pcm_writei和以前编码的音频的例子,看看其他人是怎么做的。我运气不好。
我的想法是,如果我正在等待互斥锁并等待编码,我无法从逻辑上看到音频“流畅”。我想象在每个20ms帧之间存在时间间隔,其中没有音频被发送到扬声器。我的怀疑是否正确,这可能会产生不完美的音频?
我的相关代码:
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 )
答案 0 :(得分:2)
如果写入20毫秒块之间的时间恰好是20毫秒,那么当您编写新块时,设备的缓冲区将为空。即使最小的延迟也会导致欠载。
为防止下冲,您必须尽可能保持缓冲区充满。这意味着在开始时,您必须填写它而不必在两个块之间等待。
当发送方的时钟比设备的时钟运行得快时,流最终将会欠载。这可以通过测量时钟差异,以及更改发送方的传输速率或动态重新采样数据来避免。