Loading...
Searching...
No Matches
format.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_DEBUG_FORMAT_H
#define SPA_DEBUG_FORMAT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/pod/parser.h>
#include <spa/utils/string.h>
#include <spa/debug/context.h>
#include <spa/debug/types.h>
#include <spa/param/type-info.h>
#include <spa/param/format-utils.h>
static inline int
spa_debug_strbuf_format_value(struct spa_strbuf *buffer, const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
switch (type) {
case SPA_TYPE_Bool:
spa_strbuf_append(buffer, "%s", *(int32_t *) body ? "true" : "false");
break;
case SPA_TYPE_Id:
{
const char *str = spa_debug_type_find_short_name(info, *(int32_t *) body);
char tmp[64];
if (str == NULL) {
snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
str = tmp;
}
spa_strbuf_append(buffer, "%s", str);
break;
}
case SPA_TYPE_Int:
spa_strbuf_append(buffer, "%d", *(int32_t *) body);
break;
case SPA_TYPE_Long:
spa_strbuf_append(buffer, "%" PRIi64, *(int64_t *) body);
break;
case SPA_TYPE_Float:
spa_strbuf_append(buffer, "%f", *(float *) body);
break;
case SPA_TYPE_Double:
spa_strbuf_append(buffer, "%f", *(double *) body);
break;
case SPA_TYPE_String:
spa_strbuf_append(buffer, "%s", (char *) body);
break;
case SPA_TYPE_Rectangle:
{
struct spa_rectangle *r = (struct spa_rectangle *)body;
spa_strbuf_append(buffer, "%" PRIu32 "x%" PRIu32, r->width, r->height);
break;
}
case SPA_TYPE_Fraction:
{
struct spa_fraction *f = (struct spa_fraction *)body;
spa_strbuf_append(buffer, "%" PRIu32 "/%" PRIu32, f->num, f->denom);
break;
}
case SPA_TYPE_Bitmap:
spa_strbuf_append(buffer, "Bitmap");
break;
case SPA_TYPE_Bytes:
spa_strbuf_append(buffer, "Bytes");
break;
case SPA_TYPE_Array:
{
void *p;
struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
int i = 0;
info = info && info->values ? info->values : info;
spa_strbuf_append(buffer, "< ");
SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
if (i++ > 0)
spa_strbuf_append(buffer, ", ");
spa_debug_strbuf_format_value(buffer, info, b->child.type, p, b->child.size);
}
spa_strbuf_append(buffer, " >");
break;
}
default:
spa_strbuf_append(buffer, "INVALID type %d", type);
break;
}
return 0;
}
static inline int
spa_debug_format_value(const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
char buffer[1024];
struct spa_strbuf buf;
spa_strbuf_init(&buf, buffer, sizeof(buffer));
spa_debug_strbuf_format_value(&buf, info, type, body, size);
spa_debugn("%s", buffer);
return 0;
}
static inline int spa_debugc_format(struct spa_debug_context *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *format)
{
const char *media_type;
const char *media_subtype;
struct spa_pod_prop *prop;
uint32_t mtype, mstype;
if (info == NULL)
info = spa_type_format;
if (format == NULL || SPA_POD_TYPE(format) != SPA_TYPE_Object)
return -EINVAL;
if (spa_format_parse(format, &mtype, &mstype) < 0)
return -EINVAL;
media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);
spa_debugc(ctx, "%*s %s/%s", indent, "",
media_type ? spa_debug_type_short_name(media_type) : "unknown",
media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");
SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)format, prop) {
const char *key;
const struct spa_type_info *ti;
uint32_t i, type, size, n_vals, choice;
const struct spa_pod *val;
void *vals;
char buffer[1024];
struct spa_strbuf buf;
if (prop->key == SPA_FORMAT_mediaType ||
prop->key == SPA_FORMAT_mediaSubtype)
continue;
val = spa_pod_get_values(&prop->value, &n_vals, &choice);
type = val->type;
size = val->size;
vals = SPA_POD_BODY(val);
if (type < SPA_TYPE_None || type >= _SPA_TYPE_LAST)
continue;
ti = spa_debug_type_find(info, prop->key);
key = ti ? ti->name : NULL;
spa_strbuf_init(&buf, buffer, sizeof(buffer));
spa_strbuf_append(&buf, "%*s %16s : (%s) ", indent, "",
key ? spa_debug_type_short_name(key) : "unknown",
spa_debug_type_short_name(spa_types[type].name));
if (choice == SPA_CHOICE_None) {
spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
} else {
const char *ssep, *esep, *sep;
switch (choice) {
case SPA_CHOICE_Range:
case SPA_CHOICE_Step:
ssep = "[ ";
sep = ", ";
esep = " ]";
break;
default:
case SPA_CHOICE_Enum:
case SPA_CHOICE_Flags:
ssep = "{ ";
sep = ", ";
esep = " }";
break;
}
spa_strbuf_append(&buf, "%s", ssep);
for (i = 1; i < n_vals; i++) {
vals = SPA_PTROFF(vals, size, void);
if (i > 1)
spa_strbuf_append(&buf, "%s", sep);
spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
}
spa_strbuf_append(&buf, "%s", esep);
}
spa_debugc(ctx, "%s", buffer);
}
return 0;
}
static inline int spa_debug_format(int indent,
const struct spa_type_info *info, const struct spa_pod *format)
{
return spa_debugc_format(NULL, indent, info, format);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#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 format.h:206
spa_debugn
#define spa_debugn(_fmt,...)
Definition context.h:27
spa_debugc
#define spa_debugc(_c, _fmt,...)
Definition context.h:37
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:26
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 format.h:116
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:53
spa_debug_type_short_name
static const char * spa_debug_type_short_name(const char *name)
Definition types.h:45
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:60
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:105
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 format.h:30
spa_type_media_subtype
static const struct spa_type_info spa_type_media_subtype[]
Definition format-types.h:54
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:27
spa_type_format
static const struct spa_type_info spa_type_format[]
Definition format-types.h:129
spa_type_media_type
static const struct spa_type_info spa_type_media_type[]
Definition format-types.h:38
SPA_FORMAT_mediaType
@ SPA_FORMAT_mediaType
media type (Id enum spa_media_type)
Definition format.h:93
SPA_FORMAT_mediaSubtype
@ SPA_FORMAT_mediaSubtype
media subtype (Id enum spa_media_subtype)
Definition format.h:94
SPA_POD_OBJECT_FOREACH
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:108
SPA_POD_BODY
#define SPA_POD_BODY(pod)
Definition pod.h:39
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition pod.h:28
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:347
SPA_POD_ARRAY_BODY_FOREACH
#define SPA_POD_ARRAY_BODY_FOREACH(body, _size, iter)
Definition iter.h:79
SPA_CHOICE_Step
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:149
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:147
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition pod.h:151
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:148
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:150
spa_strbuf_append
static int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition string.h:378
spa_strbuf_init
static void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition string.h:370
spa_types
static const struct spa_type_info spa_types[]
Definition type-info.h:42
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition type.h:34
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition type.h:40
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:35
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition type.h:32
SPA_TYPE_Bytes
@ SPA_TYPE_Bytes
Definition type.h:39
SPA_TYPE_Bitmap
@ SPA_TYPE_Bitmap
Definition type.h:42
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition type.h:45
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition type.h:36
SPA_TYPE_Fraction
@ SPA_TYPE_Fraction
Definition type.h:41
_SPA_TYPE_LAST
@ _SPA_TYPE_LAST
not part of ABI
Definition type.h:51
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition type.h:37
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:33
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition type.h:43
SPA_TYPE_String
@ SPA_TYPE_String
Definition type.h:38
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:198
parser.h
spa/pod/parser.h
context.h
spa/debug/context.h
string.h
spa/utils/string.h
spa_debug_context
Definition context.h:33
spa_fraction
Definition defs.h:123
spa_fraction::num
uint32_t num
Definition defs.h:124
spa_fraction::denom
uint32_t denom
Definition defs.h:125
spa_pod_array_body
Definition pod.h:121
spa_pod_array_body::child
struct spa_pod child
Definition pod.h:122
spa_pod_object
Definition pod.h:183
spa_pod_prop
Definition pod.h:208
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
spa_pod_prop::value
struct spa_pod value
Definition pod.h:226
spa_pod
Definition pod.h:43
spa_pod::type
uint32_t type
Definition pod.h:45
spa_pod::size
uint32_t size
Definition pod.h:44
spa_rectangle
Definition defs.h:102
spa_rectangle::width
uint32_t width
Definition defs.h:103
spa_rectangle::height
uint32_t height
Definition defs.h:104
spa_strbuf
Definition string.h:364
spa_strbuf::buffer
char * buffer
Definition string.h:365
spa_type_info
Definition type.h:143
spa_type_info::type
uint32_t type
Definition type.h:144
spa_type_info::values
const struct spa_type_info * values
Definition type.h:147
types.h
spa/debug/types.h