【发布时间】:2021-07-28 18:05:45
【问题描述】:
我正在尝试将音频实时流式传输到 Twilio 会议,但不知道如何使其工作。
到目前为止,我已经创建了一个 Twilio 会议,当它开始时,它将开始流式传输并将数据发送到服务器以供 WebSocket 接收。现在我收到来自 Twilio 的以音频/x-mulaw 编码的媒体消息,但不清楚下一步我应该做什么。我看到了一个示例,它联系这些消息的有效负载并将它们转换为缓冲区,以重复它接收到的音频。但我正在尝试实时流式传输这些数据,所以我不能真正做到这一点,因为我不知道音频什么时候会停止。那么有人可以向我解释一下我应该如何处理直播中的音频/x-mulaw?非常感谢。 :)
const express = require("express");
const app = express();
const http = require("http");
const WebSocket = require("ws");
const cors = require("cors");
const VoiceResponse = require("twilio").twiml.VoiceResponse;
const port = process.env.PORT || 8000;
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.urlencoded({ extended: true }));
app.use(cors());
wss.on("connection", function connection(ws) {
ws.on("message", (message) => {
message = JSON.parse(message);
switch (message.event) {
case "connected":
case "start":
// here comes the messages from all the conferences that are happening
// each message will have a callSid on start
// mediaFormat: { encoding: 'audio/x-mulaw', sampleRate: 8000, channels:1 }
console.log("callSid: ", message);
case "end":
break;
case "media":
// here messges will have the streamSid and a payload
console.log(message);
default:
break;
}
});
});
app.get(`/audio-room/conference`, (request, response) => {
const roomId = request.query["room_id"];
const voice = new VoiceResponse();
if (roomId) {
voice.start().stream({ url: "wss://4dbeaa4a0890.ngrok.io/" });
voice.dial().conference(
{
muted: true,
statusCallback: `https://4dbeaa4a0890.ngrok.io/audio-room/conference-callback?room-id=${roomId}`,
statusCallbackEvent: "start end join speaker mute",
statusCallbackMethod: "GET",
region: "us1",
waitUrl: "",
beep: false,
FriendlyName: roomId,
},
roomId.toString()
);
} else {
voice.say(
{
voice: "man",
language: "en-US",
},
`Please add room identity to your connection parameters`
);
}
response.send(voice.toString());
});
server.listen(port, function () {
console.log(`Server is listening on ${port}!`);
});
【问题讨论】:
-
考虑编辑您的问题以包含您迄今为止创建的适用代码。
标签: javascript websocket twilio live-streaming