summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/Vreceive.py
blob: 8d7150e9ee428f875e9929de3292b20adeea27fe (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
#!/ufs/guido/bin/sgi/python-405

# Receive live video UDP packets.
# Usage: Vreceive [port]

import sys
import struct
from socket import *
import select
import gl, GL, DEVICE
sys.path.append('/ufs/guido/src/video')
import LiveVideoOut

PKTMAX = 16*1024
WIDTH = 400
HEIGHT = 300
HOST = ''
PORT = 5555

def main():

	port = PORT
	if sys.argv[1:]:
		port = eval(sys.argv[1])

	width, height = WIDTH, HEIGHT

	gl.foreground()
	gl.prefsize(width, height)
	wid = gl.winopen('Vreceive')
	gl.qdevice(DEVICE.ESCKEY)
	gl.qdevice(DEVICE.WINSHUT)
	gl.qdevice(DEVICE.WINQUIT)

	x, y = gl.getorigin()
	lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \
		width, height)

	s = socket(AF_INET, SOCK_DGRAM)
	s.bind(HOST, port)

	ifdlist = [gl.qgetfd(), s.fileno()]
	ofdlist = []
	xfdlist = []
	timeout = 1.0
	selectargs = (ifdlist, ofdlist, xfdlist, timeout)

	while 1:

		if gl.qtest():
			dev, val = gl.qread()
			if dev in (DEVICE.ESCKEY, \
				DEVICE.WINSHUT, DEVICE.WINQUIT):
				break
			if dev == DEVICE.REDRAW:
				gl.clear()
		elif s.avail():
			data = s.recv(16*1024)
			pos, w, h = struct.unpack('hhh', data[:6])
			if (w, h) <> (width, height):
				x, y = gl.getorigin()
				y = y + height - h
				width, height = w, h
				lvo.close()
				lvo = LiveVideoOut.LiveVideoOut() \
				      .init(wid, (x, y, width, height), \
				            width, height)
			lvo.putnextpacket(pos, data[6:])
		else:
			x = select.select(selectargs)

	lvo.close()

main()