Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot play on a call not established yet #870

Open
seven1240 opened this issue Sep 3, 2023 · 2 comments
Open

cannot play on a call not established yet #870

seven1240 opened this issue Sep 3, 2023 · 2 comments

Comments

@seven1240
Copy link

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:[email protected]",
      to: "sip:[email protected]: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:[email protected] sip:[email protected]
Connecting to peer DeviceBuilder {
  _devices: [
    [
      {
        type: 'sip',
        from: 'sip:[email protected]',
        to: 'sip:[email protected]: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
Copy link
Author

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
Copy link
Contributor

edolix commented Sep 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants