summaryrefslogtreecommitdiffstats
path: root/multimedia/gst1-rtsp-server/files/watch.uc
blob: b568a77c11553a3902a93201193567ca9f7973a5 (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
import * as uloop from 'uloop';

export const QUIET_MS = 15000;

export function watcher_create(scan, apply, stamp) {
	let generation = 0, pending, worker, timer;
	function schedule() {
		generation++;
		pending = true;
		timer.set(QUIET_MS);
	}
	function start() {
		if (worker)
			return;
		let scan_generation = generation, topology = stamp();
		pending = false;
		worker = uloop.task(function() {
			try {
				return { inventory: scan() };
			} catch (e) {
				return { error: '' + e };
			}
		}, function(result) {
			worker = null;
			if (scan_generation != generation) {
				if (pending && timer.remaining() < 0)
					timer.set(0);
				return;
			}
			if (topology != stamp()) {
				schedule();
				return;
			}
			if (result.error) {
				warn('gst-rtsp discovery: ', result.error, '\n');
				return;
			}
			try {
				apply(result.inventory);
			} catch (e) {
				warn('gst-rtsp configuration: ', e, '\n');
			}
		});
		if (!worker)
			die('Cannot start video discovery task\n');
	}
	timer = uloop.timer(-1, start);
	return {
		event: schedule,
		stop: function() {
			timer.cancel();
			if (worker)
				worker.kill();
		}
	};
};