Нужен пример для получения статистики потока выходного потока в коде Gstreamer C? - PullRequest
1 голос
/ 04 июля 2019

Я новичок в C-кодировании и пишу основную программу транскодирования для проекта, над которым я работаю.Мне было интересно, есть ли у кого-нибудь базовый пример, который позволил бы мне, например, получить выходную статистику (фактический битрейт для видео и аудио, частота кадров, размер разрешения, уровень видео h264 и т. Д.)

Пожалуйста, см. Код ниже:

#include <gst/gst.h>
#include <glib.h>
#include <stdio.h>


static gboolean
cb_print_position (GstElement *pipeline)
{
  gint64 pos;


  if (gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos)) 
    {
    g_print ("Time: %" GST_TIME_FORMAT  "\r", GST_TIME_ARGS (pos));
  }


  /* call me again */
  return TRUE;
}




static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
  GMainLoop *loop = (GMainLoop *) data;

  switch (GST_MESSAGE_TYPE (msg)) {

    case GST_MESSAGE_EOS:
      g_print ("End of stream\n");
      g_main_loop_quit (loop);
      break;

    case GST_MESSAGE_ERROR: {
      gchar  *debug;
      GError *error;

      gst_message_parse_error (msg, &error, &debug);
      g_free (debug);

      g_printerr ("Error: %s\n", error->message);
      g_error_free (error);

      g_main_loop_quit (loop);
      break;
    }
    default:
      break;
  }

  return TRUE;
}



int main (int argc, char *argv[])
{
  GMainLoop *loop;

  GstElement *pipeline, *videotestsrcm, *x264encm, *rtmpsinkm, *flvmuxm;
  GstBus *bus;
  guint bus_watch_id;

  /* Initialisation */
  gst_init (&argc, &argv);


const gchar*nano_str;guint major, minor, micro, nano;

gst_init (&argc, &argv);

gst_version (&major, &minor, &micro, &nano);

if (nano == 1)
nano_str = "(CVS)";
else if (nano == 2)
nano_str = "(Prerelease)";
else
nano_str = "";
printf ("This program is linked against GStreamer %d.%d.%d%s\n",major, minor, micro, nano_str);

  loop = g_main_loop_new (NULL, FALSE);


  /* Create gstreamer elements */
  pipeline = gst_pipeline_new ("videotest-pipeline");
  videotestsrcm   = gst_element_factory_make ("videotestsrc", "testsource");
  x264encm = gst_element_factory_make ("x264enc", "videoencoder");
  rtmpsinkm = gst_element_factory_make ("rtmpsink", "video2sink");
  flvmuxm = gst_element_factory_make ("flvmux", "muxer");

  if (!pipeline || !videotestsrcm || !x264encm || !rtmpsinkm || !flvmuxm) {
    g_printerr ("One element could not be created. Exiting.\n");
    return -1;
  }


g_object_set (G_OBJECT (rtmpsinkm), "location" , argv[1] ,  NULL);



  /* Set up the pipeline */

  /* we add a message handler */
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
  gst_object_unref (bus);


  /* we add all elements into the pipeline */
  gst_bin_add_many (GST_BIN (pipeline),
                    videotestsrcm, x264encm, rtmpsinkm, flvmuxm, NULL);

  /* we link the elements together */
  /* videotestsrcm -> autovideosinkm */
  gst_element_link_many (videotestsrcm, x264encm, flvmuxm, rtmpsinkm, NULL);


  /* Set the pipeline to "playing" state*/
  g_print ("Now set pipeline in state playing...\n");
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

/* run pipeline */
  g_timeout_add (200, (GSourceFunc) cb_print_position, pipeline);



  /* Iterate */
  g_print ("Running...\n");
  g_main_loop_run (loop);


  /* Out of the main loop, clean up nicely */
  g_print ("Returned, stopping playback\n");
  gst_element_set_state (pipeline, GST_STATE_NULL);

  g_print ("Deleting pipeline\n");
  gst_object_unref (GST_OBJECT (pipeline));
  g_source_remove (bus_watch_id);
  g_main_loop_unref (loop);

  return 0;
}
...