serviceWindowGenerationFromRec.cpp 6.32 KB
#include "serviceWindowGenerationFromRec.h"

ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec(
	char* pathVideo, int pos, int size, int transp, char* id, char* client, int serviceType, char* rate){

	this->path_input = pathVideo;
	this->position = pos;
	this->size = size;
	this->transparency = transp;
	this->user_id = id;
	// this->client_type = client;
	this->service_type = serviceType;
	numero_legendas = INT_MAX;
    legendas_enviadas = 0;
	vetor_pts = new vector<int64_t >();
	rec = new Recognize(pathVideo, id, rate);
	tradutor = new TradutorPortGlosa();
	running = true;
	finish = false;
	PRINTL(util::_DEBUG, "Service Rec Done!\n");
}

ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec(
	char* pathVideo, int pos, int size, int transp, char* id, int mode, int serviceType){

	this->path_input = pathVideo;
	this->position = pos;
	this->size = size;
	this->transparency = transp;
	this->user_id = id;
	this->exec_mode = mode;
	this->service_type = serviceType;
	numero_legendas = INT_MAX;
    legendas_enviadas = 0;
	vetor_pts = new vector<int64_t >();
    rec = new Recognize(path_input, id);
	tradutor = new TradutorPortGlosa();
	try{
        setPathContents();
    }catch(RuntimeException ex){
        throw ServiceException(ex.getMessage());
    }
	running = true;
	finish = false;
	PRINTL(util::_DEBUG, "Service Rec Done!\n");
}

ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){
	free(vetor_pts);
	if (tradutor) delete tradutor;
	if (renderer) delete renderer;
	if (rec) delete rec;
    PRINTL(util::_DEBUG, "Service Rec finished!\n");
}

void ServiceWindowGenerationFromRec::setPathContents(){
	switch(exec_mode) {
        case DEVELOPER:
        {
            char* vStorage;
            char* vUploads;
            vStorage = getenv("VLSTORAGE");
            vUploads = getenv("VLUPLOADS");

            if(vStorage != NULL && vUploads != NULL){
                this->path_contents = vStorage;
                this->path_uploads = vUploads;
                PRINTL(util::_DEBUG, "Paths definidos pelo desenvolvedor:\n %s\n%s\n", path_contents, path_uploads);
            }else{
                this->path_contents = (char*) PATH_DEVEL_CONTENTS;
                this->path_uploads  = (char*) PATH_DEVEL_UPLOADS;
            }
            rec->setPathAudioContents(path_uploads);
        }break;

        case PRODUCTION:
        {
            ifstream conf_file(PATH_CONF_FILE, ifstream::binary);
            parsingSuccessful = reader.parse(conf_file, root);

            if(parsingSuccessful) {
                string attr = "vlibras_user/";
                attr += root.get("storage", PATH_VBOX_UPLOADS).asString();
                this->path_contents = new char[MAX_SIZE_PATH];
                strcpy(this->path_contents, attr.c_str());
                this->path_uploads = (char*) PATH_VBOX_UPLOADS;
                rec->setPathAudioContents(path_uploads);
            }else{
                conf_file.close();
                Logging::instance()->writeLog("Erro com a leitura do arquivo params.json");
                throw RuntimeException("Fail to parsing params.json");
            }
            conf_file.close();
        }break;

        case TESTER:
        {
            this->path_contents = (char*) PATH_TESTER_CONTENTS;
            this->path_uploads  = (char*) PATH_TESTER_UPLOADS;
            rec->setPathAudioContents(path_uploads);
        }break;

        default:
            throw ServiceException("Invalid execution mode!");
    }
}

void ServiceWindowGenerationFromRec::setPathLibras() {
	string final_path = "";
    path_libras = new char[MAX_SIZE_PATH];

    if(this->service_type == SERVICE_TYPE_REC)
        final_path.append(this->path_uploads).append("/").append(this->user_id);
    else
        final_path.append(this->path_contents);

    final_path.append("/").append(this->user_id).append(".mp4");
    strcpy(this->path_libras, final_path.c_str());
}

void ServiceWindowGenerationFromRec::setSizeOfSubtitles(int sub_size){
	numero_legendas = sub_size;
    if (legendas_enviadas >= numero_legendas){
    	try{
            renderer->initialize();
        }catch(lavidlib::RuntimeException &ex){
            throw ServiceException(ex.getMessage().c_str());
        }
    }
}

void ServiceWindowGenerationFromRec::addPTS(int64_t pts){
	vetor_pts->push_back(pts);
}

void ServiceWindowGenerationFromRec::notifyTranslator(unsigned char* text){
	tradutor->traduz(text);
}

//Metodo utilizado pelo reconhecedor para avisar sobre novas sentenças reconhecidas
void ServiceWindowGenerationFromRec::notifyTextRecognized(unsigned char* text, int64_t pts){
	addPTS(pts);
	notifyTranslator(text);
}

void ServiceWindowGenerationFromRec::notifyTranslation(char* glosa) {
	string sGlosa(reinterpret_cast<char*>(glosa));
    try{
        renderer->receiveGlosa(sGlosa, vetor_pts->front());
        legendas_enviadas++;
    }catch(lavidlib::RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }
	vetor_pts->erase(vetor_pts->begin());      
}

void ServiceWindowGenerationFromRec::notifyEndOfRenderization() {    
	if(this->service_type == SERVICE_TYPE_REC){
        mixer = new Mixer(this->path_input, this->path_libras, this->size, this->position,
         this->transparency, this->user_id, this->path_uploads, this->path_contents);
        mixer->initialize();
        delete mixer;
    }
    running = false;
}

void ServiceWindowGenerationFromRec::notifyEnd(int sentences_size){
    PRINTL(util::_DEBUG, "Service REC recebeu: %d sentenças.\n", sentences_size);
    try{
        setSizeOfSubtitles(sentences_size);
    }catch(ServiceException &ex) {
        finish = true;
    }
}

bool ServiceWindowGenerationFromRec::isRunning(){
	return this->running;
}

bool ServiceWindowGenerationFromRec::isFinished(){
	return this->finish;
}

void ServiceWindowGenerationFromRec::initialize(){
    PRINTL(util::_DEBUG, "Service REC Initialize.\n");
	setPathLibras();
	rec->addListener(this);
	tradutor->addListener(this);

	renderer = new Renderer(this->path_libras ,this->user_id);
	renderer->addListener(this);
    
    try{
        rec->initialize();
    } catch(RecognizeException ex){
        throw ServiceException(ex.getMessage());
    } catch(RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }  
	this->Start();
}

void ServiceWindowGenerationFromRec::Run(){
	while(isRunning()){
		usleep(200000);
	}
	finish = true;	
}