Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Jack Jansen | b05eaf1 | 1993-02-17 15:58:49 +0000 | [diff] [blame] | 2 | |
| 3 | # Send live video UDP packets. |
| 4 | # Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width] |
| 5 | # [host] .. |
| 6 | |
| 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 * |
| 21 | |
| 22 | from senddefs import * |
| 23 | |
| 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) |
| 41 | |
| 42 | |
| 43 | def main(): |
| 44 | sys.stdout = sys.stderr |
| 45 | |
| 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 |
| 55 | |
| 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) |
| 60 | |
| 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) |
| 100 | |
| 101 | for host in args: |
| 102 | hosts.append(gethostbyname(host)) |
| 103 | |
| 104 | if not hosts: |
| 105 | hosts.append(gethostbyname(DEFMCAST)) |
| 106 | |
| 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() |
| 121 | |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 122 | lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype) |
Jack Jansen | b05eaf1 | 1993-02-17 15:58:49 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 124 | lvi = DisplayVideoIn.DisplayVideoIn(pktmax, width, height, vtype) |
Jack Jansen | b05eaf1 | 1993-02-17 15:58:49 +0000 | [diff] [blame] | 125 | |
| 126 | if xpos or ypos: |
| 127 | lvi.positionvideo(xpos, ypos) |
| 128 | |
| 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)) |
| 133 | |
| 134 | frozen = 0 |
| 135 | |
| 136 | lasttime = int(time.time()) |
| 137 | nframe = 0 |
| 138 | while 1: |
| 139 | |
| 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) |
| 156 | |
| 157 | rv = lvi.getnextpacket() |
| 158 | if not rv: |
Guido van Rossum | 424e4da | 1993-12-28 21:36:50 +0000 | [diff] [blame] | 159 | time.sleep(0.010) |
Jack Jansen | b05eaf1 | 1993-02-17 15:58:49 +0000 | [diff] [blame] | 160 | continue |
| 161 | |
| 162 | pos, data = rv |
| 163 | print pos, len(data) # DBG |
| 164 | |
| 165 | if not frozen: |
| 166 | lvo.putnextpacket(pos, data) |
| 167 | |
| 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()) |
| 183 | |
| 184 | lvi.close() |
| 185 | lvo.close() |
| 186 | |
| 187 | |
| 188 | main() |