signalwire / signalwire-js

MIT License
18 stars 14 forks source link

cannot play on a call not established yet #870

Open seven1240 opened 1 year ago

seven1240 commented 1 year ago

sometimes I get an error when I immediately call play on a connected call. the call is successfully connected but looks like the state is not updated before I run play, and there's no function to wait for the state. Adding a sleep helps before play.

Error connecting call: Error: Can't call play() on a call not established yet.
    at /Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4254:16
    at new Promise (<anonymous>)
    at CallConsumer.play (/Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4252:12)
    at CallConsumer.playTTS (/Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4301:17)
    at EventEmitter.<anonymous> (/Users/seven/work/call-translator/js/connect-tts.js:27:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

js:

require('dotenv').config()
const { Voice } = require("@signalwire/realtime-api")
require("util").inspect.defaultOptions.depth = null;

const client = new Voice.Client({
  project: process.env.SIGNALWIRE_PROJECT_ID,
  token: process.env.SIGNALWIRE_TOKEN,
  contexts: ["home"],
});

console.log("Call Handler Started");

client.on("call.received", async (call) => {
  console.log("Got a new call", call.id, call.from, call.to);
  const plan = new Voice.DeviceBuilder().add(
    Voice.DeviceBuilder.Sip({
      from: "sip:seven@xyt-1.sip.signalwire.com",
      to: "sip:10000203@rts.xswitch.cn:20003;transport=tcp",
      timeout: 30,
    })
  );
  try {
    console.log("Connecting to peer", plan);
    const peer = await call.connect(plan);
    console.log("B", "Call connected! id =", peer.id);
    console.log("hello")
    let playback = await peer.playTTS({ text: 'hello', language: 'en-US' });
    await playback.ended();
    console.log("how are you")
    playback = await peer.playTTS({ text: 'how are you', language: 'en-US' });
    await playback.ended();
    console.log("wait for end")
    if (peer.active) {
      await peer.waitFor('ended')
      console.log("call ended")
    }
    if (peer.active) {
      console.log("hangup peer");
      await peer.hangup();  
    }  
  } catch (error) {
    console.error("Error connecting call:", error);
  }
  console.log("waiting b leg to end");
  await call.disconnected()
  console.log("waiting a leg to end");
  await call.waitFor('ended')
  console.log("done");
});

console.log("ready");

package.json

{
  "dependencies": {
    "@signalwire/realtime-api": "^3.10.3",
    "dotenv": "^16.3.1"
  }
}

full log:

Got a new call e5a25328-2548-432e-b70d-8a612318372c sip:+15551234567@154.8.164.96 sip:+10000777@xyt-cluecon.dapp.signalwire.com
Connecting to peer DeviceBuilder {
  _devices: [
    [
      {
        type: 'sip',
        from: 'sip:seven@xyt-1.sip.signalwire.com',
        to: 'sip:10000203@rts.xswitch.cn:20003;transport=tcp',
        timeout: 30
      }
    ]
  ]
}
B Call connected! id = undefined
hello
Error connecting call: Error: Can't call play() on a call not established yet.
    at /Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4254:16
    at new Promise (<anonymous>)
    at CallConsumer.play (/Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4252:12)
    at CallConsumer.playTTS (/Users/seven/work/call-translator/js/node_modules/@signalwire/realtime-api/dist/index.node.js:4301:17)
    at EventEmitter.<anonymous> (/Users/seven/work/call-translator/js/connect-tts.js:27:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
waiting b leg to end
waiting a leg to end
done
seven1240 commented 1 year ago

and the peer.id is not available

    const peer = await call.connect(plan);
    console.log("B", "Call connected! id =", peer.id);
B Call connected! id = undefined
edolix commented 1 year ago

Note: we're working on a new interface that will be released as a new major version that will address this (potential) race conditions.