1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/* Metadata API
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <extensions/metadata.h>
#define pw_metadata_emit(hooks,method,version,...) \
spa_hook_list_call_simple(hooks, struct pw_metadata_events, \
method, version, ##__VA_ARGS__)
#define pw_metadata_emit_property(hooks,...) pw_metadata_emit(hooks,property, 0, ##__VA_ARGS__)
struct item {
uint32_t subject;
char *key;
char *type;
char *value;
};
static void clear_item(struct item *item)
{
free(item->key);
free(item->type);
free(item->value);
spa_zero(*item);
}
static void set_item(struct item *item, uint32_t subject, const char *key, const char *type, const char *value)
{
item->subject = subject;
item->key = strdup(key);
item->type = strdup(type);
item->value = strdup(value);
}
struct sm_metadata {
struct pw_metadata impl;
struct spa_hook_list hooks;
struct pw_properties *properties;
struct pw_array metadata;
};
static void emit_properties(struct sm_metadata *this, const struct spa_dict *dict)
{
struct item *item;
pw_array_for_each(item, &this->metadata) {
pw_metadata_emit_property(&this->hooks,
item->subject,
item->key,
item->type,
item->value);
}
}
static int impl_add_listener(void *object,
struct spa_hook *listener,
const struct pw_metadata_events *events,
void *data)
{
struct sm_metadata *this = object;
struct spa_hook_list save;
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(events != NULL, -EINVAL);
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
emit_properties(this, &this->properties->dict);
spa_hook_list_join(&this->hooks, &save);
return 0;
}
static struct item *find_item(struct sm_metadata *this, uint32_t subject, const char *key)
{
struct item *item;
pw_array_for_each(item, &this->metadata) {
if (item->subject == subject && !strcmp(item->key, key))
return item;
}
return NULL;
}
static void clear_items(struct sm_metadata *this)
{
struct item *item;
pw_array_for_each(item, &this->metadata)
clear_item(item);
pw_array_reset(&this->metadata);
}
static int impl_set_property(void *object,
uint32_t subject,
const char *key,
const char *type,
const char *value)
{
struct sm_metadata *this = object;
struct item *item = NULL;
if (key == NULL)
return -EINVAL;
item = find_item(this, subject, key);
if (item == NULL) {
if (value == NULL)
return 0;
item = pw_array_add(&this->metadata, sizeof(*item));
if (item == NULL)
return -errno;
} else {
clear_item(item);
}
if (value != NULL) {
if (type == NULL)
type = "string";
set_item(item, subject, key, type, value);
pw_log_debug(NAME" %p: add id:%d key:%s type:%s value:%s", this,
subject, key, type, value);
} else {
type = NULL;
pw_array_remove(&this->metadata, item);
pw_log_debug(NAME" %p: remove id:%d key:%s", this, subject, key);
}
pw_metadata_emit_property(&this->hooks,
subject, key, type, value);
return 0;
}
static int impl_clear(void *object)
{
struct sm_metadata *this = object;
clear_items(this);
return 0;
}
struct pw_metadata_methods impl_metadata = {
PW_VERSION_METADATA_METHODS,
.add_listener = impl_add_listener,
.set_property = impl_set_property,
.clear = impl_clear,
};
struct sm_metadata *
sm_metadata_new(struct pw_properties *props)
{
struct sm_metadata *md;
if (props == NULL)
props = pw_properties_new(NULL, NULL);
if (props == NULL)
return NULL;
md = calloc(1, sizeof(*md));
md->properties = props;
pw_array_init(&md->metadata, 4096);
md->impl.iface = SPA_INTERFACE_INIT(
PW_TYPE_INTERFACE_Metadata,
PW_VERSION_METADATA,
&impl_metadata, md);
spa_hook_list_init(&md->hooks);
return md;
}
void sm_metadata_destroy(struct sm_metadata *this)
{
clear_items(this);
pw_array_clear(&this->metadata);
pw_properties_free(this->properties);
free(this);
}
|