3 # Receive live video UDP packets.
4 # Usage: Vreceive [port]
8 from socket
import * # syscalls and support functions
9 from SOCKET
import * # <sys/socket.h>
10 from IN
import * # <netinet/in.h>
14 sys
.path
.append('/ufs/guido/src/video')
19 from senddefs
import *
22 # Print usage message and exit(2).
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)'
33 # Main program: parse options and main loop.
37 sys
.stdout
= sys
.stderr
46 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'm:p:c:')
47 except getopt
.error
, msg
:
51 for opt
, optarg
in opts
:
53 port
= string
.atoi(optarg
)
55 group
= gethostbyname(optarg
)
58 except string
.atoi_error
, msg
:
59 usage('bad integer: ' + msg
)
61 s
= opensocket(group
, port
)
64 gl
.prefsize(width
, height
)
65 wid
= gl
.winopen('Vreceive')
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()]
77 selectargs
= (ifdlist
, ofdlist
, xfdlist
, timeout
)
83 if dev
in (DEVICE
.ESCKEY
, \
84 DEVICE
.WINSHUT
, DEVICE
.WINQUIT
):
86 if dev
== DEVICE
.REDRAW
:
89 data
= s
.recv(16*1024)
90 pos
, w
, h
= struct
.unpack('hhh', data
[:6])
91 if (w
, h
) <> (width
, height
):
94 gl
.winposition(x
, x
+w
-1, y
, y
+h
-1)
96 lvo
.resizevideo(width
, height
)
97 lvo
.putnextpacket(pos
, data
[6:])
99 x
= select
.select(selectargs
)
104 # Subroutine to create and properly initialize the receiving socket
106 def opensocket(group
, port
):
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
117 # Look up the group once
118 group
= gethostbyname(group
)
120 # Construct binary group address
121 group_bytes
= eval(regsub
.gsub('\.', ',', group
))
123 for byte
in group_bytes
: grpaddr
= (grpaddr
<< 8) | byte
125 # Construct struct mreq from grpaddr and ifaddr
127 mreq
= struct
.pack('ll', grpaddr
, ifaddr
)
129 # Add group membership
130 s
.setsockopt(IPPROTO_IP
, IP_ADD_MEMBERSHIP
, mreq
)