close pipeline with gstreamer WebRTCBin

76 views
Skip to first unread message

Fereshte Ebrahimian

unread,
Apr 20, 2023, 4:12:03 AM4/20/23
to gstreamer-java
I used data channel in gstreamer to send only text messages between webrtc connections but when i closed pipeline and webrtcbin , all ports that opened for ICE Candidates was still open , how can i close them?

I try to disconnect all listeners and dispose objects.


        private final CountDownLatch loop = new CountDownLatch(1);
        GstUtil.initGst();
        Pipeline pipeline = new Pipeline();
        Bus bus = pipeline.getBus();
        Bus.EOS busEOS = (Bus.EOS) source -> {
            log.info("********* Reached end of stream: " + source.toString());
        };
        bus.connect(busEOS);

        Bus.ERROR busERROR = (Bus.ERROR) (source, code, m) -> {
            log.error("Error from source: '{}', with code: {}, and message '{}'", source, code, m);
            disconnect();
        };
        bus.connect(busERROR);

        webRTCBin = new WebRTCBin("webrtcbin");
        webRTCBin.setTurnServer("turn://**********:3478");
        webRTCBin.setBundlePolicy(WebRTCBundlePolicy.MAX_BUNDLE);
        webRTCBin.set("ice-transport-policy","1");
        pipeline.add(webRTCBin);

        AtomicBoolean once = new AtomicBoolean(true);
        final WebRTCBin.CREATE_ANSWER[] listenerANSWER = new WebRTCBin.CREATE_ANSWER[1];
        WebRTCBin.ON_NEGOTIATION_NEEDED webRTCBinON_NEGOTIATION_NEEDED = (WebRTCBin.ON_NEGOTIATION_NEEDED) elem -> {
            if (once.get()) {
                log.info("onNegotiationNeeded: " + elem.getName());
                SDPMessage sdpMessage = new SDPMessage();
                sdpMessage.parseBuffer(sdpOffer);
                WebRTCSessionDescription description = new WebRTCSessionDescription(WebRTCSDPType.OFFER, sdpMessage);
                webRTCBin.setRemoteDescription(description);
                listenerANSWER[0] = answer -> {
                    String sdp = answer.getSDPMessage().toString();
                    webRTCBin.setLocalDescription(answer);
                    try {
                        SdpAnswerResponse sdpAnswerResponse = new SdpAnswerResponse("PROCESS_SDP_ANSWER", sdp);
                        webSocket.convertAndSend("/topic/messages", sdpAnswerResponse);
                    } catch (Exception e) {
                        log.error("Exception", e);
                    }
                };
                webRTCBin.createAnswer(listenerANSWER[0]);
                once.set(false);
            }
        };
        webRTCBin.connect(webRTCBinON_NEGOTIATION_NEEDED);

        pipeline.play();

        WebRTCBin.ON_ICE_CANDIDATE iceListener = (sdpMLineIndex, candidate) -> {
            log.info("ON_ICE_CANDIDATE {}", candidate);
            IceCandidateResponse iceCandidateResponse = new IceCandidateResponse("ADD_ICE_CANDIDATE",
                    new IceCandidate(candidate, "", sdpMLineIndex));
            webSocket.convertAndSend("/topic/messages", iceCandidateResponse);

        };
        webRTCBin.connect(iceListener);
       
        loop.await();

        webRTCBin.stop();
        webRTCBin.disconnect( WebRTCBin.CREATE_ANSWER.class, listenerANSWER[0]);
        webRTCBin.disconnect(WebRTCBin.ON_NEGOTIATION_NEEDED.class,webRTCBinON_NEGOTIATION_NEEDED);
        webRTCBin.disconnect(WebRTCBin.ON_ICE_CANDIDATE.class,iceListener);
        webRTCBin.getElements().forEach(Element::dispose);
        webRTCBin.dispose();



        pipeline.stop();
        bus.disconnect(busEOS);
        bus.disconnect(busERROR);
        pipeline.getElements().forEach(Element::dispose);
        pipeline.dispose();




Neil C Smith

unread,
Apr 20, 2023, 4:35:14 AM4/20/23
to gstream...@googlegroups.com
Hi,

On Thu, 20 Apr 2023 at 09:12, Fereshte Ebrahimian
<ebrahi...@gmail.com> wrote:
> I used data channel in gstreamer to send only text messages between webrtc connections but when i closed pipeline and webrtcbin , all ports that opened for ICE Candidates was still open , how can i close them?
>
> I try to disconnect all listeners and dispose objects.

OK, don't try to do this. I'm not sure how much it overlaps with your
issue (I don't have time to look too much in depth at it right now),
but have a read of
https://github.com/gstreamer-java/gst1-java-core/discussions/259

> WebRTCBin.ON_NEGOTIATION_NEEDED webRTCBinON_NEGOTIATION_NEEDED = (WebRTCBin.ON_NEGOTIATION_NEEDED) elem -> {
> if (once.get()) {
> ...
> };

Is this element the webrtcbin itself? If not, maybe try wrapping that
whole code with -

try (elem) {
if (once.get()) {
....
}

Don't do that if it is the webrtcbin though!

> pipeline.stop();
...
> pipeline.getElements().forEach(Element::dispose);
> pipeline.dispose();

Calling getElements() is a bad idea and unnecessary, as that will
create Java-side proxies for all the elements, which is what you're
trying to dispose of. Just dispose the webrtcbin and pipeline, and
dispose of any other elements received from listeners directly using
try() as shown above.

You should ideally run all GStreamer code with Gst::invokeLater

Be careful with pipeline dispose. When you call pipeline.stop() this
will cause a range of bus messages to be queued. You need to dispose
after them currently, so use Gst.invokeLater(pipeline::dispose); Make
sure you wait for all pending tasks to run if necessary before
exiting.

Best wishes,

Neil

--
Neil C Smith
Codelerity Ltd.
www.codelerity.com

Codelerity Ltd. is a company registered in England and Wales
Registered company number : 12063669
Registered office address : Office 4 219 Kensington High Street,
Kensington, London, England, W8 6BD

lionel mazeyrat

unread,
Feb 16, 2024, 4:51:43 AMFeb 16
to gstreamer-java
Hello,

I think I run into the same issue.

Did you find the right way to dispose pipeline and to free webrtcbin ports used ?

Reply all
Reply to author
Forward
0 new messages