-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_fuser.c
292 lines (248 loc) · 9.87 KB
/
video_fuser.c
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
#include <gst/gst.h>
int main(int argc, char *argv[]) {
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
GstElement *pipeline ,*comp,*video_test_src, *v4l2_src, *nvvidconv1, *nvvidconv2, *nvvidconv3, *video_convert, *main_sink, *vts_filter,*nvv1_filter,*nvv2_filter,*nvc1_filter,*vc_filter;
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Create the elements */
pipeline = gst_pipeline_new("video-pipeline");
//compositor
comp = gst_element_factory_make("nvcompositor", "comp");
//sources
video_test_src = gst_element_factory_make("videotestsrc", "video_test_src");
v4l2_src = gst_element_factory_make("v4l2src", "v4l2_src");
//capfilters
vts_filter = gst_element_factory_make("capsfilter","vts_filter");
nvv1_filter = gst_element_factory_make("capsfilter","nvv1_filter");
nvv2_filter = gst_element_factory_make("capsfilter","nvv2_filter");
nvc1_filter = gst_element_factory_make("capsfilter","nvc1_filter");
vc_filter = gst_element_factory_make("capsfilter","nvc2_filter");
//converters
nvvidconv1 = gst_element_factory_make("nvvidconv", "nvvidconv1");
nvvidconv2 = gst_element_factory_make("nvvidconv", "nvvidconv2");
nvvidconv3 = gst_element_factory_make("nvvidconv", "nvvidconv3");
video_convert = gst_element_factory_make("videoconvert", "video_convert"); // Convert to non-NVMM format
//sink
main_sink = gst_element_factory_make("xvimagesink", "main_sink");
/* Check elements creation */
if (!pipeline || !comp || !video_test_src || !v4l2_src || !nvvidconv1 || !nvvidconv2 || !nvvidconv3 || !video_convert || !main_sink || !vts_filter || !nvv1_filter|| !nvv2_filter || !nvc1_filter || !vc_filter) {
g_printerr("One or more elements could not be created. Exiting.\n");
return -1;
}
GstCaps *rgba_caps_3 = gst_caps_new_simple("video/x-raw",
"memory", G_TYPE_STRING, "NVMM",
"format", G_TYPE_STRING, "RGBA",
NULL);
g_object_set (G_OBJECT (nvv1_filter), "caps", rgba_caps_3, NULL);
g_object_set (G_OBJECT (nvc1_filter), "caps", rgba_caps_3, NULL);
gst_caps_unref (rgba_caps_3);
GstCaps *rgba_wh_caps_4 = gst_caps_new_simple("video/x-raw",
"memory", G_TYPE_STRING, "NVMM",
"format", G_TYPE_STRING, "RGBA",
"width", G_TYPE_INT, 480,
"height", G_TYPE_INT, 360,
NULL);
g_object_set (G_OBJECT (nvv2_filter), "caps", rgba_wh_caps_4, NULL);
gst_caps_unref (rgba_wh_caps_4);
GstCaps *yuv_wh_caps_2 = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "YUY2",
"width", G_TYPE_INT, 480,
"height", G_TYPE_INT, 360,
"framerate", GST_TYPE_FRACTION, 30, 1,
NULL);
g_object_set (G_OBJECT (vts_filter), "caps", yuv_wh_caps_2, NULL);
gst_caps_unref (yuv_wh_caps_2);
GstCaps *rgba_caps_1 = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "RGBA",
NULL);
g_object_set (G_OBJECT (vc_filter), "caps", rgba_caps_1, NULL);
gst_caps_unref (rgba_caps_1);
#ifdef USE_PAD_LINK_METHOD
/* Set properties for the compositor sink_0 */
GstPad *comp_src_0_pad = gst_element_get_static_pad(comp, "src");
GstPad *v4l2src_src_pad = gst_element_get_static_pad(v4l2_src, "src");
GstPad *videotestsrc_src_pad = gst_element_get_static_pad(video_test_src, "src");
GstPad *conv1_sink_pad = gst_element_get_static_pad(nvvidconv1, "sink");
GstPad *conv1_src_pad = gst_element_get_static_pad(nvvidconv1, "src");
// gst_pad_use_fixed_caps(conv1_sink_pad);
GstPad *conv2_sink_pad = gst_element_get_static_pad(nvvidconv2, "sink");
GstPad *conv2_src_pad = gst_element_get_static_pad(nvvidconv2, "src");
GstPad *conv3_sink_pad = gst_element_get_static_pad(nvvidconv3, "sink");
GstPad *conv3_src_pad = gst_element_get_static_pad(nvvidconv3, "src");
GstPad *video_convert_sink_pad = gst_element_get_static_pad(video_convert, "sink");
GstPad *video_convert_src_pad = gst_element_get_static_pad(video_convert, "src");
GstPad *main_sink_sink_pad = gst_element_get_static_pad(main_sink, "sink");
#endif
GstPad *nvv1_filter_src_pad = gst_element_get_static_pad(nvv1_filter, "src");
GstPad *nvv2_filter_src_pad = gst_element_get_static_pad(nvv2_filter, "src");
GstPad *comp_sink_0_pad = gst_element_get_request_pad(comp, "sink_0");
GstPad *comp_sink_1_pad = gst_element_get_request_pad(comp, "sink_1");
g_object_set(G_OBJECT(comp_sink_0_pad),
"xpos", 0,
"ypos", 0,
"width", 480,
"height", 360,
"zorder", 1,
"alpha", 1.0,
NULL);
/* Set properties for the compositor sink_1 */
g_object_set(G_OBJECT(comp_sink_1_pad),
"xpos", 0,
"ypos", 0,
"width", 480,
"height", 360,
"zorder", 2,
"alpha", 0.5,
NULL);
/* Set properties for the source */
g_object_set(G_OBJECT(video_test_src),
"pattern", 0, // 0 is the default pattern (smpte)
NULL);
/* Add elements to the pipeline */
gst_bin_add_many(GST_BIN(pipeline), comp, video_test_src, v4l2_src, nvvidconv1, nvvidconv2, nvvidconv3, video_convert, main_sink, vts_filter, nvv1_filter, nvv2_filter, nvc1_filter, vc_filter, NULL);
#if 0
if (!gst_pad_set_caps(conv1_sink_pad, yuv_wh_caps_2)) {
g_printerr("Failed to set caps videotest src pad.\n");
return -1;
}
#endif
#ifndef USE_PAD_LINK_METHOD
if(!gst_element_link_many(video_test_src,vts_filter,nvvidconv1,nvv1_filter, NULL)){
g_printerr("2Elements could not be linked. \n");
gst_object_unref(pipeline);
return -1 ;
}
if(!gst_element_link_many(v4l2_src,video_convert,vc_filter, nvvidconv2,nvv2_filter, NULL)){
g_printerr("3Elements could not be linked. \n");
gst_object_unref(pipeline);
return -1 ;
}
if(!gst_element_link_many(comp,nvc1_filter,nvvidconv3,main_sink, NULL)){
g_printerr("1Elements could not be linked. \n");
gst_object_unref(pipeline);
return -1 ;
}
if (gst_pad_link( nvv1_filter_src_pad, comp_sink_0_pad) != GST_PAD_LINK_OK) {
g_printerr("44Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
if (gst_pad_link( nvv2_filter_src_pad, comp_sink_1_pad) != GST_PAD_LINK_OK) {
g_printerr("33Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
#endif
#ifdef USE_PAD_LINK_METHOD
/* videotestsrc pipeline */
if (gst_pad_link( videotestsrc_src_pad, conv1_sink_pad) != GST_PAD_LINK_OK) {
g_printerr("Failed to link conv1_sink_pad to coi@mp_src0pad\n");
return -1;
}
if (gst_pad_link( conv1_src_pad, comp_sink_0_pad) != GST_PAD_LINK_OK) {
g_printerr("Failed to link conv1_sink_pad to comp_src0pad\n");
return -1;
}
// gst_object_unref(conv1_src_pad);
/* Set the caps on conv1_pad and comp_src0pad*/
#ifdef TO_REINSTATE_aFTER_VERIFICATION
if (!gst_pad_set_caps(conv1_src_pad, rgba_nvmm_caps_1)) {
g_printerr("Failed to set caps on conv1 pad.\n");
return -1;
}
if (!gst_pad_set_caps(comp_sink_0_pad, rgba_nvmm_caps_1)) {
g_printerr("Failed to set caps on sink-0comp pad.\n");
return -1;
}
#endif
// gst_object_unref(comp_sink_0_pad);
//2nd pipeline
if (gst_pad_link( v4l2src_src_pad, video_convert_sink_pad) != GST_PAD_LINK_OK) {
g_printerr("2Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
if (gst_pad_link( video_convert_src_pad, conv2_sink_pad) != GST_PAD_LINK_OK) {
g_printerr("1Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
if (gst_pad_link( conv2_src_pad, comp_sink_1_pad) != GST_PAD_LINK_OK) {
g_printerr("33Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
#if 0
/* Set the caps on conv1_pad and comp_src0pad*/
if (!gst_pad_set_caps(conv2_src_pad, rgba_wh_caps_4)) {
g_printerr("Failed to set caps on conv1 pad.\n");
return -1;
}
if (!gst_pad_set_caps(comp_sink_1_pad, rgba_wh_caps_4)) {
g_printerr("Failed to set caps on conv1 pad.\n");
return -1;
}
#endif
// gst_object_unref(comp_sink_1_pad);
//main pipeline
//here we join the two sources to compositor.
if (gst_pad_link( comp_src_0_pad, conv3_sink_pad) != GST_PAD_LINK_OK) {
g_printerr("55Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
if (gst_pad_link( conv3_src_pad, main_sink_sink_pad) != GST_PAD_LINK_OK) {
g_printerr("Failed to link sink_0 pad of comp to conv1.\n");
return -1;
}
#endif
/* Set the pipeline state to playing */
ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (pipeline);
return -1;
}
/* Wait until error or EOS */
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Parse message */
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %s\n",
GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %s\n",
debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
/* Free resources */
// gst_object_unref(conv2_sink_pad);
#if 0
gst_object_unref(comp_src_0_pad);
gst_object_unref(conv1_sink_pad);
/* Release elements */
gst_object_unref(main_sink);
gst_object_unref(video_convert);
gst_object_unref(nvvidconv3);
gst_object_unref(nvvidconv2);
gst_object_unref(nvvidconv1);
gst_object_unref(v4l2_src);
gst_object_unref(video_test_src);
gst_object_unref(comp);
gst_object_unref(pipeline);
#endif
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
return 0;
}