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

Will calling a blocking snd_pcm_writei after decoding audio cause weird playback?

如果我的问题标题很糟糕,请原谅我。我老婆老说我不擅长措辞。

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

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

我的想法是,如果我在等待互斥量并等待编码,我无法从逻辑上看到音频 "smooth." 我想在每 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 毫秒块之间的时间恰好为 20 毫秒,则在写入新块时设备的缓冲区将为空。即使是最小的延迟也会导致欠载。

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

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