#include #include #include #define DEST_HOST "127.0.0.1" #define VIDEO_SRC "videotestsrc" #define VIDEO_ENC "x264enc" #define VIDEO_PAY "rtph264pay" static void on_pad_added(GstElement *element, GstPad *pad, gboolean last, gpointer *data) { GstCaps *caps; GstStructure *str; GstPad *sinkpad; GstElement *queue = (GstElement *) data; sinkpad = gst_element_get_static_pad(queue, "sink"); if (GST_PAD_IS_LINKED(sinkpad)) { g_object_unref(sinkpad); return; } caps = gst_pad_get_caps (pad); str = gst_caps_get_structure (caps, 0); if (!g_strrstr (gst_structure_get_name (str), "video")) { gst_caps_unref (caps); gst_object_unref (sinkpad); return; } g_print("Dynamic pad created\n"); gst_caps_unref (caps); gst_pad_link(pad, sinkpad); gst_object_unref(sinkpad); } /* print the stats of a source */ static void print_source_stats (GObject * source) { GstStructure *stats; gchar *str; /* get the source stats */ g_object_get (source, "stats", &stats, NULL); /* simply dump the stats structure */ str = gst_structure_to_string (stats); g_print ("source stats: %s\n", str); gst_structure_free (stats); g_free (str); } /* this function is called every second and dumps the RTP manager stats */ static gboolean print_stats (GstElement * rtpbin) { GObject *session; GValueArray *arr; GValue *val; guint i; g_print ("***********************************\n"); /* get session 0 */ g_signal_emit_by_name (rtpbin, "get-internal-session", 0, &session); /* print all the sources in the session, this includes the internal source */ g_object_get (session, "sources", &arr, NULL); for (i = 0; i < arr->n_values; i++) { GObject *source; val = g_value_array_get_nth (arr, i); source = g_value_get_object (val); print_source_stats (source); } g_value_array_free (arr); g_object_unref (session); return TRUE; } int main (int argc, char *argv[]) { GstElement *videosrc, *timeoverlay, *decodebin, *queue, *videorate, *ffmpegcolorspace, *videoenc, *videopay; GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc; GstElement *pipeline; GMainLoop *loop; gboolean res; GstPadLinkReturn lres; GstPad *srcpad, *sinkpad; /* always init first */ gst_init (&argc, &argv); /* the pipeline to hold everything */ pipeline = gst_pipeline_new (NULL); g_assert (pipeline); /* the video capture and format conversion */ videosrc = gst_element_factory_make("videotestsrc", "videosrc"); g_assert (videosrc); // g_object_set (videosrc, "location", "/home/stijn/video.mp4", NULL); decodebin = gst_element_factory_make("decodebin", "decodebin"); g_assert (decodebin); queue = gst_element_factory_make("queue", "queue"); g_assert (queue); videorate = gst_element_factory_make("videorate", "videorate"); g_assert (videorate); ffmpegcolorspace = gst_element_factory_make("ffmpegcolorspace","ffmpegcolorspace"); g_assert (ffmpegcolorspace); /* the encoding and payloading */ videoenc = gst_element_factory_make("x264enc", "videoenc"); g_assert (videoenc); //g_object_set (videoenc, "byte-stream", TRUE, "bitrate", 600, NULL); videopay = gst_element_factory_make("rtph264pay", "videopay"); g_assert (videopay); /* add capture and payloading to the pipeline and link */ gst_bin_add_many (GST_BIN(pipeline), videosrc, decodebin, queue, videorate, ffmpegcolorspace, videoenc, videopay, NULL); g_signal_connect (decodebin, "new-decoded-pad", G_CALLBACK (on_pad_added), queue); res = gst_element_link_many(videosrc, decodebin, NULL); g_assert(res == TRUE); res = gst_element_link_many (queue, videorate, ffmpegcolorspace, videoenc, videopay, NULL); g_assert(res == TRUE); /* the rtpbin element */ rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin"); g_assert (rtpbin); gst_bin_add (GST_BIN (pipeline), rtpbin); /* the udp sinks and source we will use for RTP and RTCP */ rtpsink = gst_element_factory_make ("udpsink", "rtpsink"); g_assert (rtpsink); g_object_set (rtpsink, "port", 5000, "host", DEST_HOST, NULL); rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink"); g_assert (rtcpsink); g_object_set (rtcpsink, "port", 5001, "host", DEST_HOST, NULL); /* no need for synchronisation or preroll on the RTCP sink */ g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL); rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc"); g_assert (rtcpsrc); g_object_set (rtcpsrc, "port", 5007, NULL); gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL); /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */ sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0"); srcpad = gst_element_get_static_pad (videopay, "src"); lres = gst_pad_link (srcpad, sinkpad); g_assert (lres == GST_PAD_LINK_OK); gst_object_unref (srcpad); /* get the RTP srcpad that was created when we requested the sinkpad above and * link it to the rtpsink sinkpad*/ srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0"); sinkpad = gst_element_get_static_pad (rtpsink, "sink"); lres = gst_pad_link (srcpad, sinkpad); g_assert (lres == GST_PAD_LINK_OK); gst_object_unref (srcpad); gst_object_unref (sinkpad); /* get an RTCP srcpad for sending RTCP to the receiver */ srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0"); sinkpad = gst_element_get_static_pad (rtcpsink, "sink"); lres = gst_pad_link (srcpad, sinkpad); g_assert (lres == GST_PAD_LINK_OK); gst_object_unref (sinkpad); /* we also want to receive RTCP, request an RTCP sinkpad for session 0 and * link it to the srcpad of the udpsrc for RTCP */ srcpad = gst_element_get_static_pad (rtcpsrc, "src"); sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0"); lres = gst_pad_link (srcpad, sinkpad); g_assert (lres == GST_PAD_LINK_OK); gst_object_unref (srcpad); /* set the pipeline to playing */ g_print ("starting sender pipeline\n"); gst_element_set_state (pipeline, GST_STATE_PLAYING); /* print stats every second */ g_timeout_add (1000, (GSourceFunc) print_stats, rtpbin); /* we need to run a GLib main loop to get the messages */ loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); g_print ("stopping sender pipeline\n"); gst_element_set_state (pipeline, GST_STATE_NULL); return 0; }