3 # Send live video UDP packets.
4 # Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
15 sys
.path
.append('/ufs/guido/src/video')
22 from senddefs
import *
26 print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-c type] [-m]',
27 print '[-w width] [host] ...'
28 print '-b : broadcast on local net'
29 print '-h height : window height (default ' + `DEFHEIGHT`
+ ')'
30 print '-p port : port to use (default ' + `DEFPORT`
+ ')'
31 print '-t ttl : time-to-live (multicast only; default 1)'
32 print '-s size : max packet size (default ' + `DEFPKTMAX`
+ ')'
33 print '-S size : use this packet size/window size'
34 print '-w width : window width (default ' + `DEFWIDTH`
+ ')'
35 print '-v : print packet rate'
36 print '-x xpos : set x position'
37 print '-y ypos : set y position'
38 print '[host] ...: host(s) to send to (default multicast to ' + \
44 sys
.stdout
= sys
.stderr
57 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'bh:p:s:S:t:w:vx:y:')
58 except getopt
.error
, msg
:
62 for opt
, optarg
in opts
:
64 port
= string
.atoi(optarg
)
68 ttl
= string
.atoi(optarg
)
70 pktmax
= string
.atoi(optarg
)
71 vidmax
= SV
.PAL_XMAX
*SV
.PAL_YMAX
77 factor
= float(vidmax
)/float(pktmax
)
78 factor
= math
.sqrt(factor
)
79 width
= int(SV
.PAL_XMAX
/factor
)-7
80 height
= int(SV
.PAL_YMAX
/factor
)-5
81 print 'video:',width
,'x',height
,
82 print 'pktsize',width
*height
,'..',
85 pktmax
= string
.atoi(optarg
)
87 width
= string
.atoi(optarg
)
89 height
= string
.atoi(optarg
)
95 xpos
= string
.atoi(optarg
)
97 ypos
= string
.atoi(optarg
)
98 except string
.atoi_error
, msg
:
99 usage('bad integer: ' + msg
)
102 hosts
.append(gethostbyname(host
))
105 hosts
.append(gethostbyname(DEFMCAST
))
108 gl
.prefsize(width
, height
)
110 wid
= gl
.winopen('Vsend')
111 gl
.keepaspect(width
, height
)
113 gl
.maxsize(SV
.PAL_XMAX
, SV
.PAL_YMAX
)
115 gl
.qdevice(DEVICE
.ESCKEY
)
116 gl
.qdevice(DEVICE
.WINSHUT
)
117 gl
.qdevice(DEVICE
.WINQUIT
)
118 gl
.qdevice(DEVICE
.WINFREEZE
)
119 gl
.qdevice(DEVICE
.WINTHAW
)
120 width
, height
= gl
.getsize()
122 lvo
= LiveVideoOut
.LiveVideoOut(wid
, width
, height
, vtype
)
124 lvi
= DisplayVideoIn
.DisplayVideoIn(pktmax
, width
, height
, vtype
)
127 lvi
.positionvideo(xpos
, ypos
)
129 s
= socket(AF_INET
, SOCK_DGRAM
)
130 s
.setsockopt(SOL_SOCKET
, SO_BROADCAST
, 1)
132 s
.setsockopt(IPPROTO_IP
, IP_MULTICAST_TTL
, chr(ttl
))
136 lasttime
= int(time
.time())
141 dev
, val
= gl
.qread()
142 if dev
in (DEVICE
.ESCKEY
, \
143 DEVICE
.WINSHUT
, DEVICE
.WINQUIT
):
145 if dev
== DEVICE
.WINFREEZE
:
147 if dev
== DEVICE
.WINTHAW
:
149 if dev
== DEVICE
.REDRAW
:
151 x
, y
= gl
.getorigin()
152 if (w
, h
) <> (width
, height
):
154 lvi
.resizevideo(width
, height
)
155 lvo
.resizevideo(width
, height
)
157 rv
= lvi
.getnextpacket()
163 print pos
, len(data
) # DBG
166 lvo
.putnextpacket(pos
, data
)
168 hdr
= struct
.pack('hhh', pos
, width
, height
)
171 # print len(hdr+data) # DBG
172 s
.sendto(hdr
+ data
, (host
, port
))
173 except error
, msg
: # really socket.error
174 if msg
[0] <> 121: # no buffer space available
175 raise error
, msg
# re-raise it
176 print 'Warning:', msg
[1]
177 if pos
== 0 and verbose
:
179 if int(time
.time()) <> lasttime
:
180 print nframe
/ (time
.time()-lasttime
), 'fps'
182 lasttime
= int(time
.time())