通常通过将整个经opus编码的文件递送到浏览器来利用Web浏览器对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
}
});发布于 2015-06-20 05:43:37
Chrome的decodeAudioData函数不支持opus。在这个问题上有一个开放的bug。
https://stackoverflow.com/questions/30925174
复制相似问题