RawDev.net - Developing developement developing developers.
Home - Files - Pastebin - Mail

Mjpg-Stream + libcaca

Saturday, March 22nd, 2008 by Hekos

After couple of hours, here is it: A libcaca extension to Mjpg-Stream

snapshot36.png
snapshot37.jpg

You need img2txt from the libcaca-utilities and /dev/shm to be mounted and writable (or change to another location)

/dev/shm is present on most systems and is a mounted filesystem located on the main ram.

The source is in past the page brake.


First, a header to change: /src/httpd.h: 114

typedef enum { A_UNKNOWN, A_SNAPSHOT, A_ASCII, A_STREAM, A_COMMAND, A_FILE } answer_t;

Next, a hook: /src/httpd.c: 720

case A_ASCII:
DBG("Request for ascii\n");
send_ascii(lcfd.fd);
break;

And now, the main function:

/******************************************************************************
Description.: Send a complete HTTP response and a ascii html
Input Value.: fildescriptor fd to send the answer to
Return Value: -
******************************************************************************/
void send_ascii(int fd) {
unsigned char *frame=NULL;
int frame_size=0,i;
char buffer[BUFFER_SIZE] = {0};  if ( (frame = (unsigned char *)malloc(MAX_FRAME_SIZE)) == NULL ) {
fprintf(stderr, "not enough memory\n");
exit(EXIT_FAILURE);
}
 
/* wait for a fresh frame */
 
pthread_cond_wait(&pglobal->db_update, &pglobal->db);
 
/* read buffer */  frame_size = pglobal->size;
memcpy(frame, pglobal->buf, frame_size);
DBG("got frame (size: %d kB)\n", frame_size/1024);
 
pthread_mutex_unlock( &pglobal->db );  /* make file */  FILE *fileascii;
fileascii = fopen("/dev/shm/asciipipe", "w");
fwrite(frame, 1, frame_size, fileascii);
 
fclose(fileascii);
free(frame);
 
system("/usr/bin/img2txt -W 100 -f html /dev/shm/asciipipe > /dev/shm/asciipipe2");
 
/* response */
sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
STD_HEADER \
"Content-type: text/html\r\n" \
"\r\n");
if( write(fd, buffer, strlen(buffer)) < 0 ) return;
/* read -> send file */
 
FILE *fileascii2;
fileascii2 = fopen("/dev/shm/asciipipe2", "r");
while (fgets(buffer,BUFFER_SIZE,fileascii2))
write(fd, buffer, strlen(buffer));
}

Tags: ,

This entry was posted on Saturday, March 22nd, 2008 at 5:18 pm under Hacking, Linux, Scripting.
You can follow any responses to this entry through the RSS 2.0 feed. No Responses.
You can leave a response, or trackback from your own site.

« Linux-UVC (USB Video Class) Zemanta »