I am using NodeJS with mediasoup v3 for creating a sfu server using mediasoup v3.
But i don’t understand if what i do is well and how to add the media stream that i get from the front end to the producer.
I would like to know, how to add a media stream to a pipe producer, so every consumer who join the stream can get the media stream and see a livestream or video conference.
Here is my code for creating the pipeProducer on a nodejs back end:
async createPipeProducer(userStreamerId: string, mediaStream: MediaStream) {
const router = await this.createRouterFromWorker(this.worker);
const transport = await router.createWebRtcTransport(
{
webRtcServer : this.server, // Or ListenIps
// listenIps : [ { ip: "192.168.0.111", announcedIp: "88.12.10.41" } ],
enableUdp : true,
enableTcp : true,
preferUdp : true
}
);
transport.on("icestatechange", (iceState) => {
console.log(`ICE state changed to ${iceState}`);
});
// Get the supported RTP capabilities
const rtpCapabilities = router.rtpCapabilities;
// Create the producer
const pipeProducer = await transport.produce({
kind: 'video',
rtpParameters: rtpCapabilities["video"].codecs.find(codec => codec.mimeType.toLowerCase() === "video/vp8"),
appData: { type: 'pipe' }
});
// Set the media stream as the source of the producer
const track = mediaStream.getVideoTracks()[0];
track.onended = () => {
pipeProducer.pause();
};
// It's in this part that i am stuck
const mediaSender = await pipeProducer.getStats();
const rtpSenders = mediaSender[0].rtpSenders;
mediaSender.send(track);
return pipeProducer;
}
Am i making everything great ? Expect for add a media stream to the producer, so every consumer can join a specific producer’s stream.
I can provide more informations if it can help you to help me.
Thanks for your help.
You are mixing server side code with client side code, we do this on server side:
And this on client side:
I think you have missed many things in the documentation, perhaps a look at the demo code will give you good understanding.
Further more you have mentioned about ‘piping’, this is not used normally but only used when we need to share producer between routers within a server on between separate servers.
Apart from this the basic step to produce are these:
Create router, workers etc on server
Create transport on server, and based upon the result create transport on client as well
Use transport.produce() method to produce track on the transport you just created. It will give you producer
After that ‘producer’ event will fire on transport you created, and on that event you will send producer details on your server.
After that ‘connect’ event will fire on transport you created, and on that event you will send transport details so finally peer connection will be established and track will start flowing to the server.
i have asked chatGPT and createWebRtcTransport can be used in both back end and front end.
Here is his answer:
createWebRtcTransport is part of the mediasoup library and can be used in both the front-end and the back-end.
In a server-side application, createWebRtcTransport would typically be called on the server to create a WebRTC transport object that can be used to send and receive media streams from clients. In a client-side application, createWebRtcTransport would typically be called on the browser to create a WebRTC transport object that can be used to send and receive media streams from the server.
So, createWebRtcTransport is not specific to the front-end or the back-end, but rather it is used in both contexts.
I understand the flow now.
But i have checked the mediasoup-client library and it seems that the connect() method doesn’t exist.
why that ?
Do i still ahve to connect before using produce() method ?
If yes and if it doesn’t exist anymore, by what should i change that ?
The first call to produce will initiate the connection process internally. You should catch the “connect” event on the transport and manage the signalling.
There is no connect() method in mediasoup-client, on client side we have ‘connect’ event while on server side we have connect() method:
When you call produce() on client side then it will trigger ‘connect’ event on transport automatically on that event you will signal to your server some parameters and on server side you will call transport.connect() method and then these transports are connected.
This is wrong. You shouldn’t call transport.produce() method inside ‘produce’ event, it should be reverse i.e you should call transport.produce() outside somewhere after creating transport and registering for event, produce() method will automatically call ‘produce’, ‘connect’ events. And on these events you will signal data to your server
Then the problem is you are not creating transport correctly. To create transport on client side, which you are creating, you must create transport on server before that. And from the server’s response you should create the transport on client side and then listen to events.
Try logging this.sendTransport and see what it contains.
You definitely call the on method not on what createSendTransport returned (and which is logged in the console.log). It is some uninitialized variable: “TypeError: Cannot read properties of undefined”.
This error may be coming from somewhere else, make sure where it is coming from, show the exact line from developer tools where this error is occurring.
Also console.log(this.sendTransport); console.log(this.sendTransport.on); together after transport is created and show the result.