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