简体   繁体   中英

Live Stream Audio Conferences (Nodejs)

I'm trying to implement audio live streaming to Twilio conferences but not sure how to make it work.

So far, I have created a Twilio conference and when it starts it will start streaming and send the data to a server for a WebSocket to receive. Right now I receive media messages from Twilio encoded in audio/x-mulaw but it's not clear what I should do next. I saw an example that contacts the payloads of these messages and converts them to buffers to like repeat the audio that it receives. But I'm trying to live stream that data so I can't really do that because I don't know when the audio will stop. So could someone please explain to me how should I handle that audio/x-mulaw in live streaming? Thanks so much. :)

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}!`);
});

Your code sample helped me with a different syntax search.

In the twilio blog sample app: https://www.twilio.com/blog/live-transcribing-phone-calls-using-twilio-media-streams-and-google-speech-text

That sample code has case statements in the web socket for 'connection', 'start' and 'stop'. Maybe this helps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM