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

  1. #! /usr/bin/env python
  2.  
  3. # Send live video UDP packets.
  4. # Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
  5. #              [host] ..
  6.  
  7. import sys
  8. import time
  9. import struct
  10. import string
  11. from socket import *
  12. from SOCKET import *
  13. import gl, GL, DEVICE
  14. sys.path.append('/ufs/guido/src/video')
  15. import LiveVideoIn
  16. import LiveVideoOut
  17. import SV
  18. import getopt
  19. from IN import *
  20.  
  21. from senddefs import *
  22.  
  23. def usage(msg):
  24.     print msg
  25.     print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-c type] [-m]',
  26.     print '[-w width] [host] ...'
  27.     print '-b        : broadcast on local net'
  28.     print '-h height : window height (default ' + `DEFHEIGHT` + ')'
  29.     print '-p port   : port to use (default ' + `DEFPORT` + ')'
  30.     print '-t ttl    : time-to-live (multicast only; default 1)'
  31.     print '-s size   : max packet size (default ' + `DEFPKTMAX` + ')'
  32.     print '-w width  : window width (default ' + `DEFWIDTH` + ')'
  33.     print '-c type   : Type: rgb8, mono or grey (default rgb8)'
  34.     print '[host] ...: host(s) to send to (default multicast to ' + \
  35.         DEFMCAST + ')'
  36.     sys.exit(2)
  37.  
  38.  
  39. def main():
  40.     sys.stdout = sys.stderr
  41.  
  42.     hosts = []
  43.     port = DEFPORT
  44.     ttl = -1
  45.     pktmax = DEFPKTMAX
  46.     width = DEFWIDTH
  47.     height = DEFHEIGHT
  48.     vtype = 'rgb8'
  49.  
  50.     try:
  51.         opts, args = getopt.getopt(sys.argv[1:], 'bh:p:s:t:w:c:')
  52.     except getopt.error, msg:
  53.         usage(msg)
  54.  
  55.     try:
  56.         for opt, optarg in opts:
  57.             if opt == '-p':
  58.                 port = string.atoi(optarg)
  59.             if opt == '-b':
  60.                 host = '<broadcast>'
  61.             if opt == '-t':
  62.                 ttl = string.atoi(optarg)
  63.             if opt == '-s':
  64.                 pktmax = string.atoi(optarg)
  65.             if opt == '-w':
  66.                 width = string.atoi(optarg)
  67.             if opt == '-h':
  68.                 height = string.atoi(optarg)
  69.             if opt == '-c':
  70.                 vtype = optarg
  71.     except string.atoi_error, msg:
  72.         usage('bad integer: ' + msg)
  73.  
  74.     for host in args:
  75.         hosts.append(gethostbyname(host))
  76.  
  77.     if not hosts:
  78.         hosts.append(gethostbyname(DEFMCAST))
  79.  
  80.     if not LiveVideoIn.have_video:
  81.         print 'Sorry, no video available (use python-405)'
  82.         sys.exit(1)
  83.  
  84.     gl.foreground()
  85.     gl.prefsize(width, height)
  86.     gl.stepunit(8, 6)
  87.     wid = gl.winopen('Vsend')
  88.     gl.keepaspect(width, height)
  89.     gl.stepunit(8, 6)
  90.     gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
  91.     gl.winconstraints()
  92.     gl.qdevice(DEVICE.ESCKEY)
  93.     gl.qdevice(DEVICE.WINSHUT)
  94.     gl.qdevice(DEVICE.WINQUIT)
  95.     gl.qdevice(DEVICE.WINFREEZE)
  96.     gl.qdevice(DEVICE.WINTHAW)
  97.     width, height = gl.getsize()
  98.  
  99.     lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)
  100.  
  101.     lvi = LiveVideoIn.LiveVideoIn(pktmax, width, height, vtype)
  102.  
  103.     s = socket(AF_INET, SOCK_DGRAM)
  104.     s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
  105.     if ttl >= 0:
  106.         s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, chr(ttl))
  107.  
  108.     frozen = 0
  109.  
  110.     while 1:
  111.  
  112.         if gl.qtest():
  113.             dev, val = gl.qread()
  114.             if dev in (DEVICE.ESCKEY, \
  115.                 DEVICE.WINSHUT, DEVICE.WINQUIT):
  116.                 break
  117.             if dev == DEVICE.WINFREEZE:
  118.                 frozen = 1
  119.             if dev == DEVICE.WINTHAW:
  120.                 frozen = 0
  121.             if dev == DEVICE.REDRAW:
  122.                 w, h = gl.getsize()
  123.                 x, y = gl.getorigin()
  124.                 if (w, h) <> (width, height):
  125.                     width, height = w, h
  126.                     lvi.resizevideo(width, height)
  127.                     lvo.resizevideo(width, height)
  128.  
  129.         rv = lvi.getnextpacket()
  130.         if not rv:
  131.             time.sleep(0.010)
  132.             continue
  133.  
  134.         pos, data = rv
  135.  
  136.         if not frozen:
  137.             lvo.putnextpacket(pos, data)
  138.  
  139.         hdr = struct.pack('hhh', pos, width, height)
  140.         for host in hosts:
  141.             try:
  142.                 s.sendto(hdr + data, (host, port))
  143.             except error, msg: # really socket.error
  144.                 if msg[0] <> 121: # no buffer space available
  145.                     raise error, msg # re-raise it
  146.                 print 'Warning:', msg[1]
  147.  
  148.     lvi.close()
  149.     lvo.close()
  150.  
  151.  
  152. main()
  153.