Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 1 | #!/ufs/guido/bin/sgi/python-405 |
| 2 | |
| 3 | # Receive live video UDP packets. |
| 4 | # Usage: Vreceive [port] |
| 5 | |
| 6 | import sys |
| 7 | import struct |
| 8 | from socket import * |
| 9 | import select |
| 10 | import gl, GL, DEVICE |
| 11 | sys.path.append('/ufs/guido/src/video') |
| 12 | import LiveVideoOut |
| 13 | |
| 14 | PKTMAX = 16*1024 |
| 15 | WIDTH = 400 |
| 16 | HEIGHT = 300 |
| 17 | HOST = '' |
| 18 | PORT = 5555 |
| 19 | |
| 20 | def main(): |
| 21 | |
| 22 | port = PORT |
| 23 | if sys.argv[1:]: |
| 24 | port = eval(sys.argv[1]) |
| 25 | |
| 26 | width, height = WIDTH, HEIGHT |
| 27 | |
| 28 | gl.foreground() |
| 29 | gl.prefsize(width, height) |
| 30 | wid = gl.winopen('Vreceive') |
| 31 | gl.qdevice(DEVICE.ESCKEY) |
| 32 | gl.qdevice(DEVICE.WINSHUT) |
| 33 | gl.qdevice(DEVICE.WINQUIT) |
| 34 | |
| 35 | x, y = gl.getorigin() |
| 36 | lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \ |
| 37 | width, height) |
| 38 | |
| 39 | s = socket(AF_INET, SOCK_DGRAM) |
| 40 | s.bind(HOST, port) |
| 41 | |
| 42 | ifdlist = [gl.qgetfd(), s.fileno()] |
| 43 | ofdlist = [] |
| 44 | xfdlist = [] |
| 45 | timeout = 1.0 |
| 46 | selectargs = (ifdlist, ofdlist, xfdlist, timeout) |
| 47 | |
| 48 | while 1: |
| 49 | |
| 50 | if gl.qtest(): |
| 51 | dev, val = gl.qread() |
| 52 | if dev in (DEVICE.ESCKEY, \ |
| 53 | DEVICE.WINSHUT, DEVICE.WINQUIT): |
| 54 | break |
| 55 | if dev == DEVICE.REDRAW: |
| 56 | gl.clear() |
| 57 | elif s.avail(): |
| 58 | data = s.recv(16*1024) |
| 59 | pos, w, h = struct.unpack('hhh', data[:6]) |
| 60 | if (w, h) <> (width, height): |
| 61 | x, y = gl.getorigin() |
| 62 | y = y + height - h |
| 63 | width, height = w, h |
| 64 | lvo.close() |
| 65 | lvo = LiveVideoOut.LiveVideoOut() \ |
| 66 | .init(wid, (x, y, width, height), \ |
| 67 | width, height) |
| 68 | lvo.putnextpacket(pos, data[6:]) |
| 69 | else: |
| 70 | x = select.select(selectargs) |
| 71 | |
| 72 | lvo.close() |
| 73 | |
| 74 | main() |