Hello everyone, I’m trying to implement the client side logic and when I call the transport.produce() method it fails with error saying “connected” and so does the transport.on(“connect”) event. Even though I never connected any transports. Following is my code
Client-SIde
async function createSendTransport() {
let data;
socket.emit("create-transport", async (transportInfo) => {
console.log(transportInfo.iceCandidates);
const { id, iceParameters, iceCandidates, dtlsParameters } = transportInfo;
const transport = device.createSendTransport({
id,
iceParameters,
iceCandidates,
dtlsParameters,
});
console.log("Producer transport initialized:", transport);
transport.on("connect", async ({ dtlsParameters }, callback, errback) => {
console.log("Transport connect event triggered.", dtlsParameters);
socket.emit("connect-transport", { dtlsParameters }, (error) => {
if (error) {
console.error("Error connecting transport:", error);
return errback(error);
}
console.log("Transport connected successfully");
callback();
});
});
transport.on("connectionstatechange", async (state) => {
console.log("Transport connection state:", state);
if (state === "connected") {
console.log("Transport is connected. Ready to produce.");
} else if (state === "connecting") {
console.log("Connecting transport");
} else if (state === "failed") {
console.error("Transport connection failed.");
}
});
transport.on("produce", async ({ kind, rtpParameters }, callback, errback) => {
console.log(`Produce event triggered for ${kind}`);
const trackId = videoTrack.id;
if (!trackId) {
const errorMsg = `No track available for kind ${kind}`;
console.error(errorMsg);
return errback(errorMsg);
}
console.log(`Producing ${kind} with rtpParameters:`, rtpParameters);
socket.emit(
"produce",
{ kind, trackId, rtpParameters, transportId: transport.id },
(response) => {
if (response.error) {
console.error("Error in producing media:", response.error);
return errback(response.error);
}
console.log("Produced track ID:", response.id);
callback({ id: response.id });
}
);
});
try {
videoTrack = localStream.getVideoTracks()[0];
console.log("Video track selected:", videoTrack);
const encodings = [
{ maxBitrate: 100000 },
{ maxBitrate: 300000 },
{ maxBitrate: 900000 },
];
const codecOptions = {
videoGoogleStartBitrate: 1000,
};
console.log("Transport state:", transport.connectionState);
producer = await transport.produce({ track: videoTrack, encodings, codecOptions });
} catch (error) {
console.log("Error producing track:", error);
}
});
}