You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

233 lines
6.3 KiB

<?php
/*
* Globais e Constantes.
*/
/*
* Log do sistema
*/
$pathLog = "/var/log/asterisk/cnvrtd.log";
/*
*
* Funcoes para conversao de audio.
*/
function CnvrtWavToMp3($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_WAV_MP3, $timeOut);
}
function CnvrtWavToUlaw($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_WAV_ULAW, $timeOut);
}
function CnvrtMp3ToUlaw($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_MP3_ULAW, $timeOut);
}
/*
* Funcoes para arquivos.
*/
function CnvrtFileCp($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_FILE_CP, $timeOut);
}
function CnvrtFileMv($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_FILE_MV, $timeOut);
}
function CnvrtFileRm($src, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, '', CNVRT_FILE_RM, $timeOut);
}
function CnvrtFileExists($src, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, '', CNVRT_FILE_EXISTS, $timeOut);
}
function CnvrtFileSize($src, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, '', CNVRT_FILE_SIZE, $timeOut);
}
function CnvrtFileTarGz($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_FILE_TAR_GZ, $timeOut);
}
function CnvrtFileTarBz2($src, $dst, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, $dst, CNVRT_FILE_TAR_BZ2, $timeOut);
}
function CnvrtFileExec($src, $timeOut = CNVRT_TIMEOUT) {
return CnvrtMonitor($src, '', CNVRT_EXEC, $timeOut);
}
/*
* Funcoes do servico.
*/
function CnvrtMonitor($src, $dst, $cnvrtType, $timeOut = 3) {
$result = false;
$timeSend = 0;
$sleepTime = 1000; // loop usleep 0,001
$filename = time();
$sendName = sprintf('%s_%s.snd', CNVRT_BASE, $filename);
$procName = sprintf('%s_%s.prc', CNVRT_BASE, $filename);
$sendData = array($src, $dst, $cnvrtType, $procName);
try {
/*
* Envia informações de troca;
*/
CnvrtEncodeData($sendData, $sendName);
/*
* Aguarda retorno das informações;
*/
while ($timeOut > (int)($timeSend / 1000000)) {
if (file_exists($procName) && ($result = CnvrtDecodeData($procName, true, true)) !== false) {
list($st, $msg) = $result;
/*
* Apaga o arquivo ja processado.
*/
CnvrtKillProcess($procName, $src, false);
if (!$st) {
GeraExcept($msg);
}
return $st;
}
usleep($sleepTime);
$timeSend += $sleepTime;
}
/*
* Mata processsos.
*/
CnvrtKillProcess($procName, $src);
/*
* Timeout.
*/
GeraExcept("Não foi possível converter o arquivo, tempo excedido![$src]");
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_NOTICE);
return false;
}
}
function CnvrtEncodeData($data, $fileName = null, $silent = false) {
try {
if (!$data = base64_encode(serialize($data))) {
GeraExcept("Não foi possível codificar informações de conversão!");
}
if (is_null($fileName)) {
return $data;
}
if (!file_put_contents($fileName, $data)) {
GeraExcept("Não foi possível enviar informações de conversão!");
}
return true;
} catch (Exception $e) {
if ($silent) {
trigger_error($e->getMessage(), E_USER_NOTICE);
return false;
}
GeraExcept($e->getMessage());
}
}
function CnvrtDecodeData($dataSource, $file = true, $silent = false) {
try {
/*
* Tenta ler o arquivo.
*/
if (!$data = ($file ? file_get_contents($dataSource) : $dataSource)) {
GeraExcept("Não foi possível ler as informações de conversão!");
}
if (!$data = base64_decode($data)) {
GeraExcept("Não foi possível decodificar as informações de conversão!");
}
if (!$data = unserialize($data)) {
GeraExcept("Não foi possível recuperar as informações de conversão!");
}
/*
* Verifica se o retorno é um array, caso seja e $onlyValus = true
* retorna apenas os valores, para faciliatar o uso da funcao list.
*/
return $data;
} catch (Exception $e) {
if ($silent) {
trigger_error($e->getMessage(), E_USER_NOTICE);
return false;
}
GeraExcept($e->getMessage());
}
}
function CnvrtLog($log) {
global $pathLog;
/*
* Registra no arquivo de log do sistema.
*/
WriteLog(trim($log) . "\n", $pathLog);
}
function CnvrtKillProcess($procName, $src, $killProc = true) {
$pidFile = sprintf( "%s.pid", $procName );
if ( $killProc ) {
/* Esse arquivo é gerado pelo daemon cnvrt */
$pidFile_content = file_get_contents( $pidFile );
if( $pidFile_content !== false ){
exec( sprintf("kill -9 %s", $pidFile_content) );
}
else{ // Envia log, não foi possível usar cnvrt
CnvrtLog( "Não foi possivel obter o pid em $pidFile" );
}
$pid = CnvrtProcessId( 'ffmpeg', $src );
if($pid != 0){ // Diferente de zero, o zero terminará o processo PHP
exec( sprintf( "kill -9 %s", $pid ));
}
}
unlink( $procName );
unlink( $pidFile );
}
function CnvrtProcessId($process, $srcId) {
$out = null;
exec(sprintf('ps x| grep %s', $srcId), $out);
foreach ($out as $ret) {
if (strpos($ret, $process) !== false) {
return soNumero(substr($ret, 0, strpos($ret, ' ')));
}
}
return 0;
}
function _CnvrtWirteData($data, $prcName, $dst) {
CnvrtEncodeData($data, $tmpFile = sprintf("%s.tmp", $prcName));
if ($dst) {
exec(sprintf('chown pbx:pbx %s', $dst));
}
exec(sprintf('chown pbx:pbx %s', $tmpFile));
exec(sprintf('mv %s %s', $tmpFile, $prcName));
}
function _CnvrtPrepareData($status, $msg, $out = '', $ret = '') {
return array($status, $msg, !$out ? ($status ? 'OK' : 'ER') : $out, !$ret ? ($status ? 'OK' : 'ER') : $ret);
}
function _CnvrtRegPid($prcName) {
$tmpFile = sprintf("%s.pid", $prcName);
file_put_contents($tmpFile, getmypid());
exec(sprintf('chown pbx:pbx %s', $tmpFile));
}