summaryrefslogtreecommitdiff
path: root/Demo/sgi/video/Dsend.py
blob: 7e197f7df95500cd5d40b590f34cd87652b59e69 (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
#! /usr/bin/env python

# Send live video UDP packets.
# Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
#              [host] ..

import sys
import time
import struct
import string
import math
from socket import *
from SOCKET import *
import gl, GL, DEVICE
sys.path.append('/ufs/guido/src/video')
import DisplayVideoIn
import LiveVideoOut
import SV
import getopt
from IN import *

from senddefs import *

def usage(msg):
	print msg
	print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-c type] [-m]',
	print '[-w width] [host] ...'
	print '-b        : broadcast on local net'
	print '-h height : window height (default ' + `DEFHEIGHT` + ')'
	print '-p port   : port to use (default ' + `DEFPORT` + ')'
	print '-t ttl    : time-to-live (multicast only; default 1)'
	print '-s size   : max packet size (default ' + `DEFPKTMAX` + ')'
	print '-S size   : use this packet size/window size'
	print '-w width  : window width (default ' + `DEFWIDTH` + ')'
	print '-v        : print packet rate'
	print '-x xpos   : set x position'
	print '-y ypos   : set y position'
	print '[host] ...: host(s) to send to (default multicast to ' + \
		DEFMCAST + ')'
	sys.exit(2)


def main():
	sys.stdout = sys.stderr

	hosts = []
	port = DEFPORT
	ttl = -1
	pktmax = DEFPKTMAX
	width = DEFWIDTH
	height = DEFHEIGHT
	vtype = 'rgb'
	verbose = 0
	xpos = ypos = 0

	try:
		opts, args = getopt.getopt(sys.argv[1:], 'bh:p:s:S:t:w:vx:y:')
	except getopt.error, msg:
		usage(msg)

	try:
		for opt, optarg in opts:
			if opt == '-p':
				port = string.atoi(optarg)
			if opt == '-b':
				host = '<broadcast>'
			if opt == '-t':
				ttl = string.atoi(optarg)
			if opt == '-S':
				pktmax = string.atoi(optarg)
				vidmax = SV.PAL_XMAX*SV.PAL_YMAX
				if vidmax <= pktmax:
					width = SV.PAL_XMAX
					height = SV.PAL_YMAX
					pktmax = vidmax
				else:
					factor = float(vidmax)/float(pktmax)
					factor = math.sqrt(factor)
					width = int(SV.PAL_XMAX/factor)-7
					height = int(SV.PAL_YMAX/factor)-5
					print 'video:',width,'x',height,
					print 'pktsize',width*height,'..',
					print pktmax
			if opt == '-s':
				pktmax = string.atoi(optarg)
			if opt == '-w':
				width = string.atoi(optarg)
			if opt == '-h':
				height = string.atoi(optarg)
			if opt == '-c':
				vtype = optarg
			if opt == '-v':
				verbose = 1
			if opt == '-x':
				xpos = string.atoi(optarg)
			if opt == '-y':
				ypos = string.atoi(optarg)
	except string.atoi_error, msg:
		usage('bad integer: ' + msg)

	for host in args:
		hosts.append(gethostbyname(host))

	if not hosts:
		hosts.append(gethostbyname(DEFMCAST))

	gl.foreground()
	gl.prefsize(width, height)
	gl.stepunit(8, 6)
	wid = gl.winopen('Vsend')
	gl.keepaspect(width, height)
	gl.stepunit(8, 6)
	gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
	gl.winconstraints()
	gl.qdevice(DEVICE.ESCKEY)
	gl.qdevice(DEVICE.WINSHUT)
	gl.qdevice(DEVICE.WINQUIT)
	gl.qdevice(DEVICE.WINFREEZE)
	gl.qdevice(DEVICE.WINTHAW)
	width, height = gl.getsize()

	lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)

	lvi = DisplayVideoIn.DisplayVideoIn(pktmax, width, height, vtype)

	if xpos or ypos:
		lvi.positionvideo(xpos, ypos)

	s = socket(AF_INET, SOCK_DGRAM)
	s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
	if ttl >= 0:
		s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, chr(ttl))

	frozen = 0

	lasttime = int(time.time())
	nframe = 0
	while 1:

		if gl.qtest():
			dev, val = gl.qread()
			if dev in (DEVICE.ESCKEY, \
				DEVICE.WINSHUT, DEVICE.WINQUIT):
				break
			if dev == DEVICE.WINFREEZE:
				frozen = 1
			if dev == DEVICE.WINTHAW:
				frozen = 0
			if dev == DEVICE.REDRAW:
				w, h = gl.getsize()
				x, y = gl.getorigin()
				if (w, h) <> (width, height):
					width, height = w, h
					lvi.resizevideo(width, height)
					lvo.resizevideo(width, height)

		rv = lvi.getnextpacket()
		if not rv:
			time.sleep(0.010)
			continue

		pos, data = rv
		print pos, len(data) # DBG

		if not frozen:
			lvo.putnextpacket(pos, data)

		hdr = struct.pack('hhh', pos, width, height)
		for host in hosts:
			try:
				# print len(hdr+data) # DBG
				s.sendto(hdr + data, (host, port))
			except error, msg: # really socket.error
				if msg[0] <> 121: # no buffer space available
					raise error, msg # re-raise it
				print 'Warning:', msg[1]
		if pos == 0 and verbose:
			nframe = nframe+1
			if int(time.time()) <> lasttime:
				print nframe / (time.time()-lasttime), 'fps'
				nframe = 0
				lasttime = int(time.time())

	lvi.close()
	lvo.close()


main()