summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/gstreamer/common/qgst_debug.cpp
blob: 0461bfe0969111432b76105f52051ef8e022b435 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qgst_debug_p.h"
#include "qgstreamermessage_p.h"

#include <gst/gstclock.h>

QT_BEGIN_NAMESPACE

// NOLINTBEGIN(performance-unnecessary-value-param)

QDebug operator<<(QDebug dbg, const QGString &str)
{
    return dbg << str.get();
}

QDebug operator<<(QDebug dbg, const QGstCaps &caps)
{
    return dbg << caps.caps();
}

QDebug operator<<(QDebug dbg, const QGstStructure &structure)
{
    return dbg << structure.structure;
}

QDebug operator<<(QDebug dbg, const QGValue &value)
{
    return dbg << value.value;
}

QDebug operator<<(QDebug dbg, const QGstreamerMessage &msg)
{
    return dbg << msg.message();
}

QDebug operator<<(QDebug dbg, const QUniqueGErrorHandle &handle)
{
    return dbg << handle.get();
}

QDebug operator<<(QDebug dbg, const QUniqueGStringHandle &handle)
{
    return dbg << handle.get();
}

QDebug operator<<(QDebug dbg, const QGstElement &element)
{
    return dbg << element.element();
}

QDebug operator<<(QDebug dbg, const QGstPad &pad)
{
    return dbg << pad.pad();
}

QDebug operator<<(QDebug dbg, const GstCaps *caps)
{
    if (caps)
        return dbg << QGString(gst_caps_to_string(caps));
    else
        return dbg << "null";
}

QDebug operator<<(QDebug dbg, const GstVideoInfo *info)
{
#if GST_CHECK_VERSION(1, 20, 0)
    return dbg << QGstCaps{
        gst_video_info_to_caps(info),
        QGstCaps::NeedsRef,
    };
#else
    return dbg << QGstCaps{
        gst_video_info_to_caps(const_cast<GstVideoInfo *>(info)),
        QGstCaps::NeedsRef,
    };
#endif
}

QDebug operator<<(QDebug dbg, const GstStructure *structure)
{
    if (structure)
        return dbg << QGString(gst_structure_to_string(structure));
    else
        return dbg << "null";
}

QDebug operator<<(QDebug dbg, const GstObject *object)
{
    dbg << QGString{gst_object_get_name(const_cast<GstObject*>(object))};

    {
        QDebugStateSaver saver(dbg);
        dbg.nospace();

        dbg << "{";

        guint numProperties;
        GParamSpec **properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(object), &numProperties);

        for (guint i = 0; i < numProperties; i++) {
            GParamSpec *param = properties[i];

            const gchar *name = g_param_spec_get_name(param);
            constexpr bool trace_blurb = false;
            if constexpr (trace_blurb) {
                const gchar *blurb = g_param_spec_get_blurb(param);
                dbg << name << " (" << blurb << "): ";
            } else
                dbg << name << ": ";

            bool readable = bool(param->flags & G_PARAM_READABLE);
            if (!readable) {
                dbg << "(not readable)";
            } else if (QLatin1StringView(name) == QLatin1StringView("parent")) {
                if (object->parent)
                    dbg << QGString{ gst_object_get_name(object->parent) };
                else
                    dbg << "(none)";
            } else {
                GValue value = {};
                g_object_get_property(&const_cast<GstObject *>(object)->object, param->name,
                                      &value);
                dbg << &value;
            }
            if (i != numProperties - 1)
                dbg << ", ";
        }

        dbg << "}";

        g_free(properties);
    }
    return dbg;
}

QDebug operator<<(QDebug dbg, const GstElement *element)
{
    return dbg << GST_OBJECT_CAST(element); // LATER: output other members?
}

QDebug operator<<(QDebug dbg, const GstPad *pad)
{
    return dbg << GST_OBJECT_CAST(pad); // LATER: output other members?
}

QDebug operator<<(QDebug dbg, const GstDevice *device)
{
    GstDevice *d = const_cast<GstDevice *>(device);
    QDebugStateSaver saver(dbg);
    dbg.nospace();

    dbg << gst_device_get_display_name(d) << "(" << gst_device_get_device_class(d) << ") ";
    dbg << "Caps: " << QGstCaps{ gst_device_get_caps(d), QGstCaps::NeedsRef, } << ", ";
    dbg << "Properties: " << QUniqueGstStructureHandle{ gst_device_get_properties(d) }.get();
    return dbg;
}

namespace {

struct Timepoint
{
    explicit Timepoint(guint64 us) : ts{ us } { }
    guint64 ts;
};

QDebug operator<<(QDebug dbg, Timepoint ts)
{
    char buffer[128];
    snprintf(buffer, sizeof(buffer), "%" GST_TIME_FORMAT, GST_TIME_ARGS(ts.ts));
    dbg << buffer;
    return dbg;
}

} // namespace

QDebug operator<<(QDebug dbg, const GstMessage *msg)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace();

    dbg << GST_MESSAGE_TYPE_NAME(msg) << ", Source: " << GST_MESSAGE_SRC_NAME(msg);
    if (GST_MESSAGE_TIMESTAMP(msg) != 0xFFFFFFFFFFFFFFFF)
        dbg << ", Timestamp: " << GST_MESSAGE_TIMESTAMP(msg);

    switch (msg->type) {
    case GST_MESSAGE_ERROR: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_error(const_cast<GstMessage *>(msg), &err, &debug);

        dbg << ", Error: " << err << " (" << debug << ")";
        break;
    }

    case GST_MESSAGE_WARNING: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_warning(const_cast<GstMessage *>(msg), &err, &debug);

        dbg << ", Warning: " << err << " (" << debug << ")";
        break;
    }

    case GST_MESSAGE_INFO: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_info(const_cast<GstMessage *>(msg), &err, &debug);

        dbg << ", Info: " << err << " (" << debug << ")";
        break;
    }

    case GST_MESSAGE_QOS: {
        gboolean live;
        guint64 running_time;
        guint64 stream_time;
        guint64 timestamp;
        guint64 duration;

        gst_message_parse_qos(const_cast<GstMessage *>(msg), &live, &running_time, &stream_time,
                              &timestamp, &duration);

        dbg << ", Live: " << bool(live) << ", Running time: " << Timepoint{ running_time }
            << ", Stream time: " << Timepoint{ stream_time }
            << ", Timestamp: " << Timepoint{ timestamp } << ", Duration: " << Timepoint{ duration };
        break;
    }

    case GST_MESSAGE_STATE_CHANGED: {
        GstState oldState;
        GstState newState;
        GstState pending;

        gst_message_parse_state_changed(const_cast<GstMessage *>(msg), &oldState, &newState,
                                        &pending);

        dbg << ", Transition: " << oldState << "->" << newState;

        if (pending != GST_STATE_VOID_PENDING)
            dbg << ", Pending State: " << pending;
        break;
    }

    default:
        break;
    }
    return dbg;
}

QDebug operator<<(QDebug dbg, const GstTagList *tagList)
{
    dbg << QGString{ gst_tag_list_to_string(tagList) };
    return dbg;
}

QDebug operator<<(QDebug dbg, const GstQuery *query)
{
    dbg << GST_QUERY_TYPE_NAME(query);
    return dbg;
}

QDebug operator<<(QDebug dbg, const GstEvent *event)
{
    dbg << GST_EVENT_TYPE_NAME(event);
    return dbg;
}

QDebug operator<<(QDebug dbg, const GstPadTemplate *padTemplate)
{
    QGstCaps caps = padTemplate
            ? QGstCaps{ gst_pad_template_get_caps(const_cast<GstPadTemplate *>(padTemplate)), QGstCaps::HasRef, }
            : QGstCaps{};

    dbg << caps;
    return dbg;
}

QDebug operator<<(QDebug dbg, GstState state)
{
    return dbg << gst_element_state_get_name(state);
}

QDebug operator<<(QDebug dbg, GstStateChange transition)
{
    return dbg << gst_state_change_get_name(transition);
}

QDebug operator<<(QDebug dbg, GstStateChangeReturn stateChangeReturn)
{
    return dbg << gst_element_state_change_return_get_name(stateChangeReturn);
}

QDebug operator<<(QDebug dbg, GstMessageType type)
{
    return dbg << gst_message_type_get_name(type);
}

QDebug operator<<(QDebug dbg, GstPadDirection direction)
{
    switch (direction) {
    case GST_PAD_UNKNOWN:
        return dbg << "GST_PAD_UNKNOWN";
    case GST_PAD_SRC:
        return dbg << "GST_PAD_SRC";
    case GST_PAD_SINK:
        return dbg << "GST_PAD_SINK";
    }
    return dbg;
}

QDebug operator<<(QDebug dbg, const GValue *value)
{
    switch (G_VALUE_TYPE(value)) {
    case G_TYPE_STRING:
        return dbg << g_value_get_string(value);
    case G_TYPE_BOOLEAN:
        return dbg << g_value_get_boolean(value);
    case G_TYPE_ULONG:
        return dbg << g_value_get_ulong(value);
    case G_TYPE_LONG:
        return dbg << g_value_get_long(value);
    case G_TYPE_UINT:
        return dbg << g_value_get_uint(value);
    case G_TYPE_INT:
        return dbg << g_value_get_int(value);
    case G_TYPE_UINT64:
        return dbg << g_value_get_uint64(value);
    case G_TYPE_INT64:
        return dbg << g_value_get_int64(value);
    case G_TYPE_FLOAT:
        return dbg << g_value_get_float(value);
    case G_TYPE_DOUBLE:
        return dbg << g_value_get_double(value);
    default:
        break;
    }

    if (GST_VALUE_HOLDS_BITMASK(value)) {
        QDebugStateSaver saver(dbg);
        return dbg << Qt::hex << gst_value_get_bitmask(value);
    }

    if (GST_VALUE_HOLDS_FRACTION(value))
        return dbg << gst_value_get_fraction_numerator(value) << "/"
                   << gst_value_get_fraction_denominator(value);

    if (GST_VALUE_HOLDS_CAPS(value))
        return dbg << gst_value_get_caps(value);

    if (GST_VALUE_HOLDS_STRUCTURE(value))
        return dbg << gst_value_get_structure(value);

    if (GST_VALUE_HOLDS_ARRAY(value)) {
        const guint size = gst_value_array_get_size(value);
        const guint last = size - 1;
        dbg << "[";
        for (guint index = 0; index != size; ++index) {
            dbg << gst_value_array_get_value(value, index);
            if (index != last)
                dbg << ", ";
        }
        dbg << "}";
        return dbg;
    }

    if (G_VALUE_TYPE(value) == GST_TYPE_PAD_DIRECTION) {
        GstPadDirection direction = static_cast<GstPadDirection>(g_value_get_enum(value));
        return dbg << direction;
    }

    if (G_VALUE_TYPE(value) == GST_TYPE_PAD_TEMPLATE) {
        GstPadTemplate *padTemplate = static_cast<GstPadTemplate *>(g_value_get_object(value));
        return dbg << padTemplate;
    }

    dbg << "(not implemented: " << G_VALUE_TYPE_NAME(value) << ")";

    return dbg;
}

QDebug operator<<(QDebug dbg, const GError *error)
{
    return dbg << error->message;
}

QCompactGstMessageAdaptor::QCompactGstMessageAdaptor(const QGstreamerMessage &m)
    : QCompactGstMessageAdaptor{
          m.message(),
      }
{
}

QCompactGstMessageAdaptor::QCompactGstMessageAdaptor(GstMessage *m)
    : msg{
          m,
      }
{
}

QDebug operator<<(QDebug dbg, const QCompactGstMessageAdaptor &m)
{
    std::optional<QDebugStateSaver> saver(dbg);
    dbg.nospace();

    switch (GST_MESSAGE_TYPE(m.msg)) {
    case GST_MESSAGE_ERROR: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_error(m.msg, &err, &debug);
        dbg << err << " (" << debug << ")";
        return dbg;
    }

    case GST_MESSAGE_WARNING: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_warning(m.msg, &err, &debug);
        dbg << err << " (" << debug << ")";
        return dbg;
    }

    case GST_MESSAGE_INFO: {
        QUniqueGErrorHandle err;
        QGString debug;
        gst_message_parse_info(m.msg, &err, &debug);

        dbg << err << " (" << debug << ")";
        return dbg;
    }

    case GST_MESSAGE_STATE_CHANGED: {
        GstState oldState;
        GstState newState;
        GstState pending;

        gst_message_parse_state_changed(m.msg, &oldState, &newState, &pending);

        dbg << oldState << " -> " << newState;
        if (pending != GST_STATE_VOID_PENDING)
            dbg << " (pending: " << pending << ")";
        return dbg;
    }

    default: {
        saver.reset();
        return dbg << m.msg;
    }
    }
}

QT_END_NAMESPACE