summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/spa/debug/format.h
blob: 6f9e0a3be09a59c9b946411d4c9e202c7ad9f89c (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
/* Simple Plugin API
 *
 * Copyright © 2018 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef SPA_DEBUG_FORMAT_H
#define SPA_DEBUG_FORMAT_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \addtogroup spa_debug
 * \{
 */

#include <spa/pod/parser.h>
#include <spa/debug/log.h>
#include <spa/debug/types.h>
#include <spa/param/type-info.h>
#include <spa/param/format-utils.h>

static inline int
spa_debug_format_value(const struct spa_type_info *info,
		uint32_t type, void *body, uint32_t size)
{
	switch (type) {
	case SPA_TYPE_Bool:
		spa_debugn("%s", *(int32_t *) body ? "true" : "false");
		break;
	case SPA_TYPE_Id:
	{
		const char *str = spa_debug_type_find_short_name(info, *(int32_t *) body);
		char tmp[64];
		if (str == NULL) {
			snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
			str = tmp;
		}
		spa_debugn("%s", str);
		break;
	}
	case SPA_TYPE_Int:
		spa_debugn("%d", *(int32_t *) body);
		break;
	case SPA_TYPE_Long:
		spa_debugn("%" PRIi64, *(int64_t *) body);
		break;
	case SPA_TYPE_Float:
		spa_debugn("%f", *(float *) body);
		break;
	case SPA_TYPE_Double:
		spa_debugn("%f", *(double *) body);
		break;
	case SPA_TYPE_String:
		spa_debugn("%s", (char *) body);
		break;
	case SPA_TYPE_Rectangle:
	{
		struct spa_rectangle *r = (struct spa_rectangle *)body;
		spa_debugn("%" PRIu32 "x%" PRIu32, r->width, r->height);
		break;
	}
	case SPA_TYPE_Fraction:
	{
		struct spa_fraction *f = (struct spa_fraction *)body;
		spa_debugn("%" PRIu32 "/%" PRIu32, f->num, f->denom);
		break;
	}
	case SPA_TYPE_Bitmap:
		spa_debugn("Bitmap");
		break;
	case SPA_TYPE_Bytes:
		spa_debugn("Bytes");
		break;
	case SPA_TYPE_Array:
	{
		void *p;
		struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
		int i = 0;
		info = info && info->values ? info->values : info;
		spa_debugn("< ");
		SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
			if (i++ > 0)
				spa_debugn(", ");
			spa_debug_format_value(info, b->child.type, p, b->child.size);
		}
		spa_debugn(" >");
		break;
	}
	default:
		spa_debugn("INVALID type %d", type);
		break;
	}
	return 0;
}

static inline int spa_debug_format(int indent,
		const struct spa_type_info *info, const struct spa_pod *format)
{
	const char *media_type;
	const char *media_subtype;
	struct spa_pod_prop *prop;
	uint32_t mtype, mstype;

	if (info == NULL)
		info = spa_type_format;

	if (format == NULL || SPA_POD_TYPE(format) != SPA_TYPE_Object)
		return -EINVAL;

	if (spa_format_parse(format, &mtype, &mstype) < 0)
		return -EINVAL;

	media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
	media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);

	spa_debug("%*s %s/%s", indent, "",
		media_type ? spa_debug_type_short_name(media_type) : "unknown",
		media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");

	SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)format, prop) {
		const char *key;
		const struct spa_type_info *ti;
		uint32_t i, type, size, n_vals, choice;
		const struct spa_pod *val;
		void *vals;

		if (prop->key == SPA_FORMAT_mediaType ||
		    prop->key == SPA_FORMAT_mediaSubtype)
			continue;

		val = spa_pod_get_values(&prop->value, &n_vals, &choice);

		type = val->type;
		size = val->size;
		vals = SPA_POD_BODY(val);

		if (type < SPA_TYPE_None || type >= _SPA_TYPE_LAST)
			continue;

		ti = spa_debug_type_find(info, prop->key);
		key = ti ? ti->name : NULL;

		spa_debugn("%*s %16s : (%s) ", indent, "",
			key ? spa_debug_type_short_name(key) : "unknown",
			spa_debug_type_short_name(spa_types[type].name));

		if (choice == SPA_CHOICE_None) {
			spa_debug_format_value(ti ? ti->values : NULL, type, vals, size);
		} else {
			const char *ssep, *esep, *sep;

			switch (choice) {
			case SPA_CHOICE_Range:
			case SPA_CHOICE_Step:
				ssep = "[ ";
				sep = ", ";
				esep = " ]";
				break;
			default:
			case SPA_CHOICE_Enum:
			case SPA_CHOICE_Flags:
				ssep = "{ ";
				sep = ", ";
				esep = " }";
				break;
			}

			spa_debugn("%s", ssep);

			for (i = 1; i < n_vals; i++) {
				vals = SPA_PTROFF(vals, size, void);
				if (i > 1)
					spa_debugn("%s", sep);
				spa_debug_format_value(ti ? ti->values : NULL, type, vals, size);
			}
			spa_debugn("%s", esep);
		}
		spa_debugn("\n");
	}
	return 0;
}

/**
 * \}
 */

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif /* SPA_DEBUG_FORMAT_H */