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