PipeWire: spa/debug/format.h Source File

PipeWire 0.3.51

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/debug/log.h>
39#include <spa/debug/types.h>
40#include <spa/param/type-info.h>
41#include <spa/param/format-utils.h>
42
43static inline int
44 spa_debug_format_value(const struct spa_type_info *info,
45 uint32_t type, void *body, uint32_t size)
46{
47 switch (type) {
48 case SPA_TYPE_Bool:
49 spa_debugn("%s", *(int32_t *) body ? "true" : "false");
50 break;
51 case SPA_TYPE_Id:
52 {
53 const char *str = spa_debug_type_find_short_name(info, *(int32_t *) body);
54 char tmp[64];
55 if (str == NULL) {
56 snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
57 str = tmp;
58 }
59 spa_debugn("%s", str);
60 break;
61 }
62 case SPA_TYPE_Int:
63 spa_debugn("%d", *(int32_t *) body);
64 break;
65 case SPA_TYPE_Long:
66 spa_debugn("%" PRIi64, *(int64_t *) body);
67 break;
68 case SPA_TYPE_Float:
69 spa_debugn("%f", *(float *) body);
70 break;
71 case SPA_TYPE_Double:
72 spa_debugn("%f", *(double *) body);
73 break;
74 case SPA_TYPE_String:
75 spa_debugn("%s", (char *) body);
76 break;
77 case SPA_TYPE_Rectangle:
78 {
79 struct spa_rectangle *r = (struct spa_rectangle *)body;
80 spa_debugn("%" PRIu32 "x%" PRIu32, r->width, r->height);
81 break;
82 }
83 case SPA_TYPE_Fraction:
84 {
85 struct spa_fraction *f = (struct spa_fraction *)body;
86 spa_debugn("%" PRIu32 "/%" PRIu32, f->num, f->denom);
87 break;
88 }
89 case SPA_TYPE_Bitmap:
90 spa_debugn("Bitmap");
91 break;
92 case SPA_TYPE_Bytes:
93 spa_debugn("Bytes");
94 break;
95 case SPA_TYPE_Array:
96 {
97 void *p;
98 struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
99 int i = 0;
100 info = info && info->values ? info->values : info;
101 spa_debugn("< ");
102 SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
103 if (i++ > 0)
104 spa_debugn(", ");
105 spa_debug_format_value(info, b->child.type, p, b->child.size);
106 }
107 spa_debugn(" >");
108 break;
109 }
110 default:
111 spa_debugn("INVALID type %d", type);
112 break;
113 }
114 return 0;
115}
116
117static inline int spa_debug_format(int indent,
118 const struct spa_type_info *info, const struct spa_pod *format)
119{
120 const char *media_type;
121 const char *media_subtype;
122 struct spa_pod_prop *prop;
123 uint32_t mtype, mstype;
124
125 if (info == NULL)
126 info = spa_type_format;
127
128 if (format == NULL || SPA_POD_TYPE(format) != SPA_TYPE_Object)
129 return -EINVAL;
130
131 if (spa_format_parse(format, &mtype, &mstype) < 0)
132 return -EINVAL;
133
134 media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
135 media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);
136
137 spa_debug("%*s %s/%s", indent, "",
138 media_type ? spa_debug_type_short_name(media_type) : "unknown",
139 media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");
140
141 SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)format, prop) {
142 const char *key;
143 const struct spa_type_info *ti;
144 uint32_t i, type, size, n_vals, choice;
145 const struct spa_pod *val;
146 void *vals;
147
148 if (prop->key == SPA_FORMAT_mediaType ||
149 prop->key == SPA_FORMAT_mediaSubtype)
150 continue;
151
152 val = spa_pod_get_values(&prop->value, &n_vals, &choice);
153
154 type = val->type;
155 size = val->size;
156 vals = SPA_POD_BODY(val);
157
158 if (type < SPA_TYPE_None || type >= _SPA_TYPE_LAST)
159 continue;
160
161 ti = spa_debug_type_find(info, prop->key);
162 key = ti ? ti->name : NULL;
163
164 spa_debugn("%*s %16s : (%s) ", indent, "",
165 key ? spa_debug_type_short_name(key) : "unknown",
166 spa_debug_type_short_name(spa_types[type].name));
167
168 if (choice == SPA_CHOICE_None) {
169 spa_debug_format_value(ti ? ti->values : NULL, type, vals, size);
170 } else {
171 const char *ssep, *esep, *sep;
172
173 switch (choice) {
174 case SPA_CHOICE_Range:
175 case SPA_CHOICE_Step:
176 ssep = "[ ";
177 sep = ", ";
178 esep = " ]";
179 break;
180 default:
181 case SPA_CHOICE_Enum:
182 case SPA_CHOICE_Flags:
183 ssep = "{ ";
184 sep = ", ";
185 esep = " }";
186 break;
187 }
188
189 spa_debugn("%s", ssep);
190
191 for (i = 1; i < n_vals; i++) {
192 vals = SPA_PTROFF(vals, size, void);
193 if (i > 1)
194 spa_debugn("%s", sep);
195 spa_debug_format_value(ti ? ti->values : NULL, type, vals, size);
196 }
197 spa_debugn("%s", esep);
198 }
199 spa_debugn("\n");
200 }
201 return 0;
202}
203
208#ifdef __cplusplus
209} /* extern "C" */
210#endif
211
212#endif /* SPA_DEBUG_FORMAT_H */
format-utils.h
spa_debugn
#define spa_debugn(fmt,...)
Definition: log.h:47
spa_debug_format
static int spa_debug_format(int indent, const struct spa_type_info *info, const struct spa_pod *format)
Definition: format.h:122
spa_debug
#define spa_debug(fmt,...)
Definition: log.h:44
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_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: format.h:49
spa_type_media_subtype
static const struct spa_type_info spa_type_media_subtype[]
Definition: type-info.h:236
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: type-info.h:294
spa_type_media_type
static const struct spa_type_info spa_type_media_type[]
Definition: type-info.h:220
SPA_FORMAT_mediaType
@ SPA_FORMAT_mediaType
media type (Id enum spa_media_type)
Definition: format.h:109
SPA_FORMAT_mediaSubtype
@ SPA_FORMAT_mediaSubtype
media subtype (Id enum spa_media_subtype)
Definition: format.h:110
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.h:59
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition: 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.h:169
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition: pod.h:167
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition: pod.h:171
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition: pod.h:168
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition: pod.h:170
spa_types
static const struct spa_type_info spa_types[]
Definition: type-info.h:88
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition: type.h:54
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition: type.h:60
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition: type.h:55
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition: type.h:52
SPA_TYPE_Bytes
@ SPA_TYPE_Bytes
Definition: type.h:59
SPA_TYPE_Bitmap
@ SPA_TYPE_Bitmap
Definition: type.h:62
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition: type.h:65
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition: type.h:56
SPA_TYPE_Fraction
@ SPA_TYPE_Fraction
Definition: type.h:61
_SPA_TYPE_LAST
@ _SPA_TYPE_LAST
not part of ABI
Definition: type.h:71
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition: type.h:57
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition: type.h:53
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition: type.h:63
SPA_TYPE_String
@ SPA_TYPE_String
Definition: type.h:58
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:183
type-info.h
spa/param/type-info.h
parser.h
spa/pod/parser.h
log.h
spa/debug/log.h
spa_fraction
Definition: defs.h:121
spa_fraction::num
uint32_t num
Definition: defs.h:122
spa_fraction::denom
uint32_t denom
Definition: defs.h:123
spa_pod_array_body
Definition: pod.h:141
spa_pod_array_body::child
struct spa_pod child
Definition: pod.h:142
spa_pod_object
Definition: pod.h:203
spa_pod_prop
Definition: pod.h:228
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition: pod.h:229
spa_pod_prop::value
struct spa_pod value
Definition: pod.h:246
spa_pod
Definition: pod.h:63
spa_pod::type
uint32_t type
Definition: pod.h:65
spa_pod::size
uint32_t size
Definition: pod.h:64
spa_rectangle
Definition: defs.h:100
spa_rectangle::width
uint32_t width
Definition: defs.h:101
spa_rectangle::height
uint32_t height
Definition: defs.h:102
spa_type_info
Definition: type.h:162
spa_type_info::type
uint32_t type
Definition: type.h:163
spa_type_info::values
const struct spa_type_info * values
Definition: type.h:166
types.h
spa/debug/types.h