Vsend.py 3.43 KB
Newer Older
1
#! /usr/bin/env python
2 3

# Send live video UDP packets.
4 5
# Usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-w width]
#              [host] ..
6 7 8 9

import sys
import time
import struct
10
import string
11
from socket import *
12
from SOCKET import *
13 14 15 16
import gl, GL, DEVICE
sys.path.append('/ufs/guido/src/video')
import LiveVideoIn
import LiveVideoOut
17
import SV
18 19 20 21 22 23 24
import getopt
from IN import *

from senddefs import *

def usage(msg):
	print msg
25
	print 'usage: Vsend [-b] [-h height] [-p port] [-s size] [-t ttl] [-c type] [-m]',
26 27 28 29 30 31 32
	print '[-w width] [host] ...'
	print '-b        : broadcast on local net'
	print '-h height : window height (default ' + `DEFHEIGHT` + ')'
	print '-p port   : port to use (default ' + `DEFPORT` + ')'
	print '-t ttl    : time-to-live (multicast only; default 1)'
	print '-s size   : max packet size (default ' + `DEFPKTMAX` + ')'
	print '-w width  : window width (default ' + `DEFWIDTH` + ')'
33
	print '-c type   : Type: rgb8, mono or grey (default rgb8)'
34 35 36
	print '[host] ...: host(s) to send to (default multicast to ' + \
		DEFMCAST + ')'
	sys.exit(2)
37 38 39


def main():
40 41 42 43 44 45 46 47
	sys.stdout = sys.stderr

	hosts = []
	port = DEFPORT
	ttl = -1
	pktmax = DEFPKTMAX
	width = DEFWIDTH
	height = DEFHEIGHT
48
	vtype = 'rgb8'
49 50

	try:
51
		opts, args = getopt.getopt(sys.argv[1:], 'bh:p:s:t:w:c:')
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	except getopt.error, msg:
		usage(msg)

	try:
		for opt, optarg in opts:
			if opt == '-p':
				port = string.atoi(optarg)
			if opt == '-b':
				host = '<broadcast>'
			if opt == '-t':
				ttl = string.atoi(optarg)
			if opt == '-s':
				pktmax = string.atoi(optarg)
			if opt == '-w':
				width = string.atoi(optarg)
			if opt == '-h':
				height = string.atoi(optarg)
69 70
			if opt == '-c':
				vtype = optarg
71 72 73 74 75 76 77 78 79
	except string.atoi_error, msg:
		usage('bad integer: ' + msg)

	for host in args:
		hosts.append(gethostbyname(host))

	if not hosts:
		hosts.append(gethostbyname(DEFMCAST))

80
	if not LiveVideoIn.have_video:
81
		print 'Sorry, no video available (use python-405)'
82 83 84
		sys.exit(1)

	gl.foreground()
85 86
	gl.prefsize(width, height)
	gl.stepunit(8, 6)
87
	wid = gl.winopen('Vsend')
88
	gl.keepaspect(width, height)
89
	gl.stepunit(8, 6)
90
	gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
91 92 93 94
	gl.winconstraints()
	gl.qdevice(DEVICE.ESCKEY)
	gl.qdevice(DEVICE.WINSHUT)
	gl.qdevice(DEVICE.WINQUIT)
95 96
	gl.qdevice(DEVICE.WINFREEZE)
	gl.qdevice(DEVICE.WINTHAW)
97 98
	width, height = gl.getsize()

99
	lvo = LiveVideoOut.LiveVideoOut(wid, width, height, vtype)
100

101
	lvi = LiveVideoIn.LiveVideoIn(pktmax, width, height, vtype)
102 103

	s = socket(AF_INET, SOCK_DGRAM)
104
	s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
105 106
	if ttl >= 0:
		s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, chr(ttl))
107 108

	frozen = 0
109 110 111 112 113 114 115 116

	while 1:

		if gl.qtest():
			dev, val = gl.qread()
			if dev in (DEVICE.ESCKEY, \
				DEVICE.WINSHUT, DEVICE.WINQUIT):
				break
117 118 119 120
			if dev == DEVICE.WINFREEZE:
				frozen = 1
			if dev == DEVICE.WINTHAW:
				frozen = 0
121 122 123 124 125
			if dev == DEVICE.REDRAW:
				w, h = gl.getsize()
				x, y = gl.getorigin()
				if (w, h) <> (width, height):
					width, height = w, h
126
					lvi.resizevideo(width, height)
127
					lvo.resizevideo(width, height)
128 129 130

		rv = lvi.getnextpacket()
		if not rv:
131
			time.sleep(0.010)
132 133 134
			continue

		pos, data = rv
135 136 137

		if not frozen:
			lvo.putnextpacket(pos, data)
138 139

		hdr = struct.pack('hhh', pos, width, height)
140 141 142 143 144 145 146
		for host in hosts:
			try:
				s.sendto(hdr + data, (host, port))
			except error, msg: # really socket.error
				if msg[0] <> 121: # no buffer space available
					raise error, msg # re-raise it
				print 'Warning:', msg[1]
147 148 149 150

	lvi.close()
	lvo.close()

151

152
main()