blob: e5154c04b2e91a95311f21b76989a52acdd7f4ba [file] [log] [blame]
Guido van Rossumba066151992-09-22 17:23:17 +00001#!/ufs/guido/bin/sgi/python-405
2
3# Send live video UDP packets.
Guido van Rossum7b47c791992-09-24 15:01:37 +00004# Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
5# [host] ..
Guido van Rossumba066151992-09-22 17:23:17 +00006
7import sys
8import time
9import struct
Guido van Rossum7b47c791992-09-24 15:01:37 +000010import string
Guido van Rossumba066151992-09-22 17:23:17 +000011from socket import *
Guido van Rossum67b48951992-09-24 12:54:35 +000012from SOCKET import *
Guido van Rossumba066151992-09-22 17:23:17 +000013import gl, GL, DEVICE
14sys.path.append('/ufs/guido/src/video')
15import LiveVideoIn
16import LiveVideoOut
Guido van Rossum67b48951992-09-24 12:54:35 +000017import SV
Guido van Rossum7b47c791992-09-24 15:01:37 +000018import getopt
19from IN import *
Guido van Rossumba066151992-09-22 17:23:17 +000020
Guido van Rossum7b47c791992-09-24 15:01:37 +000021from senddefs import *
22
23def usage(msg):
24 print msg
25 print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl]',
26 print '[-w width] [host] ...'
27 print '-b : broadcast on local net'
28 print '-h height : window height (default ' + `DEFHEIGHT` + ')'
29 print '-p port : port to use (default ' + `DEFPORT` + ')'
30 print '-t ttl : time-to-live (multicast only; default 1)'
31 print '-s size : max packet size (default ' + `DEFPKTMAX` + ')'
32 print '-w width : window width (default ' + `DEFWIDTH` + ')'
33 print '[host] ...: host(s) to send to (default multicast to ' + \
34 DEFMCAST + ')'
35 sys.exit(2)
36
Guido van Rossumba066151992-09-22 17:23:17 +000037
38def main():
Guido van Rossum7b47c791992-09-24 15:01:37 +000039 sys.stdout = sys.stderr
40
41 hosts = []
42 port = DEFPORT
43 ttl = -1
44 pktmax = DEFPKTMAX
45 width = DEFWIDTH
46 height = DEFHEIGHT
47
48 try:
49 opts, args = getopt.getopt(sys.argv[1:], 'bh:p:s:t:w:')
50 except getopt.error, msg:
51 usage(msg)
52
53 try:
54 for opt, optarg in opts:
55 if opt == '-p':
56 port = string.atoi(optarg)
57 if opt == '-b':
58 host = '<broadcast>'
59 if opt == '-t':
60 ttl = string.atoi(optarg)
61 if opt == '-s':
62 pktmax = string.atoi(optarg)
63 if opt == '-w':
64 width = string.atoi(optarg)
65 if opt == '-h':
66 height = string.atoi(optarg)
67 except string.atoi_error, msg:
68 usage('bad integer: ' + msg)
69
70 for host in args:
71 hosts.append(gethostbyname(host))
72
73 if not hosts:
74 hosts.append(gethostbyname(DEFMCAST))
75
Guido van Rossumba066151992-09-22 17:23:17 +000076 if not LiveVideoIn.have_video:
Guido van Rossum7b47c791992-09-24 15:01:37 +000077 print 'Sorry, no video available (use python-405 on roos)'
Guido van Rossumba066151992-09-22 17:23:17 +000078 sys.exit(1)
79
Guido van Rossumba066151992-09-22 17:23:17 +000080 gl.foreground()
Guido van Rossum7b47c791992-09-24 15:01:37 +000081 gl.prefsize(width, height)
82 gl.stepunit(8, 6)
Guido van Rossumba066151992-09-22 17:23:17 +000083 wid = gl.winopen('Vsend')
Guido van Rossum7b47c791992-09-24 15:01:37 +000084 gl.keepaspect(width, height)
Guido van Rossumba066151992-09-22 17:23:17 +000085 gl.stepunit(8, 6)
Guido van Rossum67b48951992-09-24 12:54:35 +000086 gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
Guido van Rossumba066151992-09-22 17:23:17 +000087 gl.winconstraints()
88 gl.qdevice(DEVICE.ESCKEY)
89 gl.qdevice(DEVICE.WINSHUT)
90 gl.qdevice(DEVICE.WINQUIT)
Guido van Rossum67b48951992-09-24 12:54:35 +000091 gl.qdevice(DEVICE.WINFREEZE)
92 gl.qdevice(DEVICE.WINTHAW)
Guido van Rossumba066151992-09-22 17:23:17 +000093 width, height = gl.getsize()
94
95 x, y = gl.getorigin()
96 lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \
97 width, height)
98
99 lvi = LiveVideoIn.LiveVideoIn().init(pktmax, width, height)
100
101 s = socket(AF_INET, SOCK_DGRAM)
Guido van Rossum67b48951992-09-24 12:54:35 +0000102 s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
Guido van Rossum7b47c791992-09-24 15:01:37 +0000103 if ttl >= 0:
104 s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, chr(ttl))
Guido van Rossum67b48951992-09-24 12:54:35 +0000105
106 frozen = 0
Guido van Rossumba066151992-09-22 17:23:17 +0000107
108 while 1:
109
110 if gl.qtest():
111 dev, val = gl.qread()
112 if dev in (DEVICE.ESCKEY, \
113 DEVICE.WINSHUT, DEVICE.WINQUIT):
114 break
Guido van Rossum67b48951992-09-24 12:54:35 +0000115 if dev == DEVICE.WINFREEZE:
116 frozen = 1
117 if dev == DEVICE.WINTHAW:
118 frozen = 0
Guido van Rossumba066151992-09-22 17:23:17 +0000119 if dev == DEVICE.REDRAW:
120 w, h = gl.getsize()
121 x, y = gl.getorigin()
122 if (w, h) <> (width, height):
123 lvi.close()
124 width, height = w, h
125 lvi = LiveVideoIn.LiveVideoIn() \
126 .init(pktmax, width, height)
127 lvo.close()
128 lvo = LiveVideoOut.LiveVideoOut() \
129 .init(wid, \
130 (x, y, width, height), \
131 width, height)
132
133 rv = lvi.getnextpacket()
134 if not rv:
135 time.millisleep(10)
136 continue
137
138 pos, data = rv
Guido van Rossum67b48951992-09-24 12:54:35 +0000139
140 if not frozen:
141 lvo.putnextpacket(pos, data)
Guido van Rossumba066151992-09-22 17:23:17 +0000142
143 hdr = struct.pack('hhh', pos, width, height)
Guido van Rossum7b47c791992-09-24 15:01:37 +0000144 for host in hosts:
145 try:
146 s.sendto(hdr + data, (host, port))
147 except error, msg: # really socket.error
148 if msg[0] <> 121: # no buffer space available
149 raise error, msg # re-raise it
150 print 'Warning:', msg[1]
Guido van Rossumba066151992-09-22 17:23:17 +0000151
152 lvi.close()
153 lvo.close()
154
Guido van Rossum7b47c791992-09-24 15:01:37 +0000155
Guido van Rossumba066151992-09-22 17:23:17 +0000156main()