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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/***
This file is part of PulseAudio.
Copyright 2009 Tanu Kaskinen
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulsecore/core-util.h>
#include <pulsecore/dbus-util.h>
#include <pulsecore/namereg.h>
#include <pulsecore/protocol-dbus.h>
#include "iface-sample.h"
#define OBJECT_NAME "sample"
struct pa_dbusiface_sample {
pa_dbusiface_core *core;
pa_scache_entry *sample;
char *path;
pa_proplist *proplist;
pa_dbus_protocol *dbus_protocol;
pa_subscription *subscription;
};
static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_default_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_duration(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_bytes(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_play(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_play_to_sink(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_remove(DBusConnection *conn, DBusMessage *msg, void *userdata);
enum property_handler_index {
PROPERTY_HANDLER_INDEX,
PROPERTY_HANDLER_NAME,
PROPERTY_HANDLER_SAMPLE_FORMAT,
PROPERTY_HANDLER_SAMPLE_RATE,
PROPERTY_HANDLER_CHANNELS,
PROPERTY_HANDLER_DEFAULT_VOLUME,
PROPERTY_HANDLER_DURATION,
PROPERTY_HANDLER_BYTES,
PROPERTY_HANDLER_PROPERTY_LIST,
PROPERTY_HANDLER_MAX
};
static pa_dbus_property_handler property_handlers[PROPERTY_HANDLER_MAX] = {
[PROPERTY_HANDLER_INDEX] = { .property_name = "Index", .type = "u", .get_cb = handle_get_index, .set_cb = NULL },
[PROPERTY_HANDLER_NAME] = { .property_name = "Name", .type = "s", .get_cb = handle_get_name, .set_cb = NULL },
[PROPERTY_HANDLER_SAMPLE_FORMAT] = { .property_name = "SampleFormat", .type = "u", .get_cb = handle_get_sample_format, .set_cb = NULL },
[PROPERTY_HANDLER_SAMPLE_RATE] = { .property_name = "SampleRate", .type = "u", .get_cb = handle_get_sample_rate, .set_cb = NULL },
[PROPERTY_HANDLER_CHANNELS] = { .property_name = "Channels", .type = "au", .get_cb = handle_get_channels, .set_cb = NULL },
[PROPERTY_HANDLER_DEFAULT_VOLUME] = { .property_name = "DefaultVolume", .type = "au", .get_cb = handle_get_default_volume, .set_cb = NULL },
[PROPERTY_HANDLER_DURATION] = { .property_name = "Duration", .type = "t", .get_cb = handle_get_duration, .set_cb = NULL },
[PROPERTY_HANDLER_BYTES] = { .property_name = "Bytes", .type = "u", .get_cb = handle_get_bytes, .set_cb = NULL },
[PROPERTY_HANDLER_PROPERTY_LIST] = { .property_name = "PropertyList", .type = "a{say}", .get_cb = handle_get_property_list, .set_cb = NULL }
};
enum method_handler_index {
METHOD_HANDLER_PLAY,
METHOD_HANDLER_PLAY_TO_SINK,
METHOD_HANDLER_REMOVE,
METHOD_HANDLER_MAX
};
static pa_dbus_arg_info play_args[] = { { "volume", "u", "in" }, { "property_list", "a{say}", "in" } };
static pa_dbus_arg_info play_to_sink_args[] = { { "sink", "o", "in" },
{ "volume", "u", "in" },
{ "property_list", "a{say}", "in" } };
static pa_dbus_method_handler method_handlers[METHOD_HANDLER_MAX] = {
[METHOD_HANDLER_PLAY] = {
.method_name = "Play",
.arguments = play_args,
.n_arguments = sizeof(play_args) / sizeof(pa_dbus_arg_info),
.receive_cb = handle_play },
[METHOD_HANDLER_PLAY_TO_SINK] = {
.method_name = "PlayToSink",
.arguments = play_to_sink_args,
.n_arguments = sizeof(play_to_sink_args) / sizeof(pa_dbus_arg_info),
.receive_cb = handle_play_to_sink },
[METHOD_HANDLER_REMOVE] = {
.method_name = "Remove",
.arguments = NULL,
.n_arguments = 0,
.receive_cb = handle_remove }
};
enum signal_index {
SIGNAL_PROPERTY_LIST_UPDATED,
SIGNAL_MAX
};
static pa_dbus_arg_info property_list_updated_args[] = { { "property_list", "a{say}", NULL } };
static pa_dbus_signal_info signals[SIGNAL_MAX] = {
[SIGNAL_PROPERTY_LIST_UPDATED] = { .name = "PropertyListUpdated", .arguments = property_list_updated_args, .n_arguments = 1 }
};
static pa_dbus_interface_info sample_interface_info = {
.name = PA_DBUSIFACE_SAMPLE_INTERFACE,
.method_handlers = method_handlers,
.n_method_handlers = METHOD_HANDLER_MAX,
.property_handlers = property_handlers,
.n_property_handlers = PROPERTY_HANDLER_MAX,
.get_all_properties_cb = handle_get_all,
.signals = signals,
.n_signals = SIGNAL_MAX
};
static void handle_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t idx = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
idx = s->sample->index;
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &idx);
}
static void handle_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &s->sample->name);
}
static void handle_get_sample_format(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t sample_format = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->memchunk.memblock) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s isn't loaded into memory yet, so its sample format is unknown.", s->sample->name);
return;
}
sample_format = s->sample->sample_spec.format;
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_format);
}
static void handle_get_sample_rate(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t sample_rate = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->memchunk.memblock) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s isn't loaded into memory yet, so its sample rate is unknown.", s->sample->name);
return;
}
sample_rate = s->sample->sample_spec.rate;
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &sample_rate);
}
static void handle_get_channels(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t channels[PA_CHANNELS_MAX];
unsigned i = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->memchunk.memblock) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s isn't loaded into memory yet, so its channel map is unknown.", s->sample->name);
return;
}
for (i = 0; i < s->sample->channel_map.channels; ++i)
channels[i] = s->sample->channel_map.map[i];
pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, channels, s->sample->channel_map.channels);
}
static void handle_get_default_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t default_volume[PA_CHANNELS_MAX];
unsigned i = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->volume_is_set) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s doesn't have default volume stored.", s->sample->name);
return;
}
for (i = 0; i < s->sample->volume.channels; ++i)
default_volume[i] = s->sample->volume.values[i];
pa_dbus_send_basic_array_variant_reply(conn, msg, DBUS_TYPE_UINT32, default_volume, s->sample->volume.channels);
}
static void handle_get_duration(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint64_t duration = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->memchunk.memblock) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s isn't loaded into memory yet, so its duration is unknown.", s->sample->name);
return;
}
duration = pa_bytes_to_usec(s->sample->memchunk.length, &s->sample->sample_spec);
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT64, &duration);
}
static void handle_get_bytes(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
dbus_uint32_t bytes = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (!s->sample->memchunk.memblock) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NO_SUCH_PROPERTY,
"Sample %s isn't loaded into memory yet, so its size is unknown.", s->sample->name);
return;
}
bytes = s->sample->memchunk.length;
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_UINT32, &bytes);
}
static void handle_get_property_list(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
pa_dbus_send_proplist_variant_reply(conn, msg, s->proplist);
}
static void handle_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
DBusMessage *reply = NULL;
DBusMessageIter msg_iter;
DBusMessageIter dict_iter;
dbus_uint32_t idx = 0;
dbus_uint32_t sample_format = 0;
dbus_uint32_t sample_rate = 0;
dbus_uint32_t channels[PA_CHANNELS_MAX];
dbus_uint32_t default_volume[PA_CHANNELS_MAX];
dbus_uint64_t duration = 0;
dbus_uint32_t bytes = 0;
unsigned i = 0;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
idx = s->sample->index;
if (s->sample->memchunk.memblock) {
sample_format = s->sample->sample_spec.format;
sample_rate = s->sample->sample_spec.rate;
for (i = 0; i < s->sample->channel_map.channels; ++i)
channels[i] = s->sample->channel_map.map[i];
duration = pa_bytes_to_usec(s->sample->memchunk.length, &s->sample->sample_spec);
bytes = s->sample->memchunk.length;
}
if (s->sample->volume_is_set) {
for (i = 0; i < s->sample->volume.channels; ++i)
default_volume[i] = s->sample->volume.values[i];
}
pa_assert_se((reply = dbus_message_new_method_return(msg)));
dbus_message_iter_init_append(reply, &msg_iter);
pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_INDEX].property_name, DBUS_TYPE_UINT32, &idx);
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_NAME].property_name, DBUS_TYPE_STRING, &s->sample->name);
if (s->sample->memchunk.memblock) {
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_FORMAT].property_name, DBUS_TYPE_UINT32, &sample_format);
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_SAMPLE_RATE].property_name, DBUS_TYPE_UINT32, &sample_rate);
pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_CHANNELS].property_name, DBUS_TYPE_UINT32, channels, s->sample->channel_map.channels);
}
if (s->sample->volume_is_set)
pa_dbus_append_basic_array_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DEFAULT_VOLUME].property_name, DBUS_TYPE_UINT32, default_volume, s->sample->volume.channels);
if (s->sample->memchunk.memblock) {
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_DURATION].property_name, DBUS_TYPE_UINT64, &duration);
pa_dbus_append_basic_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_BYTES].property_name, DBUS_TYPE_UINT32, &bytes);
}
pa_dbus_append_proplist_variant_dict_entry(&dict_iter, property_handlers[PROPERTY_HANDLER_PROPERTY_LIST].property_name, s->proplist);
pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
pa_assert_se(dbus_connection_send(conn, reply, NULL));
dbus_message_unref(reply);
}
static void handle_play(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
DBusMessageIter msg_iter;
dbus_uint32_t volume = 0;
pa_proplist *property_list = NULL;
pa_sink *sink = NULL;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
dbus_message_iter_get_basic(&msg_iter, &volume);
pa_assert_se(dbus_message_iter_next(&msg_iter));
if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
return;
if (!PA_VOLUME_IS_VALID(volume)) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume.");
goto finish;
}
if (!(sink = pa_namereg_get_default_sink(s->sample->core))) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
"Can't play sample %s, because there are no sinks available.", s->sample->name);
goto finish;
}
if (pa_scache_play_item(s->sample->core, s->sample->name, sink, volume, property_list, NULL) < 0) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Playing sample %s failed.", s->sample->name);
goto finish;
}
pa_dbus_send_empty_reply(conn, msg);
finish:
if (property_list)
pa_proplist_free(property_list);
}
static void handle_play_to_sink(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
DBusMessageIter msg_iter;
const char *sink_path = NULL;
dbus_uint32_t volume = 0;
pa_proplist *property_list = NULL;
pa_sink *sink = NULL;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
dbus_message_iter_get_basic(&msg_iter, &sink_path);
pa_assert_se(dbus_message_iter_next(&msg_iter));
dbus_message_iter_get_basic(&msg_iter, &volume);
pa_assert_se(dbus_message_iter_next(&msg_iter));
if (!(property_list = pa_dbus_get_proplist_arg(conn, msg, &msg_iter)))
return;
if (!(sink = pa_dbusiface_core_get_sink(s->core, sink_path))) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "%s: No such sink.", sink_path);
goto finish;
}
if (!PA_VOLUME_IS_VALID(volume)) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid volume.");
goto finish;
}
if (pa_scache_play_item(s->sample->core, s->sample->name, sink, volume, property_list, NULL) < 0) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Playing sample %s failed.", s->sample->name);
goto finish;
}
pa_dbus_send_empty_reply(conn, msg);
finish:
if (property_list)
pa_proplist_free(property_list);
}
static void handle_remove(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_dbusiface_sample *s = userdata;
pa_assert(conn);
pa_assert(msg);
pa_assert(s);
if (pa_scache_remove_item(s->sample->core, s->sample->name) < 0) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Removing sample %s failed.", s->sample->name);
return;
}
pa_dbus_send_empty_reply(conn, msg);
}
static void subscription_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
pa_dbusiface_sample *s = userdata;
DBusMessage *signal_msg = NULL;
pa_assert(c);
pa_assert(s);
/* We can't use idx != s->sample->index, because the s->sample pointer may
* be stale at this point. */
if (pa_idxset_get_by_index(c->scache, idx) != s->sample)
return;
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
return;
if (!pa_proplist_equal(s->proplist, s->sample->proplist)) {
DBusMessageIter msg_iter;
pa_proplist_update(s->proplist, PA_UPDATE_SET, s->sample->proplist);
pa_assert_se(signal_msg = dbus_message_new_signal(s->path,
PA_DBUSIFACE_SAMPLE_INTERFACE,
signals[SIGNAL_PROPERTY_LIST_UPDATED].name));
dbus_message_iter_init_append(signal_msg, &msg_iter);
pa_dbus_append_proplist(&msg_iter, s->proplist);
pa_dbus_protocol_send_signal(s->dbus_protocol, signal_msg);
dbus_message_unref(signal_msg);
signal_msg = NULL;
}
}
pa_dbusiface_sample *pa_dbusiface_sample_new(pa_dbusiface_core *core, pa_scache_entry *sample) {
pa_dbusiface_sample *s = NULL;
pa_assert(core);
pa_assert(sample);
s = pa_xnew0(pa_dbusiface_sample, 1);
s->core = core;
s->sample = sample;
s->path = pa_sprintf_malloc("%s/%s%u", PA_DBUS_CORE_OBJECT_PATH, OBJECT_NAME, sample->index);
s->proplist = pa_proplist_copy(sample->proplist);
s->dbus_protocol = pa_dbus_protocol_get(sample->core);
s->subscription = pa_subscription_new(sample->core, PA_SUBSCRIPTION_MASK_SAMPLE_CACHE, subscription_cb, s);
pa_assert_se(pa_dbus_protocol_add_interface(s->dbus_protocol, s->path, &sample_interface_info, s) >= 0);
return s;
}
void pa_dbusiface_sample_free(pa_dbusiface_sample *s) {
pa_assert(s);
pa_assert_se(pa_dbus_protocol_remove_interface(s->dbus_protocol, s->path, sample_interface_info.name) >= 0);
pa_proplist_free(s->proplist);
pa_dbus_protocol_unref(s->dbus_protocol);
pa_subscription_free(s->subscription);
pa_xfree(s->path);
pa_xfree(s);
}
const char *pa_dbusiface_sample_get_path(pa_dbusiface_sample *s) {
pa_assert(s);
return s->path;
}
|