From be982f880df6388bd00386b2cae8c318f902f1b2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 24 Sep 2014 23:34:21 +0200 Subject: moved pipeline creation subs to own file --- src/pipelines.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 src/pipelines.c (limited to 'src/pipelines.c') diff --git a/src/pipelines.c b/src/pipelines.c new file mode 100644 index 0000000..f2f787c --- /dev/null +++ b/src/pipelines.c @@ -0,0 +1,272 @@ +/* + * sydra + * + * sydra is a toolbox which allows you to set up multiple bidirectional + * Video/Audio streams from external locations. + * sydra has been written to be used for the Elevate Festival in Graz + * Austria in order to involve external locations to present themselves + * at the festival. + * Sydra is based on GStreamer and is written in C. + * + * + * Copyright (C) 2014 Christian Pointner + * + * This file is part of sydra. + * + * sydra is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * sydra is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with sydra. If not, see . + * + * In addition, as a special exception, the copyright holders hereby + * grant permission for non-GPL-compatible GStreamer plugins to be used + * and distributed together with GStreamer and sydra. + * This permission goes above and beyond the permissions granted by the + * GPL license sydra is covered by. + */ + +#include "datatypes.h" + +#include +#include + +#include "options.h" +#include "utils.h" +#include "log.h" + + +struct media_elements { + const char* name_; + + const char* src_str_; + GstElement* src_; + + GstElement* tee_raw_; + + const char* enc_str_; + GstElement* enc_; + + GstElement* tee_enc_; + + const char* payloader_str_; + GstElement* payloader_; +}; + +static gboolean create_media_elements(struct media_elements *me, GstElement* pipeline, GstElement *rtp, uint32_t idx) +{ + char bin_name[32]; + + snprintf(bin_name, sizeof(bin_name), "%s source", me->name_); + me->src_ = sydra_create_bin_from_desc(bin_name, me->src_str_); + me->tee_raw_ = sydra_create_element("tee", NULL); + GstElement *qr = sydra_create_element("queue", NULL); + + snprintf(bin_name, sizeof(bin_name), "%s encoder", me->name_); + me->enc_ = sydra_create_bin_from_desc(bin_name, me->enc_str_); + me->tee_enc_ = sydra_create_element("tee", NULL); + GstElement *qe = sydra_create_element("queue", NULL); + + me->payloader_ = sydra_create_element(me->payloader_str_, NULL); + + if(!me->src_ || !me->tee_raw_ || !qr || !me->enc_ || !me->tee_enc_ || !qe || !me->payloader_) { + return FALSE; + } + + log_printf(DEBUG, "%s path created successfully!", me->name_); + + gst_bin_add_many (GST_BIN(pipeline), me->src_, me->tee_raw_, qr, me->enc_, me->tee_enc_, qe, me->payloader_, NULL); + gst_element_link(me->src_, me->tee_raw_); + gst_element_link_many(qr, me->enc_, me->tee_enc_, NULL); + gst_element_link(qe, me->payloader_); + + char pad_name[32]; + snprintf(pad_name, sizeof(bin_name), "send_rtp_sink_%u", idx); + if(!sydra_link_request_static(me->tee_raw_, "src_%u", qr, "sink") || + !sydra_link_request_static(me->tee_enc_, "src_%u", qe, "sink") || + !sydra_link_static_request(me->payloader_, "src", rtp, pad_name)) { + return FALSE; + } + + log_printf(DEBUG, "%s path linked successfully!", me->name_); + return TRUE; +} + +static gboolean create_udp_sinks(options_t* opt, GstElement* pipeline, GstElement* rtp, struct udp_sinks *sinks) +{ + sinks->rtp_video_.udp_ = sydra_create_element("multiudpsink", "udprtpv"); + sinks->rtcp_video_.udp_ = sydra_create_element("multiudpsink", "udprtcpv"); + sinks->rtp_audio_.udp_ = sydra_create_element("multiudpsink", "udprtpa"); + sinks->rtcp_audio_.udp_ = sydra_create_element("multiudpsink", "udprtcpa"); + + if(!(sinks->rtp_video_.udp_) || !(sinks->rtcp_video_.udp_) || !(sinks->rtp_audio_.udp_) || !(sinks->rtcp_audio_.udp_)) + return FALSE; + + log_printf(DEBUG, "udp elements created successfully!"); + + int rtp_port_local = opt->rtp_port_base_local_; + g_object_set(G_OBJECT(sinks->rtp_video_.udp_), "bind-port", rtp_port_local++, NULL); + g_object_set(G_OBJECT(sinks->rtcp_video_.udp_), "bind-port", rtp_port_local++, "sync", FALSE, "async", FALSE, NULL); + g_object_set(G_OBJECT(sinks->rtp_audio_.udp_), "bind-port", rtp_port_local++, NULL); + g_object_set(G_OBJECT(sinks->rtcp_audio_.udp_), "bind-port", rtp_port_local++, "sync", FALSE, "async", FALSE, NULL); + + if(opt->rtp_host_) { + int rtp_port = opt->rtp_port_base_; + g_signal_emit_by_name(G_OBJECT(sinks->rtp_video_.udp_), "add", opt->rtp_host_, rtp_port++, NULL); + g_signal_emit_by_name(G_OBJECT(sinks->rtcp_video_.udp_), "add", opt->rtp_host_, rtp_port++, NULL); + g_signal_emit_by_name(G_OBJECT(sinks->rtp_audio_.udp_), "add", opt->rtp_host_, rtp_port++, NULL); + g_signal_emit_by_name(G_OBJECT(sinks->rtcp_audio_.udp_), "add", opt->rtp_host_, rtp_port++, NULL); + // TODO: add this to the clients_ list + } + if(opt->rtp_addr_local_) { + g_object_set(G_OBJECT(sinks->rtp_video_.udp_), "bind-address", opt->rtp_addr_local_, NULL); + g_object_set(G_OBJECT(sinks->rtcp_video_.udp_), "bind-address", opt->rtp_addr_local_, NULL); + g_object_set(G_OBJECT(sinks->rtp_audio_.udp_), "bind-address", opt->rtp_addr_local_, NULL); + g_object_set(G_OBJECT(sinks->rtcp_audio_.udp_), "bind-address", opt->rtp_addr_local_, NULL); + } + + gst_bin_add_many(GST_BIN (pipeline), sinks->rtp_video_.udp_, sinks->rtcp_video_.udp_, sinks->rtp_audio_.udp_, sinks->rtcp_audio_.udp_, NULL); + + if(!sydra_link_static_static(rtp, "send_rtp_src_0", sinks->rtp_video_.udp_, "sink") || + !sydra_link_request_static(rtp, "send_rtcp_src_0", sinks->rtcp_video_.udp_, "sink") || + !sydra_link_static_static(rtp, "send_rtp_src_1", sinks->rtp_audio_.udp_, "sink") || + !sydra_link_request_static(rtp, "send_rtcp_src_1", sinks->rtcp_audio_.udp_, "sink")) + return FALSE; + + log_printf(DEBUG, "udp elements linked successfully!"); + + return TRUE; +} + +static gboolean create_preview_elements(const char* preview_bin_desc, GstElement* pipeline, GstElement* tee) +{ + GstElement *qr = sydra_create_element("queue", NULL); + GstElement *to = sydra_create_element("textoverlay", NULL); + GstElement *preview_bin = sydra_create_bin_from_desc("preview sink", preview_bin_desc); + + if(!qr || !to || !preview_bin) { + return FALSE; + } + + log_printf(DEBUG, "preview path created successfully!"); + + g_object_set(G_OBJECT(to), "text", " local ", "halignment", 1 , "valignment", 0, + "shaded-background", TRUE, "font-desc", "Sans 18", NULL); + + gst_bin_add_many (GST_BIN(pipeline), qr, to, preview_bin, NULL); + gst_element_link_many(qr, to, preview_bin, NULL); + + if(!sydra_link_request_static(tee, "src_%u", qr, "sink")) { + return FALSE; + } + + log_printf(DEBUG, "preview path linked successfully!"); + return TRUE; +} + +static gboolean create_recorder_elements(options_t* opt, GstElement* pipeline, struct media_elements *mv, struct media_elements *ma) +{ + GstElement *qv = sydra_create_element("queue", NULL); + GstElement *qa = sydra_create_element("queue", NULL); + GstElement *mux = sydra_create_element(opt->rec_mux_, NULL); + GstElement *sink = sydra_create_element("filesink", NULL); + + if(!qv || !qa || !mux || !sink) { + return FALSE; + } + + GstElement *ev = NULL, *tv = mv->tee_enc_; + if(opt->video_enc_rec_) { + ev = sydra_create_bin_from_desc("record video encoder", opt->video_enc_rec_); + if(!ev) return FALSE; + tv = mv->tee_raw_; + } + GstElement *ea = NULL, *ta = ma->tee_enc_; + if(opt->audio_enc_rec_) { + ea = sydra_create_bin_from_desc("record audio encoder", opt->audio_enc_rec_); + if(!ea) return FALSE; + ta = ma->tee_raw_; + } + + log_printf(DEBUG, "recorder path created successfully!"); + + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + struct tm bd_time; + localtime_r(&(now.tv_sec), &bd_time); + char recfile[1024]; //TODO: fix this hardcoded length + recfile[0] = 0; + strftime(recfile, sizeof(recfile), opt->rec_name_format_, &bd_time); + g_object_set(G_OBJECT(sink), "location", recfile, NULL); + + gst_bin_add_many(GST_BIN(pipeline), qv, qa, mux, sink, NULL); + gst_element_link(mux, sink); + GstElement* sv = qv; + if(ev) { + gst_bin_add(GST_BIN(pipeline), ev); + gst_element_link(qv, ev); + sv = ev; + } + GstElement* sa = qa; + if(ev) { + gst_bin_add(GST_BIN(pipeline), ea); + gst_element_link(qa, ea); + sa = ea; + } + + if(!sydra_link_request_static(tv, "src_%u", qv, "sink") || + !sydra_link_static_compatible(sv, "src", mux) || + !sydra_link_request_static(ta, "src_%u", qa, "sink") || + !sydra_link_static_compatible(sa, "src", mux)) { + return FALSE; + } + + log_printf(DEBUG, "recorder path linked successfully!"); + return TRUE; +} + +GstElement* create_pipeline(options_t* opt, struct udp_sinks *udp) +{ + GstElement *pipeline = gst_pipeline_new ("sydra"); + if(!pipeline) { + log_printf(ERROR, "Creating pipeline failed!"); + return NULL; + } + GstElement *rtp = sydra_create_element("rtpbin", "rtpbin"); + if(!rtp || !gst_bin_add(GST_BIN(pipeline), rtp)) { + return NULL; + } + log_printf(DEBUG, "rtpbin created successfully!"); + + struct media_elements video = { "video", opt->video_src_, NULL, NULL, + opt->video_enc_, NULL, NULL, + opt->video_payloader_, NULL }; + struct media_elements audio = { "audio", opt->audio_src_, NULL, NULL, + opt->audio_enc_, NULL, NULL, + opt->audio_payloader_, NULL }; + if(!create_media_elements(&video, pipeline, rtp, 0) || + !create_media_elements(&audio, pipeline, rtp, 1) || + !create_udp_sinks(opt, pipeline, rtp, udp)) { + return NULL; + } + + if(opt->preview_videosink_) { + if(!create_preview_elements(opt->preview_videosink_, pipeline, video.tee_raw_)) + return NULL; + } + + if(opt->rec_mux_) { + if(!create_recorder_elements(opt, pipeline, &video, &audio)) + return NULL; + } + + log_printf(DEBUG, "pipeline created successfully!"); + return pipeline; +} -- cgit v1.2.3