Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Demo / sgi / video / Dsend.py
blob7e197f7df95500cd5d40b590f34cd87652b59e69
1 #! /usr/bin/env python
3 # Send live video UDP packets.
4 # Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
5 # [host] ..
7 import sys
8 import time
9 import struct
10 import string
11 import math
12 from socket import *
13 from SOCKET import *
14 import gl, GL, DEVICE
15 sys.path.append('/ufs/guido/src/video')
16 import DisplayVideoIn
17 import LiveVideoOut
18 import SV
19 import getopt
20 from IN import *
22 from senddefs import *
24 def usage(msg):
25 print msg
26 print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-c type] [-m]',
27 print '[-w width] [host] ...'
28 print '-b : broadcast on local net'
29 print '-h height : window height (default ' + `DEFHEIGHT` + ')'
30 print '-p port : port to use (default ' + `DEFPORT` + ')'
31 print '-t ttl : time-to-live (multicast only; default 1)'
32 print '-s size : max packet size (default ' + `DEFPKTMAX` + ')'
33 print '-S size : use this packet size/window size'
34 print '-w width : window width (default ' + `DEFWIDTH` + ')'
35 print '-v : print packet rate'
36 print '-x xpos : set x position'
37 print '-y ypos : set y position'
38 print '[host] ...: host(s) to send to (default multicast to ' + \
39 DEFMCAST + ')'
40 sys.exit(2)
43 def main():
44 sys.stdout = sys.stderr
46 hosts = []
47 port = DEFPORT
48 ttl = -1
49 pktmax = DEFPKTMAX
50 width = DEFWIDTH
51 height = DEFHEIGHT
52 vtype = 'rgb'
53 verbose = 0
54 xpos = ypos = 0
56 try:
57 opts, args = getopt.getopt(sys.argv[1:], 'bh:p:s:S:t:w:vx:y:')
58 except getopt.error, msg:
59 usage(msg)
61 try:
62 for opt, optarg in opts:
63 if opt == '-p':
64 port = string.atoi(optarg)
65 if opt == '-b':
66 host = '<broadcast>'
67 if opt == '-t':
68 ttl = string.atoi(optarg)
69 if opt == '-S':
70 pktmax = string.atoi(optarg)
71 vidmax = SV.PAL_XMAX*SV.PAL_YMAX
72 if vidmax <= pktmax:
73 width = SV.PAL_XMAX
74 height = SV.PAL_YMAX
75 pktmax = vidmax
76 else:
77 factor = float(vidmax)/float(pktmax)
78 factor = math.sqrt(factor)
79 width = int(SV.PAL_XMAX/factor)-7
80 height = int(SV.PAL_YMAX/factor)-5
81 print 'video:',width,'x',height,
82 print 'pktsize',width*height,'..',
83 print pktmax
84 if opt == '-s':
85 pktmax = string.atoi(optarg)
86 if opt == '-w':
87 width = string.atoi(optarg)
88 if opt == '-h':
89 height = string.atoi(optarg)
90 if opt == '-c':
91 vtype = optarg
92 if opt == '-v':
93 verbose = 1
94 if opt == '-x':
95 xpos = string.atoi(optarg)
96 if opt == '-y':
97 ypos = string.atoi(optarg)
98 except string.atoi_error, msg:
99 usage('bad integer: ' + msg)
101 for host in args:
102 hosts.append(gethostbyname(host))
104 if not hosts:
105 hosts.append(gethostbyname(DEFMCAST))
107 gl.foreground()
108 gl.prefsize(width, height)
109 gl.stepunit(8, 6)
110 wid = gl.winopen('Vsend')
111 gl.keepaspect(width, height)
112 gl.stepunit(8, 6)
113 gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
114 gl.winconstraints()
115 gl.qdevice(DEVICE.ESCKEY)
116 gl.qdevice(DEVICE.WINSHUT)
117 gl.qdevice(DEVICE.WINQUIT)
118 gl.qdevice(DEVICE.WINFREEZE)
119 gl.qdevice(DEVICE.WINTHAW)
120 width, height = gl.getsize()
122 lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)
124 lvi = DisplayVideoIn.DisplayVideoIn(pktmax, width, height, vtype)
126 if xpos or ypos:
127 lvi.positionvideo(xpos, ypos)
129 s = socket(AF_INET, SOCK_DGRAM)
130 s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
131 if ttl >= 0:
132 s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, chr(ttl))
134 frozen = 0
136 lasttime = int(time.time())
137 nframe = 0
138 while 1:
140 if gl.qtest():
141 dev, val = gl.qread()
142 if dev in (DEVICE.ESCKEY, \
143 DEVICE.WINSHUT, DEVICE.WINQUIT):
144 break
145 if dev == DEVICE.WINFREEZE:
146 frozen = 1
147 if dev == DEVICE.WINTHAW:
148 frozen = 0
149 if dev == DEVICE.REDRAW:
150 w, h = gl.getsize()
151 x, y = gl.getorigin()
152 if (w, h) <> (width, height):
153 width, height = w, h
154 lvi.resizevideo(width, height)
155 lvo.resizevideo(width, height)
157 rv = lvi.getnextpacket()
158 if not rv:
159 time.sleep(0.010)
160 continue
162 pos, data = rv
163 print pos, len(data) # DBG
165 if not frozen:
166 lvo.putnextpacket(pos, data)
168 hdr = struct.pack('hhh', pos, width, height)
169 for host in hosts:
170 try:
171 # print len(hdr+data) # DBG
172 s.sendto(hdr + data, (host, port))
173 except error, msg: # really socket.error
174 if msg[0] <> 121: # no buffer space available
175 raise error, msg # re-raise it
176 print 'Warning:', msg[1]
177 if pos == 0 and verbose:
178 nframe = nframe+1
179 if int(time.time()) <> lasttime:
180 print nframe / (time.time()-lasttime), 'fps'
181 nframe = 0
182 lasttime = int(time.time())
184 lvi.close()
185 lvo.close()
188 main()