Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Memory error / setInterval () doesn't work
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
David Fang  
View profile  
 More options Nov 12 2012, 12:17 pm
From: David Fang <xiang.fa...@gmail.com>
Date: Mon, 12 Nov 2012 09:17:10 -0800 (PST)
Local: Mon, Nov 12 2012 12:17 pm
Subject: Memory error / setInterval () doesn't work

Hi,

We are writing a script that reads a large set of JPG files on our server
(infinite, since we have another process that keeps writing JPG files to
the same directory) and send them to users' browsers as an MJPEG stream at
 fixed time interval (variable "frameDelay" in code below). This is similar
to what an IP camera would do.

We found two separate issues with this seemingly simple script:
(1) the memory usage of this script keeps going up and always ends up being
killed by the system (Ubuntu);
(2) setInterval () doesn't  seem to work - the script sends JPG files at a
much faster rate than 1000/frameDealy (for example, when frameDelay is set
to 40, meaning 25 frames/sec, it actually sends about 70 frames/sec).

It's possible that they were caused by our mistakes in the code, but we
have inspected this seemingly simple script many many times ;-(  Therefore
I'm posting the code below.  Any comments / suggestions are greatly
appreciated!

===================================================
....(removed some code here)......

app.get('/stream', function (req, res) {

    res.writeHead(200, {
        'Content-Type':'multipart/x-mixed-replace;boundary="' + boundary +
'"',
        'Transfer-Encoding':'none',
        'Connection':'keep-alive',
        'Expires':'Fri, 01 Jan 1990 00:00:00 GMT',
        'Cache-Control':'no-cache, no-store, max-age=0, must-revalidate',
        'Pragma':'no-cache'
    });

    res.write(CRLF + "--" + boundary + CRLF);

    setInterval(function () {
        if(fileList.length<=1){
            fileList = fs.readdirSync(location).sort();
        }else{
            var fname = fileList.shift();
            if(fs.existsSync(location+fname)){
               var data = fs.readFileSync(location+fname);
                res.write('Content-Type:image/jpeg' + CRLF +
'Content-Length: ' + data.length + CRLF + CRLF);
                res.write(data);
                res.write(CRLF + '--' + boundary + CRLF);                  
                fs.unlinkSync(location+fname);
            }else{
                console.log("File doesn't find")
            }
        }
            console.log("new response:" + fname);
    }, frameDelay);

});

app.listen(port);
console.log("Server running at port " + port);
===========================================

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mscdex  
View profile   Translate to Translated (View Original)
 More options Nov 12 2012, 2:56 pm
From: mscdex <msc...@gmail.com>
Date: Mon, 12 Nov 2012 11:55:51 -0800 (PST)
Local: Mon, Nov 12 2012 2:55 pm
Subject: Re: Memory error / setInterval () doesn't work
On Nov 12, 12:17 pm, David Fang <xiang.fa...@gmail.com> wrote:

> It's possible that they were caused by our mistakes in the code, but we
> have inspected this seemingly simple script many many times ;-(  Therefore
> I'm posting the code below.  Any comments / suggestions are greatly
> appreciated!

Did you try using `req.connection.setNoDelay(true);` ?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »