Hi,
The program below grabs frames from an MJPEG camera stuffs them in a circular buffer and displays them with a predefined delay.
While visually this behaves as expected the problem I have is that memory usage continuously increases while the program runs and eventually stops when it reaches the limit.
The IPcapture library example grabs the frames by reading the cam1 instance directly as so: buffer[x] = cam1;
But I've had to change this slightly because it would always show the last frame regardless of where in the buffer I was reading from. My guess is that assigning this way is similar to using a pointer to the actual IPcapture object and not a copy of its image data. So I did this instead: buffer[x] = cam1.get();
This works as expected making a copy of the frame and storing it in the PImage buffer. The problem I have then is that it seems to be making a copy of the whole IPcapture object and not just the image data taking up more memory every time I copy it to the buffer.
Is this a problem with the way the library works or am I not doing this properly? Any input appreciated. Thanks!
import ipcapture.*;
IPCapture cam1;
// Circular buffer
PImage[] cam1_buffer;
// This determines the size of the circular buffe
int nFrames = 500;
// These are used to iterate through the buffer while writing and reading
// the read counter should always be at least one frame ahead of the write counter
int iWrite = 0, iRead = 1;
void setup() {
fullScreen();
// Set the cam's URL and start it
cam1 = new IPCapture(this, "http://" + "192.168.0.251:88/cgi-bin/CGIStream.cgi?cmd=GetMJStream&usr=root&pwd=root", "root", "root");
cam1.start();
cam1.pixelWidth = 640;
cam1.pixelHeight = 480;
// Declare a frame buffer sized accordingly to our desired number of frames
cam1_buffer = new PImage[nFrames];
}
void draw() {
// Gets a frame from the camera and store it in our buffer
if (cam1.isAvailable()) {
cam1.read();
cam1_buffer[iWrite] = cam1.get();
// Reads a frame from our buffer
if(cam1_buffer[iRead] == null){
// Display this text if the buffer isn't full yet.
clear();
fill(255);
text("BUFFERING " + str(iWrite) +" / " + str(nFrames),300,10);
}
else{
// Here we display our delayed frames
image(cam1_buffer[iRead],0,0);
}
// Increment write and read counters
iWrite++;
iRead++;
// Start writing over the begining of our buffer every time we reach the end
if(iRead >= nFrames-1){
iRead = 0;
}
if(iWrite >= nFrames-1){
iWrite = 0;
}
}
}