C尾指针问题中的环形缓冲区(用于音频流)

时间:2012-05-15 18:19:45

标签: c audio-streaming

我目前正在编写一个用于音频流的嵌入式应用程序。嵌入式应用程序将接收通过wifi发送的音频数据包,缓冲数据包,然后将音频数据发送到解码器芯片。我编写了一个环形缓冲区实现(在stackoverflow上的一篇优秀文章的帮助下),但有时会得到一些奇怪的行为。在音频方面,我听到在播放过程中歌曲的某些部分会重复出现。我发现这是由于尾指针被设置为缓冲区的开头两次。

(在我的实现中,头指针标记有效数据的结束,而尾指针标记有效数据的开头)

例如,我看到了:

  • 头指针重置为缓冲区的开始
  • 尾部指针重置为缓冲区的开始
  • 尾部指针重置为缓冲区的开始< - 这是我听到音频重复的地方
  • 头指针重置为缓冲区的开始

这是环形缓冲区实现:

typedef struct ring_buffer
{
    UINT8 *buffer;      /* data buffer */
    UINT8 *buffer_end;  /* end of data buffer */
    size_t capacity;    /* maximum number of mp3Bytes in the buffer */
    size_t count;       /* number of mp3Bytes in the buffer */
    size_t typesize;    /* size of each mp3Byte in the buffer */
    UINT8 *head;        /* ring buffer head pointer */
    UINT8 *tail;        /* ring buffer tail pointer */
} ring_buffer;

PUBLIC UINT8
AppAudioStream_RingBufInit(ring_buffer *rb, size_t capacity, size_t typesize)
{   
    /* alloc buffer of size capacity * typesize */
    rb->buffer = malloc(capacity * typesize);
    if(rb->buffer == NULL)
    {
        printf("ring buffer init fail\r\n");
        return RING_BUF_INIT_FAIL;
    }

    /* init rb buffer to 0 */
    memset(rb->buffer, 0, capacity * typesize);

    /* rb struct element init */
    rb->capacity = capacity;
    rb->buffer_end = rb->buffer + capacity * typesize;
    rb->count = 0;
    rb->typesize = typesize;
    rb->head = rb->buffer;
    rb->tail = rb->buffer;

    return RING_BUF_INIT_DONE;
}

PUBLIC VOID
AppAudioStream_RingBufWrite(ring_buffer *rb, UINT8 *mp3Byte)
{   
    /* default: allow overwriting if ring buffer is full */
    memcpy(rb->head, mp3Byte, rb->typesize);
    rb->head = rb->head + rb->typesize;
    if(rb->head == rb->buffer_end) {
        printf("head back to start\r\n");
        rb->head = rb->buffer;
    }

    if(rb->count == rb->capacity) {
        printf("buffer full\r\n");
        if (rb->head > rb->tail)
            rb->tail = rb->tail + rb->typesize;
    } else { 
        rb->count++;
    }
}

PUBLIC VOID
AppAudioStream_RingBufRead(ring_buffer *rb, UINT8 *mp3Byte)
{
    /* insert 'comfort noise' if the ring buffer is empty */
    if(rb->count == 0){
        printf("buffer empty\r\n");
        *mp3Byte = NOISE_BYTE;
    } else {
        /* copy data to mp3Byte and increase tail pointer */
        memcpy(mp3Byte, rb->tail, rb->typesize);
        rb->tail = rb->tail + rb->typesize;
        if(rb->tail == rb->buffer_end) {
            printf("TAIL back to start\r\n");
            printf("Tbuffer count: %i\r\n", rb->count);
            rb->tail = rb->buffer;
        }   
        rb->count--;
    }
}

以下是调用环形缓冲区写入函数的方法:

while (1)
{
    AppAudioStream_BufRecv(sd, dataLen, &addr);
}

PUBLIC VOID 
AppAudioStream_BufRecv(int sd, INT32 dataLen, struct sockaddr_in *addr)
{
    INT32 addrlen = sizeof(struct sockaddr_in);
    UINT8 j, i = 0;
    UINT8 *audioByte;

    /* listen to incoming audio data packets */
    dataLen = recvfrom(sd, (char *) appRxBuf, sizeof(appRxBuf), 0, 
                       (struct sockaddr *)&addr, &addrlen);

    /* set pointer to first element in recieve buffer */
    audioByte = appRxBuf;

    /* buffer received packets into FIFO */
    while (dataLen > 0)
    {
        /* write 1 byte into audio FIFO */
        AppAudioStream_RingBufWrite(&audioFIFO, audioByte);

        /* increase pointer index and update # of bytes left to write */
        audioByte++;
        dataLen--;
    }

    /* wait until buffer is 2/3 full to start decoding */
    if (audioFIFO.count >= FIFO_TWO_THIRD_FULL 
        && audioStreamStatus == GSN_STREAM_BUFFERING) {
        audioStreamStatus = GSN_STREAM_START; 
        //printf("stream start\r\n");
    }
}

在每2 ms发生一次回调中调用环形缓冲区读取函数(这基本上是一个ISR):

PRIVATE VOID 
AppAudioStream_DecoderCb(UINT32* pDummy, UINT32 TimerHandle)
{
    UINT8 spiWriteCount = 0;
    UINT8 mp3Byte;
    int i = 0;
    GSN_SPI_NUM_T spiPortNumber = GSN_SPI_NUM_0;

    /* read 32 bytes of data from FIFO and write to SPI */
    while (spiWriteCount < DATA_WRITE_AMT)
    {
        /* set stream status to decoding */
        audioStreamStatus = GSN_STREAM_DECODING;

        /* read 1 byte of audio data from FIFO */
        AppAudioStream_RingBufRead(&audioFIFO, &mp3Byte);

        /* write 1 byte of audio data out to VS1053 */
        AppSpi_SdiByteWrite(spiPortNumber, &mp3Byte);

        /* increase byte written count */
        spiWriteCount++; 
    }
}

非常感谢任何帮助/见解。我相信我现在只是忽略了一些非常明显的东西。

谢谢!

1 个答案:

答案 0 :(得分:3)

如果你从回调中读取这个环形缓冲区并从其他地方写入它,那么显然你会得到各种奇怪的错误,因为环形缓冲区缺乏保护。您需要添加互斥锁,信号量,中断禁用或适用于您的特定系统的任何内容。

此外,回调和应用程序其余部分之间共享的所有变量都应声明为volatile,以防止出现危险的优化程序错误(*)。除此之外,这些变量可能需要使用互斥锁进行保护,以防止竞争条件。

如果上述任何一个错误是原因,那么压力测试的一个好方法是创建一个多线程的应用程序。创建10个写入缓冲区的线程,以及10个读取和打印的线程。记录输出,看看你是否得到奇数值或垃圾打印。


(*)(这句话没有与线程安全有关,所以请不要发表关于volatile不足以保证线程安全的评论,因为我从来没有说过,我再也不会进行那场愚蠢的辩论了。)