Reading from socket file handle took too long

E

Etienne Desautels

Hi,

I'm working on project where I need to grab video from an Axis IP
camera. This camera send a stream of a multipart message on HTTP. I
write this code (at the bottom of the message) to read and uncompress
each jpeg images. I call nextFrame() 24 times per seconds (or every
0,0416 sec.) to read the next image that arrived.

Everything works well but one thing. My problem is that running
nextFrame() took, most of the time, more then 0,0414 sec. so my video
is lagging. I monitor every call and I found that the culprit is when I
read from the socket file handle. It's the only bottleneck in my code.
I try different approaches to the problem but I always hit the same
problem.

I don't think it's normal that read() take more then 0,04 sec to read
1000 bytes from memory (the socket file handle). How can I avoid this
problem ?

I'm on Linux 2.6 (last Ubuntu) with python 2.4 on P4 2.3.

Thanks

Etienne


CODE
------------------------------------------------------------------------
----------------------------

class Axis:
url = ""
user = "user"
password = "password"
header = {}
header["Authorization"] = "Basic " + b64encode(user + ":" + password)
http = urllib2.HTTPHandler()
state = 0
limit = 1000
imageData = None
sizeX = 352
sizeY = 240
fps = 24
compression = 25

def startGrab(self):
try:
url = self.url + "/axis-cgi/mjpg/video.cgi?resolution=" +
str(self.sizeX) \
+ "x" + str(self.sizeY) + "&compression=" \
+ str(self.compression) + "&req_fps=" + str(self.fps)
req = urllib2.Request(url, None, self.header)
self.fh = self.http.http_open(req)
self.buffer = ""
self.state = 1
except:
self.state = 0

def stopGrab(self):
self.fh.close()
self.state = 0

def nextFrame(self):
if self.state:
try:
temp = self.fh.read(self.limit) # <-- TAKE A LOT OF TIME
self.buffer += temp
boundary = self.buffer.find("\r\nContent-Type: image/jpeg\r\n") + 30
if boundary >= 30:
end = self.buffer.find("\r\n\r\n--myboundary", boundary) + 2
if end >= 2:
image = Image.open(StringIO(self.buffer[boundary:end]))
self.imageData = image.tostring("raw",image.mode,0,-1)
self.buffer = self.buffer[end:]

except:
pass
 
B

Ben Sizer

Etienne said:
Everything works well but one thing. My problem is that running
nextFrame() took, most of the time, more then 0,0414 sec. so my video
is lagging. I monitor every call and I found that the culprit is when I
read from the socket file handle. It's the only bottleneck in my code.

If you're sure this is the problem, you might try calling setSockOpt on
the socket to set the nodelay flag, something like this:

mySocket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)

I don't know enough about the option to be sure it'll work, but it
can't hurt to try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top