Handling QoS

An element will have to install an event function on its source pads in order to receive QOS events. Usually, the element will need to store the value of the QOS event and use them in the data processing function. The element will need to use a lock to protect these QoS values as shown in the example below. Also make sure to pass the QoS event upstream.


    [...]

    case GST_EVENT_QOS:
    {
      GstQOSType type;
      gdouble proportion;
      GstClockTimeDiff diff;
      GstClockTime timestamp;

      gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);

      GST_OBJECT_LOCK (decoder);
      priv->qos_proportion = proportion;
      priv->qos_timestamp = timestamp;
      priv->qos_diff = diff;
      GST_OBJECT_UNLOCK (decoder);

      res = gst_pad_push_event (decoder->sinkpad, event);
      break;
    }

    [...]

    

With the QoS values, there are two types of corrections that an element can do:

Short term correction

The timestamp and the jitter value in the QOS event can be used to perform a short term correction. If the jitter is positive, the previous buffer arrived late and we can be sure that a buffer with a timestamp < timestamp + jitter is also going to be late. We can thus drop all buffers with a timestamp less than timestamp + jitter.

If the buffer duration is known, a better estimation for the next likely timestamp as: timestamp + 2 * jitter + duration.

A possible algorithm typically looks like this:


  [...]

  GST_OBJECT_LOCK (dec);
  qos_proportion = priv->qos_proportion;
  qos_timestamp = priv->qos_timestamp;
  qos_diff = priv->qos_diff;
  GST_OBJECT_UNLOCK (dec);

  /* calculate the earliest valid timestamp */
  if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (qos_timestamp))) {
    if (G_UNLIKELY (qos_diff > 0)) {
      earliest_time = qos_timestamp + 2 * qos_diff + frame_duration;
    } else {
      earliest_time = qos_timestamp + qos_diff;
    }
  } else {
    earliest_time = GST_CLOCK_TIME_NONE;
  }

  /* compare earliest_time to running-time of next buffer */
  if (earliest_time > timestamp)
    goto drop_buffer;

  [...]

      

Long term correction

Long term corrections are a bit more difficult to perform. They rely on the value of the proportion in the QOS event. Elements should reduce the amount of resources they comsume by the proportion field in the QoS message.

Here are some possible strategies to achieve this:

  • Permanently dropping frames or reducing the CPU or bandwidth requirements of the element. Some decoders might be able to skip decoding of B frames.

  • Switch to lower quality processing or reduce the algorithmic complexity. Care should be taken that this doesn't introduce disturbing visual or audible glitches.

  • Switch to a lower quality source to reduce network bandwidth.

  • Assign more CPU cycles to critical parts of the pipeline. This could, for example, be done by increasing the thread priority.

In all cases, elements should be prepared to go back to their normal processing rate when the proportion member in the QOS event approaches the ideal proportion of 1.0 again.