summaryrefslogtreecommitdiff
path: root/pipewire-pulseaudio/src/context.c
blob: d5735d843519260eaf93b180cca59b9f4874b1e4 (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
/* PipeWire
 * Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <errno.h>

#include <spa/param/props.h>

#include <pipewire/pipewire.h>

#include <pulse/context.h>
#include <pulse/timeval.h>
#include <pulse/error.h>

#include "internal.h"

int pa_context_set_error(const pa_context *c, int error) {
	pa_assert(error >= 0);
	pa_assert(error < PA_ERR_MAX);
	if (c && c->error != error) {
		pw_log_debug("context %p: error %d %s", c, error, pa_strerror(error));
		((pa_context*)c)->error = error;
	}
	return error;
}

static void global_free(pa_context *c, struct global *g)
{
	spa_list_remove(&g->link);

	if (g->destroy)
		g->destroy(g);
	if (g->proxy) {
		spa_hook_remove(&g->object_listener);
		spa_hook_remove(&g->proxy_listener);
		pw_proxy_destroy(g->proxy);
	}
	if (g->props)
		pw_properties_free(g->props);
	free(g);
}

static void context_unlink(pa_context *c)
{
	pa_stream *s, *t;
	struct global *g;
	pa_operation *o;

	pw_log_debug("context %p: unlink %d", c, c->state);

	c->disconnect = true;
	c->state_callback = NULL;
	c->state_userdata = NULL;

	spa_list_for_each_safe(s, t, &c->streams, link) {
		pa_stream_set_state(s, c->state == PA_CONTEXT_FAILED ?
				PA_STREAM_FAILED : PA_STREAM_TERMINATED);
	}
	spa_list_consume(g, &c->globals, link)
		global_free(c, g);

	spa_list_consume(o, &c->operations, link)
		pa_operation_cancel(o);
}

void pa_context_set_state(pa_context *c, pa_context_state_t st) {
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	if (c->state == st)
		return;

	pw_log_debug("context %p: state %d", c, st);

	pa_context_ref(c);

	c->state = st;

	if (c->state_callback)
		c->state_callback(c, c->state_userdata);

	if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED)
		context_unlink(c);

	pa_context_unref(c);
}

static void context_fail(pa_context *c, int error) {
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	pw_log_debug("context %p: error %d", c, error);

	pa_context_set_error(c, error);
	pa_context_set_state(c, PA_CONTEXT_FAILED);
}

SPA_EXPORT
pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name)
{
	return pa_context_new_with_proplist(mainloop, name, NULL);
}

struct global *pa_context_find_global(pa_context *c, uint32_t id)
{
	struct global *g;
	spa_list_for_each(g, &c->globals, link) {
		if (g->id == id)
			return g;
	}
	return NULL;
}

struct global *pa_context_find_global_by_name(pa_context *c, uint32_t mask, const char *name)
{
	struct global *g;
	const char *str;
	uint32_t id = atoi(name);

	spa_list_for_each(g, &c->globals, link) {
		if ((g->mask & mask) == 0)
			continue;
		if (g->props != NULL &&
		    (str = pw_properties_get(g->props, PW_KEY_NODE_NAME)) != NULL &&
		    strcmp(str, name) == 0)
			return g;
		if (g->id == id)
			return g;
	}
	return NULL;
}

struct global *pa_context_find_linked(pa_context *c, uint32_t idx)
{
	struct global *g, *f;

	spa_list_for_each(g, &c->globals, link) {
		if (g->type != PW_TYPE_INTERFACE_EndpointLink)
			continue;

		pw_log_debug("context %p: %p %d %d:%d %d:%d", c, g, idx,
				g->link_info.output->id, g->link_info.output->endpoint_info.node_id,
				g->link_info.input->id, g->link_info.input->endpoint_info.node_id);

		if (g->link_info.input->id == idx ||
		    g->link_info.input->endpoint_info.node_id == idx)
			f = g->link_info.output;
		else if (g->link_info.output->id == idx ||
		    g->link_info.output->endpoint_info.node_id == idx)
			f = g->link_info.input;
		else
			continue;

		if (f == NULL || ((f->mask & (PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SINK)) == 0))
			continue;

		return f;
	}
	return NULL;
}

static void emit_event(pa_context *c, struct global *g, pa_subscription_event_type_t event)
{
	if (c->subscribe_callback && (c->subscribe_mask & g->mask)) {
		pw_log_debug("context %p: obj %d: emit %d:%d", c, g->id, event, g->event);
		c->subscribe_callback(c,
				event | g->event,
				g->id,
				c->subscribe_userdata);
	}
}

static void update_device_props(struct global *g)
{
	pa_card_info *i = &g->card_info.info;
	const char *s;

	if ((s = pa_proplist_gets(i->proplist, PW_KEY_DEVICE_ICON_NAME)))
		pa_proplist_sets(i->proplist, PA_PROP_DEVICE_ICON_NAME, s);
}

static void device_event_info(void *object, const struct pw_device_info *update)
{
        struct global *g = object;
	pa_card_info *i = &g->card_info.info;
	const char *str;
	uint32_t n;
	struct pw_device_info *info;

	pw_log_debug("global %p: id:%d change-mask:%08lx", g, g->id, update->change_mask);
	info = g->info = pw_device_info_update(g->info, update);

	i->index = g->id;
	i->name = info->props ?
		spa_dict_lookup(info->props, PW_KEY_DEVICE_NAME) : "unknown";
	str = info->props ? spa_dict_lookup(info->props, PW_KEY_MODULE_ID) : NULL;
	i->owner_module = str ? (unsigned)atoi(str) : SPA_ID_INVALID;
	if (info->change_mask & PW_DEVICE_CHANGE_MASK_PROPS) {
		i->driver = info->props ?
			spa_dict_lookup(info->props, PW_KEY_DEVICE_API) : NULL;

		if (i->proplist)
			pa_proplist_update_dict(i->proplist, info->props);
		else {
			i->proplist = pa_proplist_new_dict(info->props);
		}
		update_device_props(g);
	}
	info->change_mask = update->change_mask;
	if (update->change_mask & PW_DEVICE_CHANGE_MASK_PARAMS) {
		for (n = 0; n < info->n_params; n++) {
			if (!(info->params[n].flags & SPA_PARAM_INFO_READ))
				continue;

			switch (info->params[n].id) {
			case SPA_PARAM_EnumProfile:
				pw_device_enum_params((struct pw_device*)g->proxy,
					0, SPA_PARAM_EnumProfile, 0, -1, NULL);
				break;
			case SPA_PARAM_Profile:
				pw_device_enum_params((struct pw_device*)g->proxy,
					0, SPA_PARAM_Profile, 0, -1, NULL);
				break;
			default:
				break;
			}
		}
	}
	g->pending_seq = pw_proxy_sync(g->proxy, 0);
}

static void device_event_param(void *object, int seq,
		uint32_t id, uint32_t index, uint32_t next,
		const struct spa_pod *param)
{
	struct global *g = object;

	switch (id) {
	case SPA_PARAM_EnumProfile:
	{
		uint32_t id;
		const char *name;
		struct param *p;

		if (spa_pod_parse_object(param,
				SPA_TYPE_OBJECT_ParamProfile, NULL,
				SPA_PARAM_PROFILE_index, SPA_POD_Int(&id),
				SPA_PARAM_PROFILE_name,  SPA_POD_String(&name)) < 0) {
			pw_log_warn("device %d: can't parse profile", g->id);
			return;
		}
		p = malloc(sizeof(struct param) + SPA_POD_SIZE(param));
		if (p) {
			p->id = id;
			p->seq = seq;
			p->param = SPA_MEMBER(p, sizeof(struct param), struct spa_pod);
			memcpy(p->param, param, SPA_POD_SIZE(param));
			spa_list_append(&g->card_info.profiles, &p->link);
			g->card_info.n_profiles++;
		}
		pw_log_debug("device %d: enum profile %d: \"%s\"", g->id, id, name);
		break;
	}
	case SPA_PARAM_Profile:
	{
		uint32_t id;
		if (spa_pod_parse_object(param,
				SPA_TYPE_OBJECT_ParamProfile, NULL,
				SPA_PARAM_PROFILE_index, SPA_POD_Int(&id)) < 0) {
			pw_log_warn("device %d: can't parse profile", g->id);
			return;
		}
		g->card_info.active_profile = id;
		pw_log_debug("device %d: current profile %d", g->id, id);
		break;
	}
	default:
		break;
	}
}

static const struct pw_device_events device_events = {
	PW_VERSION_DEVICE_EVENTS,
	.info = device_event_info,
	.param = device_event_param,
};

static void device_destroy(void *data)
{
	struct global *global = data;
	struct param *p;

	if (global->card_info.info.proplist)
		pa_proplist_free(global->card_info.info.proplist);
	spa_list_consume(p, &global->card_info.profiles, link) {
		spa_list_remove(&p->link);
		free(p);
	}
	if (global->info)
		pw_device_info_free(global->info);
}

static void endpoint_event_info(void *object, const struct pw_endpoint_info *update)
{
	struct global *g = object;
	struct pw_endpoint_info *info = g->info;
	uint32_t i;

	pw_log_debug("update %d %08x", g->id, update->change_mask);
	if (info == NULL) {
		info = g->info = calloc(1, sizeof(*info));
		info->id = update->id;
		info->name = update->name ? strdup(update->name) : NULL;
		info->media_class = update->media_class ? strdup(update->media_class) : NULL;
		info->direction = update->direction;
		info->flags = update->flags;
	}
	info->change_mask = update->change_mask;

	if (update->change_mask & PW_ENDPOINT_CHANGE_MASK_STREAMS)
		info->n_streams = update->n_streams;
	if (update->change_mask & PW_ENDPOINT_CHANGE_MASK_SESSION)
		info->session_id = update->session_id;
	if (update->change_mask & PW_ENDPOINT_CHANGE_MASK_PARAMS && !g->subscribed) {
		uint32_t subscribed[32], n_subscribed = 0;

		info->n_params = update->n_params;
		free(info->params);
		info->params = malloc(info->n_params * sizeof(struct spa_param_info));
		memcpy(info->params, update->params,
			info->n_params * sizeof(struct spa_param_info));

		for (i = 0; i < info->n_params; i++) {
			switch (info->params[i].id) {
			case SPA_PARAM_EnumRoute:
			case SPA_PARAM_Props:
				subscribed[n_subscribed++] = info->params[i].id;
				break;
			default:
				break;
			}
		}
		if (n_subscribed > 0) {
			pw_endpoint_subscribe_params((struct pw_endpoint*)g->proxy,
					subscribed, n_subscribed);
			g->subscribed = true;
		}
	}
	if (update->change_mask & PW_ENDPOINT_CHANGE_MASK_PROPS) {
		if (info->props)
			pw_properties_free ((struct pw_properties *)info->props);
		info->props =
			(struct spa_dict *) pw_properties_new_dict (update->props);

#if 0
		i->name = info->props ?
			spa_dict_lookup(info->props, PW_KEY_ENDPOINT_NAME) : "unknown";
		str = info->props ? spa_dict_lookup(info->props, PW_KEY_MODULE_ID) : NULL;
		i->owner_module = str ? (unsigned)atoi(str) : SPA_ID_INVALID;
		i->driver = info->props ?
			spa_dict_lookup(info->props, PW_KEY_DEVICE_API) : "unknown";

		if (i->proplist)
			pa_proplist_update_dict(i->proplist, info->props);
		else {
			i->proplist = pa_proplist_new_dict(info->props);
		}
#endif
	}
	g->pending_seq = pw_proxy_sync(g->proxy, 0);
}

static void parse_props(struct global *g, const struct spa_pod *param)
{
	struct spa_pod_prop *prop;
	struct spa_pod_object *obj = (struct spa_pod_object *) param;

	SPA_POD_OBJECT_FOREACH(obj, prop) {
		switch (prop->key) {
		case SPA_PROP_volume:
			spa_pod_get_float(&prop->value, &g->endpoint_info.volume);
			break;
		case SPA_PROP_mute:
			spa_pod_get_bool(&prop->value, &g->endpoint_info.mute);
			break;
		case SPA_PROP_channelVolumes:
		{
			uint32_t n_vals;

			n_vals = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
					g->endpoint_info.channel_volumes, SPA_AUDIO_MAX_CHANNELS);

			if (n_vals != g->endpoint_info.n_channel_volumes) {
				emit_event(g->context, g, PA_SUBSCRIPTION_EVENT_REMOVE);
				emit_event(g->context, g, PA_SUBSCRIPTION_EVENT_NEW);
				g->endpoint_info.n_channel_volumes = n_vals;
			}
			break;
		}
		default:
			break;
		}
	}
}

/* routing information on the endpoint is mapped to sink/source ports. */
static void parse_route(struct global *g, const struct spa_pod *param)
{
}

static void endpoint_event_param(void *object, int seq,
		uint32_t id, uint32_t index, uint32_t next,
		const struct spa_pod *param)
{
	struct global *g = object;
	pw_log_debug("update param %d %d", g->id, id);

	switch (id) {
	case SPA_PARAM_Props:
		parse_props(g, param);
		break;
	case SPA_PARAM_EnumRoute:
		parse_route(g, param);
		break;
	default:
		break;
	}
}

static const struct pw_endpoint_events endpoint_events = {
	PW_VERSION_ENDPOINT_EVENTS,
	.info = endpoint_event_info,
	.param = endpoint_event_param,
};

static void endpoint_destroy(void *data)
{
	struct global *global = data;
	if (global->info) {
		struct pw_endpoint_info *info = global->info;
		free(info->name);
		free(info->params);
		if (info->props)
			pw_properties_free ((struct pw_properties *)info->props);
		free(info);
	}
}

static void module_event_info(void *object, const struct pw_module_info *info)
{
        struct global *g = object;
	pa_module_info *i = &g->module_info.info;

	pw_log_debug("update %d", g->id);

        info = g->info = pw_module_info_update(g->info, info);

	i->index = g->id;
	if (info->change_mask & PW_MODULE_CHANGE_MASK_PROPS) {
		if (i->proplist)
			pa_proplist_update_dict(i->proplist, info->props);
		else
			i->proplist = pa_proplist_new_dict(info->props);
	}

	i->name = info->name;
	i->argument = info->args;
	i->n_used = -1;
	i->auto_unload = false;
	g->pending_seq = pw_proxy_sync(g->proxy, 0);
}

static const struct pw_module_events module_events = {
	PW_VERSION_MODULE_EVENTS,
	.info = module_event_info,
};

static void module_destroy(void *data)
{
	struct global *global = data;
	if (global->module_info.info.proplist)
		pa_proplist_free(global->module_info.info.proplist);
	if (global->info)
		pw_module_info_free(global->info);
}

static void client_event_info(void *object, const struct pw_client_info *info)
{
        struct global *g = object;
	const char *str;
	pa_client_info *i = &g->client_info.info;

	pw_log_debug("update %d", g->id);
	info = g->info = pw_client_info_update(g->info, info);

	i->index = g->id;
	str = info->props ? spa_dict_lookup(info->props, PW_KEY_MODULE_ID) : NULL;
	i->owner_module = str ? (unsigned)atoi(str) : SPA_ID_INVALID;

	if (info->change_mask & PW_CLIENT_CHANGE_MASK_PROPS) {
		if (i->proplist)
			pa_proplist_update_dict(i->proplist, info->props);
		else
			i->proplist = pa_proplist_new_dict(info->props);
		i->name = info->props ?
			spa_dict_lookup(info->props, PW_KEY_APP_NAME) : NULL;
		i->driver = info->props ?
			spa_dict_lookup(info->props, PW_KEY_PROTOCOL) : NULL;
	}
	g->pending_seq = pw_proxy_sync(g->proxy, 0);
}

static const struct pw_client_events client_events = {
	PW_VERSION_CLIENT_EVENTS,
	.info = client_event_info,
};

static void client_destroy(void *data)
{
	struct global *global = data;
	if (global->client_info.info.proplist)
		pa_proplist_free(global->client_info.info.proplist);
	if (global->info)
		pw_client_info_free(global->info);
}

static void proxy_destroy(void *data)
{
	struct global *g = data;
	spa_hook_remove(&g->proxy_listener);
	g->proxy = NULL;
}

static void proxy_done(void *data, int seq)
{
	struct global *g = data;
	pa_subscription_event_type_t event;

	if (g->pending_seq == seq) {
		if (g->init) {
			g->init = false;
			event = PA_SUBSCRIPTION_EVENT_NEW;
		} else {
			event = PA_SUBSCRIPTION_EVENT_CHANGE;
		}
		emit_event(g->context, g, event);
	}
}

static const struct pw_proxy_events proxy_events = {
	PW_VERSION_PROXY_EVENTS,
	.destroy = proxy_destroy,
	.done = proxy_done,
};

static int set_mask(pa_context *c, struct global *g)
{
	const char *str;
	const void *events = NULL;
	pw_destroy_t destroy;
	uint32_t client_version;

	switch (g->type) {
	case PW_TYPE_INTERFACE_Device:
		if (g->props == NULL)
			return 0;
		if ((str = pw_properties_get(g->props, PW_KEY_MEDIA_CLASS)) == NULL)
			return 0;
		if (strcmp(str, "Audio/Device") != 0)
			return 0;

		/* devices are turned into card objects */
		pw_log_debug("found card %d", g->id);
		g->mask = PA_SUBSCRIPTION_MASK_CARD;
		g->event = PA_SUBSCRIPTION_EVENT_CARD;

		events = &device_events;
                client_version = PW_VERSION_DEVICE;
                destroy = device_destroy;
                spa_list_init(&g->card_info.profiles);
		break;

	case PW_TYPE_INTERFACE_Endpoint:
		if (g->props == NULL)
			return 0;

		if ((str = pw_properties_get(g->props, PW_KEY_PRIORITY_SESSION)) != NULL)
			g->priority_session = pw_properties_parse_int(str);

		if ((str = pw_properties_get(g->props, PW_KEY_MEDIA_CLASS)) == NULL) {
			pw_log_warn("endpoint %d without "PW_KEY_MEDIA_CLASS, g->id);
			return 0;
		}
		g->endpoint_info.monitor = SPA_ID_INVALID;

		/* endpoints get transformed into sink/source or sink_input/source_output */
		if (strcmp(str, "Audio/Sink") == 0) {
			pw_log_debug("found sink %d", g->id);
			g->mask = PA_SUBSCRIPTION_MASK_SINK;
			g->event = PA_SUBSCRIPTION_EVENT_SINK;
		}
		else if (strcmp(str, "Audio/Source") == 0) {
			pw_log_debug("found source %d", g->id);
			g->mask = PA_SUBSCRIPTION_MASK_SOURCE;
			g->event = PA_SUBSCRIPTION_EVENT_SOURCE;
			if ((str = pw_properties_get(g->props, PW_KEY_ENDPOINT_MONITOR)) != NULL) {
				struct global *f;
				f = pa_context_find_global(c, pw_properties_parse_int(str));
				if (f != NULL) {
					g->endpoint_info.monitor = f->id;
					f->endpoint_info.monitor = g->id;
				}
			}
		}
		else if (strcmp(str, "Stream/Output/Audio") == 0) {
			pw_log_debug("found sink input %d", g->id);
			g->mask = PA_SUBSCRIPTION_MASK_SINK_INPUT;
			g->event = PA_SUBSCRIPTION_EVENT_SINK_INPUT;
		}
		else if (strcmp(str, "Stream/Input/Audio") == 0) {
			pw_log_debug("found source output %d", g->id);
			g->mask = PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT;
			g->event = PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT;
		}

		if ((str = pw_properties_get(g->props, PW_KEY_ENDPOINT_CLIENT_ID)) != NULL)
			g->endpoint_info.client_id = atoi(str);
		else if ((str = pw_properties_get(g->props, PW_KEY_CLIENT_ID)) != NULL)
			g->endpoint_info.client_id = atoi(str);
		if ((str = pw_properties_get(g->props, PW_KEY_DEVICE_ID)) != NULL)
			g->endpoint_info.device_id = atoi(str);
		if ((str = pw_properties_get(g->props, PW_KEY_NODE_ID)) != NULL) {
			pa_stream *s;
			g->endpoint_info.node_id = atoi(str);
			spa_list_for_each(s, &c->streams, link) {
				if (pw_stream_get_node_id(s->stream) == g->endpoint_info.node_id)
					s->endpoint_id = g->id;
			}
		}

		events = &endpoint_events;
                client_version = PW_VERSION_ENDPOINT;
                destroy = endpoint_destroy;
		g->endpoint_info.volume = 1.0;
		g->endpoint_info.mute = false;
		break;

	case PW_TYPE_INTERFACE_EndpointStream:
		if (g->props == NULL)
			return 0;

		if ((str = pw_properties_get(g->props, PW_KEY_ENDPOINT_ID)) == NULL) {
			pw_log_warn("endpoint stream %d without "PW_KEY_ENDPOINT_ID, g->id);
			return 0;
		}
		/* streams get transformed into profiles on the device */
		pw_log_debug("found endpoint stream %d", g->id);
		break;

	case PW_TYPE_INTERFACE_Module:
		pw_log_debug("found module %d", g->id);
		g->mask = PA_SUBSCRIPTION_MASK_MODULE;
		g->event = PA_SUBSCRIPTION_EVENT_MODULE;
		events = &module_events;
                client_version = PW_VERSION_MODULE;
                destroy = module_destroy;
		break;

	case PW_TYPE_INTERFACE_Client:
		pw_log_debug("found client %d", g->id);
		g->mask = PA_SUBSCRIPTION_MASK_CLIENT;
		g->event = PA_SUBSCRIPTION_EVENT_CLIENT;
		events = &client_events;
                client_version = PW_VERSION_CLIENT;
                destroy = client_destroy;
		break;

	case PW_TYPE_INTERFACE_EndpointLink:
		if ((str = pw_properties_get(g->props, PW_KEY_ENDPOINT_LINK_OUTPUT_ENDPOINT)) == NULL)
			return 0;
		g->link_info.output = pa_context_find_global(c, pw_properties_parse_int(str));
		if ((str = pw_properties_get(g->props, PW_KEY_ENDPOINT_LINK_INPUT_ENDPOINT)) == NULL)
			return 0;
		g->link_info.input = pa_context_find_global(c, pw_properties_parse_int(str));

		if (g->link_info.output == NULL || g->link_info.input == NULL)
			return 0;

		pw_log_debug("link %d->%d", g->link_info.output->id, g->link_info.input->id);

		if (!g->link_info.output->init)
			emit_event(c, g->link_info.output, PA_SUBSCRIPTION_EVENT_CHANGE);
		if (!g->link_info.input->init)
			emit_event(c, g->link_info.input, PA_SUBSCRIPTION_EVENT_CHANGE);

		break;

	default:
		return 0;
	}

	pw_log_debug("global %p: id:%u mask %d/%d", g, g->id, g->mask, g->event);

	if (events) {
		pw_log_debug("bind %d", g->id);

		g->proxy = pw_registry_bind(c->registry, g->id, g->type,
	                                      client_version, 0);
		if (g->proxy == NULL)
	                return -ENOMEM;

		pw_proxy_add_object_listener(g->proxy, &g->object_listener, events, g);
		pw_proxy_add_listener(g->proxy, &g->proxy_listener, &proxy_events, g);
		g->destroy = destroy;
	} else {
		emit_event(c, g, PA_SUBSCRIPTION_EVENT_NEW);
	}

	return 1;
}

static inline void insert_global(pa_context *c, struct global *global)
{
	struct global *g, *t;

	spa_list_for_each_safe(g, t, &c->globals, link) {
		if (g->priority_session <= global->priority_session) {
			break;
		}
	}
	spa_list_append(&g->link, &global->link);
}

static void registry_event_global(void *data, uint32_t id,
                                  uint32_t permissions, uint32_t type, uint32_t version,
                                  const struct spa_dict *props)
{
	pa_context *c = data;
	struct global *g;
	int res;

	g = calloc(1, sizeof(struct global));
	pw_log_debug("context %p: global %d %u %p", c, id, type, g);
	g->context = c;
	g->id = id;
	g->type = type;
	g->init = true;
	g->props = props ? pw_properties_new_dict(props) : NULL;

	res = set_mask(c, g);
	insert_global(c, g);

	if (res != 1)
		global_free(c, g);
}

static void registry_event_global_remove(void *object, uint32_t id)
{
	pa_context *c = object;
	struct global *g;

	pw_log_debug("context %p: remove %d", c, id);
	if ((g = pa_context_find_global(c, id)) == NULL)
		return;

	emit_event(c, g, PA_SUBSCRIPTION_EVENT_REMOVE);

	pw_log_debug("context %p: free %d %p", c, id, g);
	global_free(c, g);
}

static const struct pw_registry_events registry_events =
{
	PW_VERSION_REGISTRY_EVENTS,
	.global = registry_event_global,
	.global_remove = registry_event_global_remove,
};

static void complete_operations(pa_context *c, int seq)
{
	pa_operation *o, *t;
	spa_list_for_each_safe(o, t, &c->operations, link) {
		if (o->seq != seq)
			continue;
		pa_operation_ref(o);
		if (o->callback)
			o->callback(o, o->userdata);
		pa_operation_unref(o);
	}
}

static void core_info(void *data, const struct pw_core_info *info)
{
	pa_context *c = data;
	c->core_info = pw_core_info_update(c->core_info, info);
}

static void core_done(void *data, uint32_t id, int seq)
{
	pa_context *c = data;
	pw_log_debug("done %d", seq);
	complete_operations(c, seq);
}

static const struct pw_core_events core_events = {
	PW_VERSION_CORE_EVENTS,
	.info = core_info,
	.done = core_done
};

static void remote_state_changed(void *data, enum pw_remote_state old,
				 enum pw_remote_state state, const char *error)
{
	pa_context *c = data;

	switch(state) {
	case PW_REMOTE_STATE_ERROR:
		if (c->core) {
			spa_hook_remove(&c->core_listener);
			c->core = NULL;
		}
		context_fail(c, PA_ERR_CONNECTIONTERMINATED);
		break;
	case PW_REMOTE_STATE_UNCONNECTED:
		if (c->core) {
			spa_hook_remove(&c->core_listener);
			c->core = NULL;
		}
		if (!c->disconnect)
			context_fail(c, PA_ERR_CONNECTIONTERMINATED);
		break;
	case PW_REMOTE_STATE_CONNECTING:
		pa_context_set_state(c, PA_CONTEXT_CONNECTING);
		break;
	case PW_REMOTE_STATE_CONNECTED:
		pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
		pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);

		c->core = pw_remote_get_core(c->remote);
		pw_core_add_listener(c->core, &c->core_listener, &core_events, c);

		pa_context_set_state(c, PA_CONTEXT_READY);
		break;
	}
}

static const struct pw_remote_events remote_events = {
	PW_VERSION_REMOTE_EVENTS,
	.state_changed = remote_state_changed,
};

struct success_data {
	pa_context_success_cb_t cb;
	void *userdata;
	int ret;
};

static void on_success(pa_operation *o, void *userdata)
{
	struct success_data *d = userdata;
	pa_context *c = o->context;
	pa_operation_done(o);
	if (d->cb)
		d->cb(c, d->ret, d->userdata);
}

SPA_EXPORT
pa_operation* pa_context_subscribe(pa_context *c, pa_subscription_mask_t m, pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);

	c->subscribe_mask = m;

	if (c->registry == NULL) {
		c->registry = pw_core_get_registry(c->core,
				PW_VERSION_REGISTRY, 0);
		pw_registry_add_listener(c->registry,
				&c->registry_listener,
				&registry_events, c);
	}

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->ret = 0;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);

	return o;
}

SPA_EXPORT
pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, PA_CONST pa_proplist *p)
{
	struct pw_impl_core *core;
	struct pw_loop *loop;
	struct pw_remote *r;
	struct pw_properties *props;
	pa_context *c;

	pa_assert(mainloop);

	props = pw_properties_new(NULL, NULL);
	if (name)
		pw_properties_set(props, PA_PROP_APPLICATION_NAME, name);
	pw_properties_set(props, PW_KEY_CLIENT_API, "pulseaudio");
	if (p)
		pw_properties_update(props, &p->props->dict);

	loop = mainloop->userdata;
	core = pw_impl_core_new(loop, NULL, 0);

	r = pw_remote_new(core, props, sizeof(struct pa_context));
	if (r == NULL)
		return NULL;

	c = pw_remote_get_user_data(r);
	c->loop = loop;
	c->core_impl = core;
	c->remote = r;

	pw_remote_add_listener(r, &c->remote_listener, &remote_events, c);

	c->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
	c->refcount = 1;
	c->client_index = PA_INVALID_INDEX;

	if (name)
		pa_proplist_sets(c->proplist, PA_PROP_APPLICATION_NAME, name);

	c->mainloop = mainloop;
	c->error = 0;
	c->state = PA_CONTEXT_UNCONNECTED;

	spa_list_init(&c->globals);

	spa_list_init(&c->streams);
	spa_list_init(&c->operations);

	return c;
}

static void do_core_destroy(pa_mainloop_api*m, void *userdata)
{
	pa_context *c = userdata;
	pw_impl_core_destroy(c->core_impl);
}

static void context_free(pa_context *c)
{
	pw_log_debug("context %p: free", c);

	context_unlink(c);

	if (c->proplist)
		pa_proplist_free(c->proplist);
	if (c->core_info)
		pw_core_info_free(c->core_info);

	pa_mainloop_api_once(c->mainloop, do_core_destroy, c);
}

SPA_EXPORT
void pa_context_unref(pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	if (--c->refcount == 0)
		context_free(c);
}

SPA_EXPORT
pa_context* pa_context_ref(pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);
	c->refcount++;
	return c;
}

SPA_EXPORT
void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
		return;

	c->state_callback = cb;
	c->state_userdata = userdata;
}

SPA_EXPORT
void pa_context_set_event_callback(pa_context *c, pa_context_event_cb_t cb, void *userdata)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
		return;

	c->event_callback = cb;
	c->event_userdata = userdata;
}

SPA_EXPORT
int pa_context_errno(PA_CONST pa_context *c)
{
	if (!c)
		return PA_ERR_INVALID;

	pa_assert(c->refcount >= 1);

	return c->error;
}

SPA_EXPORT
int pa_context_is_pending(PA_CONST pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE);

	return !spa_list_is_empty(&c->operations);
}

SPA_EXPORT
pa_context_state_t pa_context_get_state(PA_CONST pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);
	return c->state;
}

SPA_EXPORT
int pa_context_connect(pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api)
{
	int res;

	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
	PA_CHECK_VALIDITY(c, !(flags & ~(PA_CONTEXT_NOAUTOSPAWN|PA_CONTEXT_NOFAIL)), PA_ERR_INVALID);
	PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);

	pa_context_ref(c);

	c->no_fail = !!(flags & PA_CONTEXT_NOFAIL);

	res = pw_remote_connect(c->remote);

	pa_context_unref(c);

	return res;
}

SPA_EXPORT
void pa_context_disconnect(pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	c->disconnect = true;
	pw_remote_disconnect(c->remote);

	if (PA_CONTEXT_IS_GOOD(c->state))
		pa_context_set_state(c, PA_CONTEXT_TERMINATED);
}

struct notify_data {
	pa_context_notify_cb_t cb;
	void *userdata;
};

static void on_notify(pa_operation *o, void *userdata)
{
	struct notify_data *d = userdata;
	pa_context *c = o->context;
	pa_operation_done(o);
	if (d->cb)
		d->cb(c, d->userdata);
}

SPA_EXPORT
pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct notify_data *d;

	o = pa_operation_new(c, NULL, on_notify, sizeof(struct notify_data));
	d = o->userdata;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);

	return o;
}

SPA_EXPORT
pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->ret = PA_ERR_ACCESS;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);
	pw_log_warn("Not Implemented");

	return o;
}

SPA_EXPORT
pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->ret = PA_ERR_ACCESS;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);
	pw_log_warn("Not Implemented");

	return o;
}

SPA_EXPORT
pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->ret = PA_ERR_ACCESS;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);
	pw_log_warn("Not Implemented");

	return o;
}

SPA_EXPORT
int pa_context_is_local(PA_CONST pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, -1);

	return 1;
}

SPA_EXPORT
pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata)
{
	struct spa_dict dict;
	struct spa_dict_item items[1];
	pa_operation *o;
	struct success_data *d;

	pa_assert(c);
	pa_assert(c->refcount >= 1);
	pa_assert(name);

	PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);

	items[0] = SPA_DICT_ITEM_INIT(PA_PROP_APPLICATION_NAME, name);
	dict = SPA_DICT_INIT(items, 1);
	pw_remote_update_properties(c->remote, &dict);

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);

	return o;
}

SPA_EXPORT
const char* pa_context_get_server(PA_CONST pa_context *c)
{
	const struct pw_core_info *info;

	pa_assert(c);
	pa_assert(c->refcount >= 1);

	info = c->core_info;
	PA_CHECK_VALIDITY_RETURN_NULL(c, info && info->name, PA_ERR_NOENTITY);

	return info->name;
}

SPA_EXPORT
uint32_t pa_context_get_protocol_version(PA_CONST pa_context *c)
{
	return PA_PROTOCOL_VERSION;
}

SPA_EXPORT
uint32_t pa_context_get_server_protocol_version(PA_CONST pa_context *c)
{
	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, PA_INVALID_INDEX);

	return PA_PROTOCOL_VERSION;
}

SPA_EXPORT
pa_operation *pa_context_proplist_update(pa_context *c, pa_update_mode_t mode, PA_CONST pa_proplist *p, pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	spa_assert(c);
	spa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_NULL(c, mode == PA_UPDATE_SET ||
			mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
	PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);

	pa_proplist_update(c->proplist, mode, p);

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);
	return o;
}

SPA_EXPORT
pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[], pa_context_success_cb_t cb, void *userdata)
{
	pa_operation *o;
	struct success_data *d;

	spa_assert(c);
	spa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_NULL(c, keys && keys[0], PA_ERR_INVALID);
	PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);

	pw_log_warn("Not Implemented");

	o = pa_operation_new(c, NULL, on_success, sizeof(struct success_data));
	d = o->userdata;
	d->cb = cb;
	d->userdata = userdata;
	pa_operation_sync(o);
	return o;
}

SPA_EXPORT
uint32_t pa_context_get_index(PA_CONST pa_context *c)
{
	return c->client_index;
}

SPA_EXPORT
pa_time_event* pa_context_rttime_new(PA_CONST pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata)
{
	struct timeval tv;

	pa_assert(c);
	pa_assert(c->refcount >= 1);
	pa_assert(c->mainloop);

	if (usec == PA_USEC_INVALID)
		return c->mainloop->time_new(c->mainloop, NULL, cb, userdata);

	pa_timeval_store(&tv, usec);

	return c->mainloop->time_new(c->mainloop, &tv, cb, userdata);
}

SPA_EXPORT
void pa_context_rttime_restart(PA_CONST pa_context *c, pa_time_event *e, pa_usec_t usec)
{
	struct timeval tv;

	pa_assert(c);
	pa_assert(c->refcount >= 1);
	pa_assert(c->mainloop);

	if (usec == PA_USEC_INVALID)
		c->mainloop->time_restart(e, NULL);
	else {
		pa_timeval_store(&tv, usec);
		c->mainloop->time_restart(e, &tv);
	}
}

SPA_EXPORT
size_t pa_context_get_tile_size(PA_CONST pa_context *c, const pa_sample_spec *ss)
{
	size_t fs, mbs;

	pa_assert(c);
	pa_assert(c->refcount >= 1);

	PA_CHECK_VALIDITY_RETURN_ANY(c, !ss || pa_sample_spec_valid(ss), PA_ERR_INVALID, (size_t) -1);

	fs = ss ? pa_frame_size(ss) : 1;
	mbs = PA_ROUND_DOWN(4096, fs);
	return PA_MAX(mbs, fs);
}

SPA_EXPORT
int pa_context_load_cookie_from_file(pa_context *c, const char *cookie_file_path)
{
	return 0;
}