GStreamer modify the elements rank

From RidgeRun Developer Wiki


This wiki describes how to modify the order in which elements are selected automatically by GStreamer. When the user creates a new pipeline it is possible to select from many options of elements that perform the same task, but each element provides different options or improvements.

In this wiki, we are going to describe the process with h264 video decoders

How GStreamer decides which element to use

Each GStreamer element have a detail called rank that defines the priority used by the autoplugger when it wants to connect a new element but have multiple options

Following two gst-inspect show the rank of avdec_h264 and nvdec, GStreamer defines 4 priorities:

GST_RANK_NONE (0) – will be chosen last or not at all
GST_RANK_MARGINAL (64) – unlikely to be chosen
GST_RANK_SECONDARY (128) – likely to be chosen
GST_RANK_PRIMARY (256) – will be chosen first 

But the user can configure the rank with a guint number.

In this case, both decoders have the same rank (primary (256)), if a decodebin was connected to the pipeline the autoplugger search the decoders with the highest rank, two in this case, so it selects the decoder by name, in this case, the decoder avdec_h264 is the first founded.

gst-inspect-1.0 avdec_h264

Factory Details:
  Rank                     primary (256)
  Long-name                libav H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 decoder
  Klass                    Codec/Decoder/Video
  Description              libav h264 decoder
  Author                   Wim Taymans <wim.taymans@gmail.com>, Ronald Bultje <rbultje@ronald.bitfreak.net>, Edward Hervey <bilboed@bilboed.com>

Plugin Details:
  Name                     libav
  Description              All libav codecs and formats (local snapshot)
  Filename                 /usr/lib64/gstreamer-1.0/libgstlibav.so
  Version                  1.16.2
  License                  LGPL
  Source module            gst-libav
  Binary package           gst-libav 1.0 rpmfusion rpm
  Origin URL               http://rpmfusion.org/

gst-inspect-1.0 nvdec

Factory Details:
  Rank                     primary (256)
  Long-name                NVDEC video decoder
  Klass                    Codec/Decoder/Video/Hardware
  Description              NVDEC video decoder
  Author                   Ericsson AB, http://www.ericsson.com

Plugin Details:
  Name                     nvdec
  Description              GStreamer NVDEC plugin
  Filename                 /usr/lib64/gstreamer-1.0/libgstnvdec.so
  Version                  1.16.2
  License                  BSD
  Source module            gst-plugins-bad
  Source release date      2019-12-03
  Binary package           Fedora GStreamer-plugins-bad package
  Origin URL               http://download.fedoraproject.org

Modify the elements rank

Let's assume it is desired to select another available decoder, in this case, we are going to choose the nvdec because it provides hardware acceleration.

First we need to get the GstRegistry that contains the information and metadata of a list of plugins.

GstRegistry* plugins_register = gst_registry_get();

Now it is necessary to get the GstPluginFeature from the GstRegistry, in this case, we are going to increase the rank of the decoder that we want to use (nvdec):

GstPluginFeature* nvdec = gst_registry_lookup_feature(plugins_register, "nvdec");
if(nvdec == NULL) {
    return;
}

With the following line it is possible set a new rank to the nvdec, the new rank is GST_RANK_PRIMARY + 1 (257)

gst_plugin_feature_set_rank(nvdec, GST_RANK_PRIMARY + 1);
gst_object_unref(nvdec);

Now if a decodebin is created it will select the decoder with the highest rank and it is the the nvdec with rank = 257

Modify the rank more than once

If you need to modify the rank more than one time and it is not GST_RANK_PRIMARY (256) anymore, is possible to get the current rank of the element:

GstRegistry* plugins_register = gst_registry_get();
GstPluginFeature* nvdec = gst_registry_lookup_feature(plugins_register, "nvdec");
if(nvdec == NULL) {
    return;
}
guint nvdec_rank = gst_plugin_feature_get_rank (nvdec)
gst_plugin_feature_set_rank(nvdec, nvdec_rank + 1);

gst_object_unref(nvdec);

Disable an element

If the rank of an element is configured to 0 or GST_RANK_NONE it is disabled completely, and the autoplugger can't find it when searching for available elements.

GstRegistry* plugins_register = gst_registry_get();
GstPluginFeature* avdec_h264 = gst_registry_lookup_feature(plugins_register, "avdec_h264");
if(avdec_h264 == NULL) {
    return;
}
gst_plugin_feature_set_rank(avdec_h264, GST_RANK_NONE);
gst_object_unref(avdec_h264);

Observation

The changes of the rank of each elements persist only in runtime, if the application stops, the rank return to the default value.



For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.


Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.