Web 浏览器对 opus 音频编解码器的支持通常通过将整个 opus 编码文件传送到浏览器来使用,例如,众所周知,这适用于 firefox 和 chrome。我的场景不同,因为我将 opus 数据包从服务器流式传输到浏览器。在 linux 服务器上,我使用 opus_encode_float 对音频进行编码。它通过 WebSocket 传递给 Web 浏览器客户端。在浏览器中,我使用 Web Audio API 的 decodeAudioData 来尝试解码相同的数据。它在 Firefox 和 chrome 中失败,出现空异常。
在我看来,这应该可行,如果不是去年,那么现在就真的很快了。谁能告诉我浏览器中 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
}
});