1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* 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 <equinox@spreadspace.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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 <gst/gst.h>
#include <stdio.h>
#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_sender_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;
}
|