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

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

	this->path_input = pathVideo;
	this->sub_language = sublanguage;
	this->position = pos;
	this->size = size;
	this->transparency = transp;
	this->user_id = id;
	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();
	renderer = new Renderer();
	running = true;
	finish = false;
	DPRINTF("Done!\n");
}

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

	this->path_input = pathVideo;
	this->sub_language = sublanguage;
	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(path_input, id);
	tradutor = new TradutorPortGlosa();
	renderer = new Renderer();
	try{
        setPathContents();
    }catch(RuntimeException ex){
        throw ServiceException(ex.getMessage());
    }
	running = true;
	finish = false;
	DPRINTF("Done!\n");
}

ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){
	free(vetor_pts);
	if (tradutor) delete tradutor;
	if (renderer) delete renderer;
	if (rec) delete rec;
	if (mixer) delete mixer;
	DDDPRINTF("Service Rec finished!\n");
}

void ServiceWindowGenerationFromRec::setPathContents(){
	if(strcmp(client_type,DEVELOPER) == 0){
		this->path_contents = PATH_DEVEL_CONTENTS;
		this->path_uploads = PATH_DEVEL_UPLOADS;
		rec->setPathAudioContents(path_uploads);
	}else if(strcmp(client_type, PRODUCTION) == 0){
		ifstream conf_file(PATH_CONF_FILE, ifstream::binary);
        parsingSuccessful = reader.parse(conf_file, root);
        if(!parsingSuccessful){
        	conf_file.close();
            throw ServiceException("Fail to parsing param.json");
        }
        string 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 = PATH_VBOX_UPLOADS;
		rec->setPathAudioContents(path_uploads);
		conf_file.close();
	}else{
		throw ServiceException("Invalid client!");
	}
}

void ServiceWindowGenerationFromRec::setSizeOfSubtitles(int sub_size){
	numero_legendas = sub_size;
    if (legendas_enviadas >= numero_legendas){
    	try{
            renderer->finalize();
        }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(vector<string> * glosas) {
	string glosa = "";
	for (int i = 0; i < glosas->size(); i++) {
        locale loc;
        string glosa_lower = "";
        for (int k = 0; k < glosas->at(i).length(); k++){
            glosa_lower += std::tolower(glosas->at(i).at(k), loc);
        }
		glosa += glosa_lower;
        glosa += " ";
        
    }
    int64_t pts_notificado = vetor_pts->front();
    while(renderer->isSending())
        usleep(10000);
    try{
        renderer->receiveGlosa(glosa, pts_notificado);
        legendas_enviadas++;
    }catch(lavidlib::RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }
	vetor_pts->erase(vetor_pts->begin());
    legendas_enviadas++;       
}

void ServiceWindowGenerationFromRec::notifyEndOfRenderization() {
    running = false;
}

void ServiceWindowGenerationFromRec::notifyEnd(int sentences_size){
	DPRINTF("Service REC recebeu: %d sentenças.\n", sentences_size);
	setSizeOfSubtitles(sentences_size);
}

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

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

void ServiceWindowGenerationFromRec::initialize(){
	DPRINTF("Service REC Initialize.\n");

	rec->addListener(this);
	tradutor->addListener(this);
	renderer->addListener(this);
    
    try{
    	renderer->Start();
        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;	
}