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


//Construtor Service 1
ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* path_video, char* path_srt, int sublanguage, 
        int position, int size, int transparency, char* id, char* client_type, int serviceType) {

	extrator_factory = new ExtratorFactory();
    setPathInput(path_video);
    setPathSRT(path_srt);
    setClientType(client_type);
    setPosition(position);
    setSize(size);
    setTransparency(transparency);
    setSubLanguage(sublanguage);
	setServiceType(serviceType);
	setUserId(id);
    finish = false;
    DPRINTF("Done!\n");
}

//Construtor Service 4
ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* path_srt, int transparency, char* id, char* client_type, int serviceType) {    

    extrator_factory = new ExtratorFactory();
    setPathInput(path_srt);
    setClientType(client_type);
	setTransparency(transparency);
	setServiceType(serviceType);
	setUserId(id);
    finish = false;
    DPRINTF("Done!\n");
}

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

void ServiceWindowGenerationFromSRT::notifyTranslator(unsigned char* subtitle) {

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

void ServiceWindowGenerationFromSRT::notifySubtitle(unsigned char *subtitle, int64_t pts){
        adicionaPTS(pts);
        notifyTranslator(subtitle);    
}

bool ServiceWindowGenerationFromSRT::isFinished(){
    return finish;
}

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

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

void ServiceWindowGenerationFromSRT::initialize() {

    DDPRINTF("Service SRT Initialize.\n");
    
    //codigo abaixo sera arrumado apos a aplicação do factory pattern

    if(serviceType == 1){
	    
	    extratorSRT = (ExtratorSRT*)extrator_factory->getExtrator(SRT);
	    extratorSRT->addListener(this);
	    extratorSRT->setFilePath(path_srt);
	    
	    ServiceWindowGeneration::initialize();
	    
        try{                               
            extratorSRT->initialize();  
        }catch(ExtratorException 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 = (ExtratorSRT*)extrator_factory->getExtrator(SRT);	    		
	    extratorSRT->addListener(this);
	    extratorSRT->setFilePath(path_input);
	    
	    ServiceWindowGeneration::initialize();

	    try{
            extratorSRT->initialize();            
        }catch(ExtratorException ex){
            throw ServiceException(ex.getMessage());
        }
        this->Start();
    }

}

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