簡體   English   中英

現場 Stream 音頻會議(Nodejs)

[英]Live Stream Audio Conferences (Nodejs)

我正在嘗試將音頻實時流式傳輸到 Twilio 會議,但不知道如何使其工作。

到目前為止,我已經創建了一個 Twilio 會議,當它開始時,它將開始流式傳輸並將數據發送到服務器以供 WebSocket 接收。 現在我收到來自 Twilio 的媒體消息,以音頻/x-mulaw 編碼,但不清楚下一步我應該做什么。 我看到了一個示例,它聯系這些消息的有效負載並將它們轉換為緩沖區,以重復接收到的音頻。 但是我正在嘗試使用 stream 的數據,所以我不能真正做到這一點,因為我不知道音頻什么時候會停止。 那么有人可以向我解釋一下我應該如何處理直播中的音頻/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}!`);
});

您的代碼示例幫助我進行了不同的語法搜索。

在 twilio 博客示例應用程序中: https://www.twilio.com/blog/live-trancribing-phone-calls-using-twilioech-media-phone-calls-using-twilioech-media-stream-and-

該示例代碼在 web 套接字中有用於“連接”、“開始”和“停止”的 case 語句。 也許這有幫助?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM