Discussions

Ask a Question
Back to All

Need help with Streaming API

Hi,

I’ve been working on creating a streaming API on a website. For the API requests, I’m using PHP curl, and for establishing the RTCPeerConnection, I’m using JavaScript. Here’s the flow I’ve implemented:

The user clicks a button, which sends an API request to create a new session (streaming.new API).
Once the session is created, I initiate the RTCPeerConnection to gather candidate information.
With this candidate information, I submit the ICE details, which returns a success status, indicating that the submission was successful.
After this, I send a "Start session" API request.
However, this final request results in an error, and I’m not sure why this is happening.
{
"code": 10002,
"message": "session state wrong: connecting"
}
Any insights into why this error might be occurring with the RTCPeerConnection setup would be appreciated.

function create_RTCPeerConnection(argument , api_key) {
const sdpType = argument.sdp_type;
const sdpData = argument.sdp_data;
const ice_servers = argument.iceServers;

 if(peerConnection != null){
 	const peerConnection = new RTCPeerConnection({ iceServers: ice_servers , iceTransportPolicy: 'all'});
 	const remoteDescription = new RTCSessionDescription({
	    type: sdpType,
	    sdp: sdpData
	});
 }

 let iceCandidates = [];

	peerConnection.onicecandidate = (event) => {
        if (event.candidate) {
            // Collect ICE candidates in a format that can be serialized
            iceCandidates.push({
                candidate: event.candidate.candidate,
                sdpMid: event.candidate.sdpMid,
                sdpMLineIndex: event.candidate.sdpMLineIndex,
                foundation: event.candidate.foundation,
                component: event.candidate.component
            });
							start_session_in_heygen(api_key, argument, iceCandidates);
       
    };
peerConnection.setRemoteDescription(remoteDescription)
		    .then(() => {
		      
		        return peerConnection.createAnswer(); 
		    })
		    .then((answer) => {
		       
		        return peerConnection.setLocalDescription(answer);
		    })
		    .then(() => {
		        console.log('Local description set. ICE gathering should start.');
		        console.log(peerConnection);
		    })
		    .catch((error) => {
		        console.error('Error in setting up peer connection:', error);
		    });

	


}