Reply To: Serving a growing MP3 file?

#17169
Anonymous
Inactive

Well, I found a simple solution that doesn’t involve Firefly, or any other server software (except ssh), and may be of interest of others, so I’ll post it here.

I wrote a small C program called growcat, see below, that can be used in the following way:

(ssh server.name.com “/path/to/growcat /path/to/file/to/stream.mp3”) >localfile.mp3

The code of the program, is:


/*
* Read a file and output to standard out. If the file ends,
* don't terminate, but instead hang, waiting for new input.
*/
#include
#include

main(int argc, const char* argv[]) {
/*
* The first arg, if any, is expected to be the input file name
* if there are no arguments, stdin is used as the input file.
*/
FILE* infile = stdin;
if (argc > 0) {
const char* filename = argv[1];
infile = fopen(filename, "r");
}

/*
* Loop forever, wait for a while when the end of the input
* file is reached.
*/
int bytecount = 0;
int oldbytecount = 0;
while(1) {
int c = fgetc(infile);
while (c != EOF) {
++bytecount;
fputc(c, stdout);
c = fgetc(infile);
}
/* EOF reached, first check if it has stopped growing */
if (oldbytecount == bytecount) {
/* file has stopped growing and EOF is reached. Terminating! */
exit(0);
}
/* File is still growing, wait 10s and try again */
sleep(10);
oldbytecount = bytecount;
}
}