Vreceive.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Demo » sgi » video » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Demo » sgi » video » Vreceive.py
#! /usr/bin/env python

# Receive live video UDP packets.
# Usage: Vreceive [port]

import sys
import struct
from socket import *# syscalls and support functions
fromSOCKET*# 
fromIN*# 
select
import struct
import gl, GL, DEVICE
sys.path.append('/ufs/guido/src/video')
import LiveVideoOut
import regsub
import getopt

from senddefs import *


# Print usage message and exit(2).

def usage(msg):
  print msg
  print 'usage: Vreceive [-m mcastgrp] [-p port] [-c type]'
  print '-m mcastgrp: multicast group (default ' + `DEFMCAST` + ')'
  print '-p port    : port (default ' + `DEFPORT` + ')'
  print '-c type    : signal type: rgb8, grey or mono (default rgb8)'
  sys.exit(2)


# Main program: parse options and main loop.

def main():

  sys.stdout = sys.stderr

  group = DEFMCAST
  port = DEFPORT
  width = DEFWIDTH
  height = DEFHEIGHT
  vtype = 'rgb8'

  try:
    opts, args = getopt.getopt(sys.argv[1:], 'm:p:c:')
  except getopt.error, msg:
    usage(msg)

  try:
    for opt, optarg in opts:
      if opt == '-p':
        port = string.atoi(optarg)
      if opt == '-m':
        group = gethostbyname(optarg)
      if opt == '-c':
        vtype = optarg
  except string.atoi_error, msg:
    usage('bad integer: ' + msg)

  s = opensocket(group, port)

  gl.foreground()
  gl.prefsize(width, height)
  wid = gl.winopen('Vreceive')
  gl.winconstraints()
  gl.qdevice(DEVICE.ESCKEY)
  gl.qdevice(DEVICE.WINSHUT)
  gl.qdevice(DEVICE.WINQUIT)

  lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)

  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:
        lvo.reshapewindow()
    elif s.avail():
      data = s.recv(16*1024)
      pos, w, h = struct.unpack('hhh', data[:6])
      if (w, h) <> (width, height):
        x, y =  gl.getorigin()
        y = y + height - h
        gl.winposition(x, x+w-1, y, y+h-1)
        width, height = w, h
        lvo.resizevideo(width, height)
      lvo.putnextpacket(pos, data[6:])
    else:
      x = select.select(selectargs)

  lvo.close()


# 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)

  # Bind the port to it
  s.bind('', port)

  # Look up the group once
  group = gethostbyname(group)

  # Construct binary group address
  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


main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.