home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / sgi / video / Vreceive.py < prev    next >
Text File  |  1996-11-27  |  3KB  |  136 lines

  1. #! /usr/bin/env python
  2.  
  3. # Receive live video UDP packets.
  4. # Usage: Vreceive [port]
  5.  
  6. import sys
  7. import struct
  8. from socket import *            # syscalls and support functions
  9. from SOCKET import *            # <sys/socket.h>
  10. from IN import *            # <netinet/in.h>
  11. import select
  12. import struct
  13. import gl, GL, DEVICE
  14. sys.path.append('/ufs/guido/src/video')
  15. import LiveVideoOut
  16. import regsub
  17. import getopt
  18.  
  19. from senddefs import *
  20.  
  21.  
  22. # Print usage message and exit(2).
  23.  
  24. def usage(msg):
  25.     print msg
  26.     print 'usage: Vreceive [-m mcastgrp] [-p port] [-c type]'
  27.     print '-m mcastgrp: multicast group (default ' + `DEFMCAST` + ')'
  28.     print '-p port    : port (default ' + `DEFPORT` + ')'
  29.     print '-c type    : signal type: rgb8, grey or mono (default rgb8)'
  30.     sys.exit(2)
  31.  
  32.  
  33. # Main program: parse options and main loop.
  34.  
  35. def main():
  36.  
  37.     sys.stdout = sys.stderr
  38.  
  39.     group = DEFMCAST
  40.     port = DEFPORT
  41.     width = DEFWIDTH
  42.     height = DEFHEIGHT
  43.     vtype = 'rgb8'
  44.  
  45.     try:
  46.         opts, args = getopt.getopt(sys.argv[1:], 'm:p:c:')
  47.     except getopt.error, msg:
  48.         usage(msg)
  49.  
  50.     try:
  51.         for opt, optarg in opts:
  52.             if opt == '-p':
  53.                 port = string.atoi(optarg)
  54.             if opt == '-m':
  55.                 group = gethostbyname(optarg)
  56.             if opt == '-c':
  57.                 vtype = optarg
  58.     except string.atoi_error, msg:
  59.         usage('bad integer: ' + msg)
  60.  
  61.     s = opensocket(group, port)
  62.  
  63.     gl.foreground()
  64.     gl.prefsize(width, height)
  65.     wid = gl.winopen('Vreceive')
  66.     gl.winconstraints()
  67.     gl.qdevice(DEVICE.ESCKEY)
  68.     gl.qdevice(DEVICE.WINSHUT)
  69.     gl.qdevice(DEVICE.WINQUIT)
  70.  
  71.     lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)
  72.  
  73.     ifdlist = [gl.qgetfd(), s.fileno()]
  74.     ofdlist = []
  75.     xfdlist = []
  76.     timeout = 1.0
  77.     selectargs = (ifdlist, ofdlist, xfdlist, timeout)
  78.  
  79.     while 1:
  80.  
  81.         if gl.qtest():
  82.             dev, val = gl.qread()
  83.             if dev in (DEVICE.ESCKEY, \
  84.                 DEVICE.WINSHUT, DEVICE.WINQUIT):
  85.                 break
  86.             if dev == DEVICE.REDRAW:
  87.                 lvo.reshapewindow()
  88.         elif s.avail():
  89.             data = s.recv(16*1024)
  90.             pos, w, h = struct.unpack('hhh', data[:6])
  91.             if (w, h) <> (width, height):
  92.                 x, y =  gl.getorigin()
  93.                 y = y + height - h
  94.                 gl.winposition(x, x+w-1, y, y+h-1)
  95.                 width, height = w, h
  96.                 lvo.resizevideo(width, height)
  97.             lvo.putnextpacket(pos, data[6:])
  98.         else:
  99.             x = select.select(selectargs)
  100.  
  101.     lvo.close()
  102.  
  103.  
  104. # Subroutine to create and properly initialize the receiving socket
  105.  
  106. def opensocket(group, port):
  107.  
  108.     # Create the socket
  109.     s = socket(AF_INET, SOCK_DGRAM)
  110.  
  111.     # Allow multiple copies of this program on one machine
  112.     s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed)
  113.  
  114.     # Bind the port to it
  115.     s.bind('', port)
  116.  
  117.     # Look up the group once
  118.     group = gethostbyname(group)
  119.  
  120.     # Construct binary group address
  121.     group_bytes = eval(regsub.gsub('\.', ',', group))
  122.     grpaddr = 0
  123.     for byte in group_bytes: grpaddr = (grpaddr << 8) | byte
  124.  
  125.     # Construct struct mreq from grpaddr and ifaddr
  126.     ifaddr = INADDR_ANY
  127.     mreq = struct.pack('ll', grpaddr, ifaddr)
  128.  
  129.     # Add group membership
  130.     s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
  131.  
  132.     return s
  133.  
  134.  
  135. main()
  136.