首页 > 解决方案 > 直播音频会议(Nodejs)

问题描述

我正在尝试将音频实时流式传输到 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}!`);
});

标签: javascriptwebsockettwiliolive-streaming

解决方案


您的代码示例帮助我进行了不同的语法搜索。

在 twilio 博客示例应用程序中:https ://www.twilio.com/blog/live-trancribing-phone-calls-using-twilio-media-streams-and-google-speech-text

该示例代码在 Web 套接字中包含用于“连接”、“开始”和“停止”的 case 语句。也许这有帮助?


推荐阅读