Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 1 | #!/ufs/guido/bin/sgi/python-405 |
| 2 | |
| 3 | # Send live video UDP packets. |
| 4 | # Usage: Vsend [host [port]] |
| 5 | |
| 6 | import sys |
| 7 | import time |
| 8 | import struct |
| 9 | from socket import * |
| 10 | import gl, GL, DEVICE |
| 11 | sys.path.append('/ufs/guido/src/video') |
| 12 | import LiveVideoIn |
| 13 | import LiveVideoOut |
| 14 | |
| 15 | PKTMAX_UCAST = 16*1024 - 6 |
| 16 | PKTMAX_BCAST = 1450 |
| 17 | WIDTH = 400 |
| 18 | HEIGHT = 300 |
| 19 | HOST = '<broadcast>' |
| 20 | PORT = 5555 |
| 21 | |
| 22 | def main(): |
| 23 | if not LiveVideoIn.have_video: |
| 24 | print 'Sorry, no video (use python-405 on roos)' |
| 25 | sys.exit(1) |
| 26 | |
| 27 | host = HOST |
| 28 | port = PORT |
| 29 | if sys.argv[1:]: |
| 30 | host = sys.argv[1] |
| 31 | if sys.argv[2:]: |
| 32 | port = eval(sys.argv[2]) |
| 33 | |
| 34 | if host == '<broadcast>': |
| 35 | pktmax = PKTMAX_BCAST |
| 36 | else: |
| 37 | pktmax = PKTMAX_UCAST |
| 38 | |
| 39 | gl.foreground() |
| 40 | gl.prefsize(WIDTH, HEIGHT) |
| 41 | wid = gl.winopen('Vsend') |
| 42 | gl.keepaspect(WIDTH, HEIGHT) |
| 43 | gl.stepunit(8, 6) |
| 44 | gl.winconstraints() |
| 45 | gl.qdevice(DEVICE.ESCKEY) |
| 46 | gl.qdevice(DEVICE.WINSHUT) |
| 47 | gl.qdevice(DEVICE.WINQUIT) |
| 48 | width, height = gl.getsize() |
| 49 | |
| 50 | x, y = gl.getorigin() |
| 51 | lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \ |
| 52 | width, height) |
| 53 | |
| 54 | lvi = LiveVideoIn.LiveVideoIn().init(pktmax, width, height) |
| 55 | |
| 56 | s = socket(AF_INET, SOCK_DGRAM) |
| 57 | s.allowbroadcast(1) |
| 58 | |
| 59 | while 1: |
| 60 | |
| 61 | if gl.qtest(): |
| 62 | dev, val = gl.qread() |
| 63 | if dev in (DEVICE.ESCKEY, \ |
| 64 | DEVICE.WINSHUT, DEVICE.WINQUIT): |
| 65 | break |
| 66 | if dev == DEVICE.REDRAW: |
| 67 | w, h = gl.getsize() |
| 68 | x, y = gl.getorigin() |
| 69 | if (w, h) <> (width, height): |
| 70 | lvi.close() |
| 71 | width, height = w, h |
| 72 | lvi = LiveVideoIn.LiveVideoIn() \ |
| 73 | .init(pktmax, width, height) |
| 74 | lvo.close() |
| 75 | lvo = LiveVideoOut.LiveVideoOut() \ |
| 76 | .init(wid, \ |
| 77 | (x, y, width, height), \ |
| 78 | width, height) |
| 79 | |
| 80 | rv = lvi.getnextpacket() |
| 81 | if not rv: |
| 82 | time.millisleep(10) |
| 83 | continue |
| 84 | |
| 85 | pos, data = rv |
| 86 | lvo.putnextpacket(pos, data) |
| 87 | |
| 88 | hdr = struct.pack('hhh', pos, width, height) |
| 89 | s.sendto(hdr + data, (host, port)) |
| 90 | |
| 91 | lvi.close() |
| 92 | lvo.close() |
| 93 | |
| 94 | main() |