serviceWindowGenerationFromSRT.cpp 4.6 KB
#include "serviceWindowGenerationFromSRT.h"

#define PATH_LIBRAS "libras/video/"

using namespace std;

//Construtor Service 2
ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(
			char* path_video, char* path_srt, int sublanguage, int position, int size, int transparency, int _serviceType) {
	setPathInput(path_video, path_srt);
    setPosition(position);
    setSize(size);
    setTransparency(transparency);
    setSubLanguage(sublanguage);
	setServiceType(_serviceType);
    this->finish = false;
    DPRINTF("Done!\n");
}

//Construtor Service 5
ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* path_srt, int transparency, char* id, int _serviceType){    
	setPathInput(path_srt);
	setTransparency(transparency);
	setServiceType(_serviceType);

	string new_path_libras = PATH_LIBRAS;
	new_path_libras.append((string) id).append(".ts");
	char* pathl = new char[strlen(new_path_libras.c_str()) + 1];
	strcpy(pathl, (char*)new_path_libras.c_str());
	setPathLibras(pathl);
	finish_srt = false;
    DPRINTF("Done!\n");
}

ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() {
    if (mutex_serviceSRT) delete mutex_serviceSRT;
    delete extratorSRT;
    DDDPRINTF("Service SRT finalized!\n");
}

void ServiceWindowGenerationFromSRT::setPathInput(char* path_video, char* path_srt) {
    this->path_input = path_video;
    this->path_srt = path_srt;
}

void ServiceWindowGenerationFromSRT::setPathInput(char* path_srt){
    this->path_srt = path_srt;
}

void ServiceWindowGenerationFromSRT::notifyEnd(int sub_size) {
    DDPRINTF("Service SRT recebeu: %d legendas.\n", sub_size);
    setSizeOfSubtitles(sub_size);
}


void ServiceWindowGenerationFromSRT::notifySubtitle(unsigned char *legenda, int64_t pts){
    if (getRunningOption() == '3') {
        notificaSincronizadorSemTradutor(legenda, pts);
    } else {	
        adicionaPTS(pts);
        notificaTradutor(legenda);
    }
}

void ServiceWindowGenerationFromSRT::notificaSincronizadorSemTradutor(unsigned char * legenda, int64_t pts) {
    
    char* pch = strtok((char*) legenda, " ");

    while (pch != NULL) {
        string pch_string = (string) pch;
        sincronizador->recebeglosa(pch_string, pts);
        printf("%s\n", pch);
        pch = strtok(NULL, " ");
    }
    free(pch);

}

void ServiceWindowGenerationFromSRT::notificaTradutor(unsigned char* legenda) {

    const char* constchar = (const char*) legenda;
    char* legenda_copy = new char[strlen(constchar)+1];
    strcpy(legenda_copy, constchar);
    tradutor->traduz((unsigned char*) legenda_copy);
    free(legenda_copy);
}

void ServiceWindowGenerationFromSRT::initialize() {

    DDPRINTF("Service SRT Initialize.\n");
    
    if(serviceType == 2){

        /*
            Este serviço utiliza o arquivo de vídeo (em formato TS) do usuário para
            capturar as informações referente ao relógio (PCR) para calcular as
            etiquetas de apresentação (PTS) dos sinais em Libras.
        */

	    inputfile = new InputFile(this->path_input);	    
	    extratorSRT = new ExtratorSRT();	    
	    monitor_pcr_base = new MonitorPCR();

	    monitor_pcr_base->addListenerPCRBase(extratorSRT);
		
	    inputfile->registraOuvinte(monitor_pcr_base);
	    extratorSRT->addListener(this);
	    extratorSRT->setFilePath((char*) path_srt);
	    
	    ServiceWindowGeneration::initialize();
	    
        try{
            extratorSRT->initialize();  
            inputfile->initialize();                                
        }catch(ExtratorSrtException ex){
            throw ServiceException(ex.getMessage());
        }catch(InputException ex){
            throw ServiceException(ex.getMessage());
        }     
		this->Start();

    } else{

        /**
            Este serviço utiliza apenas o arquivo de legendas (SRT) como entrada,
            portanto, não é preciso monitorar as informações do PCR a partir do
            objeto InputFile().
        */
	    
	    extratorSRT = new ExtratorSRT();	    
	    monitor_pcr_base = new MonitorPCR();
	    monitor_pcr_base->addListenerPCRBase(extratorSRT);		
	    extratorSRT->addListener(this);
	    extratorSRT->setFilePath((char*) path_srt);
	    
	    ServiceWindowGeneration::initialize();

	    extratorSRT->notifyPCRBase((uint64_t) 1000);
	    try{
            extratorSRT->initialize();            
        }catch(ExtratorSrtException ex){
            throw ServiceException(ex.getMessage());
        }
        this->Start();
    }

}

bool ServiceWindowGenerationFromSRT::finished() {
    return finish_srt;
}

void ServiceWindowGenerationFromSRT::Run() {
    while (isRunning()) {
        usleep(200000); //200ms
    }
    finish_srt = true;
}