{"id":1202,"date":"2024-01-20T22:37:52","date_gmt":"2024-01-20T14:37:52","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1202"},"modified":"2024-01-20T22:37:52","modified_gmt":"2024-01-20T14:37:52","slug":"gstreamer-tutorial-2","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1202","title":{"rendered":"GStreamer tutorial 2"},"content":{"rendered":"<h2>Goal<\/h2>\n<p>The previous tutorial showed how to build a pipeline automatically. Now we are going to build a pipeline manually by instantiating each element and linking them all together. In the process, we will learn:<\/p>\n<ul>\n<li>\n<p>What is a GStreamer element and how to create one.<br \/>\n\u4ec0\u4e48\u662f GStreamer \u5143\u4ef6\uff0c\u5982\u4f55\u521b\u5efa\u5b83<\/p>\n<\/li>\n<li>\n<p>How to connect elements to each other.<br \/>\n\u5982\u4f55\u5c06\u5143\u4ef6\u8fde\u63a5\u8d77\u6765<\/p>\n<\/li>\n<li>\n<p>How to customize an element's behavior.<br \/>\n\u5982\u4f55\u81ea\u5b9a\u4e49\u4e00\u4e2a\u5143\u4ef6\u7684\u884c\u4e3a<\/p>\n<\/li>\n<li>\n<p>How to watch the bus for error conditions and extract information from GStreamer messages.<br \/>\n\u5982\u4f55\u89c2\u5bdf\u603b\u7ebf\u4e0a\u53d1\u751f\u7684\u9519\u8bef\uff0c\u4ece GstMessage \u4e2d\u63d0\u53d6\u4fe1\u606f<\/p>\n<\/li>\n<\/ul>\n<h2>\u6e90\u4ee3\u7801<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;gst\/gst.h&gt;\n\n#ifdef __APPLE__\n#include &lt;TargetConditionals.h&gt;\n#endif\n\nint\ntutorial_main (int argc, char *argv[])\n{\n  GstElement *pipeline, *source, *sink;\n  GstBus *bus;\n  GstMessage *msg;\n  GstStateChangeReturn ret;\n\n  \/* Initialize GStreamer *\/\n  gst_init (&amp;argc, &amp;argv);\n\n  \/* Create the elements *\/\n  source = gst_element_factory_make (&quot;videotestsrc&quot;, &quot;source&quot;);\n  sink = gst_element_factory_make (&quot;autovideosink&quot;, &quot;sink&quot;);\n\n  \/* Create the empty pipeline *\/\n  pipeline = gst_pipeline_new (&quot;test-pipeline&quot;);\n\n  if (!pipeline || !source || !sink) {\n    g_printerr (&quot;Not all elements could be created.\\n&quot;);\n    return -1;\n  }\n\n  \/* Build the pipeline *\/\n  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);\n  if (gst_element_link (source, sink) != TRUE) {\n    g_printerr (&quot;Elements could not be linked.\\n&quot;);\n    gst_object_unref (pipeline);\n    return -1;\n  }\n\n  \/* Modify the source&#039;s properties *\/\n  g_object_set (source, &quot;pattern&quot;, 0, NULL);\n\n  \/* Start playing *\/\n  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);\n  if (ret == GST_STATE_CHANGE_FAILURE) {\n    g_printerr (&quot;Unable to set the pipeline to the playing state.\\n&quot;);\n    gst_object_unref (pipeline);\n    return -1;\n  }\n\n  \/* Wait until error or EOS *\/\n  bus = gst_element_get_bus (pipeline);\n  msg =\n      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,\n      GST_MESSAGE_ERROR | GST_MESSAGE_EOS);\n\n  \/* Parse message *\/\n  if (msg != NULL) {\n    GError *err;\n    gchar *debug_info;\n\n    switch (GST_MESSAGE_TYPE (msg)) {\n      case GST_MESSAGE_ERROR:\n        gst_message_parse_error (msg, &amp;err, &amp;debug_info);\n        g_printerr (&quot;Error received from element %s: %s\\n&quot;,\n            GST_OBJECT_NAME (msg-&gt;src), err-&gt;message);\n        g_printerr (&quot;Debugging information: %s\\n&quot;,\n            debug_info ? debug_info : &quot;none&quot;);\n        g_clear_error (&amp;err);\n        g_free (debug_info);\n        break;\n      case GST_MESSAGE_EOS:\n        g_print (&quot;End-Of-Stream reached.\\n&quot;);\n        break;\n      default:\n        \/* We should not reach here because we only asked for ERRORs and EOS *\/\n        g_printerr (&quot;Unexpected message received.\\n&quot;);\n        break;\n    }\n    gst_message_unref (msg);\n  }\n\n  \/* Free resources *\/\n  gst_object_unref (bus);\n  gst_element_set_state (pipeline, GST_STATE_NULL);\n  gst_object_unref (pipeline);\n  return 0;\n}\n\nint\nmain (int argc, char *argv[])\n{\n#if defined(__APPLE__) &amp;&amp; TARGET_OS_MAC &amp;&amp; !TARGET_OS_IPHONE\n  return gst_macos_main (tutorial_main, argc, argv, NULL);\n#else\n  return tutorial_main (argc, argv);\n#endif\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal The previous tutorial showed how to build a pipeline automatically. Now we are going to build a pipeline manually by instantiating each element and linking them all together. In the process, we will learn: What is a GStreamer element and how to create one. \u4ec0\u4e48\u662f GStreamer \u5143\u4ef6\uff0c\u5982\u4f55\u521b\u5efa\u5b83 How to connect elements to each other. [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1202\" title=\"Permanent Link to: GStreamer tutorial 2\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1202","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1202"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1202"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1202\/revisions"}],"predecessor-version":[{"id":1203,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1202\/revisions\/1203"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}