summaryrefslogtreecommitdiff
path: root/modules/vidloop/vidloop.c
blob: 381e97cb7dd22b0c333f79a57263c24cf3f592a9 (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
/**
 * @file vidloop.c  Video loop
 *
 * Copyright (C) 2010 Creytiv.com
 */
#define _BSD_SOURCE 1
#include <string.h>
#include <time.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>


/** Internal pixel-format */
#ifndef VIDLOOP_INTERNAL_FMT
#define VIDLOOP_INTERNAL_FMT (VID_FMT_YUV420P)
#endif


/** Video Statistics */
struct vstat {
	uint64_t tsamp;
	uint32_t frames;
	size_t bytes;
	uint32_t bitrate;
	double efps;
};


/** Video loop */
struct video_loop {
	const struct vidcodec *vc_enc;
	const struct vidcodec *vc_dec;
	struct config_video cfg;
	struct videnc_state *enc;
	struct viddec_state *dec;
	struct vidisp_st *vidisp;
	struct vidsrc_st *vsrc;
	struct list filtencl;
	struct list filtdecl;
	struct vstat stat;
	struct tmr tmr_bw;
	uint16_t seq;
	bool need_conv;
	int err;
};


static struct video_loop *gvl;


static int display(struct video_loop *vl, struct vidframe *frame)
{
	struct le *le;
	int err = 0;

	if (!vidframe_isvalid(frame))
		return 0;

	/* Process video frame through all Video Filters */
	for (le = vl->filtdecl.head; le; le = le->next) {

		struct vidfilt_dec_st *st = le->data;

		if (st->vf->dech)
			err |= st->vf->dech(st, frame);
	}

	if (err) {
		warning("vidloop: error in video-filters (%m)\n", err);
	}

	/* display frame */
	err = vidisp_display(vl->vidisp, "Video Loop", frame);
	if (err == ENODEV) {
		info("vidloop: video-display was closed\n");
		vl->vidisp = mem_deref(vl->vidisp);
		vl->err = err;
	}

	return err;
}


static int packet_handler(bool marker, const uint8_t *hdr, size_t hdr_len,
			  const uint8_t *pld, size_t pld_len, void *arg)
{
	struct video_loop *vl = arg;
	struct vidframe frame;
	struct mbuf *mb;
	int err = 0;

	mb = mbuf_alloc(hdr_len + pld_len);
	if (!mb)
		return ENOMEM;

	if (hdr_len)
		mbuf_write_mem(mb, hdr, hdr_len);
	mbuf_write_mem(mb, pld, pld_len);

	mb->pos = 0;

	vl->stat.bytes += mbuf_get_left(mb);

	/* decode */
	frame.data[0] = NULL;
	if (vl->vc_dec && vl->dec) {
		err = vl->vc_dec->dech(vl->dec, &frame, marker, vl->seq++, mb);
		if (err) {
			warning("vidloop: codec decode: %m\n", err);
			goto out;
		}
	}

	if (vidframe_isvalid(&frame)) {

		display(vl, &frame);
	}

 out:
	mem_deref(mb);

	return 0;
}


static void vidsrc_frame_handler(struct vidframe *frame, void *arg)
{
	struct video_loop *vl = arg;
	struct vidframe *f2 = NULL;
	struct le *le;
	int err = 0;

	++vl->stat.frames;

	if (frame->fmt != VIDLOOP_INTERNAL_FMT) {

		if (!vl->need_conv) {
			info("vidloop: NOTE: pixel-format conversion"
			     " needed: %s  -->  %s\n",
			     vidfmt_name(frame->fmt),
			     vidfmt_name(VIDLOOP_INTERNAL_FMT));
			vl->need_conv = true;
		}

		if (vidframe_alloc(&f2, VIDLOOP_INTERNAL_FMT, &frame->size))
			return;

		vidconv(f2, frame, 0);

		frame = f2;
	}

	/* Process video frame through all Video Filters */
	for (le = vl->filtencl.head; le; le = le->next) {

		struct vidfilt_enc_st *st = le->data;

		if (st->vf->ench)
			err |= st->vf->ench(st, frame);
	}

	if (vl->vc_enc && vl->enc) {
		(void)vl->vc_enc->ench(vl->enc, false, frame);
	}
	else {
		vl->stat.bytes += vidframe_size(frame->fmt, &frame->size);
		(void)display(vl, frame);
	}

	mem_deref(f2);
}


static void vidloop_destructor(void *arg)
{
	struct video_loop *vl = arg;

	tmr_cancel(&vl->tmr_bw);
	mem_deref(vl->vsrc);
	mem_deref(vl->enc);
	mem_deref(vl->dec);
	mem_deref(vl->vidisp);
	list_flush(&vl->filtencl);
	list_flush(&vl->filtdecl);
}


static int enable_codec(struct video_loop *vl)
{
	struct videnc_param prm;
	const char *name = NULL;
	int err;

	prm.fps     = vl->cfg.fps;
	prm.pktsize = 1480;
	prm.bitrate = vl->cfg.bitrate;
	prm.max_fs  = -1;

	/* Use the first video codec */

	vl->vc_enc = vidcodec_find_encoder(name);
	if (!vl->vc_enc) {
		warning("vidloop: could not find encoder (%s)\n", name);
		return ENOENT;
	}

	info("vidloop: enabled encoder %s (%u fps, %u bit/s)\n",
	     vl->vc_enc->name, prm.fps, prm.bitrate);

	vl->vc_dec = vidcodec_find_decoder(name);
	if (!vl->vc_dec) {
		warning("vidloop: could not find decoder (%s)\n", name);
		return ENOENT;
	}

	info("vidloop: enabled decoder %s\n", vl->vc_dec->name);

	err = vl->vc_enc->encupdh(&vl->enc, vl->vc_enc, &prm, NULL,
				  packet_handler, vl);
	if (err) {
		warning("vidloop: update encoder failed: %m\n", err);
		return err;
	}

	if (vl->vc_dec->decupdh) {
		err = vl->vc_dec->decupdh(&vl->dec, vl->vc_dec, NULL);
		if (err) {
			warning("vidloop: update decoder failed: %m\n", err);
			return err;
		}
	}

	return 0;
}


static void disable_codec(struct video_loop *vl)
{
	vl->enc = mem_deref(vl->enc);
	vl->dec = mem_deref(vl->dec);
	vl->vc_enc = NULL;
	vl->vc_dec = NULL;
}


static void print_status(struct video_loop *vl)
{
	(void)re_fprintf(stderr,
			 "\rstatus:"
			 " [%s] [%s] EFPS=%.1f      %u kbit/s       \r",
			 vl->vc_enc ? vl->vc_enc->name : "",
			 vl->vc_dec ? vl->vc_dec->name : "",
			 vl->stat.efps, vl->stat.bitrate);
}


static void calc_bitrate(struct video_loop *vl)
{
	const uint64_t now = tmr_jiffies();

	if (now > vl->stat.tsamp) {

		const uint32_t dur = (uint32_t)(now - vl->stat.tsamp);

		vl->stat.efps = 1000.0f * vl->stat.frames / dur;

		vl->stat.bitrate = (uint32_t) (8 * vl->stat.bytes / dur);
	}

	vl->stat.frames = 0;
	vl->stat.bytes = 0;
	vl->stat.tsamp = now;
}


static void timeout_bw(void *arg)
{
	struct video_loop *vl = arg;

	if (vl->err) {
		info("error in video-loop -- closing (%m)\n", vl->err);
		gvl = mem_deref(gvl);
		return;
	}

	tmr_start(&vl->tmr_bw, 2000, timeout_bw, vl);

	calc_bitrate(vl);
	print_status(vl);
}


static int vsrc_reopen(struct video_loop *vl, const struct vidsz *sz)
{
	struct vidsrc_prm prm;
	int err;

	info("vidloop: %s,%s: open video source: %u x %u at %u fps\n",
	     vl->cfg.src_mod, vl->cfg.src_dev,
	     sz->w, sz->h, vl->cfg.fps);

	prm.orient = VIDORIENT_PORTRAIT;
	prm.fps    = vl->cfg.fps;

	vl->vsrc = mem_deref(vl->vsrc);
	err = vidsrc_alloc(&vl->vsrc, vl->cfg.src_mod, NULL, &prm, sz,
			   NULL, vl->cfg.src_dev, vidsrc_frame_handler,
			   NULL, vl);
	if (err) {
		warning("vidloop: vidsrc '%s' failed: %m\n",
			vl->cfg.src_dev, err);
	}

	return err;
}


static int video_loop_alloc(struct video_loop **vlp, const struct vidsz *size)
{
	struct video_loop *vl;
	struct config *cfg;
	struct le *le;
	int err = 0;

	cfg = conf_config();
	if (!cfg)
		return EINVAL;

	vl = mem_zalloc(sizeof(*vl), vidloop_destructor);
	if (!vl)
		return ENOMEM;

	vl->cfg = cfg->video;
	tmr_init(&vl->tmr_bw);

	/* Video filters */
	for (le = list_head(vidfilt_list()); le; le = le->next) {
		struct vidfilt *vf = le->data;
		void *ctx = NULL;

		info("vidloop: added video-filter `%s'\n", vf->name);

		err |= vidfilt_enc_append(&vl->filtencl, &ctx, vf);
		err |= vidfilt_dec_append(&vl->filtdecl, &ctx, vf);
		if (err) {
			warning("vidloop: vidfilt error: %m\n", err);
		}
	}

	err = vsrc_reopen(vl, size);
	if (err)
		goto out;

	info("vidloop: open video display (%s.%s)\n",
	     vl->cfg.disp_mod, vl->cfg.disp_dev);

	err = vidisp_alloc(&vl->vidisp, vl->cfg.disp_mod, NULL,
			   vl->cfg.disp_dev, NULL, vl);
	if (err) {
		warning("vidloop: video display failed: %m\n", err);
		goto out;
	}

	tmr_start(&vl->tmr_bw, 1000, timeout_bw, vl);

 out:
	if (err)
		mem_deref(vl);
	else
		*vlp = vl;

	return err;
}


/**
 * Start the video loop (for testing)
 */
static int vidloop_start(struct re_printf *pf, void *arg)
{
	struct vidsz size;
	struct config *cfg = conf_config();
	int err = 0;

	(void)arg;

	size.w = cfg->video.width;
	size.h = cfg->video.height;

	if (gvl) {
		if (gvl->vc_enc)
			disable_codec(gvl);
		else {
			err = enable_codec(gvl);
			if (err) {
				gvl = mem_deref(gvl);
				return err;
			}
		}

		(void)re_hprintf(pf, "%sabled codec: %s\n",
				 gvl->vc_enc ? "En" : "Dis",
				 gvl->vc_enc ? gvl->vc_enc->name : "");
	}
	else {
		(void)re_hprintf(pf, "Enable video-loop on %s,%s: %u x %u\n",
				 cfg->video.src_mod, cfg->video.src_dev,
				 size.w, size.h);

		err = video_loop_alloc(&gvl, &size);
		if (err) {
			warning("vidloop: alloc: %m\n", err);
		}
	}

	return err;
}


static int vidloop_stop(struct re_printf *pf, void *arg)
{
	(void)arg;

	if (gvl)
		(void)re_hprintf(pf, "Disable video-loop\n");
	gvl = mem_deref(gvl);
	return 0;
}


static const struct cmd cmdv[] = {
	{'v', 0, "Start video-loop", vidloop_start },
	{'V', 0, "Stop video-loop",  vidloop_stop  },
};


static int module_init(void)
{
	return cmd_register(cmdv, ARRAY_SIZE(cmdv));
}


static int module_close(void)
{
	vidloop_stop(NULL, NULL);
	cmd_unregister(cmdv);
	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(vidloop) = {
	"vidloop",
	"application",
	module_init,
	module_close,
};