aboutsummaryrefslogtreecommitdiff
path: root/tests/pa-mock.cpp
blob: 5b433fee345b94c02aa0754b5b0e55975b4d5dbc (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

#include <atomic>
#include <functional>
#include <vector>

#include <pulse/pulseaudio.h>
#include <pulse/glib-mainloop.h>
#include <gio/gio.h>
#include <math.h>

#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "PA-Mock"

G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb);
G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb);
G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask);
G_DEFINE_QUARK("pa-mock-current-state", state);
G_DEFINE_QUARK("pa-mock-future-state", state_future);

/* Core class of the PA Mock state */
class PAMockContext {
public:
	/* Accounting */
	std::atomic<unsigned int> refcnt;

	/* State stuff */
	std::vector<std::function<void(void)>> stateCallbacks;
	pa_context_state_t currentState;
	pa_context_state_t futureState;

	/* Event stuff */
	std::vector<std::function<void(pa_subscription_event_type_t, uint32_t)>> eventCallbacks;
	pa_subscription_mask_t mask;

	PAMockContext ()
		: refcnt(1)
		, currentState(PA_CONTEXT_UNCONNECTED)
		, futureState(PA_CONTEXT_UNCONNECTED)
		, mask(PA_SUBSCRIPTION_MASK_NULL)
	{
		g_debug("Creating Context: %p", this);
	}

	~PAMockContext () {
		g_debug("Destroying Context: %p", this);
	}

	/* Ref counting */
	void ref () {
		refcnt++;
	}

	void unref () {
		refcnt--;
		if (refcnt == 0)
			delete this;
	}

	/* C/C++ boundry */
	operator pa_context *() {
		return reinterpret_cast<pa_context *>(this);
	}

	/* State Stuff */
	void setState (pa_context_state_t state)
	{
		if (state == currentState)
			return;

		currentState = state;
		for (auto callback : stateCallbacks) {
			callback();
		}
	}

	void idleOnce (std::function<void(void)> idleFunc) {
		auto allocated = new std::function<void(void)>(idleFunc);

		g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
			[](gpointer data) -> gboolean {
				std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data);
				(*pidleFunc)();
				return G_SOURCE_REMOVE;
			},
			allocated,
			[](gpointer data) -> void {
				std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data);
				delete pidleFunc;
			});
	}

	void queueState (pa_context_state_t state)
	{
		idleOnce([this, state](){
			setState(state);
		});
	}

	pa_context_state_t getState (void)
	{
		return currentState;
	}

	void addStateCallback (std::function<void(void)> &callback)
	{
		stateCallbacks.push_back(callback);
	}
};

struct pa_context {
	operator PAMockContext * () {
		return reinterpret_cast<PAMockContext *>(this);
	}
};

/* *******************************
 * context.h
 * *******************************/

pa_context *
pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist)
{
	return *(new PAMockContext());
}

void
pa_context_unref (pa_context *c) {
	reinterpret_cast<PAMockContext*>(c)->unref();
}

pa_context *
pa_context_ref (pa_context *c) {
	reinterpret_cast<PAMockContext*>(c)->ref();
	return c;
}

int
pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api)
{
	reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_READY);
	return 0;
}

void
pa_context_disconnect (pa_context *c)
{
	reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_UNCONNECTED);
}

int
pa_context_errno (pa_context *c)
{
	return 0;
}

void
pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata)
{
	std::function<void(void)> cppcb([c, cb, userdata]() {
		cb(c, userdata);
	});
	reinterpret_cast<PAMockContext*>(c)->addStateCallback(cppcb);
}

pa_context_state_t
pa_context_get_state (pa_context *c)
{
	return reinterpret_cast<PAMockContext*>(c)->getState();
}

/* *******************************
 * introspect.h
 * *******************************/

typedef struct {
	pa_server_info_cb_t cb;
	gpointer userdata;
	pa_context * context;
} get_server_info_t;

static void
get_server_info_free (gpointer data)
{
	get_server_info_t * info = (get_server_info_t *)data;
	g_object_unref(info->context);
	g_free(info);
}

static gboolean
get_server_info_cb (gpointer data)
{
	pa_server_info server{
		.user_name = "user",
		.host_name = "host",
		.server_version = "1.2.3",
		.server_name = "server",
		.sample_spec = {
			.format = PA_SAMPLE_U8,
			.rate = 44100,
			.channels = 1
		},
		.default_sink_name = "default-sink",
		.default_source_name = "default-source",
		.cookie = 1234,
		.channel_map = {
			.channels = 0
		}
	};
	get_server_info_t * info = (get_server_info_t *)data;

	info->cb(info->context, &server, info->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	get_server_info_t * info = g_new(get_server_info_t, 1);
	info->cb = cb;
	info->userdata = userdata;
	info->context = (pa_context *)g_object_ref(c);

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		get_server_info_cb,
		info,
		get_server_info_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_sink_info_cb_t cb;
	gpointer userdata;
	pa_context * context;
	uint32_t index;
} get_sink_info_t;

static void
get_sink_info_free (gpointer data)
{
	get_sink_info_t * info = (get_sink_info_t *)data;
	g_object_unref(info->context);
	g_free(info);
}

static gboolean
get_sink_info_cb (gpointer data)
{
	#if 0
	pa_sink_port_info active_port = {
		.name = "speaker"
	};
	#endif
	pa_sink_info sink = {0};
	#if 0
		.name = "default-sink",
		.index = 0,
		.description = "Default Sink",
		.channel_map = {
			.channels = 0
		},
		.active_port = &active_port
	};
	#endif
	get_sink_info_t * info = (get_sink_info_t *)data;

	info->cb(info->context, &sink, 1, info->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(g_strcmp0(name, "default-sink") == 0, nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	get_sink_info_t * info = g_new(get_sink_info_t, 1);
	info->cb = cb;
	info->userdata = userdata;
	info->context = (pa_context *)g_object_ref(c);

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		get_sink_info_cb,
		info,
		get_sink_info_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

pa_operation*
pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata)
{
	/* Only have one today, so this is the same */
	return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata);
}

typedef struct {
	pa_sink_input_info_cb_t cb;
	gpointer userdata;
	pa_context * context;
} get_sink_input_info_t;

static void
get_sink_input_info_free (gpointer data)
{
	get_sink_input_info_t * info = (get_sink_input_info_t *)data;
	pa_context_unref(info->context);
	g_free(info);
}

static gboolean
get_sink_input_info_cb (gpointer data)
{
	pa_sink_input_info sink = { 0 };

	get_sink_input_info_t * info = (get_sink_input_info_t *)data;

	info->cb(info->context, &sink, 0, info->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation *
pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1);
	info->cb = cb;
	info->userdata = userdata;
	info->context = (pa_context *)g_object_ref(c);

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		get_sink_input_info_cb,
		info,
		get_sink_input_info_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_source_info_cb_t cb;
	gpointer userdata;
	pa_context * context;
} get_source_info_t;

static void
get_source_info_free (gpointer data)
{
	get_source_info_t * info = (get_source_info_t *)data;
	g_object_unref(info->context);
	g_free(info);
}

static gboolean
get_source_info_cb (gpointer data)
{
	pa_source_info source = {
		.name = "default-source"
	};
	get_source_info_t * info = (get_source_info_t *)data;

	info->cb(info->context, &source, 0, info->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	get_source_info_t * info = g_new(get_source_info_t, 1);
	info->cb = cb;
	info->userdata = userdata;
	info->context = (pa_context *)g_object_ref(c);

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		get_source_info_cb,
		info,
		get_source_info_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_source_output_info_cb_t cb;
	gpointer userdata;
	pa_context * context;
} get_source_output_t;

static void
get_source_output_free (gpointer data)
{
	get_source_output_t * info = (get_source_output_t *)data;
	g_object_unref(info->context);
	g_free(info);
}

static gboolean
get_source_output_cb (gpointer data)
{
	pa_source_output_info source = {0};
	source.name = "default source";

	get_source_output_t * info = (get_source_output_t *)data;

	info->cb(info->context, &source, 0, info->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	get_source_output_t * info = g_new(get_source_output_t, 1);
	info->cb = cb;
	info->userdata = userdata;
	info->context = (pa_context *)g_object_ref(c);

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		get_source_output_cb,
		info,
		get_source_output_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_context_success_cb_t cb;
	gpointer userdata;
	pa_context * context;
	int mute;
} set_sink_mute_t;

static void
set_sink_mute_free (gpointer data)
{
	set_sink_mute_t * mute = (set_sink_mute_t *)data;
	g_object_unref(mute->context);
	g_free(mute);
}

static gboolean
set_sink_mute_cb (gpointer data)
{
	set_sink_mute_t * mute = (set_sink_mute_t *)data;

	if (mute->cb != nullptr)
		mute->cb(mute->context, 1, mute->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);

	set_sink_mute_t * data = g_new(set_sink_mute_t, 1);
	data->cb = cb;
	data->userdata = userdata;
	data->context = pa_context_ref(c);
	data->mute = mute;

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		set_sink_mute_cb,
		data,
		set_sink_mute_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_context_success_cb_t cb;
	gpointer userdata;
	pa_context * context;
	pa_cvolume cvol;
} set_sink_volume_t;

static void
set_sink_volume_free (gpointer data)
{
	set_sink_volume_t * vol = (set_sink_volume_t *)data;
	g_object_unref(vol->context);
	g_free(vol);
}

static gboolean
set_sink_volume_cb (gpointer data)
{
	set_sink_volume_t * vol = (set_sink_volume_t *)data;

	vol->cb(vol->context, 1, vol->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	set_sink_volume_t * data = g_new(set_sink_volume_t, 1);
	data->cb = cb;
	data->userdata = userdata;
	data->context = pa_context_ref(c);
	data->cvol.channels = cvol->channels;

	int i;
	for (i = 0; i < cvol->channels; i++)
		data->cvol.values[i] = cvol->values[i];

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		set_sink_volume_cb,
		data,
		set_sink_volume_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_context_success_cb_t cb;
	gpointer userdata;
	pa_context * context;
	pa_cvolume cvol;
} set_source_volume_t;

static void
set_source_volume_free (gpointer data)
{
	set_source_volume_t * vol = (set_source_volume_t *)data;
	g_object_unref(vol->context);
	g_free(vol);
}

static gboolean
set_source_volume_cb (gpointer data)
{
	set_source_volume_t * vol = (set_source_volume_t *)data;

	vol->cb(vol->context, 1, vol->userdata);

	return G_SOURCE_REMOVE;
}

pa_operation*
pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);
	g_return_val_if_fail(cb != nullptr, nullptr);

	set_source_volume_t * data = g_new(set_source_volume_t, 1);
	data->cb = cb;
	data->userdata = userdata;
	data->context = pa_context_ref(c);
	data->cvol.channels = cvol->channels;

	int i;
	for (i = 0; i < cvol->channels; i++)
		data->cvol.values[i] = cvol->values[i];

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		set_source_volume_cb,
		data,
		set_source_volume_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

/* *******************************
 * subscribe.h
 * *******************************/

typedef struct {
	pa_context_success_cb_t cb;
	gpointer userdata;
	pa_context * context;
	pa_subscription_mask_t mask;
} subscribe_mask_t;

static void
subscribe_mask_free (gpointer data)
{
	subscribe_mask_t * mask_data = (subscribe_mask_t *)data;
	g_object_unref(mask_data->context);
	g_free(mask_data);
}

static gboolean
subscribe_mask_cb (gpointer data)
{
	subscribe_mask_t * mask_data = (subscribe_mask_t *)data;
	g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask));
	if (mask_data->cb != nullptr)
		mask_data->cb(mask_data->context, 1, mask_data->userdata);
	return G_SOURCE_REMOVE;
}

pa_operation *
pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata)
{
	g_return_val_if_fail(G_IS_OBJECT(c), nullptr);

	subscribe_mask_t * data = g_new0(subscribe_mask_t, 1);
	data->cb = callback;
	data->userdata = userdata;
	data->context = pa_context_ref(c);
	data->mask = mask;

	g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		subscribe_mask_cb,
		data,
		subscribe_mask_free);

	GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr);
	pa_operation * oper = (pa_operation *)goper;
	return oper;
}

typedef struct {
	pa_context_subscribe_cb_t cb;
	gpointer userdata;
} subscribe_cb_t;

void
pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata)
{
	g_return_if_fail(G_IS_OBJECT(c));

	subscribe_cb_t * sub = g_new0(subscribe_cb_t, 1);
	sub->cb = callback;
	sub->userdata = userdata;

	g_object_set_qdata_full(G_OBJECT(c), subscribe_cb_quark(), sub, g_free);
}

/* *******************************
 * glib-mainloop.h
 * *******************************/

struct pa_glib_mainloop {
	GMainContext * context;
};

struct pa_mainloop_api mock_mainloop = { 0 };

pa_mainloop_api *
pa_glib_mainloop_get_api (pa_glib_mainloop * g)
{
	return &mock_mainloop;
}

pa_glib_mainloop *
pa_glib_mainloop_new (GMainContext * c)
{
	pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1);

	if (c == nullptr)
		loop->context = g_main_context_default();
	else
		loop->context = c;

	g_main_context_ref(loop->context);
	return loop;
}

void
pa_glib_mainloop_free (pa_glib_mainloop * g)
{
	g_main_context_unref(g->context);
	g_free(g);
}

/* *******************************
 * operation.h
 * *******************************/

void
pa_operation_unref (pa_operation * operation)
{
	g_return_if_fail(G_IS_OBJECT(operation));
	g_object_unref(G_OBJECT(operation));
}

/* *******************************
 * proplist.h
 * *******************************/

pa_proplist *
pa_proplist_new (void)
{
	return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}

void
pa_proplist_free (pa_proplist * p)
{
	g_return_if_fail(p != nullptr);
	g_hash_table_destroy((GHashTable *)p);
}

const char *
pa_proplist_gets (pa_proplist * p, const char * key)
{
	g_return_val_if_fail(p != nullptr, nullptr);
	g_return_val_if_fail(key != nullptr, nullptr);
	return (const char *)g_hash_table_lookup((GHashTable *)p, key);
}

int
pa_proplist_sets (pa_proplist *p, const char * key, const char * value)
{
	g_return_val_if_fail(p != nullptr, -1);
	g_return_val_if_fail(key != nullptr, -1);
	g_return_val_if_fail(value != nullptr, -1);

	g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value));
	return 0;
}

/* *******************************
 * error.h
 * *******************************/

const char *
pa_strerror (int error)
{
	return "This is error text";
}

/* *******************************
 * volume.h
 * *******************************/

pa_volume_t
pa_sw_volume_from_dB (double f)
{
	double linear = pow(10.0, f / 20.0);

	pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM);

	if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) {
		return PA_VOLUME_MAX;
	} else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) {
		return PA_VOLUME_MUTED;
	} else {
		return calculated;
	}
}

pa_cvolume *
pa_cvolume_init (pa_cvolume * cvol)
{
	g_return_val_if_fail(cvol != nullptr, nullptr);

	cvol->channels = 0;

	unsigned int i;
	for (i = 0; i < PA_CHANNELS_MAX; i++)
		cvol->values[i] = PA_VOLUME_INVALID;

	return cvol;
}

pa_cvolume *
pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume)
{
	g_return_val_if_fail(cvol != nullptr, nullptr);
	g_warn_if_fail(channels > 0);
	g_return_val_if_fail(channels <= PA_CHANNELS_MAX, nullptr);

	cvol->channels = channels;

	unsigned int i;
	for (i = 0; i < channels; i++) {
		if (G_UNLIKELY(volume > PA_VOLUME_MAX)) {
			cvol->values[i] = PA_VOLUME_MAX;
		} else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) {
			cvol->values[i] = PA_VOLUME_MUTED;
		} else {
			cvol->values[i] = volume;
		}
	}

	return cvol;
}

pa_volume_t
pa_cvolume_max (const pa_cvolume * cvol)
{
	g_return_val_if_fail(cvol != nullptr, PA_VOLUME_MUTED);
	pa_volume_t max = PA_VOLUME_MUTED;

	unsigned int i;
	for (i = 0; i < cvol->channels; i++)
		max = MAX(max, cvol->values[i]);
	
	return max;
}

pa_cvolume *
pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max)
{
	g_return_val_if_fail(cvol != nullptr, nullptr);

	pa_volume_t originalmax = pa_cvolume_max(cvol);

	if (originalmax <= PA_VOLUME_MUTED)
		return pa_cvolume_set(cvol, cvol->channels, max);

	unsigned int i;
	for (i = 0; i < cvol->channels; i++) {
		pa_volume_t calculated = (cvol->values[i] * max) / originalmax;

		if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) {
			cvol->values[i] = PA_VOLUME_MAX;
		} else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) {
			cvol->values[i] = PA_VOLUME_MUTED;
		} else {
			cvol->values[i] = calculated;
		}
	}

	return cvol;
}