Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opus_decode returning corrupted stream

I am decoding ogg opus data which is streaming live from icecast server. I am using libopus to decode. Data gets decoded sometimes but most of the times op_decode() returns -4 which indicates corrupted stream. This is the callback function used to access data using curl library.

#define SAMPLE_RATE 48000
#define CHANNELS 2
#define MAX_FRAME_SIZE 6*960
size_t play_stream(void *buffer, size_t size, size_t nmemb, void *userp)
{
    FILE *fp;
    opus_int16 out[MAX_FRAME_SIZE * CHANNELS];
    int error;
    int i;
    unsigned char pcm_bytes[MAX_FRAME_SIZE * CHANNELS * 2];
    int frame_size;

    frame_size = opus_decode(decoder, (unsigned char*)buffer, (opus_int32)size * nmemb, out, MAX_FRAME_SIZE, 0);

    if (frame_size < 0)
    {
        fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));

    }


    return size * nmemb;
}

Can someone please help me out?

like image 385
Saju Alexander Avatar asked May 30 '26 16:05

Saju Alexander


1 Answers

The opus_decode() function decodes an Opus packet, not an Ogg Opus stream. You could use the Ogg library to get the packets out of the stream and then libopus to decode the packets, but an easier way to is to use the opusfile library. Opusfile can even read the stream directly from the network.

like image 78
mark4o Avatar answered Jun 02 '26 07:06

mark4o