1

opus オーディオ コーデックの Web ブラウザー サポートは、通常、opus でエンコードされたファイル全体をブラウザーに配信することによって利用されます。これは、たとえば、firefox や chrome で機能することが知られています。私のシナリオは、opus パケットをサーバーからブラウザーにストリーミングするという点で異なります。Linux サーバーでは、opus_encode_float でオーディオをエンコードします。WebSocket を介して Web ブラウザ クライアントに配信されます。ブラウザーでは、Web Audio API の decodeAudioData を使用して、同じデータをデコードしようとします。Firefox と chrome では null 例外で失敗します。

私には、これはうまくいくはずです. ブラウザでのopus実装のステータスを教えてくれる人、または私が間違っていることを教えてくれる人はいますか? 前もって感謝します。

// .h
#define OPUS_FRAME_SIZE 1920
#define MAX_FRAME_SIZE 6*OPUS_FRAME_SIZE
#define MAX_PACKET_SIZE (4*OPUS_FRAME_SIZE)

class OpusEncoder;  // forward declaration as we don't want to include opus.h here
class xxx  {
    OpusEncoder *encoder;   // Holds the state of the opus encoder
    unsigned char opusOutBuf[MAX_PACKET_SIZE];
}

// .cpp
#include <opus.h>
#define CHANNELS 2
#define SAMPLE_RATE 48000
#define APPLICATION OPUS_APPLICATION_AUDIO
#define BITRATE 64000

// one time code
// Create a new encoder state
int err;
encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err);
if (err<0)
{
    LogIt(prError) << "Failed to create an Opus encoder: " << opus_strerror(err);
    return;
}
err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
if (err<0)
{
    LogIt(prError) << "Opus failed to set bitrate: " << opus_strerror(err);
    return ;
}


// per packet code
int nbBytes = opus_encode_float(encoder, IncomingAudio, OPUS_FRAME_SIZE, opusOutBuf, MAX_PACKET_SIZE);
if (nbBytes < 0)
{
    LogIt(prError) << "Opus encode failed: " << opus_strerror(nbBytes);
    return;
}


// Client side javascript
// OpusEncodedArrayBuffer is an unmodified binary packet that 
// arrived via websocket onmessage(evt); it is evt.data
window.context.decodeAudioData(OpusEncodedArrayBuffer, function(buffer) { // use "classic" callback
    if (buffer) {   // I would LIKE to arrive here, but so far no joy.
       // ...
    }
},
function(e){
    if (e) {
        tell("error in decodeAudioData: " + e)  // I haven't seen this case yet
    } else {          // This ALWAYS happens, using latest firefox or chrome
        tell("error in decodeAudioData"); // webaudio api spec says decodeAudioData does not return an object error
    }
});
4

1 に答える 1