| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | #!/ufs/guido/bin/sgi/python-405 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Receive live video UDP packets. | 
					
						
							|  |  |  | # Usage: Vreceive [port] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import struct | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | from socket import *			# syscalls and support functions | 
					
						
							|  |  |  | from SOCKET import *			# <sys/socket.h> | 
					
						
							|  |  |  | from IN import *			# <netinet/in.h> | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | import select | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | import struct | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | import gl, GL, DEVICE | 
					
						
							|  |  |  | sys.path.append('/ufs/guido/src/video') | 
					
						
							|  |  |  | import LiveVideoOut | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | import regsub | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | import getopt | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | from senddefs import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | # Print usage message and exit(2). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | def usage(msg): | 
					
						
							|  |  |  | 	print msg | 
					
						
							|  |  |  | 	print 'usage: Vreceive [-m mcastgrp] [-p port]' | 
					
						
							|  |  |  | 	print '-m mcastgrp: multicast group (default ' + `DEFMCAST` + ')' | 
					
						
							|  |  |  | 	print '-p port    : port (default ' + `DEFPORT` + ')' | 
					
						
							|  |  |  | 	sys.exit(2) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | # Main program: parse options and main loop. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | def main(): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | 	sys.stdout = sys.stderr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	group = DEFMCAST | 
					
						
							|  |  |  | 	port = DEFPORT | 
					
						
							|  |  |  | 	width = DEFWIDTH | 
					
						
							|  |  |  | 	height = DEFHEIGHT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	try: | 
					
						
							|  |  |  | 		opts, args = getopt.getopt(sys.argv[1:], 'm:p:') | 
					
						
							|  |  |  | 	except getopt.error, msg: | 
					
						
							|  |  |  | 		usage(msg) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | 	try: | 
					
						
							|  |  |  | 		for opt, optarg in opts: | 
					
						
							|  |  |  | 			if opt == '-p': | 
					
						
							|  |  |  | 				port = string.atoi(optarg) | 
					
						
							|  |  |  | 			if opt == '-m': | 
					
						
							|  |  |  | 				group = gethostbyname(optarg) | 
					
						
							|  |  |  | 	except string.atoi_error, msg: | 
					
						
							|  |  |  | 		usage('bad integer: ' + msg) | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 15:01:37 +00:00
										 |  |  | 	s = opensocket(group, port) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	gl.foreground() | 
					
						
							|  |  |  | 	gl.prefsize(width, height) | 
					
						
							|  |  |  | 	wid = gl.winopen('Vreceive') | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 	gl.winconstraints() | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 	gl.qdevice(DEVICE.ESCKEY) | 
					
						
							|  |  |  | 	gl.qdevice(DEVICE.WINSHUT) | 
					
						
							|  |  |  | 	gl.qdevice(DEVICE.WINQUIT) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 	lvo = LiveVideoOut.LiveVideoOut().init(wid, width, height) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ifdlist = [gl.qgetfd(), s.fileno()] | 
					
						
							|  |  |  | 	ofdlist = [] | 
					
						
							|  |  |  | 	xfdlist = [] | 
					
						
							|  |  |  | 	timeout = 1.0 | 
					
						
							|  |  |  | 	selectargs = (ifdlist, ofdlist, xfdlist, timeout) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while 1: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if gl.qtest(): | 
					
						
							|  |  |  | 			dev, val = gl.qread() | 
					
						
							|  |  |  | 			if dev in (DEVICE.ESCKEY, \ | 
					
						
							|  |  |  | 				DEVICE.WINSHUT, DEVICE.WINQUIT): | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			if dev == DEVICE.REDRAW: | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 				lvo.reshapewindow() | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 		elif s.avail(): | 
					
						
							|  |  |  | 			data = s.recv(16*1024) | 
					
						
							|  |  |  | 			pos, w, h = struct.unpack('hhh', data[:6]) | 
					
						
							|  |  |  | 			if (w, h) <> (width, height): | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 				x, y =  gl.getorigin() | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 				y = y + height - h | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 				gl.winposition(x, x+w-1, y, y+h-1) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 				width, height = w, h | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 				lvo.resizevideo(width, height) | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | 			lvo.putnextpacket(pos, data[6:]) | 
					
						
							|  |  |  | 		else: | 
					
						
							|  |  |  | 			x = select.select(selectargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lvo.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Subroutine to create and properly initialize the receiving socket | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def opensocket(group, port): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Create the socket | 
					
						
							|  |  |  | 	s = socket(AF_INET, SOCK_DGRAM) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Allow multiple copies of this program on one machine | 
					
						
							|  |  |  | 	s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 	# Bind the port to it | 
					
						
							|  |  |  | 	s.bind('', port) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | 	# Look up the group once | 
					
						
							|  |  |  | 	group = gethostbyname(group) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 	# Construct binary group address | 
					
						
							| 
									
										
										
										
											1992-09-24 12:54:35 +00:00
										 |  |  | 	group_bytes = eval(regsub.gsub('\.', ',', group)) | 
					
						
							|  |  |  | 	grpaddr = 0 | 
					
						
							|  |  |  | 	for byte in group_bytes: grpaddr = (grpaddr << 8) | byte | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Construct struct mreq from grpaddr and ifaddr | 
					
						
							|  |  |  | 	ifaddr = INADDR_ANY | 
					
						
							|  |  |  | 	mreq = struct.pack('ll', grpaddr, ifaddr) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Add group membership | 
					
						
							|  |  |  | 	s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-24 16:03:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-22 17:23:17 +00:00
										 |  |  | main() |