|
| 1 | +#!/ufs/guido/bin/sgi/python3.3 |
| 2 | +from gl import * |
| 3 | +from GL import * |
| 4 | +from DEVICE import * |
| 5 | +import time |
| 6 | +import sys |
| 7 | +import getopt |
| 8 | +import socket |
| 9 | +import posix |
| 10 | +import vtime |
| 11 | + |
| 12 | +SYNCPORT = 10000 |
| 13 | +CTLPORT = 10001 |
| 14 | + |
| 15 | +VP_GBXORG = 0x1000001 |
| 16 | +VP_GBYORG = 0x1000002 |
| 17 | +VP_FBXORG = 0x1000003 |
| 18 | +VP_FBYORG = 0x1000004 |
| 19 | +VP_WIDTH = 0x1000005 |
| 20 | +VP_HEIGHT = 0x1000006 |
| 21 | + |
| 22 | +class Struct(): pass |
| 23 | +epoch = Struct() |
| 24 | + |
| 25 | +def getvideosize(): |
| 26 | + w = getvideo(VP_WIDTH) |
| 27 | + h = getvideo(VP_HEIGHT) |
| 28 | + print getvideo(VP_GBXORG), getvideo(VP_GBYORG) |
| 29 | + print getvideo(VP_FBXORG), getvideo(VP_FBYORG) |
| 30 | + x = 0 |
| 31 | + y = 0 |
| 32 | + return x,y,w,h |
| 33 | +def saveframe(f,x,y,w,h,pf, notime): |
| 34 | + readsource(SRC_FRONT) |
| 35 | + if pf: |
| 36 | + w = w/pf*pf |
| 37 | + h = h/pf*pf |
| 38 | + data = None |
| 39 | + data = lrectread(x,y,x+w-1,y+h-1) |
| 40 | + if pf: data = packrect(w,h,pf,data) |
| 41 | + if notime: t = 0 |
| 42 | + else: t = time.millitimer()-epoch.epoch |
| 43 | + f.write(`t` + ',' + `len(data)` + '\n') |
| 44 | + f.write(data) |
| 45 | + readsource(SRC_FRAMEGRABBER) |
| 46 | +def drawframe(x,y,w,h,col): |
| 47 | + drawmode(OVERDRAW) |
| 48 | + color(col) |
| 49 | + bgnline() |
| 50 | + 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) |
| 51 | + endline() |
| 52 | + drawmode(NORMALDRAW) |
| 53 | +def main(): |
| 54 | + foreground() |
| 55 | + pf = 2 |
| 56 | + ausync = 0 |
| 57 | + austart = 0 |
| 58 | + optlist, args = getopt.getopt(sys.argv[1:],'ca:s') |
| 59 | + for opt, arg in optlist: |
| 60 | + if opt = '-c': |
| 61 | + pf = 0 |
| 62 | + elif opt = '-a': |
| 63 | + ausync = 1 |
| 64 | + aumachine = arg |
| 65 | + elif opt = '-s': |
| 66 | + austart = 1 |
| 67 | + else: |
| 68 | + print 'Usage: camcorder [-c] [-a audiomachine [-s]]' |
| 69 | + sys.exit(1) |
| 70 | + if austart: |
| 71 | + if not ausync: |
| 72 | + print 'Cannot use -s without -a' |
| 73 | + sys.exit(1) |
| 74 | + print 'Starting audio recorder...' |
| 75 | + posix.system('rsh '+aumachine+' syncrecord '+socket.gethostname()+' &') |
| 76 | + if ausync: |
| 77 | + print 'Syncing to audio recorder...' |
| 78 | + globtime = vtime.VTime().init(1,aumachine,SYNCPORT) |
| 79 | + ctl = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
| 80 | + ctl.bind((socket.gethostname(),CTLPORT)) |
| 81 | + aua = (socket.gethostbyname(aumachine), CTLPORT) |
| 82 | + print 'Done.' |
| 83 | + vidx, vidy, w, h = getvideosize() |
| 84 | + prefsize(w,h) |
| 85 | + win = winopen('Camcorder') |
| 86 | + if len(args) > 1: |
| 87 | + f = open(args, 'w') |
| 88 | + else: |
| 89 | + f = open('film.video', 'w') |
| 90 | + w, h = getsize() |
| 91 | + realw, realh = w, h |
| 92 | + doublebuffer() |
| 93 | + RGBmode() |
| 94 | + gconfig() |
| 95 | + qdevice(LEFTMOUSE) |
| 96 | + qdevice(RKEY) |
| 97 | + qdevice(SKEY) |
| 98 | + qdevice(CKEY) |
| 99 | + qdevice(PKEY) |
| 100 | + qdevice(ESCKEY) |
| 101 | + inrunning = 1 |
| 102 | + outrunning = 0 |
| 103 | + stop = 'stop' |
| 104 | + readsource(SRC_FRAMEGRABBER) |
| 105 | + mousing = 0 |
| 106 | + epoch.epoch = time.millitimer() |
| 107 | + stoptime = epoch.epoch |
| 108 | + sizewritten = 0 |
| 109 | + x, y = realw/4, realh/4 |
| 110 | + w, h = w/2, h/2 |
| 111 | + drawframe(x,y,w,h,1) |
| 112 | + nframe = 0 |
| 113 | + try: |
| 114 | + num = 0 |
| 115 | + while 1: |
| 116 | + insingle = 0 |
| 117 | + outsingle = 0 |
| 118 | + if mousing: |
| 119 | + drawframe(x,y,w,h,0) |
| 120 | + ox, oy = getorigin() |
| 121 | + if sizewritten: |
| 122 | + x = getvaluator(MOUSEX)-ox |
| 123 | + y = getvaluator(MOUSEY)-oy |
| 124 | + else: |
| 125 | + w = getvaluator(MOUSEX)-x-ox |
| 126 | + h = getvaluator(MOUSEY)-y-oy |
| 127 | + drawframe(x,y,w,h,1) |
| 128 | + if qtest(): |
| 129 | + ev, val = qread() |
| 130 | + if ev = LEFTMOUSE and val = 1: |
| 131 | + drawframe(x,y,w,h,0) |
| 132 | + mousing = 1 |
| 133 | + ox, oy = getorigin() |
| 134 | + x = getvaluator(MOUSEX)-ox |
| 135 | + y = getvaluator(MOUSEY)-oy |
| 136 | + elif ev = LEFTMOUSE and val = 0: |
| 137 | + mousing = 0 |
| 138 | + if not sizewritten: |
| 139 | + f.write('CMIF video 1.0\n') |
| 140 | + f.write(`w,h,pf` + '\n') |
| 141 | + sizewritten = 1 |
| 142 | + if ev = RKEY and val = 1: |
| 143 | + if not inrunning: |
| 144 | + ringbell() |
| 145 | + else: |
| 146 | + outrunning = 1 |
| 147 | + wasstopped = time.millitimer() - stoptime |
| 148 | + epoch.epoch = epoch.epoch + wasstopped |
| 149 | + nframe = 0 |
| 150 | + starttime = time.millitimer() |
| 151 | + if ausync: |
| 152 | + ctl.sendto(`(1,starttime)`, aua) |
| 153 | + elif ev = PKEY and val = 1 and outrunning: |
| 154 | + outrunning = 0 |
| 155 | + stoptime = time.millitimer() |
| 156 | + if ausync: |
| 157 | + ctl.sendto(`(0,stoptime)`, aua) |
| 158 | + nf = nframe * 1000.0 / (time.millitimer()-starttime) |
| 159 | + drawmode(OVERDRAW) |
| 160 | + color(0) |
| 161 | + clear() |
| 162 | + color(1) |
| 163 | + cmov2i(5,5) |
| 164 | + charstr('Recorded ' + `nf` + ' frames/sec') |
| 165 | + drawmode(NORMALDRAW) |
| 166 | + elif ev = PKEY and val = 1 and not outrunning: |
| 167 | + outsingle = 1 |
| 168 | + elif ev = CKEY and val = 1: |
| 169 | + inrunning = 1 |
| 170 | + elif ev = SKEY and val = 1: |
| 171 | + if outrunning: |
| 172 | + ringbell() |
| 173 | + elif inrunning: |
| 174 | + inrunning = 0 |
| 175 | + else: |
| 176 | + insingle = 1 |
| 177 | + elif ev = ESCKEY: |
| 178 | + if ausync: |
| 179 | + ctl.sendto(`(2,time.millitimer())`, aua) |
| 180 | + raise stop |
| 181 | + if inrunning or insingle: |
| 182 | + rectcopy(vidx,vidy,realw,realh,0,0) |
| 183 | + swapbuffers() |
| 184 | + if outrunning or outsingle: |
| 185 | + nframe = nframe + 1 |
| 186 | + if not sizewritten: |
| 187 | + f.write('CMIF video 1.0\n') |
| 188 | + f.write(`w,h,pf` + '\n') |
| 189 | + sizewritten = 1 |
| 190 | + saveframe(f, x, y, w, h, pf, outsingle) |
| 191 | + except stop: |
| 192 | + pass |
| 193 | + drawmode(OVERDRAW) |
| 194 | + color(0) |
| 195 | + clear() |
| 196 | +# |
| 197 | +main() |
0 commit comments