{"id":928,"date":"2023-02-13T11:38:18","date_gmt":"2023-02-13T03:38:18","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=928"},"modified":"2025-10-09T13:55:19","modified_gmt":"2025-10-09T05:55:19","slug":"gstreamer-01","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2023\/02\/13\/gstreamer-01\/","title":{"rendered":"GStreamer 01 Hello World"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u603b\u8ff0<\/h2>\n<p>&emsp;&emsp;\u672c\u7ae0\u4ee5RK3568\uff0cOV13850\u4e3a\u4f8b\u3002\u8fd9\u662fgst-launch-1.0\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-shell\"># \u5b9e\u65f6\u9884\u89c8\ngst-launch-1.0 v4l2src device=\/dev\/video0 !video\/x-raw, format=NV12, width=640, height=480, framerate=30\/1 ! autovideosink\n\n# \u91c7\u96c6H.264\ngst-launch-1.0 v4l2src device=\/dev\/video0 num-buffers=100 !video\/x-raw,format=NV12, width=640,height=480 ! tee name=t ! queue !mpph264enc !queue !h264parse !qtmux !filesink location=13850_h264.mp4 t. ! queue !autovideosink\n\n# \u64ad\u653e\u89c6\u9891\ngst-launch-1.0 filesrc location=13850_h264.mp4 !qtdemux !queue !h264parse !mppvideodec !autovideosink<\/code><\/pre>\n<h2>\u4e8c\u3001\u4ee3\u7801<\/h2>\n<pre><code class=\"language-c\">#include &lt;gstreamer-1.0\/gst\/gst.h&gt;\n\nint glibDemo(int argc, char** argv)\n{\n    GstElement *pipeline;\n    GstBus *bus;\n    GstMessage *msg;\n\n    \/* Initialize GStreamer *\/\n    gst_init (&amp;argc, &amp;argv);\n\n    \/* Build the pipeline *\/\n    pipeline = gst_parse_launch(\n        &quot;filesrc location=13850_h264.mp4 !qtdemux !queue !h264parse !mppvideodec !autovideosink&quot;,\n        NULL\n    );\n\n    \/* Start playing *\/\n    gst_element_set_state (pipeline, GST_STATE_PLAYING);\n\n    \/* Wait until error or EOS *\/\n    bus = gst_element_get_bus (pipeline);\n    msg = gst_bus_timed_pop_filtered (\n        bus, GST_CLOCK_TIME_NONE, \n        GST_MESSAGE_ERROR | GST_MESSAGE_EOS\n    );\n\n    \/* See next tutorial for proper error message handling\/parsing *\/\n    if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {\n        g_error (&quot;An error occurred! Re-run with the GST_DEBUG=*:WARN environment &quot;\n        &quot;variable set for more details.&quot;);\n    }\n\n    \/* Free resources *\/\n    gst_message_unref (msg);\n    gst_object_unref (bus);\n    gst_element_set_state (pipeline, GST_STATE_NULL);\n    gst_object_unref (pipeline);\n    return 0;\n}<\/code><\/pre>\n<h2>\u4e09\u3001\u89e3\u91ca<\/h2>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u56de\u987e\u4e0a\u8ff0\u7684\u4ee3\u7801\uff0c\u5e76\u7406\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n<h3>3.1 \u521d\u59cb\u5316<\/h3>\n<pre><code class=\"language-c\">GstElement *pipeline;\nGstBus *bus;<\/code><\/pre>\n<p>\u8fd9\u6c38\u8fdc\u662f\u5199GStreamer\u7684\u7b2c\u4e00\u6761\u547d\u4ee4\u3002\u9664\u5176\u4ed6\u4e8b\u9879\u5916\uff0c<code>gst_init()<\/code>\u6709\u5982\u4e0b\u4f5c\u7528\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u6240\u6709\u5185\u90e8\u7ed3\u6784\u3002<\/li>\n<li>\u786e\u4fdd\u6240\u6709\u63d2\u4ef6\u53ef\u7528\u3002<\/li>\n<li>\u6267\u884c\u4efb\u4f55\u53ef\u80fd\u9002\u7528\u4e8eGStreamer\u7684\u547d\u4ee4\u884c\u9009\u9879\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/* Initialize GStreamer *\/\ngst_init (&amp;argc, &amp;argv);\n\n\/* Build the pipeline *\/<\/code><\/pre>\n<h3>3.2 \u7ba1\u7ebf<\/h3>\n<p>&emsp;&emsp;GStreamer\u662f\u4e00\u4e2a\u65e8\u5728\u5904\u7406\u591a\u5a92\u4f53\u6d41\u7684\u6846\u67b6\u3002 \u5a92\u4f53\u4ece\u201csource\u201d\u5143\u7d20\uff08\u751f\u4ea7\u8005\uff09\u5411\u4e0b\u4f20\u64ad\u5230\u201csink\u201d\u5143\u7d20\uff08\u6d88\u8d39\u8005\uff09\uff0c\u901a\u8fc7\u4e00\u7cfb\u5217\u6267\u884c\u5404\u79cd\u4efb\u52a1\u7684\u4e2d\u95f4\u5143\u7d20\u3002\u6240\u6709\u4e92\u8fde\u5143\u7d20\u7684\u96c6\u5408\u79f0\u4e3a\u201cpipeline\u201d\u3002<\/p>\n<p>&emsp;&emsp;\u5728GStreamer\u4e2d\uff0c\u901a\u5e38\u624b\u52a8\u5730\u7ec4\u88c5\u5404\u4e2a\u5143\u7d20\u6765\u6784\u5efa\u7ba1\u9053\uff0c\u4f46\u662f\uff0c\u5f53\u7ba1\u9053\u8db3\u591f\u7b80\u5355\u5e76\u4e14\u4e0d\u9700\u8981\u4efb\u4f55\u9ad8\u7ea7\u529f\u80fd\u65f6\uff0c\u5c31\u53ef\u4ee5\u91c7\u7528\u5feb\u6377\u65b9\u5f0f\uff1agst_parse_launch()\u3002\u8fd9\u79cd\u65b9\u5f0f\u5c06\u76f4\u63a5\u4f7f\u7528\u4e0egst-launch-1.0\u8bed\u6cd5\u76f8\u540c\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u7ba1\u9053\u3002<\/p>\n<pre><code class=\"language-c\">\/* Build the pipeline *\/\npipeline = gst_parse_launch(\n    &quot;filesrc location=13850_h264.mp4 !qtdemux !queue !h264parse !mppvideodec !autovideosink&quot;,\n    NULL\n);<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u548c\u4e0b\u9762\u7684\u547d\u4ee4\u662f\u7b49\u6548\u7684\uff1a<\/p>\n<pre><code class=\"language-shell\">gst-launch-1.0 filesrc location=13850_h264.mp4 !qtdemux !queue !h264parse !mppvideodec !autovideosink<\/code><\/pre>\n<h3>3.3 \u64ad\u653e<\/h3>\n<p>&emsp;&emsp;\u5728\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u6784\u5efa\u4e86\u4e00\u4e2a\u7ba1\u9053\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\uff0c\u6211\u4eec\u4e3a\u7ba1\u7ebf\u6dfb\u52a0\u4e86\u4e00\u4e2a\u672c\u5730\u7684\u89c6\u9891\u6587\u4ef6\u3002\u4f46\u6b64\u65f6\uff0c\u9664\u975e\u6211\u4eec\u624b\u52a8\u7684\u53bb\u64ad\u653e\u5b83\uff0c\u5426\u5219\u7ba1\u7ebf\u672c\u8eab\u5e76\u4e0d\u4f1a\u64ad\u653e\u89c6\u9891\u3002<\/p>\n<pre><code class=\"language-c\">\/* Start playing *\/\ngst_element_set_state (pipeline, GST_STATE_PLAYING);<\/code><\/pre>\n<p>\u6b64\u65f6<code>gst_element_set_state()<\/code>\u5c06\u8bbe\u7f6e\u7ba1\u7ebf\uff08\u5b9e\u4f8b\u4e2d\u6211\u4eec\u552f\u4e00\u7684\u5143\u7d20\uff09\u72b6\u6001\u8bbe\u7f6e\u4e3a\u201cPLAYING\u201d\uff0c\u6b64\u65f6\u89c6\u9891\u624d\u88ab\u64ad\u653e\u3002\u8fd9\u6761\u547d\u4ee4\u5c06\u4f1a\u4e00\u76f4\u7b49\u5230\u6d41\u7ed3\u675f\uff08EOS\uff09\u6216\u8005\u9047\u5230\u9519\u8bef\u3002<\/p>\n<h3>3.4 \u6e05\u7406<\/h3>\n<p>&emsp;&emsp;\u5728\u5e94\u7528\u7a0b\u5e8f\u7ed3\u675f\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u7684\u8fdb\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-c\">\/* Wait until error or EOS *\/\nbus = gst_element_get_bus (pipeline);\nmsg = gst_bus_timed_pop_filtered (\n    bus, GST_CLOCK_TIME_NONE, \n    GST_MESSAGE_ERROR | GST_MESSAGE_EOS\n);\n\n\/* See next tutorial for proper error message handling\/parsing *\/\nif (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {\n    g_error (&quot;An error occurred! Re-run with the GST_DEBUG=*:WARN environment &quot;\n    &quot;variable set for more details.&quot;);\n}\n\n\/* Free resources *\/\ngst_message_unref (msg);\ngst_object_unref (bus);\ngst_element_set_state (pipeline, GST_STATE_NULL);\ngst_object_unref (pipeline);<\/code><\/pre>\n<ul>\n<li><code>gst_bus_timed_pop_filtered()<\/code>\u8fd4\u56de\u4e00\u6761\u6d88\u606f\uff0c\u8be5\u6d88\u606f\u4f7f\u7528<code>gst_message_unref<\/code>\u91ca\u653e\u3002<\/li>\n<li><code>gst_element_get_bus()<\/code>\u8fd4\u56de\u7684\u603b\u7ebf\u5219\u7531<code>gst_object_unref()<\/code>\u91ca\u653e\u3002<\/li>\n<li>\u6700\u540e\u8fd9\u662f\u53d6\u6d88\u7ba1\u7ebf\u7684\u5f15\u7528\uff0c\u5e76\u91ca\u653e\u5b83\u3002<\/li>\n<\/ul>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u56de\u987e\u4e00\u4e0b\u6587\u7ae0\u7684\u5185\u5bb9\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528<code>gst_init()<\/code>\u521d\u59cb\u5316GStreamer\u3002<\/li>\n<li>\u4f7f\u7528<code>gst_parse_launch()<\/code>\u5feb\u901f\u6784\u5efa\u7ba1\u7ebf\u3002<\/li>\n<li>\u4f7f\u7528<code>gst_element_set_state()<\/code>\u8bbe\u7f6e\u7ba1\u7ebf\u7684\u72b6\u6001\u4ee5\u64ad\u653e\u89c6\u9891\u3002<\/li>\n<li>\u6e05\u7406\u4e0e\u91ca\u653e\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u603b\u8ff0 &emsp;&emsp;\u672c\u7ae0\u4ee5RK3568\uff0cOV13850\u4e3a\u4f8b\u3002\u8fd9\u662fgst-launch-1.0\u7684\u8bed\u6cd5\uff1a # \u5b9e\u65f6\u9884\u89c8  &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[239],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/928"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=928"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/928\/revisions"}],"predecessor-version":[{"id":929,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/928\/revisions\/929"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}