Best reference for implementing custom demuxing element?

Hello. I am working on a demuxer element which takes a video stream on its sink and produces two srcs, one which contains the same video data and another which contains json data as either text/plain or application/json.

Then the plan is I will write several elements which process this text data.

I have found gstreamer is very convenient when it comes to composability, and rather than bind to appsink, I have been writing elements to allow them to be easily swapped, configured, etc, directly with gstreamer as a framework.

I first attempted this by adding a second section pad to a BaseTransform element, which is what I have been using to implement custom elements up until this point.

However, I realized during this process that BaseTransform is not intended to be used for creating demuxers. I almost got it working, I could see the video buffers going out and being received and processed by downstream elements, but the json buffers would fill the downstream queue and nothing was downstream of the queue was able to receive them, which I tested with progressreport and fakesink. So I felt like I was doing something pretty wrong.

I found a few demuxer implementations I was able to reference, but they are pretty different from what I am used to writing as compared to the more standard transform and sink elements.

I am wondering if anyone has any recommendations for existing elements source code might be a suitable reference how to go about writing a demuxer like this.


I could also do this with a tee element and a transform element, so I may investigate that further as well. However I wasn’t sure if the tee element would make a copy of the buffer for each src pad attached and might want to avoid that if possible.

The best reference for a custom demuxer is an existing demuxer that’s similar to what you want to do :slightly_smiling_face:

tee does not copy any data. It will increase the refcount of the buffers it pushes out on the source pads, so all downstream elements in the tee branches will see and can read the same shared data. If anything in one of those branches wants to modify the buffer, it would see that the buffer is used by other elements/branches as well and would have to make a copy before modifying it (copy-on-write). If everyone is just reading the data, no copying will be needed.

1 Like

@tpm I really appreciate your response. I think I will move forward with the tee element for now and investigate writing a custom demuxer later on.