summaryrefslogtreecommitdiff
path: root/test/mock/mock_vidcodec.c
blob: c93f64809d2ff1f60e2dda972528c74f3058e290 (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
/**
 * @file mock/mock_vidcodec.c Mock video codec
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */

#include <string.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include "../test.h"


#define HDR_SIZE 12


struct hdr {
	enum vidfmt fmt;
	unsigned width;
	unsigned height;
};

struct videnc_state {
	videnc_packet_h *pkth;
	void *arg;
};

struct viddec_state {
	struct vidframe *frame;
};


static int hdr_decode(struct hdr *hdr, struct mbuf *mb)
{
	if (mbuf_get_left(mb) < HDR_SIZE)
		return EBADMSG;

	hdr->fmt    = ntohl(mbuf_read_u32(mb));
	hdr->width  = ntohl(mbuf_read_u32(mb));
	hdr->height = ntohl(mbuf_read_u32(mb));

	return 0;
}


static void decode_destructor(void *arg)
{
	struct viddec_state *vds = arg;

	mem_deref(vds->frame);
}


static int mock_encode_update(struct videnc_state **vesp,
			      const struct vidcodec *vc,
			      struct videnc_param *prm, const char *fmtp,
			      videnc_packet_h *pkth, void *arg)
{
	struct videnc_state *ves;

	if (!vesp || !vc || !prm || prm->pktsize < (HDR_SIZE + 1))
		return EINVAL;

	ves = *vesp;

	if (!ves) {

		ves = mem_zalloc(sizeof(*ves), NULL);
		if (!ves)
			return ENOMEM;

		*vesp = ves;
	}

	ves->pkth    = pkth;
	ves->arg     = arg;

	return 0;
}


static int mock_encode(struct videnc_state *ves, bool update,
		       const struct vidframe *frame)
{
	struct mbuf *hdr;
	uint8_t payload[2] = {0,0};
	int err;

	if (!ves || !frame)
		return EINVAL;

	hdr = mbuf_alloc(16);

	err  = mbuf_write_u32(hdr, htonl(frame->fmt));
	err |= mbuf_write_u32(hdr, htonl(frame->size.w));
	err |= mbuf_write_u32(hdr, htonl(frame->size.h));
	if (err)
		goto out;

	err = ves->pkth(true, hdr->buf, hdr->end,
			payload, sizeof(payload), ves->arg);
	if (err)
		goto out;

 out:
	mem_deref(hdr);

	return err;
}


static int mock_decode_update(struct viddec_state **vdsp,
			      const struct vidcodec *vc, const char *fmtp)
{
	struct viddec_state *vds;
	int err = 0;
	(void)vc;
	(void)fmtp;

	if (!vdsp)
		return EINVAL;

	vds = *vdsp;

	if (vds)
		return 0;

	vds = mem_zalloc(sizeof(*vds), decode_destructor);
	if (!vds)
		return ENOMEM;

	if (err)
		mem_deref(vds);
	else
		*vdsp = vds;

	return err;
}


static int mock_decode(struct viddec_state *vds, struct vidframe *frame,
		       bool *intra, bool marker, uint16_t seq, struct mbuf *mb)
{
	struct vidsz size;
	struct hdr hdr;
	int err, i;

	if (!vds || !frame || !intra || !mb)
		return EINVAL;

	*intra = false;

	err = hdr_decode(&hdr, mb);
	if (err) {
		warning("mock_vidcodec: could not decode header (%m)\n", err);
		return err;
	}

	size.w = hdr.width;
	size.h = hdr.height;

	if (!vds->frame) {
		err = vidframe_alloc(&vds->frame, hdr.fmt, &size);
		if (err)
			goto out;
	}

	for (i=0; i<4; i++) {
		frame->data[i]     = vds->frame->data[i];
		frame->linesize[i] = vds->frame->linesize[i];
	}

	frame->size.w = vds->frame->size.w;
	frame->size.h = vds->frame->size.h;
	frame->fmt    = vds->frame->fmt;

 out:
	return err;
}


static struct vidcodec vc_dummy = {
	.name      = "H266",
	.encupdh   = mock_encode_update,
	.ench      = mock_encode,
	.decupdh   = mock_decode_update,
	.dech      = mock_decode,
};


void mock_vidcodec_register(void)
{
	vidcodec_register(&vc_dummy);
}


void mock_vidcodec_unregister(void)
{
	vidcodec_unregister(&vc_dummy);
}