1

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

我编写了一些代码来读取由另一个线程填充的缓冲区。缓冲区充满了由 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 )
4

1 回答 1

2

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

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

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

于 2016-03-24T21:07:17.797 回答