How to know network bandwidth in run-time in gstreamer

Chuck Crisler ccrisler at mutualink.net
Fri Sep 11 13:41:29 PDT 2015


It is very simple. Initialize a static counter to 0. For each packet
received, increment that counter by the size in bytes. Record the time for
the first and last packets. There are several time functions available.
When you have reached your target number of packets, calculate the bitrate
(bytes * 8 / seconds).

This is in file gstudpsrc.c, good plug-ins/udp.

Line 155 in my version:
#define UDP_DEFAULT_BR_PACKET_COUNT    200

LIne 176: (part of the property enumeration)
  PROP_TGT_PKT_CNT,

LIne 215 - define the signals
enum
{
  /* methods */
  /* signals */
  SIGNAL_BITRATE_MEASURED,

  /* FILL ME */
  LAST_SIGNAL
};

static guint gst_udp_signals[LAST_SIGNAL] = { 0 };

Line 276 - Send the bitrate to the controlling application as a signal.
Define the signal.
  /**
   * GstUDPSrc::bitrate-measured:
   * @gstudpsink: the source emitting the signal
   * @bitrate: the measured bitrate, in kbps
   *
   * Signal emited when the bitrate has been calculated from
   * the specified number of packets received.
   */
  gst_udp_signals[SIGNAL_BITRATE_MEASURED] =
      g_signal_new ("bitrate-measured", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0,
      NULL, NULL, gst_udp_marshal_VOID_INT, G_TYPE_NONE, 1,
      G_TYPE_INT);

LIne 342 - Define a property to allow the app to set the target number of
packets.

  g_object_class_install_property (gobject_class, PROP_TGT_PKT_CNT,
      g_param_spec_int ("packet-count", "Input stream packet count",
          "Number of packets to receive to determine input bitrate",
          0, 5000, UDP_DEFAULT_BR_PACKET_COUNT, G_PARAM_READWRITE));

Line 384 - Initialize some parameters.

  udpsrc->target_pkt_cnt = UDP_DEFAULT_BR_PACKET_COUNT;
  udpsrc->pkt_rcv_cnt = 0;
  udpsrc->bytes_received = 0;

In function gst_udpsrc_create(), starting around line 618 in my version:

  GST_LOG_OBJECT (udpsrc, "read %d bytes", (int) readsize);

  if (udpsrc->pkt_rcv_cnt > -1) {
    if (G_UNLIKELY (0 == udpsrc->pkt_rcv_cnt))
    {
      g_get_current_time (&udpsrc->start_time);
    }
    // Increment the number of packets read.
    udpsrc->pkt_rcv_cnt++;
    udpsrc->bytes_received += ((int) readsize);
    // Check if we have reached the number to determine the incoming
bitrate.
    if (G_UNLIKELY (udpsrc->pkt_rcv_cnt >= udpsrc->target_pkt_cnt)) {
      GTimeVal now;
      gint iBitRate;
      gint64 delta_time;
      // Target has been reached. Calculate the Rx bitrate.
      g_get_current_time(&now);
      delta_time = (now.tv_sec - udpsrc->start_time.tv_sec) * 1000000;  //
Convert seconds to microseconds.
      if (now.tv_usec >= udpsrc->start_time.tv_usec) {
        delta_time += (now.tv_usec - udpsrc->start_time.tv_usec);
      }
      else {
        delta_time += (now.tv_usec - udpsrc->start_time.tv_usec);
      }
      // 7812.5 = 8000000 / 1024
      // It is used to convert from bytes measured in microseconds to kbits
per second.
      iBitRate = (gint64)(udpsrc->bytes_received * 7812.5) / delta_time;
      GST_DEBUG_OBJECT (udpsrc, "PktsRcv: %d StartSec: %ld StartuSec: %ld
RxBytes: %d", udpsrc->target_pkt_cnt,
    udpsrc->start_time.tv_sec, udpsrc->start_time.tv_usec,
udpsrc->bytes_received);
      GST_DEBUG_OBJECT (udpsrc, "StopSec: %ld StopuSec: %ld dTime: %ld BR:
%d",
        now.tv_sec, now.tv_usec, delta_time, iBitRate);
      if (iBitRate < 100)
        iBitRate = 512;
      // Emit the signal with the bitrate.
      g_signal_emit (G_OBJECT (udpsrc),
        gst_udp_signals[SIGNAL_BITRATE_MEASURED], 0, iBitRate);

      udpsrc->pkt_rcv_cnt = -1;  // Prevent ever triggering again.
    }
  }

  *buf = GST_BUFFER_CAST (outbuf);

  return GST_FLOW_OK;

Line 801, get the target packet count property.

    case PROP_TGT_PKT_CNT:
      udpsrc->target_pkt_cnt = g_value_get_int (value);
      break;

Line 851, set the target packet count property.

    case PROP_TGT_PKT_CNT:
      g_value_set_int (value, udpsrc->target_pkt_cnt);
      break;



On Fri, Sep 11, 2015 at 4:05 PM, gstreader <reachtoarpi at gmail.com> wrote:

> Thanks for your comment. How you calculated elapsed time?
> Could you please post that part of code for udpsrc how you calculated
> network bandwidth?
>
> Thanks in advance.
>
>
>
> --
> View this message in context:
> http://gstreamer-devel.966125.n4.nabble.com/How-to-know-network-bandwidth-in-run-time-in-gstreamer-tp4673494p4673589.html
> Sent from the GStreamer-devel mailing list archive at Nabble.com.
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/gstreamer-devel/attachments/20150911/93857fcd/attachment.html>


More information about the gstreamer-devel mailing list