- Got rid of newmodule.c
[python/dscho.git] / Demo / sgi / video / Vreceive.py
blob84aa5a531b3ec299595d802c8ea562019e856e5d
1 #! /usr/bin/env python
3 # Receive live video UDP packets.
4 # Usage: Vreceive [port]
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
19 from senddefs import *
22 # Print usage message and exit(2).
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)
33 # Main program: parse options and main loop.
35 def main():
37 sys.stdout = sys.stderr
39 group = DEFMCAST
40 port = DEFPORT
41 width = DEFWIDTH
42 height = DEFHEIGHT
43 vtype = 'rgb8'
45 try:
46 opts, args = getopt.getopt(sys.argv[1:], 'm:p:c:')
47 except getopt.error, msg:
48 usage(msg)
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)
61 s = opensocket(group, port)
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)
71 lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)
73 ifdlist = [gl.qgetfd(), s.fileno()]
74 ofdlist = []
75 xfdlist = []
76 timeout = 1.0
77 selectargs = (ifdlist, ofdlist, xfdlist, timeout)
79 while 1:
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)
101 lvo.close()
104 # Subroutine to create and properly initialize the receiving socket
106 def opensocket(group, port):
108 # Create the socket
109 s = socket(AF_INET, SOCK_DGRAM)
111 # Allow multiple copies of this program on one machine
112 s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed)
114 # Bind the port to it
115 s.bind('', port)
117 # Look up the group once
118 group = gethostbyname(group)
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
125 # Construct struct mreq from grpaddr and ifaddr
126 ifaddr = INADDR_ANY
127 mreq = struct.pack('ll', grpaddr, ifaddr)
129 # Add group membership
130 s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
132 return s
135 main()