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 |
Guido van Rossum | 67b4895 | 1992-09-24 12:54:35 +0000 | [diff] [blame^] | 8 | from socket import * # syscalls and support functions |
| 9 | from SOCKET import * # <sys/socket.h> |
| 10 | from IN import * # <netinet/in.h> |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 11 | import select |
Guido van Rossum | 67b4895 | 1992-09-24 12:54:35 +0000 | [diff] [blame^] | 12 | import struct |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 13 | import gl, GL, DEVICE |
| 14 | sys.path.append('/ufs/guido/src/video') |
| 15 | import LiveVideoOut |
Guido van Rossum | 67b4895 | 1992-09-24 12:54:35 +0000 | [diff] [blame^] | 16 | import regsub |
| 17 | |
| 18 | MYGROUP = '225.0.0.250' |
| 19 | PORT = 5555 |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 20 | |
| 21 | PKTMAX = 16*1024 |
| 22 | WIDTH = 400 |
| 23 | HEIGHT = 300 |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 24 | |
| 25 | def main(): |
| 26 | |
| 27 | port = PORT |
| 28 | if sys.argv[1:]: |
| 29 | port = eval(sys.argv[1]) |
| 30 | |
Guido van Rossum | 67b4895 | 1992-09-24 12:54:35 +0000 | [diff] [blame^] | 31 | s = opensocket(MYGROUP, port) |
| 32 | |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 33 | width, height = WIDTH, HEIGHT |
| 34 | |
| 35 | gl.foreground() |
| 36 | gl.prefsize(width, height) |
| 37 | wid = gl.winopen('Vreceive') |
| 38 | gl.qdevice(DEVICE.ESCKEY) |
| 39 | gl.qdevice(DEVICE.WINSHUT) |
| 40 | gl.qdevice(DEVICE.WINQUIT) |
| 41 | |
| 42 | x, y = gl.getorigin() |
| 43 | lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \ |
| 44 | width, height) |
| 45 | |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 46 | ifdlist = [gl.qgetfd(), s.fileno()] |
| 47 | ofdlist = [] |
| 48 | xfdlist = [] |
| 49 | timeout = 1.0 |
| 50 | selectargs = (ifdlist, ofdlist, xfdlist, timeout) |
| 51 | |
| 52 | while 1: |
| 53 | |
| 54 | if gl.qtest(): |
| 55 | dev, val = gl.qread() |
| 56 | if dev in (DEVICE.ESCKEY, \ |
| 57 | DEVICE.WINSHUT, DEVICE.WINQUIT): |
| 58 | break |
| 59 | if dev == DEVICE.REDRAW: |
| 60 | gl.clear() |
| 61 | elif s.avail(): |
| 62 | data = s.recv(16*1024) |
| 63 | pos, w, h = struct.unpack('hhh', data[:6]) |
| 64 | if (w, h) <> (width, height): |
| 65 | x, y = gl.getorigin() |
| 66 | y = y + height - h |
| 67 | width, height = w, h |
| 68 | lvo.close() |
| 69 | lvo = LiveVideoOut.LiveVideoOut() \ |
| 70 | .init(wid, (x, y, width, height), \ |
| 71 | width, height) |
| 72 | lvo.putnextpacket(pos, data[6:]) |
| 73 | else: |
| 74 | x = select.select(selectargs) |
| 75 | |
| 76 | lvo.close() |
| 77 | |
Guido van Rossum | 67b4895 | 1992-09-24 12:54:35 +0000 | [diff] [blame^] | 78 | |
| 79 | # Subroutine to create and properly initialize the receiving socket |
| 80 | |
| 81 | def opensocket(group, port): |
| 82 | |
| 83 | # Create the socket |
| 84 | s = socket(AF_INET, SOCK_DGRAM) |
| 85 | |
| 86 | # Bind the port to it |
| 87 | s.bind('', port) |
| 88 | |
| 89 | # Allow multiple copies of this program on one machine |
| 90 | s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed) |
| 91 | |
| 92 | # Look up the group once |
| 93 | group = gethostbyname(group) |
| 94 | |
| 95 | # Ugly: construct binary group address |
| 96 | group_bytes = eval(regsub.gsub('\.', ',', group)) |
| 97 | grpaddr = 0 |
| 98 | for byte in group_bytes: grpaddr = (grpaddr << 8) | byte |
| 99 | |
| 100 | # Construct struct mreq from grpaddr and ifaddr |
| 101 | ifaddr = INADDR_ANY |
| 102 | mreq = struct.pack('ll', grpaddr, ifaddr) |
| 103 | |
| 104 | # Add group membership |
| 105 | s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq) |
| 106 | |
| 107 | return s |
| 108 | |
Guido van Rossum | ba06615 | 1992-09-22 17:23:17 +0000 | [diff] [blame] | 109 | main() |