summaryrefslogtreecommitdiff
path: root/test/mock/mock_auplay.c
blob: 9857d9fb4058ad3350f2d659db1ab98f5b81d796 (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
/**
 * @file mock/mock_auplay.c Mock audio player
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <re.h>
#include <baresip.h>
#include "../test.h"


struct auplay_st {
	const struct auplay *ap;      /* inheritance */

	struct tmr tmr;
	struct auplay_prm prm;
	int16_t *sampv;
	size_t sampc;
	auplay_write_h *wh;
	void *arg;
};


static struct {
	mock_sample_h *sampleh;
	void *arg;
} mock;


static void tmr_handler(void *arg)
{
	struct auplay_st *st = arg;

	tmr_start(&st->tmr, st->prm.ptime, tmr_handler, st);

	if (st->wh)
		st->wh(st->sampv, st->sampc, st->arg);

	/* feed the audio-samples back to the test */
	if (mock.sampleh)
		mock.sampleh(st->sampv, st->sampc, mock.arg);
}


static void auplay_destructor(void *arg)
{
	struct auplay_st *st = arg;

	tmr_cancel(&st->tmr);
	mem_deref(st->sampv);
}


static int mock_auplay_alloc(struct auplay_st **stp, const struct auplay *ap,
			    struct auplay_prm *prm, const char *device,
			    auplay_write_h *wh, void *arg)
{
	struct auplay_st *st;
	int err = 0;
	(void)device;

	if (!stp || !ap || !prm)
		return EINVAL;

	st = mem_zalloc(sizeof(*st), auplay_destructor);
	if (!st)
		return ENOMEM;

	st->ap   = ap;
	st->prm  = *prm;
	st->wh   = wh;
	st->arg  = arg;

	st->sampc = prm->srate * prm->ch * prm->ptime / 1000;

	st->sampv = mem_zalloc(2 * st->sampc, NULL);
	if (!st->sampv) {
		err = ENOMEM;
		goto out;
	}

	tmr_start(&st->tmr, 0, tmr_handler, st);

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


int mock_auplay_register(struct auplay **auplayp,
			 mock_sample_h *sampleh, void *arg)
{
	mock.sampleh = sampleh;
	mock.arg = arg;

	return auplay_register(auplayp, baresip_auplayl(),
			      "mock-auplay", mock_auplay_alloc);
}