improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Demo / sgi / video / camcorder.py
blob9b7618c436bb88c466011f509e492cc5c905f885
1 from gl import *
2 from GL import *
3 from DEVICE import *
4 import time
5 import sys
6 import getopt
7 import socket
8 import posix
9 import vtime
11 # Preallocation parameter
12 PREALLOC = 4 # Megabyte
14 # Sync audio parameters
15 SYNCPORT = 10000
16 CTLPORT = 10001
18 from vpregs import *
20 class Struct(): pass
21 epoch = Struct()
23 def getvideosize():
24 w = getvideo(VP_WIDTH)
25 h = getvideo(VP_HEIGHT)
26 print 'WIDTH,HEIGHT:', w, h
27 print 'GB{X,Y}ORG:', getvideo(VP_GBXORG), getvideo(VP_GBYORG)
28 print 'FB{X,Y}ORG:', getvideo(VP_FBXORG), getvideo(VP_FBYORG)
29 x = 0
30 y = 0
31 return x,y,w,h
33 framelist = []
35 def prealloc(w, h):
36 nbytes = w*h*4
37 limit = PREALLOC*1024*1024
38 total = 0
39 list = []
40 print 'Prealloc to', PREALLOC, 'Megabytes...'
41 while total+nbytes <= limit:
42 list.append('x'*nbytes)
43 total = total + nbytes
44 print 'Done.'
46 def grabframe(f,x,y,w,h,pf):
47 readsource(SRC_FRONT)
48 if pf:
49 w = w/pf*pf
50 h = h/pf*pf
51 data = lrectread(x,y,x+w-1,y+h-1)
52 t = time.millitimer()-epoch.epoch
53 framelist.append(data, t)
54 readsource(SRC_FRAMEGRABBER)
56 def saveframes(f, w, h, pf):
57 for data, t in framelist:
58 if pf:
59 w = w/pf*pf
60 h = h/pf*pf
61 data = packrect(w,h,pf,data)
62 f.write(`t` + ',' + `len(data)` + '\n')
63 f.write(data)
64 framelist[:] = []
66 def saveframe(f,x,y,w,h,pf, notime):
67 readsource(SRC_FRONT)
68 if pf:
69 w = w/pf*pf
70 h = h/pf*pf
71 data = lrectread(x,y,x+w-1,y+h-1)
72 if pf: data = packrect(w,h,pf,data)
73 if notime: t = 0
74 else: t = time.millitimer()-epoch.epoch
75 f.write(`t` + ',' + `len(data)` + '\n')
76 f.write(data)
77 readsource(SRC_FRAMEGRABBER)
79 def drawframe(x,y,w,h,col):
80 drawmode(OVERDRAW)
81 color(col)
82 bgnline()
83 v2i(x-1,y-1) ; v2i(x+w,y-1); v2i(x+w,y+h); v2i(x-1,y+h); v2i(x-1,y-1)
84 endline()
85 drawmode(NORMALDRAW)
87 def usage():
88 sys.stderr.write('Usage: camcorder ' + \
89 '[-c] [-p packfactor] [-a audiomachine [-s]] [outputfile]\n')
90 sys.exit(2)
92 def wrheader(f, w, h, pf):
93 f.write('CMIF video 1.0\n')
94 f.write(`w,h,pf` + '\n')
95 print 'width,height,pf:', w, h, pf,
96 if pf == 0: pf = 4
97 print '(i.e.,', w*h*pf, 'bytes/frame)'
99 def main():
100 foreground()
101 pf = 2
102 ausync = 0
103 austart = 0
104 optlist, args = getopt.getopt(sys.argv[1:],'ca:sp:')
105 for opt, arg in optlist:
106 if opt == '-c':
107 pf = 0
108 elif opt == '-a':
109 ausync = 1
110 aumachine = arg
111 elif opt == '-s':
112 austart = 1
113 elif opt == '-p':
114 pf = int(eval(arg))
115 else:
116 usage()
117 if args:
118 if len(args) > 1:
119 print 'Too many arguments'
120 usage()
121 filename = args[0]
122 else:
123 filename = 'film.video'
124 if austart:
125 if not ausync:
126 print 'Cannot use -s without -a'
127 usage()
128 print 'Starting audio recorder...'
129 posix.system('rsh '+aumachine+' syncrecord '+socket.gethostname()+' &')
130 if ausync:
131 print 'Syncing to audio recorder...'
132 globtime = vtime.VTime().init(1,aumachine,SYNCPORT)
133 ctl = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
134 ctl.bind((socket.gethostname(),CTLPORT))
135 aua = (socket.gethostbyname(aumachine), CTLPORT)
136 print 'Done.'
137 vidx, vidy, w, h = getvideosize()
138 #prefsize(w,h)
139 winx, winy = 1280-w-10, 1024-h-30
140 prefposition(winx,winx+w-1,winy,winy+h-1)
141 win = winopen(filename)
142 f = open(filename, 'w')
143 w, h = getsize()
144 realw, realh = w, h
145 ####doublebuffer()
146 RGBmode()
147 gconfig()
148 qdevice(LEFTMOUSE)
149 qdevice(RKEY)
150 qdevice(SKEY)
151 qdevice(CKEY)
152 qdevice(PKEY)
153 qdevice(ESCKEY)
154 qdevice(WINQUIT)
155 qdevice(WINSHUT)
156 inrunning = 1
157 outrunning = 0
158 stop = 'stop'
159 readsource(SRC_FRAMEGRABBER)
160 mousing = 0
161 epoch.epoch = time.millitimer()
162 stoptime = epoch.epoch
163 sizewritten = 0
164 x, y = realw/4, realh/4
165 w, h = w/2, h/2
166 prealloc(w, h)
167 try:
168 drawframe(x,y,w,h,1)
169 nframe = 0
170 num = 0
171 while 1:
172 insingle = 0
173 outsingle = 0
174 if mousing:
175 drawframe(x,y,w,h,0)
176 ox, oy = getorigin()
177 if sizewritten:
178 x = getvaluator(MOUSEX)-ox
179 y = getvaluator(MOUSEY)-oy
180 else:
181 w = getvaluator(MOUSEX)-x-ox
182 h = getvaluator(MOUSEY)-y-oy
183 drawframe(x,y,w,h,1)
184 if qtest() or \
185 not (mousing or inrunning or insingle or outrunning or outsingle):
186 ev, val = qread()
187 if ev == LEFTMOUSE and val == 1:
188 drawframe(x,y,w,h,0)
189 mousing = 1
190 ox, oy = getorigin()
191 x = getvaluator(MOUSEX)-ox
192 y = getvaluator(MOUSEY)-oy
193 elif ev == LEFTMOUSE and val == 0:
194 if h < 0:
195 y, h = y+h, -h
196 if w < 0:
197 x, w = x+w, -w
198 mousing = 0
199 if not sizewritten:
200 wrheader(f, w, h, pf)
201 sizewritten = 1
202 prealloc(w, h)
203 elif ev == RKEY and val == 1:
204 if not inrunning:
205 ringbell()
206 else:
207 outrunning = 1
208 wasstopped = time.millitimer() - stoptime
209 epoch.epoch = epoch.epoch + wasstopped
210 nframe = 0
211 starttime = time.millitimer()
212 if ausync:
213 ctl.sendto(`(1,starttime)`, aua)
214 elif ev == PKEY and val == 1 and outrunning:
215 outrunning = 0
216 stoptime = time.millitimer()
217 if ausync:
218 ctl.sendto(`(0,stoptime)`, aua)
219 fps = nframe * 1000.0 / (time.millitimer()-starttime)
220 print 'Recorded', nframe,
221 print 'frames at', 0.1*int(fps*10),'frames/sec'
222 print 'Saving...'
223 saveframes(f, w, h, pf)
224 print 'Done.'
225 elif ev == PKEY and val == 1 and not outrunning:
226 outsingle = 1
227 elif ev == CKEY and val == 1:
228 inrunning = 1
229 elif ev == SKEY and val == 1:
230 if outrunning:
231 ringbell()
232 elif inrunning:
233 inrunning = 0
234 else:
235 insingle = 1
236 elif ev in (ESCKEY, WINQUIT, WINSHUT):
237 if ausync:
238 ctl.sendto(`(2,time.millitimer())`, aua)
239 raise stop
240 elif ev == REDRAW:
241 drawframe(x,y,w,h,0)
242 reshapeviewport()
243 drawframe(x,y,w,h,1)
244 if inrunning or insingle:
245 if outrunning:
246 rectcopy(vidx+x,vidy+y,vidx+x+w-1,vidy+y+h-1,x,y)
247 else:
248 rectcopy(vidx,vidy,vidx+realw-1,vidx+realh-1,0,0)
249 ####swapbuffers()
250 if outrunning or outsingle:
251 nframe = nframe + 1
252 if not sizewritten:
253 wrheader(f, w, h, pf)
254 sizewritten = 1
255 if outrunning:
256 grabframe(f, x, y, w, h, pf)
257 else:
258 saveframe(f, x, y, w, h, pf, outsingle)
259 except stop:
260 pass
261 finally:
262 drawmode(OVERDRAW)
263 color(0)
264 clear()
266 main()