Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba06615

Browse files
committed
New modules LiveVideo{In,Out} (interfaces will change!).
New programs V{send,receive} to send/receive video over UDP. Comment typo changed in Vaddcache.
1 parent 42e07af commit ba06615

5 files changed

Lines changed: 320 additions & 1 deletion

File tree

Demo/sgi/video/LiveVideoIn.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Live video input class.
2+
# Note that importing this module attempts to initialize video.
3+
4+
5+
# Check if video is available.
6+
# There are three reasons for failure here:
7+
# (1) this version of Python may not have the sv or imageop modules;
8+
# (2) this machine may not have a video board;
9+
# (3) initializing the video board may fail for another reason.
10+
# The global variable have_video is set to true iff we reall do have video.
11+
12+
try:
13+
import sv
14+
import SV
15+
import imageop
16+
try:
17+
v = sv.OpenVideo()
18+
have_video = 1
19+
except sv.error:
20+
have_video = 0
21+
except ImportError:
22+
have_video = 0
23+
24+
25+
# The live video input class.
26+
# Only instantiate this if have_video is true!
27+
28+
class LiveVideoIn:
29+
30+
# Initialize an instance.
31+
# Parameters:
32+
# - vw, vh specify the size of the video window.
33+
# This initializes continuous capture.
34+
35+
def init(self, pktmax, vw, vh):
36+
if not have_video:
37+
raise RuntimeError, 'no video available'
38+
realvw = vh*SV.PAL_XMAX/SV.PAL_YMAX
39+
if realvw < vw:
40+
print 'Funny, image too narrow...'
41+
self.realwidth, self.realheight = v.QuerySize(realvw, vh)
42+
##print 'Recording video in size', \
43+
## self.realwidth, self.realheight
44+
self.width = vw
45+
self.height = vh
46+
self.x0 = (self.realwidth-self.width)/2
47+
self.x1 = self.x0 + self.width - 1
48+
self.y0 = (self.realheight-self.height)/2
49+
self.y1 = self.y0 + self.height - 1
50+
# Compute # full lines per packet
51+
self.lpp = pktmax / self.width
52+
self.pktsize = self.lpp*self.width
53+
##print 'lpp =', self.lpp, '; pktsize =', self.pktsize
54+
# Initialize capture
55+
v.SetSize(self.realwidth, self.realheight)
56+
dummy = v.InitContinuousCapture(SV.RGB8_FRAMES, \
57+
self.realwidth, self.realheight, 2, 5)
58+
self.data = None
59+
self.lpos = 0
60+
return self
61+
62+
# Remove an instance.
63+
# This turns off continuous capture.
64+
65+
def close(self):
66+
v.EndContinuousCapture()
67+
68+
# Get the next video packet.
69+
# This returns (lpos, data) where:
70+
# - lpos is the line position
71+
# - data is a piece of data
72+
# The dimensions of data are:
73+
# - pixel depth = 1 byte
74+
# - scan line width = self.width (the vw argument to init())
75+
# - number of scan lines = self.lpp (PKTMAX / vw)
76+
77+
def getnextpacket(self):
78+
if not self.data:
79+
try:
80+
cd, id = v.GetCaptureData()
81+
except sv.error:
82+
return None
83+
data = cd.InterleaveFields(1)
84+
cd.UnlockCaptureData()
85+
self.data = imageop.crop(data, 1, \
86+
self.realwidth, \
87+
self.realheight, \
88+
self.x0, self.y0, \
89+
self.x1, self.y1)
90+
self.lpos = 0
91+
data = self.data[:self.pktsize]
92+
self.data = self.data[self.pktsize:]
93+
lpos = self.lpos
94+
self.lpos = self.lpos + self.lpp
95+
return lpos, data

Demo/sgi/video/LiveVideoOut.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Live video output (display video on the screen, presumably from the net)
2+
3+
import gl
4+
from VFile import Displayer
5+
6+
7+
# Video output (displayer) class.
8+
9+
class LiveVideoOut:
10+
11+
def init(self, wid, xywh, vw, vh):
12+
##print 'Init', wid, xywh
13+
##print 'video', vw, vw
14+
self.vw = vw
15+
self.vh = vh
16+
self.disp = Displayer().init()
17+
info = ('rgb8', vw, vh, 1, 8, 0, 0, 0, 0)
18+
self.disp.setinfo(info)
19+
self.wid = wid
20+
oldwid = gl.winget()
21+
gl.winset(wid)
22+
self.disp.initcolormap()
23+
self.resize(xywh)
24+
gl.winset(oldwid)
25+
return self
26+
27+
def resize(self, (x, y, w, h)):
28+
oldwid = gl.winget()
29+
gl.winset(self.wid)
30+
##print 'Resize', x, y, w, h
31+
gl.winposition(x, x+w-1, y, y+h-1)
32+
gl.reshapeviewport()
33+
if w < self.vw or h < self.vh:
34+
self.toosmall = 1
35+
else:
36+
self.disp.xorigin = (w-self.vw)/2
37+
self.disp.yorigin = (h-self.vh)/2
38+
self.toosmall = 0
39+
##print 'VIDEO OFFSET:', \
40+
## self.disp.xorigin, self.disp.yorigin
41+
self.disp.clear()
42+
gl.winset(oldwid)
43+
44+
def putnextpacket(self, pos, data):
45+
if self.toosmall:
46+
return
47+
oldwid = gl.winget()
48+
gl.winset(self.wid)
49+
nline = len(data)/self.vw
50+
if nline*self.vw <> len(data):
51+
print 'Incorrect-sized video fragment'
52+
self.disp.showpartframe(data, None, (0, pos, self.vw, nline))
53+
gl.winset(oldwid)
54+
55+
def close(self):
56+
print 'Done video out'

Demo/sgi/video/Vaddcache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# Options:
1212
#
13-
# file ... : file(s) to inspect; default film.video
13+
# file ... : file(s) to modify; default film.video
1414

1515

1616
import sys

Demo/sgi/video/Vreceive.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/ufs/guido/bin/sgi/python-405
2+
3+
# Receive live video UDP packets.
4+
# Usage: Vreceive [port]
5+
6+
import sys
7+
import struct
8+
from socket import *
9+
import select
10+
import gl, GL, DEVICE
11+
sys.path.append('/ufs/guido/src/video')
12+
import LiveVideoOut
13+
14+
PKTMAX = 16*1024
15+
WIDTH = 400
16+
HEIGHT = 300
17+
HOST = ''
18+
PORT = 5555
19+
20+
def main():
21+
22+
port = PORT
23+
if sys.argv[1:]:
24+
port = eval(sys.argv[1])
25+
26+
width, height = WIDTH, HEIGHT
27+
28+
gl.foreground()
29+
gl.prefsize(width, height)
30+
wid = gl.winopen('Vreceive')
31+
gl.qdevice(DEVICE.ESCKEY)
32+
gl.qdevice(DEVICE.WINSHUT)
33+
gl.qdevice(DEVICE.WINQUIT)
34+
35+
x, y = gl.getorigin()
36+
lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \
37+
width, height)
38+
39+
s = socket(AF_INET, SOCK_DGRAM)
40+
s.bind(HOST, port)
41+
42+
ifdlist = [gl.qgetfd(), s.fileno()]
43+
ofdlist = []
44+
xfdlist = []
45+
timeout = 1.0
46+
selectargs = (ifdlist, ofdlist, xfdlist, timeout)
47+
48+
while 1:
49+
50+
if gl.qtest():
51+
dev, val = gl.qread()
52+
if dev in (DEVICE.ESCKEY, \
53+
DEVICE.WINSHUT, DEVICE.WINQUIT):
54+
break
55+
if dev == DEVICE.REDRAW:
56+
gl.clear()
57+
elif s.avail():
58+
data = s.recv(16*1024)
59+
pos, w, h = struct.unpack('hhh', data[:6])
60+
if (w, h) <> (width, height):
61+
x, y = gl.getorigin()
62+
y = y + height - h
63+
width, height = w, h
64+
lvo.close()
65+
lvo = LiveVideoOut.LiveVideoOut() \
66+
.init(wid, (x, y, width, height), \
67+
width, height)
68+
lvo.putnextpacket(pos, data[6:])
69+
else:
70+
x = select.select(selectargs)
71+
72+
lvo.close()
73+
74+
main()

Demo/sgi/video/Vsend.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/ufs/guido/bin/sgi/python-405
2+
3+
# Send live video UDP packets.
4+
# Usage: Vsend [host [port]]
5+
6+
import sys
7+
import time
8+
import struct
9+
from socket import *
10+
import gl, GL, DEVICE
11+
sys.path.append('/ufs/guido/src/video')
12+
import LiveVideoIn
13+
import LiveVideoOut
14+
15+
PKTMAX_UCAST = 16*1024 - 6
16+
PKTMAX_BCAST = 1450
17+
WIDTH = 400
18+
HEIGHT = 300
19+
HOST = '<broadcast>'
20+
PORT = 5555
21+
22+
def main():
23+
if not LiveVideoIn.have_video:
24+
print 'Sorry, no video (use python-405 on roos)'
25+
sys.exit(1)
26+
27+
host = HOST
28+
port = PORT
29+
if sys.argv[1:]:
30+
host = sys.argv[1]
31+
if sys.argv[2:]:
32+
port = eval(sys.argv[2])
33+
34+
if host == '<broadcast>':
35+
pktmax = PKTMAX_BCAST
36+
else:
37+
pktmax = PKTMAX_UCAST
38+
39+
gl.foreground()
40+
gl.prefsize(WIDTH, HEIGHT)
41+
wid = gl.winopen('Vsend')
42+
gl.keepaspect(WIDTH, HEIGHT)
43+
gl.stepunit(8, 6)
44+
gl.winconstraints()
45+
gl.qdevice(DEVICE.ESCKEY)
46+
gl.qdevice(DEVICE.WINSHUT)
47+
gl.qdevice(DEVICE.WINQUIT)
48+
width, height = gl.getsize()
49+
50+
x, y = gl.getorigin()
51+
lvo = LiveVideoOut.LiveVideoOut().init(wid, (x, y, width, height), \
52+
width, height)
53+
54+
lvi = LiveVideoIn.LiveVideoIn().init(pktmax, width, height)
55+
56+
s = socket(AF_INET, SOCK_DGRAM)
57+
s.allowbroadcast(1)
58+
59+
while 1:
60+
61+
if gl.qtest():
62+
dev, val = gl.qread()
63+
if dev in (DEVICE.ESCKEY, \
64+
DEVICE.WINSHUT, DEVICE.WINQUIT):
65+
break
66+
if dev == DEVICE.REDRAW:
67+
w, h = gl.getsize()
68+
x, y = gl.getorigin()
69+
if (w, h) <> (width, height):
70+
lvi.close()
71+
width, height = w, h
72+
lvi = LiveVideoIn.LiveVideoIn() \
73+
.init(pktmax, width, height)
74+
lvo.close()
75+
lvo = LiveVideoOut.LiveVideoOut() \
76+
.init(wid, \
77+
(x, y, width, height), \
78+
width, height)
79+
80+
rv = lvi.getnextpacket()
81+
if not rv:
82+
time.millisleep(10)
83+
continue
84+
85+
pos, data = rv
86+
lvo.putnextpacket(pos, data)
87+
88+
hdr = struct.pack('hhh', pos, width, height)
89+
s.sendto(hdr + data, (host, port))
90+
91+
lvi.close()
92+
lvo.close()
93+
94+
main()

0 commit comments

Comments
 (0)