function stream() { global $cfg, $db; $track_id = get('track_id'); $stream_id = (int) get('stream_id'); $sid = get('sid'); $hash = get('hash'); if ($stream_id != -1 && isset($cfg['encode_extension'][$stream_id]) == false) { header('HTTP/1.1 400 Bad Request'); exit; } if ($hash) { authenticateStream(); } else { authenticateShareStream(); } $query = mysql_query('SELECT LOWER(SUBSTRING_INDEX(relative_file, ".", -1)) AS extension, track.artist, title, album, album.year, disc, discs, number, relative_file, mime_type, miliseconds, filesize, filemtime, audio_bitrate FROM track, album WHERE track_id = "' . mysql_real_escape_string($track_id) . '" AND track.album_id = album.album_id'); $track = mysql_fetch_assoc($query); $file = $cfg['media_dir'] . $track['relative_file']; if (sourceFile($track['extension'], $track['audio_bitrate'], $stream_id)) { // Stream from source streamFile($file, $track['mime_type']); } elseif ($cache = cacheGetFile($track_id, $stream_id)) { // Stream from cache cacheUpdateTag($track_id, $stream_id, $cache); streamFile($cache, $cfg['encode_mime_type'][$stream_id]); } else { // Real time transcode stream ini_set('zlib.output_compression', 'off'); ini_set('max_execution_time', 0); if (file_exists(NJB_HOME_DIR . '-')) { @unlink(NJB_HOME_DIR . '-'); } $cmd = $cfg['decode_stdout'][$track['extension']] . ' | ' . $cfg['encode_stdout'][$stream_id]; $cmd = str_replace('%source', escapeCmdArg($file), $cmd); header('Accept-Ranges: none'); header('Content-Type: ' . $cfg['encode_mime_type'][$stream_id]); if (@passthru($cmd) == false) { header('HTTP/1.1 500 Internal Server Error'); exit; } } }
function transcode($track_id, $profile) { global $cfg, $db; $query = mysql_query('SELECT LOWER(SUBSTRING_INDEX(relative_file, ".", -1)) AS extension, relative_file, miliseconds, filesize, audio_bitrate FROM track WHERE track_id = "' . mysql_real_escape_string($track_id) . '"'); $track = mysql_fetch_assoc($query); $source = $cfg['media_dir'] . $track['relative_file']; if (sourceFile($track['extension'], $track['audio_bitrate'], $profile)) { // Return source file return $source; } elseif ($file = cacheGetFile($track_id, $profile)) { cacheUpdateTag($track_id, $profile, $file); return $file; } else { // Return transcoded file $cache_dir = cacheGetDir($track_id, $profile); $pathinfo = pathinfo($track['relative_file']); $destination = $pathinfo['filename']; $destination = $cache_dir . $destination . '.' . $cfg['encode_extension'][$profile]; // Transcode if (NJB_WINDOWS) { $cmd = $cfg['decode_stdout'][$track['extension']] . ' | ' . $cfg['encode_file'][$profile]; } else { $cmd = $cfg['decode_stdout'][$track['extension']] . ' 2>&1 | ' . $cfg['encode_file'][$profile] . ' 2>&1'; } $cmd = str_replace('%source', escapeCmdArg($source), $cmd); $cmd = str_replace('%destination', escapeCmdArg($destination), $cmd); $cmd_output = array(); $cmd_return = 0; @exec($cmd, $cmd_output, $cmd_return); if ($cmd_return != 0) { message(__FILE__, __LINE__, 'error', '[b]Exec error[/b][br][b]Command:[/b] ' . $cmd . '[br][b]System output:[/b] ' . implode('[br]', $cmd_output) . '[br][b]System return code:[/b] ' . $cmd_return); } if (is_file($destination) == false) { message(__FILE__, __LINE__, 'error', '[b]Destination file not created[/b][br]File: ' . $destination . '[br]Command: ' . $cmd); } cacheUpdateTag($track_id, $profile, $destination); return $destination; } }