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