PipeWire: spa/debug/format.h Source File

PipeWire 1.4.2 |

Loading...
Searching...
No Matches
format.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_DEBUG_FORMAT_H
6#define SPA_DEBUG_FORMAT_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
16
17#include <spa/pod/parser.h>
18#include <spa/utils/string.h>
19#include <spa/debug/context.h>
20#include <spa/debug/types.h>
21#include <spa/param/type-info.h>
22#include <spa/param/format-utils.h>
23
24#ifndef SPA_API_DEBUG_FORMAT
25 #ifdef SPA_API_IMPL
26 #define SPA_API_DEBUG_FORMAT SPA_API_IMPL
27 #else
28 #define SPA_API_DEBUG_FORMAT static inline
29 #endif
30#endif
31
32
33 SPA_API_DEBUG_FORMAT int
34 spa_debug_strbuf_format_value(struct spa_strbuf *buffer, const struct spa_type_info *info,
35 uint32_t type, void *body, uint32_t size)
36{
37
38 switch (type) {
39 case SPA_TYPE_Bool:
40 spa_strbuf_append(buffer, "%s", *(int32_t *) body ? "true" : "false");
41 break;
42 case SPA_TYPE_Id:
43 {
44 const char *str = spa_debug_type_find_short_name(info, *(int32_t *) body);
45 char tmp[64];
46 if (str == NULL) {
47 snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
48 str = tmp;
49 }
50 spa_strbuf_append(buffer, "%s", str);
51 break;
52 }
53 case SPA_TYPE_Int:
54 spa_strbuf_append(buffer, "%d", *(int32_t *) body);
55 break;
56 case SPA_TYPE_Long:
57 spa_strbuf_append(buffer, "%" PRIi64, *(int64_t *) body);
58 break;
59 case SPA_TYPE_Float:
60 spa_strbuf_append(buffer, "%f", *(float *) body);
61 break;
62 case SPA_TYPE_Double:
63 spa_strbuf_append(buffer, "%f", *(double *) body);
64 break;
65 case SPA_TYPE_String:
66 spa_strbuf_append(buffer, "%s", (char *) body);
67 break;
68 case SPA_TYPE_Rectangle:
69 {
70 struct spa_rectangle *r = (struct spa_rectangle *)body;
71 spa_strbuf_append(buffer, "%" PRIu32 "x%" PRIu32, r->width, r->height);
72 break;
73 }
74 case SPA_TYPE_Fraction:
75 {
76 struct spa_fraction *f = (struct spa_fraction *)body;
77 spa_strbuf_append(buffer, "%" PRIu32 "/%" PRIu32, f->num, f->denom);
78 break;
79 }
80 case SPA_TYPE_Bitmap:
81 spa_strbuf_append(buffer, "Bitmap");
82 break;
83 case SPA_TYPE_Bytes:
84 spa_strbuf_append(buffer, "Bytes");
85 break;
86 case SPA_TYPE_Array:
87 {
88 void *p;
89 struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
90 int i = 0;
91 info = info && info->values ? info->values : info;
92 spa_strbuf_append(buffer, "< ");
93 SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
94 if (i++ > 0)
95 spa_strbuf_append(buffer, ", ");
96 spa_debug_strbuf_format_value(buffer, info, b->child.type, p, b->child.size);
97 }
98 spa_strbuf_append(buffer, " >");
99 break;
100 }
101 default:
102 spa_strbuf_append(buffer, "INVALID type %d", type);
103 break;
104 }
105 return 0;
106}
107
108 SPA_API_DEBUG_FORMAT int
109 spa_debug_format_value(const struct spa_type_info *info,
110 uint32_t type, void *body, uint32_t size)
111{
112 char buffer[1024];
113 struct spa_strbuf buf;
114 spa_strbuf_init(&buf, buffer, sizeof(buffer));
115 spa_debug_strbuf_format_value(&buf, info, type, body, size);
116 spa_debugn("%s", buffer);
117 return 0;
118}
119
120 SPA_API_DEBUG_FORMAT int spa_debugc_format(struct spa_debug_context *ctx, int indent,
121 const struct spa_type_info *info, const struct spa_pod *format)
122{
123 const char *media_type;
124 const char *media_subtype;
125 struct spa_pod_prop *prop;
126 uint32_t mtype, mstype;
127
128 if (info == NULL)
129 info = spa_type_format;
130
131 if (format == NULL || SPA_POD_TYPE(format) != SPA_TYPE_Object)
132 return -EINVAL;
133
134 if (spa_format_parse(format, &mtype, &mstype) < 0)
135 return -EINVAL;
136
137 media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
138 media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);
139
140 spa_debugc(ctx, "%*s %s/%s", indent, "",
141 media_type ? spa_debug_type_short_name(media_type) : "unknown",
142 media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");
143
144 SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)format, prop) {
145 const char *key;
146 const struct spa_type_info *ti;
147 uint32_t i, type, size, n_vals, choice;
148 const struct spa_pod *val;
149 void *vals;
150 char buffer[1024];
151 struct spa_strbuf buf;
152
153 if (prop->key == SPA_FORMAT_mediaType ||
154 prop->key == SPA_FORMAT_mediaSubtype)
155 continue;
156
157 val = spa_pod_get_values(&prop->value, &n_vals, &choice);
158
159 type = val->type;
160 size = val->size;
161 vals = SPA_POD_BODY(val);
162
163 if (type < SPA_TYPE_None || type >= _SPA_TYPE_LAST)
164 continue;
165
166 ti = spa_debug_type_find(info, prop->key);
167 key = ti ? ti->name : NULL;
168
169 spa_strbuf_init(&buf, buffer, sizeof(buffer));
170 spa_strbuf_append(&buf, "%*s %16s : (%s) ", indent, "",
171 key ? spa_debug_type_short_name(key) : "unknown",
172 spa_debug_type_short_name(spa_types[type].name));
173
174 if (choice == SPA_CHOICE_None) {
175 spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
176 } else {
177 const char *ssep, *esep, *sep;
178
179 switch (choice) {
180 case SPA_CHOICE_Range:
181 case SPA_CHOICE_Step:
182 ssep = "[ ";
183 sep = ", ";
184 esep = " ]";
185 break;
186 default:
187 case SPA_CHOICE_Enum:
188 case SPA_CHOICE_Flags:
189 ssep = "{ ";
190 sep = ", ";
191 esep = " }";
192 break;
193 }
194
195 spa_strbuf_append(&buf, "%s", ssep);
196
197 for (i = 1; i < n_vals; i++) {
198 vals = SPA_PTROFF(vals, size, void);
199 if (i > 1)
200 spa_strbuf_append(&buf, "%s", sep);
201 spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
202 }
203 spa_strbuf_append(&buf, "%s", esep);
204 }
205 spa_debugc(ctx, "%s", buffer);
206 }
207 return 0;
208}
209
210 SPA_API_DEBUG_FORMAT int spa_debug_format(int indent,
211 const struct spa_type_info *info, const struct spa_pod *format)
212{
213 return spa_debugc_format(NULL, indent, info, format);
214}
216
217 */
218
219#ifdef __cplusplus
220} /* extern "C" */
221#endif
222
223#endif /* SPA_DEBUG_FORMAT_H */
format-utils.h
r
uint32_t int int const char int r
Definition core.h:447
spa_debug_type_find_short_name
SPA_API_DEBUG_TYPES const char * spa_debug_type_find_short_name(const struct spa_type_info *info, uint32_t type)
Definition types.h:68
spa_debugn
#define spa_debugn(_fmt,...)
Definition context.h:28
spa_debugc
#define spa_debugc(_c, _fmt,...)
Definition context.h:49
spa_debug_type_find
SPA_API_DEBUG_TYPES const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition types.h:37
spa_debug_format
SPA_API_DEBUG_FORMAT int spa_debug_format(int indent, const struct spa_type_info *info, const struct spa_pod *format)
Definition format.h:217
spa_debug_format_value
SPA_API_DEBUG_FORMAT int spa_debug_format_value(const struct spa_type_info *info, uint32_t type, void *body, uint32_t size)
Definition format.h:116
spa_debug_strbuf_format_value
SPA_API_DEBUG_FORMAT int spa_debug_strbuf_format_value(struct spa_strbuf *buffer, const struct spa_type_info *info, uint32_t type, void *body, uint32_t size)
Definition format.h:41
SPA_API_DEBUG_FORMAT
#define SPA_API_DEBUG_FORMAT
Definition format.h:35
spa_debugc_format
SPA_API_DEBUG_FORMAT int spa_debugc_format(struct spa_debug_context *ctx, int indent, const struct spa_type_info *info, const struct spa_pod *format)
Definition format.h:127
spa_debug_type_short_name
SPA_API_DEBUG_TYPES const char * spa_debug_type_short_name(const char *name)
Definition types.h:56
spa_debug_type_find_name
SPA_API_DEBUG_TYPES const char * spa_debug_type_find_name(const struct spa_type_info *info, uint32_t type)
Definition types.h:61
spa_type_media_subtype
static const struct spa_type_info spa_type_media_subtype[]
Definition format-types.h:55
spa_type_format
static const struct spa_type_info spa_type_format[]
Definition format-types.h:135
spa_format_parse
SPA_API_FORMAT_UTILS int spa_format_parse(const struct spa_pod *format, uint32_t *media_type, uint32_t *media_subtype)
Definition format-utils.h:37
spa_type_media_type
static const struct spa_type_info spa_type_media_type[]
Definition format-types.h:39
SPA_FORMAT_mediaType
@ SPA_FORMAT_mediaType
media type (Id enum spa_media_type)
Definition format.h:93
SPA_FORMAT_mediaSubtype
@ SPA_FORMAT_mediaSubtype
media subtype (Id enum spa_media_subtype)
Definition format.h:94
spa_pod_get_values
SPA_API_POD_ITER struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:363
SPA_POD_OBJECT_FOREACH
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:124
SPA_POD_BODY
#define SPA_POD_BODY(pod)
Definition pod.h:39
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition pod.h:28
SPA_POD_ARRAY_BODY_FOREACH
#define SPA_POD_ARRAY_BODY_FOREACH(body, _size, iter)
Definition iter.h:95
SPA_CHOICE_Step
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:149
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:147
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition pod.h:151
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:148
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:150
spa_strbuf_append
SPA_API_STRING int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition string.h:390
spa_strbuf_init
SPA_API_STRING void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition string.h:380
spa_types
static const struct spa_type_info spa_types[]
Definition type-info.h:38
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition type.h:45
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition type.h:51
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:46
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition type.h:43
SPA_TYPE_Bytes
@ SPA_TYPE_Bytes
Definition type.h:50
SPA_TYPE_Bitmap
@ SPA_TYPE_Bitmap
Definition type.h:53
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition type.h:56
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition type.h:47
SPA_TYPE_Fraction
@ SPA_TYPE_Fraction
Definition type.h:52
_SPA_TYPE_LAST
@ _SPA_TYPE_LAST
not part of ABI
Definition type.h:62
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition type.h:48
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:44
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition type.h:54
SPA_TYPE_String
@ SPA_TYPE_String
Definition type.h:49
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
parser.h
spa/pod/parser.h
context.h
spa/debug/context.h
string.h
spa/utils/string.h
spa_debug_context
Definition context.h:45
spa_fraction
Definition defs.h:137
spa_fraction::num
uint32_t num
Definition defs.h:138
spa_fraction::denom
uint32_t denom
Definition defs.h:139
spa_pod_array_body
Definition pod.h:121
spa_pod_array_body::child
struct spa_pod child
Definition pod.h:122
spa_pod_object
Definition pod.h:183
spa_pod_prop
Definition pod.h:208
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
spa_pod_prop::value
struct spa_pod value
Definition pod.h:226
spa_pod
Definition pod.h:43
spa_pod::type
uint32_t type
Definition pod.h:45
spa_pod::size
uint32_t size
Definition pod.h:44
spa_rectangle
Definition defs.h:116
spa_strbuf
Definition string.h:374
spa_type_info
Definition type.h:154
spa_type_info::type
uint32_t type
Definition type.h:155
spa_type_info::values
const struct spa_type_info * values
Definition type.h:158
types.h
spa/debug/types.h