versatica / libmediasoupclient

mediasoup client side C++ library
https://mediasoup.org
ISC License
291 stars 178 forks source link

Transport produce method must be called in thread #124

Closed richmonkey closed 3 years ago

richmonkey commented 3 years ago

Because Transportlistener->onProducer must access network, so Transport.produce must be called in thread. It's not a issuse, but as a library, it's not very good. Can split transport.produce into two step.

  1. sendResult = transport.prepareProduce(params...);
  2. request server, transport produce(sendResult...);
  3. if request success, got producerId from server, call native transport.produce(producerId, sendResult) else request failure, call native transport.cancelProduce(sendResult)

If api like this, it's my free to call mediaclient api in main thread or other thread.

richmonkey commented 3 years ago

Need more work to do, Handler.SetupTransport is another async method, Only if precreate local dtlsParameters is possible, the target is possible.

if device.createSendTransport() can get local dtlsParameters after createSendTransport, request server connectTransport with local dtlsParameters . last transport.produce

richmonkey commented 3 years ago

How to get local dtlsParameters in the step device.createSendTransport()? in handler create peerconnect, add transceiver, createoffer, get dtlsparamters from localsdp, close peerconnection. save the local dtlsParamters in last produce step, when after createoffer, modify the sdp's dtlsparamters field with saved local dtlsParamters, setlocaldescription with the changed sdp. Is it possible?

richmonkey commented 3 years ago

My opinion:library's listener is async, it's complex for caller, also not good smell.

snnz commented 3 years ago

Transport.produce must be called in thread.

Actually, calls to Transport.produce should be queued somehow, otherwise the handler may try to connect the transport more than once, not to mention that internal manipulations with sdp are not thread-safe.

richmonkey commented 3 years ago

“calls to Transport.produce should be queued somehow”, I know this. meidasoupclient is not threadsafe. I want to express: So for simple, call meidasoupclient always in one thread, mediasoupcliennt can't be called in ui thread, because Transport.produce is sync method and long time(access network).

ibc commented 3 years ago

This is not a bug. This is by design. Nobody said that libmediasoupclient can be used in UI thread. Please let's use the mediasoup forum for further discussions.