/
GstShow.cpp
380 lines (300 loc) · 10.5 KB
/
GstShow.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "log.h"
#include "GstShow.hpp"
#include "GstCommon.hpp"
#include <gst/net/gstnet.h>
#define PLAYBACK_DELAY_MS 50
#define PIPELINE_LATENCY_MS 500
/*!
* \brief GstOutput::init
* initialise all variables
*/
void GstShow::init()
{
pipeline = NULL;
source[0] = NULL;
source[1] = NULL;
scale[0] = NULL;
scale[1] = NULL;
scalefilter[0] = NULL;
scalefilter[1] = NULL;
queue[0] = NULL;
queue[1] = NULL;
mixer = NULL;
videosink = NULL;
buffer = NULL;
num_frames = 0;
framerate = 0;
}
/*!
* \brief GstOutput::close
* ends the pipeline by sending EOS and destroys the pipeline and all
* elements afterwards
*/
void GstShow::close()
{
GstStateChangeReturn status;
if (pipeline)
{
handleMessage(pipeline);
//
//for (int i=0; i<2;i++)
//{
// if (gst_app_src_end_of_stream(GST_APP_SRC(source[i])) != GST_FLOW_OK)
// {
// L_(lwarning) << ("Cannot send EOS to GStreamer pipeline\n");
// return;
// }
//}
//
////wait for EOS to trickle down the pipeline. This will let all elements finish properly
//GstBus* bus = gst_element_get_bus(pipeline);
//GstMessage *msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
//if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR)
//{
// L_(lwarning) << ("Error during GstOut finalization\n");
// return;
//}
//
//if(msg != NULL)
//{
// gst_message_unref(msg);
// g_object_unref(G_OBJECT(bus));
//}
status = gst_element_set_state (pipeline, GST_STATE_NULL);
if (status == GST_STATE_CHANGE_ASYNC)
{
// wait for status update
GstState st1;
GstState st2;
status = gst_element_get_state(pipeline, &st1, &st2, GST_CLOCK_TIME_NONE);
}
if (status == GST_STATE_CHANGE_FAILURE)
{
handleMessage (pipeline);
gst_object_unref (GST_OBJECT (pipeline));
pipeline = NULL;
L_(lwarning) << ("Unable to stop gstreamer pipeline\n");
return;
}
status = gst_element_set_state (pipeline, GST_STATE_NULL);
if (status == GST_STATE_CHANGE_ASYNC)
{
// wait for status update
GstState st1;
GstState st2;
status = gst_element_get_state(pipeline, &st1, &st2, GST_CLOCK_TIME_NONE);
}
if (status == GST_STATE_CHANGE_FAILURE)
{
handleMessage (pipeline);
gst_object_unref (GST_OBJECT (pipeline));
pipeline = NULL;
L_(lwarning) << ("Unable to stop gstreamer pipeline\n");
return;
}
gst_object_unref (GST_OBJECT (pipeline));
pipeline = NULL;
}
}
void
GstShow::source_created (GstElement * pipe, GstElement * source)
{
g_object_set (source, "latency", PLAYBACK_DELAY_MS,
"ntp-time-source", 3, "buffer-mode", 4, "ntp-sync", TRUE, NULL);
g_print("Done setting source!\n") ;
}
void GstShow::newPad(GstElement *src,
GstPad *new_pad,
gpointer data)
{
GstPad *sink_pad;
GstElement *color = (GstElement *) data;
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
new_pad_caps = gst_pad_get_current_caps (new_pad);
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
new_pad_type = gst_structure_get_name (new_pad_struct);
g_print (" It has type '%s'.\n", new_pad_type);
sink_pad = gst_element_get_static_pad (color, "sink");
if (!sink_pad){
L_(lerror) << "Gstreamer: no pad named sink";
return;
}
/* If our converter is already linked, we have nothing to do here */
if (gst_pad_is_linked (sink_pad)) {
g_print (" We are already linked. Ignoring.\n");
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (sink_pad);
return;
}
/* Attempt the link */
ret = gst_pad_link (new_pad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)) {
g_print (" Type is '%s' but link failed.\n", new_pad_type);
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (sink_pad);
return;
} else {
g_print (" Link succeeded (type '%s').\n", new_pad_type);
}
}
/*!
* \brief GstOutput::openAppSrc
* \param filename filename to output to
* \param fourcc desired codec fourcc
* \param fps desired framerate
* \param frameSize the size of the expected frames
* \param is_color color or grayscale
* \return success
*
*
*/
bool GstShow::open(int xwinid, std::string Laddress, std::string Raddress)
{
// init gstreamer
gst_initializer::init();
// init vars
//int bufsize = 0;
bool file = false;
char *uriL = NULL;
char *uriR = NULL;
init_pipeline(xwinid);
if(!gst_uri_is_valid(Laddress.c_str()) || !gst_uri_is_valid(Raddress.c_str()))
{
uriL = realpath(Laddress.c_str(), NULL);
uriR = realpath(Raddress.c_str(), NULL);
if(uriL != NULL && uriR != NULL)
{
uriL = g_filename_to_uri(uriL, NULL, NULL);
uriR = g_filename_to_uri(uriR, NULL, NULL);
if(uriL != NULL && uriR != NULL)
{
file = true;
}
else
{
L_(lerror) << "GStreamer: Error opening file";
close();
return false;
}
}
} else {
file = false;
uriL = g_strdup(Laddress.c_str());
uriR = g_strdup(Raddress.c_str());
}
if (!file)
{
global_clock = gst_system_clock_obtain ();
gst_net_time_provider_new (global_clock, "0.0.0.0", 8554);
if (global_clock != NULL)
L_(ldebug2) << ("Clock created!");
else
{
L_(lerror) << ("Could not creaye clock!");
return false;
}
gst_pipeline_use_clock (GST_PIPELINE (pipeline), global_clock);
}
for (int i = 0; i<2; i++)
{
source[i] = gst_element_factory_make("uridecodebin", NULL);
gst_bin_add_many(GST_BIN(pipeline), source[i], NULL);
if (i==0)
g_object_set (source[i], "uri", uriL, NULL);
if (i==1)
g_object_set (source[i], "uri", uriR, NULL);
//sync if not file!
if (!file)
{
g_signal_connect (source[i], "source-setup", G_CALLBACK (source_created), NULL);
}
//Set latency if we have a stream
if (!file)
{
/* Set this high enough so that it's higher than the minimum latency
* on all receivers */
gst_pipeline_set_latency (GST_PIPELINE (pipeline), PIPELINE_LATENCY_MS * GST_MSECOND);
}
g_signal_connect(source[i], "pad-added", G_CALLBACK(newPad), queue[i]);
}
return finish_pipeline();
}
bool GstShow::init_pipeline(const int xwinid)
{
pipeline = gst_pipeline_new ("xvoverlay");
//create base pipeline elements
videosink = gst_element_factory_make("xvimagesink", NULL);
gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (videosink), xwinid);
mixer = gst_element_factory_make("videomixer", "mix");
///* Manually linking the videoboxes to the mixer */
GstPadTemplate *mixer_sink_pad_template = gst_element_class_get_pad_template(GST_ELEMENT_GET_CLASS(mixer), "sink_%u");
if(mixer_sink_pad_template == NULL) {
g_printerr("Could not get mixer pad template.\n");
// gst_object_unref(something);
return false;
}
GstPad* mixerpads[2];
mixerpads[0] = gst_element_request_pad(mixer, mixer_sink_pad_template, NULL, NULL);
mixerpads[1] = gst_element_request_pad(mixer, mixer_sink_pad_template, NULL, NULL);
g_object_set(mixerpads[0], "xpos", 0, NULL);
g_object_set(mixerpads[0], "ypos", 0, NULL);
g_object_set(mixerpads[0], "alpha",1.0, NULL);
g_object_set(mixerpads[1], "xpos", 640, NULL);
g_object_set(mixerpads[1], "ypos", 0, NULL);
g_object_set(mixerpads[1], "alpha",1.0, NULL);
gst_object_unref(mixerpads[0]);
gst_object_unref(mixerpads[1]);
// prepare queue and scale
for (int i = 0; i<2; i++)
{
queue[i] = gst_element_factory_make("queue", NULL);
scale[i] = gst_element_factory_make("videoscale", NULL);
scalefilter[i] = gst_element_factory_make("capsfilter", NULL);
GstCaps *caps = gst_caps_new_simple("video/x-raw",
"width", G_TYPE_INT, 640,
"height", G_TYPE_INT, 480,
//"format", G_TYPE_STRING, "BGR",
NULL);
caps = gst_caps_fixate(caps);
g_object_set(G_OBJECT(scalefilter[i]), "caps", caps, NULL);
gst_caps_unref(caps);
}
gst_bin_add_many(GST_BIN(pipeline), queue[0], queue[1], scale[0], scale[1],
scalefilter[0], scalefilter[1], mixer, videosink, NULL);
return true;
}
bool GstShow::finish_pipeline()
{
GError *err = NULL;
GstStateChangeReturn stateret;
for (int i = 0; i<2; i++)
{
if(!gst_element_link_many(queue[i], scale[i], scalefilter[i], mixer, NULL)) {
L_(lerror) << ("GStreamer: cannot link elements in finish\n");
return false;
}
}
if(!gst_element_link(mixer , videosink)) {
L_(lerror) << ("GStreamer: cannot link mixer to sink\n");
return false;
}
// prepare the pipeline
stateret = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
if(stateret == GST_STATE_CHANGE_FAILURE) {
handleMessage(pipeline);
L_(lerror) << ( "GStreamer: cannot put pipeline to play\n");
}
handleMessage(pipeline);
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
return true;
}