1992-12-24 07:37:53 -04:00
|
|
|
#!/ufs/guido/bin/sgi/python
|
1992-09-22 14:23:17 -03:00
|
|
|
|
|
|
|
# Receive live video UDP packets.
|
|
|
|
# Usage: Vreceive [port]
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import struct
|
1992-09-24 09:54:35 -03:00
|
|
|
from socket import * # syscalls and support functions
|
|
|
|
from SOCKET import * # <sys/socket.h>
|
|
|
|
from IN import * # <netinet/in.h>
|
1992-09-22 14:23:17 -03:00
|
|
|
import select
|
1992-09-24 09:54:35 -03:00
|
|
|
import struct
|
1992-09-22 14:23:17 -03:00
|
|
|
import gl, GL, DEVICE
|
|
|
|
sys.path.append('/ufs/guido/src/video')
|
|
|
|
import LiveVideoOut
|
1992-09-24 09:54:35 -03:00
|
|
|
import regsub
|
1992-09-24 12:01:37 -03:00
|
|
|
import getopt
|
1992-09-24 09:54:35 -03:00
|
|
|
|
1992-09-24 12:01:37 -03:00
|
|
|
from senddefs import *
|
|
|
|
|
|
|
|
|
1992-09-24 13:03:56 -03:00
|
|
|
# Print usage message and exit(2).
|
|
|
|
|
1992-09-24 12:01:37 -03:00
|
|
|
def usage(msg):
|
|
|
|
print msg
|
1992-12-14 08:25:21 -04:00
|
|
|
print 'usage: Vreceive [-m mcastgrp] [-p port] [-c type]'
|
1992-09-24 12:01:37 -03:00
|
|
|
print '-m mcastgrp: multicast group (default ' + `DEFMCAST` + ')'
|
|
|
|
print '-p port : port (default ' + `DEFPORT` + ')'
|
1992-12-14 08:25:21 -04:00
|
|
|
print '-c type : signal type: rgb8, grey or mono (default rgb8)'
|
1992-09-24 12:01:37 -03:00
|
|
|
sys.exit(2)
|
1992-09-22 14:23:17 -03:00
|
|
|
|
|
|
|
|
1992-09-24 13:03:56 -03:00
|
|
|
# Main program: parse options and main loop.
|
|
|
|
|
1992-09-22 14:23:17 -03:00
|
|
|
def main():
|
|
|
|
|
1992-09-24 12:01:37 -03:00
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
|
|
|
group = DEFMCAST
|
|
|
|
port = DEFPORT
|
|
|
|
width = DEFWIDTH
|
|
|
|
height = DEFHEIGHT
|
1992-12-14 08:25:21 -04:00
|
|
|
vtype = 'rgb8'
|
1992-09-24 12:01:37 -03:00
|
|
|
|
|
|
|
try:
|
1992-12-14 08:25:21 -04:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'm:p:c:')
|
1992-09-24 12:01:37 -03:00
|
|
|
except getopt.error, msg:
|
|
|
|
usage(msg)
|
1992-09-22 14:23:17 -03:00
|
|
|
|
1992-09-24 12:01:37 -03:00
|
|
|
try:
|
|
|
|
for opt, optarg in opts:
|
|
|
|
if opt == '-p':
|
|
|
|
port = string.atoi(optarg)
|
|
|
|
if opt == '-m':
|
|
|
|
group = gethostbyname(optarg)
|
1992-12-14 08:25:21 -04:00
|
|
|
if opt == '-c':
|
|
|
|
vtype = optarg
|
1992-09-24 12:01:37 -03:00
|
|
|
except string.atoi_error, msg:
|
|
|
|
usage('bad integer: ' + msg)
|
1992-09-24 09:54:35 -03:00
|
|
|
|
1992-09-24 12:01:37 -03:00
|
|
|
s = opensocket(group, port)
|
1992-09-22 14:23:17 -03:00
|
|
|
|
|
|
|
gl.foreground()
|
|
|
|
gl.prefsize(width, height)
|
|
|
|
wid = gl.winopen('Vreceive')
|
1992-09-24 13:03:56 -03:00
|
|
|
gl.winconstraints()
|
1992-09-22 14:23:17 -03:00
|
|
|
gl.qdevice(DEVICE.ESCKEY)
|
|
|
|
gl.qdevice(DEVICE.WINSHUT)
|
|
|
|
gl.qdevice(DEVICE.WINQUIT)
|
|
|
|
|
1992-12-14 08:25:21 -04:00
|
|
|
lvo = LiveVideoOut.LiveVideoOut().init(wid, width, height, vtype)
|
1992-09-22 14:23:17 -03: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 13:03:56 -03:00
|
|
|
lvo.reshapewindow()
|
1992-09-22 14:23:17 -03: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 13:03:56 -03:00
|
|
|
x, y = gl.getorigin()
|
1992-09-22 14:23:17 -03:00
|
|
|
y = y + height - h
|
1992-09-24 13:03:56 -03:00
|
|
|
gl.winposition(x, x+w-1, y, y+h-1)
|
1992-09-22 14:23:17 -03:00
|
|
|
width, height = w, h
|
1992-09-24 13:03:56 -03:00
|
|
|
lvo.resizevideo(width, height)
|
1992-09-22 14:23:17 -03:00
|
|
|
lvo.putnextpacket(pos, data[6:])
|
|
|
|
else:
|
|
|
|
x = select.select(selectargs)
|
|
|
|
|
|
|
|
lvo.close()
|
|
|
|
|
1992-09-24 09:54:35 -03: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 13:03:56 -03:00
|
|
|
# Bind the port to it
|
|
|
|
s.bind('', port)
|
|
|
|
|
1992-09-24 09:54:35 -03:00
|
|
|
# Look up the group once
|
|
|
|
group = gethostbyname(group)
|
|
|
|
|
1992-09-24 13:03:56 -03:00
|
|
|
# Construct binary group address
|
1992-09-24 09:54:35 -03: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 13:03:56 -03:00
|
|
|
|
1992-09-22 14:23:17 -03:00
|
|
|
main()
|