blob: 415df593180a58e6c160c9a16358353bb8584934 [file] [log] [blame]
Guido van Rossumba066151992-09-22 17:23:17 +00001#!/ufs/guido/bin/sgi/python-405
2
3# Receive live video UDP packets.
4# Usage: Vreceive [port]
5
6import sys
7import struct
Guido van Rossum67b48951992-09-24 12:54:35 +00008from socket import * # syscalls and support functions
9from SOCKET import * # <sys/socket.h>
10from IN import * # <netinet/in.h>
Guido van Rossumba066151992-09-22 17:23:17 +000011import select
Guido van Rossum67b48951992-09-24 12:54:35 +000012import struct
Guido van Rossumba066151992-09-22 17:23:17 +000013import gl, GL, DEVICE
14sys.path.append('/ufs/guido/src/video')
15import LiveVideoOut
Guido van Rossum67b48951992-09-24 12:54:35 +000016import regsub
17
18MYGROUP = '225.0.0.250'
19PORT = 5555
Guido van Rossumba066151992-09-22 17:23:17 +000020
21PKTMAX = 16*1024
22WIDTH = 400
23HEIGHT = 300
Guido van Rossumba066151992-09-22 17:23:17 +000024
25def main():
26
27 port = PORT
28 if sys.argv[1:]:
29 port = eval(sys.argv[1])
30
Guido van Rossum67b48951992-09-24 12:54:35 +000031 s = opensocket(MYGROUP, port)
32
Guido van Rossumba066151992-09-22 17:23:17 +000033 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 Rossumba066151992-09-22 17:23:17 +000046 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 Rossum67b48951992-09-24 12:54:35 +000078
79# Subroutine to create and properly initialize the receiving socket
80
81def 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 Rossumba066151992-09-22 17:23:17 +0000109main()