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


struct vidsrc_st {
	const struct vidsrc *vs;  /* inheritance */

	struct vidframe *frame;
	struct tmr tmr;
	int fps;
	vidsrc_frame_h *frameh;
	void *arg;
};


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

	tmr_start(&st->tmr, 1000/st->fps, tmr_handler, st);

	if (st->frameh)
		st->frameh(st->frame, st->arg);
}


static void vidsrc_destructor(void *arg)
{
	struct vidsrc_st *st = arg;

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


static int mock_vidsrc_alloc(struct vidsrc_st **stp, const struct vidsrc *vs,
			     struct media_ctx **ctx, struct vidsrc_prm *prm,
			     const struct vidsz *size, const char *fmt,
			     const char *dev, vidsrc_frame_h *frameh,
			     vidsrc_error_h *errorh, void *arg)
{
	struct vidsrc_st *st;
	int err = 0;
	(void)ctx;
	(void)fmt;
	(void)dev;
	(void)errorh;

	if (!stp || !prm || !size || !frameh)
		return EINVAL;

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

	st->vs   = vs;
	st->fps    = prm->fps;
	st->frameh = frameh;
	st->arg    = arg;

	err = vidframe_alloc(&st->frame, VID_FMT_YUV420P, size);
	if (err)
		goto out;

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

	info("mock_vidsrc: new instance with size %u x %u (%d fps)\n",
	     size->w, size->h, prm->fps);

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

	return err;
}


int mock_vidsrc_register(struct vidsrc **vidsrcp)
{
	return vidsrc_register(vidsrcp, "mock-vidsrc",
			       mock_vidsrc_alloc, NULL);
}