diff --git a/stream_source.c b/stream_source.c index 8ecbbbc..3285bdc 100644 --- a/stream_source.c +++ b/stream_source.c @@ -23,6 +23,7 @@ #include "gst/gstbin.h" #include "gst/gstbus.h" +#include "gst/gstcaps.h" #include "gst/gstclock.h" #include "gst/gstelement.h" #include "gst/gstelementfactory.h" @@ -30,6 +31,7 @@ #include "gst/gstobject.h" #include "gst/gstpipeline.h" #include "gst/gstutils.h" +#include "gst/gstvalue.h" #include #include #include @@ -55,6 +57,7 @@ typedef struct _ConfigData { GUri *url; ///< URL of the server to stream to. Must be a valid URI. GUri *device; ///< Device file. Must be an absolute path. GUri *ssh_key; ///< Path to SSH Key used to authenticate with the server. + GString *passphrase; ///< Passphrase used to encrypt srt traffic with } ConfigData; /** @@ -80,9 +83,12 @@ static int config_callback(void *user, const char *section, const char *name, co } } if (CONF_KEY("remote", "url")) { - GUri *uri = g_uri_parse(value, G_URI_FLAGS_NONE, NULL); - if (uri) { - conf->url = uri; + char *extended_value = calloc(strlen(value) + strlen("https://") + 1, sizeof(char)); + sprintf(extended_value, "https://%s", value); + GUri *url = g_uri_parse(extended_value, G_URI_FLAGS_NONE, NULL); + free(extended_value); + if (url) { + conf->url = url; return 0; } } @@ -96,19 +102,26 @@ static int config_callback(void *user, const char *section, const char *name, co return 0; } } + if (CONF_KEY("remote", "passphrase")) { + conf->passphrase = g_string_new(value); + } return -1; } int main(int argc, char *argv[]) { GstBus *bus; GstMessage *msg; - GstElement *video_queue; + GstElement *video_capsfilter; GstElement *video_convert; - GstElement *video_sink; + GstElement *video_encode; + GstElement *video_parse; + GstElement *mux; + GstElement *srt_sink; CustomData data; GstStateChangeReturn ret; gboolean terminate = FALSE; ConfigData conf; + GString *srt_uri; // Read configuration data from ini file and write to conf. config_callback is called for each value. ini_parse("../config.ini", config_callback, &conf); @@ -116,21 +129,54 @@ int main(int argc, char *argv[]) { // Initialize Gstreamer and set up the pipeline along with its elements. gst_init(&argc, &argv); data.source = gst_element_factory_make("v4l2src", "source"); - video_queue = gst_element_factory_make("queue", "video_queue"); + video_capsfilter = gst_element_factory_make("capsfilter", "video_capsfilter"); video_convert = gst_element_factory_make("videoconvert", "video_convert"); - video_sink = gst_element_factory_make("autovideosink", "video_sink"); + video_encode = gst_element_factory_make("x264enc", "video_encode"); + video_parse = gst_element_factory_make("h264parse", "video_parse"); + mux = gst_element_factory_make("matroskamux", "mux"); + srt_sink = gst_element_factory_make("srtsink", "srt_sink"); data.pipeline = gst_pipeline_new("media-pipeline"); - if (!data.source || !video_queue || !video_convert || !video_sink || !data.pipeline) { + if (!data.source || !video_capsfilter || !video_convert || !video_encode || !video_parse || !mux || !srt_sink || !data.pipeline) { g_printerr("Couldn't create all elements.\n"); + if (!data.source) g_printerr("Couldn't create source.\n"); + if (!video_capsfilter) g_printerr("Couldn't create capsfilter.\n"); + if (!video_convert) g_printerr("Couldn't create convert.\n"); + if (!video_encode) g_printerr("Couldn't create encode.\n"); + if (!video_parse) g_printerr("Couldn't create parse.\n"); + if (!mux) g_printerr("Couldn't create mux.\n"); + if (!srt_sink) g_printerr("Couldn't create sink.\n"); + if (!data.pipeline) g_printerr("Couldn't create pipeline.\n"); return -1; } + // Use the device file specified by configuration as video input source. g_object_set(data.source, "device", g_uri_to_string(conf.device) + strlen("file://"), NULL); + // Set video framerate to 30/1 on video/x-raw + GstCaps *video_caps = gst_caps_new_simple("video/x-raw", "framerate", GST_TYPE_FRACTION, 30, 1, NULL); + g_object_set(video_capsfilter, "caps", video_caps, NULL); + gst_caps_unref(video_caps); + + g_object_set(video_encode, "tune", "zerolatency", NULL); + g_object_set(video_encode, "speed-preset", "veryfast", NULL); + g_object_set(video_encode, "bitrate", 2500, NULL); + g_object_set(video_encode, "key-int-max", 60, NULL); + g_object_set(video_encode, "aud", TRUE, NULL); + + g_object_set(video_parse, "config-interval", -1, NULL); + + g_object_set(mux, "streamable", TRUE, NULL); + + srt_uri = g_string_new(""); + g_string_printf(srt_uri, "srt://%s?mode=caller&pbkeylen=32&passphrase=%s&latency=150", g_uri_to_string(conf.url) + strlen("https://"), conf.passphrase->str); + g_print("%s\n", srt_uri->str); + g_object_set(srt_sink, "uri", srt_uri->str, NULL); + + g_object_set(mux, "streamable", TRUE, NULL); // Add all elements to the pipeline and link them together. - gst_bin_add_many(GST_BIN(data.pipeline), data.source, video_queue, video_convert, video_sink, NULL); - if (!gst_element_link_many(data.source, video_queue, video_convert, video_sink, NULL)) { + gst_bin_add_many(GST_BIN(data.pipeline), data.source, video_capsfilter, video_convert, video_encode, video_parse, mux, srt_sink, NULL); + if (!gst_element_link_many(data.source, video_capsfilter, video_convert, video_encode, video_parse, mux, srt_sink, NULL)) { g_printerr("Couldn't link stream elements.\n"); gst_object_unref(data.pipeline); return -1;