Friday, May 4, 2012

Pushing Images to a Server

Once the images are generated by the Kinect and ffmpeg, we need to push them to a server before they can be read by any other application. For each image that ffmpeg generates, we initially calculate the size the image (in is the File pointer).

fseek (in , 0 , SEEK_END);
int lSize = ftell (in);
rewind (in);
We then send this to the server so that it knows how many bytes it needs to read before it writes it to the file system.

sprintf(Buffer, "%d", lSize);
send(s, Buffer, sizeof(Buffer), 0);
Now we need to send the actual image to the server. We read MAX_BUF bytes from the currently opened file into the Buffer and send it over the socket to the server. If the remaining bytes in the file are less than MAX_BUF, fread fails, so we need to keep track of how many bytes were read and how many more need to be read. If the remaining bytes are less than MAX_BUF, we read and send only the remaining number of bytes to the server. 
while (1) {
bzero(Buffer, sizeof(Buffer));
len = fread(Buffer,sizeof(Buffer),1, in);
if(len <= 0) { 
break; 
}
send(s, Buffer, sizeof(Buffer), 0);
sz+= len*MAX_BUF;
int rem = lSize - sz;
if (rem > 0 && rem < MAX_BUF) {
len = fread(Buffer, rem, 1, in);
send(s, Buffer, rem, 0);
sz+= len*MAX_BUF;
break; 
}
}

No comments:

Post a Comment