-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipeline_logger.cpp
More file actions
executable file
·354 lines (323 loc) · 10.1 KB
/
Copy pathpipeline_logger.cpp
File metadata and controls
executable file
·354 lines (323 loc) · 10.1 KB
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
/*
* Copyright (C) 2016 RDK Management
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string>
#include <gst/gst.h>
#define DESC_ARRAY_SIZE 10
static void correct_indentation(std::string *desc, int desc_lines)
{
int max_size = 0;
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if( (desc_lines & (0x1 << i)) && (desc[i].length() > max_size))
{
max_size = desc[i].length();
}
}
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(desc_lines & (0x1 << i))
{
int size_diff = max_size - desc[i].length();
if(size_diff > 0)
{
for(int j = 0; j < size_diff; j++)
{
desc[i] = " " + desc[i];
}
}
}
}
}
static int add_element_to_list(GstElement *element, std::string *desc, std::string *head, int indeces)
{
GstIterator *it = gst_element_iterate_sink_pads(element);
bool done = false, has_pad = false, pos_found = false;
GValue item = G_VALUE_INIT;
GstPad *pad;
int size_diff = 0, max_size = 0, index = 0;
int desc_lines = 0, head_indeces = 0;
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(indeces & (0x01 << i))
{
if( (desc[i].length() > max_size))
{
max_size = desc[i].length();
}
if(!pos_found)
{
pos_found = true;
index = i;
}
desc_lines = desc_lines | (0x1 << i) ;
}
}
correct_indentation(desc, desc_lines);
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(indeces & (0x01 << i))
{
size_diff = max_size - desc[i].length();
if(size_diff > 0)
{
for(int j = 0; j < size_diff; j++)
{
desc[i] = "─" + desc[i];
}
}
if(i != index)
{
auto element_name = gst_element_get_name(element);
int size = strlen(element_name);
for (int j = 0; j < size; j++)
{
desc[i] = " " + desc[i];
}
g_free(element_name);
}
head[i] = "\0";
}
}
auto element_name = gst_element_get_name(element);
desc[index] = element_name + desc [index];
g_free(element_name);
correct_indentation(desc, desc_lines);
int i = 0;
while(!done && ( index < DESC_ARRAY_SIZE))
{
switch (gst_iterator_next(it, &item)) {
case GST_ITERATOR_OK:
pad = static_cast <GstPad *>(g_value_get_object(&item));
if(nullptr == pad)
{
g_value_reset(&item);
break;
}
while(!head[index].empty())
{
index++;
}
head[index] = GST_ELEMENT_NAME(element);
head[index] += ":";
head[index] += GST_PAD_NAME(pad);
desc[index] = ">" + desc [index];
has_pad = true;
head_indeces |= 0x1 << index;
g_value_reset(&item);
break;
case GST_ITERATOR_RESYNC:
gst_iterator_resync(it);
break;
default:
done = TRUE;
break;
}
}
g_value_unset(&item);
gst_iterator_free(it);
if(!has_pad)
{
while(!head[index].empty())
{
index++;
}
head[index] = "--:--";
head_indeces |= 0x1 << index;
}
correct_indentation(desc, head_indeces);
return desc_lines;
}
static int find_head(GstElement *element, std::string *desc, std::string *head, int index, int &indeces)
{
GstIterator *it = gst_element_iterate_src_pads(element);
GValue item = G_VALUE_INIT;
gboolean done = FALSE;
GstPad *pad, *peer_pad;
GstElement *peer_parent;
std::string src_pad;
int i, j = 0, pos;
while (!done) {
switch (gst_iterator_next(it, &item)) {
case GST_ITERATOR_OK:
pad = static_cast <GstPad *>(g_value_get_object(&item));
if(nullptr == pad)
{
g_value_reset(&item);
break;
}
peer_pad = gst_pad_get_peer(pad);
if(peer_pad)
{
peer_parent = gst_pad_get_parent_element(peer_pad);
if(peer_parent)
{
src_pad = GST_ELEMENT_NAME(peer_parent);
src_pad += ":";
src_pad += GST_PAD_NAME(peer_pad);
for(i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(head[i].compare(src_pad) == 0)
{
indeces = indeces | (0x1 << i);
}
}
gst_object_unref(peer_parent);
}
gst_object_unref(peer_pad);
}
g_value_reset(&item);
break;
case GST_ITERATOR_RESYNC:
gst_iterator_resync(it);
break;
default:
done = TRUE;
break;
}
}
g_value_unset(&item);
gst_iterator_free(it);
if(indeces == 0)
{
for(i = index; i < DESC_ARRAY_SIZE; i++)
{
if(head[i].empty())
{
size_t pipeline_start = desc[index].find("]");
if ((pipeline_start != std::string::npos) && (i != index))
{
int diff = desc[index].size() - pipeline_start;
int gap = (diff > 0)? (diff - desc[i].size()) : 0;
for(int j = 0; (gap > 0) && (j < gap); j++)
{
desc [i] = " " + desc[i];
}
desc [i] = "]" + desc[i];
}
indeces = indeces | (0x1 << i);
break;
}
}
}
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
int a = indeces & (0x1 << i);
if( a != 0)
{
pos = i;
break;
}
}
return pos;
}
static int print_pipeline(GstElement * element, std::string *desc, int index)
{
GstIterator *itr;
GstElement * child_element;
GValue next_item = {0, };
std::string head[DESC_ARRAY_SIZE] = {};
int desc_lines = 0, pos = 0;
desc_lines = 0x1 << index;
itr = gst_bin_iterate_sorted(GST_BIN(element));
int numChildren = GST_BIN_NUMCHILDREN(element);
while(numChildren --)
{
int indeces = 0;
switch (gst_iterator_next(itr, &next_item))
{
case GST_ITERATOR_OK:
child_element = GST_ELEMENT(g_value_get_object(&next_item));
pos = find_head(child_element, desc, head, index, indeces);
correct_indentation(desc, indeces);
if(GST_IS_BIN(child_element))
{
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(indeces & (0x01 << i))
{
desc[i] = "]" + desc[i];
}
}
int child_indeces = print_pipeline(child_element, desc, pos);
desc_lines = desc_lines | child_indeces;
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(child_indeces & (0x01 << i))
{
desc[i] = "[" + desc[i];
}
}
}
desc_lines = desc_lines | add_element_to_list(child_element, desc, head, indeces);
g_value_reset(&next_item);
break;
case GST_ITERATOR_RESYNC:
gst_iterator_resync(itr);
break;
default:
break;
}
}
correct_indentation(desc, desc_lines);
gst_iterator_free(itr);
return desc_lines;
}
static std::string trim_excess_whitespace(const std::string &str)
{
std::string result;
result.reserve(str.size()); // Reserve space to avoid unnecessary reallocations
bool previous_space = false;
for(char c : str)
{
if (c == ' ')
{
if (!previous_space)
{
result += c;
}
previous_space = true;
}
else
{
result += c;
previous_space = false;
}
}
return result;
}
extern "C" void dump_pipeline_info(GstBin *bin)
{
std::string desc[DESC_ARRAY_SIZE] = {};
printf("Dumping pipeline: %s\n", GST_ELEMENT_NAME(GST_ELEMENT(bin)));
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
desc[i] = "]";
}
int indeces = print_pipeline(GST_ELEMENT(bin), desc, 0);
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
desc[i] = "[" + desc[i];
}
for(int i = 0; i < DESC_ARRAY_SIZE; i++)
{
if(indeces & (0x1 << i))
{
printf("%d : %s\n", desc[i].size(), trim_excess_whitespace(desc[i]).c_str());
}
}
}