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


struct ausrc_st {
	const struct ausrc *as;      /* inheritance */

	struct tmr tmr;
	struct ausrc_prm prm;
	int16_t *sampv;
	size_t sampc;
	ausrc_read_h *rh;
	void *arg;
};


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

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

	if (st->rh)
		st->rh(st->sampv, st->sampc, st->arg);
}


static void ausrc_destructor(void *arg)
{
	struct ausrc_st *st = arg;

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


static int mock_ausrc_alloc(struct ausrc_st **stp, const struct ausrc *as,
			    struct media_ctx **ctx,
			    struct ausrc_prm *prm, const char *device,
			    ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
	struct ausrc_st *st;
	int err = 0;
	(void)ctx;
	(void)errh;

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

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

	st->as   = as;
	st->prm  = *prm;
	st->rh   = rh;
	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_ausrc_register(struct ausrc **ausrcp)
{
	return ausrc_register(ausrcp, "mock-ausrc", mock_ausrc_alloc);
}