PipeWire: format.h Source File

PipeWire 0.3.66

Loading...
Searching...
No Matches
debug/format.h
Go to the documentation of this file.
1/* Simple Plugin API
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef SPA_DEBUG_FORMAT_H
26#define SPA_DEBUG_FORMAT_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
37#include <spa/pod/parser.h>
38#include <spa/utils/string.h>
39#include <spa/debug/context.h>
40#include <spa/debug/types.h>
41#include <spa/param/type-info.h>
42#include <spa/param/format-utils.h>
43
44static inline int
45 spa_debug_strbuf_format_value(struct spa_strbuf *buffer, const struct spa_type_info *info,
46 uint32_t type, void *body, uint32_t size)
47{
48
49 switch (type) {
50 case SPA_TYPE_Bool:
51 spa_strbuf_append(buffer, "%s", *(int32_t *) body ? "true" : "false");
52 break;
53 case SPA_TYPE_Id:
54 {
55 const char *str = spa_debug_type_find_short_name(info, *(int32_t *) body);
56 char tmp[64];
57 if (str == NULL) {
58 snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
59 str = tmp;
60 }
61 spa_strbuf_append(buffer, "%s", str);
62 break;
63 }
64 case SPA_TYPE_Int:
65 spa_strbuf_append(buffer, "%d", *(int32_t *) body);
66 break;
67 case SPA_TYPE_Long:
68 spa_strbuf_append(buffer, "%" PRIi64, *(int64_t *) body);
69 break;
70 case SPA_TYPE_Float:
71 spa_strbuf_append(buffer, "%f", *(float *) body);
72 break;
73 case SPA_TYPE_Double:
74 spa_strbuf_append(buffer, "%f", *(double *) body);
75 break;
76 case SPA_TYPE_String:
77 spa_strbuf_append(buffer, "%s", (char *) body);
78 break;
79 case SPA_TYPE_Rectangle:
80 {
81 struct spa_rectangle *r = (struct spa_rectangle *)body;
82 spa_strbuf_append(buffer, "%" PRIu32 "x%" PRIu32, r->width, r->height);
83 break;
84 }
85 case SPA_TYPE_Fraction:
86 {
87 struct spa_fraction *f = (struct spa_fraction *)body;
88 spa_strbuf_append(buffer, "%" PRIu32 "/%" PRIu32, f->num, f->denom);
89 break;
90 }
91 case SPA_TYPE_Bitmap:
92 spa_strbuf_append(buffer, "Bitmap");
93 break;
94 case SPA_TYPE_Bytes:
95 spa_strbuf_append(buffer, "Bytes");
96 break;
97 case SPA_TYPE_Array:
98 {
99 void *p;
100 struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
101 int i = 0;
102 info = info && info->values ? info->values : info;
103 spa_strbuf_append(buffer, "< ");
104 SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
105 if (i++ > 0)
106 spa_strbuf_append(buffer, ", ");
107 spa_debug_strbuf_format_value(buffer, info, b->child.type, p, b->child.size);
108 }
109 spa_strbuf_append(buffer, " >");
110 break;
111 }
112 default:
113 spa_strbuf_append(buffer, "INVALID type %d", type);
114 break;
115 }
116 return 0;
117}
118
119static inline int
120 spa_debug_format_value(const struct spa_type_info *info,
121 uint32_t type, void *body, uint32_t size)
122{
123 char buffer[1024];
124 struct spa_strbuf buf;
125 spa_strbuf_init(&buf, buffer, sizeof(buffer));
126 spa_debug_strbuf_format_value(&buf, info, type, body, size);
127 spa_debugn("%s", buffer);
128 return 0;
129}
130
131static inline int spa_debugc_format(struct spa_debug_context *ctx, int indent,
132 const struct spa_type_info *info, const struct spa_pod *format)
133{
134 const char *media_type;
135 const char *media_subtype;
136 struct spa_pod_prop *prop;
137 uint32_t mtype, mstype;
138
139 if (info == NULL)
140 info = spa_type_format;
141
142 if (format == NULL || SPA_POD_TYPE(format) != SPA_TYPE_Object)
143 return -EINVAL;
144
145 if (spa_format_parse(format, &mtype, &mstype) < 0)
146 return -EINVAL;
147
148 media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
149 media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);
150
151 spa_debugc(ctx, "%*s %s/%s", indent, "",
152 media_type ? spa_debug_type_short_name(media_type) : "unknown",
153 media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");
154
155 SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)format, prop) {
156 const char *key;
157 const struct spa_type_info *ti;
158 uint32_t i, type, size, n_vals, choice;
159 const struct spa_pod *val;
160 void *vals;
161 char buffer[1024];
162 struct spa_strbuf buf;
163
164 if (prop->key == SPA_FORMAT_mediaType ||
165 prop->key == SPA_FORMAT_mediaSubtype)
166 continue;
167
168 val = spa_pod_get_values(&prop->value, &n_vals, &choice);
169
170 type = val->type;
171 size = val->size;
172 vals = SPA_POD_BODY(val);
173
174 if (type < SPA_TYPE_None || type >= _SPA_TYPE_LAST)
175 continue;
176
177 ti = spa_debug_type_find(info, prop->key);
178 key = ti ? ti->name : NULL;
179
180 spa_strbuf_init(&buf, buffer, sizeof(buffer));
181 spa_strbuf_append(&buf, "%*s %16s : (%s) ", indent, "",
182 key ? spa_debug_type_short_name(key) : "unknown",
183 spa_debug_type_short_name(spa_types[type].name));
184
185 if (choice == SPA_CHOICE_None) {
186 spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
187 } else {
188 const char *ssep, *esep, *sep;
189
190 switch (choice) {
191 case SPA_CHOICE_Range:
192 case SPA_CHOICE_Step:
193 ssep = "[ ";
194 sep = ", ";
195 esep = " ]";
196 break;
197 default:
198 case SPA_CHOICE_Enum:
199 case SPA_CHOICE_Flags:
200 ssep = "{ ";
201 sep = ", ";
202 esep = " }";
203 break;
204 }
205
206 spa_strbuf_append(&buf, "%s", ssep);
207
208 for (i = 1; i < n_vals; i++) {
209 vals = SPA_PTROFF(vals, size, void);
210 if (i > 1)
211 spa_strbuf_append(&buf, "%s", sep);
212 spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
213 }
214 spa_strbuf_append(&buf, "%s", esep);
215 }
216 spa_debugc(ctx, "%s", buffer);
217 }
218 return 0;
219}
220
221static inline int spa_debug_format(int indent,
222 const struct spa_type_info *info, const struct spa_pod *format)
223{
224 return spa_debugc_format(NULL, indent, info, format);
225}
230#ifdef __cplusplus
231} /* extern "C" */
232#endif
233
234#endif /* SPA_DEBUG_FORMAT_H */
format-utils.h
spa_debug_format
static int spa_debug_format(int indent, const struct spa_type_info *info, const struct spa_pod *format)
Definition: debug/format.h:226
spa_debugn
#define spa_debugn(_fmt,...)
Definition: spa/include/spa/debug/context.h:47
spa_debugc
#define spa_debugc(_c, _fmt,...)
Definition: spa/include/spa/debug/context.h:57
spa_debug_type_find
static const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition: types.h:46
spa_debugc_format
static int spa_debugc_format(struct spa_debug_context *ctx, int indent, const struct spa_type_info *info, const struct spa_pod *format)
Definition: debug/format.h:136
spa_debug_type_find_name
static const char * spa_debug_type_find_name(const struct spa_type_info *info, uint32_t type)
Definition: types.h:73
spa_debug_type_short_name
static const char * spa_debug_type_short_name(const char *name)
Definition: types.h:65
spa_debug_type_find_short_name
static const char * spa_debug_type_find_short_name(const struct spa_type_info *info, uint32_t type)
Definition: types.h:80
spa_debug_format_value
static int spa_debug_format_value(const struct spa_type_info *info, uint32_t type, void *body, uint32_t size)
Definition: debug/format.h:125
spa_debug_strbuf_format_value
static 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: debug/format.h:50
spa_type_media_subtype
static const struct spa_type_info spa_type_media_subtype[]
Definition: format-types.h:74
spa_format_parse
static int spa_format_parse(const struct spa_pod *format, uint32_t *media_type, uint32_t *media_subtype)
Definition: format-utils.h:47
spa_type_format
static const struct spa_type_info spa_type_format[]
Definition: format-types.h:148
spa_type_media_type
static const struct spa_type_info spa_type_media_type[]
Definition: format-types.h:58
SPA_FORMAT_mediaType
@ SPA_FORMAT_mediaType
media type (Id enum spa_media_type)
Definition: param/format.h:112
SPA_FORMAT_mediaSubtype
@ SPA_FORMAT_mediaSubtype
media subtype (Id enum spa_media_subtype)
Definition: param/format.h:113
SPA_POD_OBJECT_FOREACH
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition: iter.h:128
SPA_POD_BODY
#define SPA_POD_BODY(pod)
Definition: pod/pod.h:59
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition: pod/pod.h:48
spa_pod_get_values
static struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition: iter.h:367
SPA_POD_ARRAY_BODY_FOREACH
#define SPA_POD_ARRAY_BODY_FOREACH(body, _size, iter)
Definition: iter.h:99
SPA_CHOICE_Step
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition: pod/pod.h:169
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition: pod/pod.h:167
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition: pod/pod.h:171
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition: pod/pod.h:168
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition: pod/pod.h:170
spa_strbuf_append
static int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition: string.h:398
spa_strbuf_init
static void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition: string.h:390
spa_types
static const struct spa_type_info spa_types[]
Definition: utils/type-info.h:62
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition: spa/include/spa/utils/type.h:54
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition: spa/include/spa/utils/type.h:60
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition: spa/include/spa/utils/type.h:55
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition: spa/include/spa/utils/type.h:52
SPA_TYPE_Bytes
@ SPA_TYPE_Bytes
Definition: spa/include/spa/utils/type.h:59
SPA_TYPE_Bitmap
@ SPA_TYPE_Bitmap
Definition: spa/include/spa/utils/type.h:62
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition: spa/include/spa/utils/type.h:65
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition: spa/include/spa/utils/type.h:56
SPA_TYPE_Fraction
@ SPA_TYPE_Fraction
Definition: spa/include/spa/utils/type.h:61
_SPA_TYPE_LAST
@ _SPA_TYPE_LAST
not part of ABI
Definition: spa/include/spa/utils/type.h:71
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition: spa/include/spa/utils/type.h:57
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition: spa/include/spa/utils/type.h:53
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition: spa/include/spa/utils/type.h:63
SPA_TYPE_String
@ SPA_TYPE_String
Definition: spa/include/spa/utils/type.h:58
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:210
parser.h
spa/pod/parser.h
context.h
spa/debug/context.h
string.h
spa/utils/string.h
spa_debug_context
Definition: spa/include/spa/debug/context.h:53
spa_fraction
Definition: defs.h:139
spa_fraction::num
uint32_t num
Definition: defs.h:140
spa_fraction::denom
uint32_t denom
Definition: defs.h:141
spa_pod_array_body
Definition: pod/pod.h:141
spa_pod_array_body::child
struct spa_pod child
Definition: pod/pod.h:142
spa_pod_object
Definition: pod/pod.h:203
spa_pod_prop
Definition: pod/pod.h:228
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition: pod/pod.h:229
spa_pod_prop::value
struct spa_pod value
Definition: pod/pod.h:246
spa_pod
Definition: pod/pod.h:63
spa_pod::type
uint32_t type
Definition: pod/pod.h:65
spa_pod::size
uint32_t size
Definition: pod/pod.h:64
spa_rectangle
Definition: defs.h:118
spa_rectangle::width
uint32_t width
Definition: defs.h:119
spa_rectangle::height
uint32_t height
Definition: defs.h:120
spa_strbuf
Definition: string.h:384
spa_strbuf::buffer
char * buffer
Definition: string.h:385
spa_type_info
Definition: spa/include/spa/utils/type.h:162
spa_type_info::type
uint32_t type
Definition: spa/include/spa/utils/type.h:163
spa_type_info::values
const struct spa_type_info * values
Definition: spa/include/spa/utils/type.h:166
types.h
spa/debug/types.h