= MAX_STALL_TIME) { exit; } } else { // Output $buffersecs of silence to start out. $did_stall = 1; output_silence_packets($buffersecs * GSM_PACKETS_PER_SECOND); } } if (DEBUG) { global $stderr; fwrite($stderr, "Found file: $filename\n"); } $size = filesize($filename); // Only want to handle complete packets. if ($size % GSM_PACKET_LENGTH) { $size -= ($size % GSM_PACKET_LENGTH); } $seekpos = $size - ($buffersecs * GSM_PACKETS_PER_SECOND); if ($seekpos < 0) { if ($did_stall) { // Just start at the beginning of the file, since there's // plenty of silence in the pipeline. $seekpos = 0; } else { // Need to buffer some silence first to have // a full $buffersecs of silence. output_silence_packets((-$seekpos) / GSM_PACKET_LENGTH); } } $fp = fopen($filename, 'r'); if (DEBUG) { global $stderr; fwrite($stderr, "Seek pos: $seekpos\n"); } $totalbytes = 0; if ($seekpos > 0) { // Need to output a full $buffersecs worth of // file audio. fseek($fp, $seekpos); $totalbytes = $seekpos; $readbytes = fread($fp, $buffersecs * GSM_PACKETS_PER_SECOND * GSM_PACKET_LENGTH); $totalbytes += strlen($readbytes); echo $readbytes; } else if ($seekpos < 0) { // Just output the first $size bytes immediately. $readbytes = fread($fp, $size); $totalbytes += strlen($readbytes); echo $readbytes; } // Now just parcel out the data as it becomes available. $no_data_time = 0; while(1) { // Check for eof. clearstatcache(); $fsize = filesize($filename); if ($totalbytes >= $fsize) { if (DEBUG) { global $stderr; fwrite($stderr, "totalbytes >= fsize: $totalbytes >= $fsize\n"); } if ($no_data_time >= NO_DATA_TIMEOUT) { break; } $no_data_time++; sleep(1); continue; } $no_data_time = 0; // Get at most ECHO_RATE GSM packets per output. $readbytes = fread($fp, ECHO_RATE * GSM_PACKET_LENGTH); if (strlen($readbytes) == 0) { continue; } $totalbytes += strlen($readbytes); // Debugging code. if (DEBUG) { global $stderr; fwrite($stderr, "Output bytes: $totalbytes\n"); } echo $readbytes; } // End of file reached. Close it and finish. fclose($fp); // Debugging code. if (DEBUG) { global $stderr; fwrite($stderr, "File ended: $filename\n"); } } $g_silence = pack("C33", 0xd8, 0x20, 0xa2, 0xe1, 0x5a, 0x50, 0x00, 0x49, 0x24, 0x92, 0x49, 0x24, 0x50, 0x00, 0x49, 0x24, 0x92, 0x49, 0x24, 0x50, 0x00, 0x49, 0x24, 0x92, 0x49, 0x24, 0x50, 0x00, 0x49, 0x24, 0x92, 0x49, 0x24); function output_silence_packets($num_packets) { global $g_silence; for ($i = 0; $i < $num_packets; $i++) { echo $g_silence; } } function filespec_find($filespec) { // This ls command sorts by modification time, newest to oldest $filelist = trim(`/bin/ls -t $filespec`); $filearray = preg_split("/\s/", $filelist, -1, PREG_SPLIT_NO_EMPTY); if (count($filearray) == 0) { return ""; } else { return $filearray[0]; } } ?>