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

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

	this->path_input = pathVideo;
	this->path_srt = pathSRT;
	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 >();
	extrator_factory = new ExtratorFactory();
	try{
        setPathContents();
    }catch(RuntimeException ex){
        throw ServiceException(ex.getMessage());
    }
	running = true;
	finish = false;
	DPRINTF("Done!\n");
}

ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* pathSRT, int sublanguage, int transp, char* id, char* client, int serviceType) {
	this->path_srt = pathSRT;
    this->sub_language = sublanguage;
	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 >();
	extrator_factory = new ExtratorFactory();
	try{
        setPathContents();
    }catch(RuntimeException ex){
        throw ServiceException(ex.getMessage());
    }
	running = true;
	finish = false;
	DPRINTF("Done!\n");
}

ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() {
	free(vetor_pts);
	if (this->mixer) delete mixer;
	if (tradutor) delete tradutor;
	if (renderer) delete renderer;
	if (extratorSRT)delete extratorSRT;
	if (extrator_factory) delete extrator_factory;
    DDDPRINTF("Service SRT finalized!\n");
}

void ServiceWindowGenerationFromSRT::setPathContents() {
	if(strcmp(client_type,DEVELOPER) == 0){
        this->path_contents = PATH_DEVEL_CONTENTS;
        this->path_uploads = PATH_DEVEL_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 = "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 = PATH_VBOX_UPLOADS;
        conf_file.close();
    }else{
        throw ServiceException("Invalid client!");
    }
}

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

    if(this->service_type == SERVICE_TYPE_SRT)
        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 ServiceWindowGenerationFromSRT::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 ServiceWindowGenerationFromSRT::addPTS(int64_t pts){
	vetor_pts->push_back(pts);
}

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){
    addPTS(pts);
    if (sub_language == 1)
        notifyTranslator(subtitle);
    else{
        string glosa(reinterpret_cast<char*>(subtitle));
        notifyRenderer(glosa);
    }
}

void ServiceWindowGenerationFromSRT::notifyTranslation(char* glosa) {
    string sGlosa(glosa);
    notifyRenderer(sGlosa);
}

void ServiceWindowGenerationFromSRT::notifyRenderer(string glosa) {
    while(renderer->isSending())
        usleep(10000);
    try{
        renderer->receiveGlosa(glosa, vetor_pts->front());
        legendas_enviadas++;
    }catch(lavidlib::RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }
    vetor_pts->erase(vetor_pts->begin());
}

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

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

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

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

void ServiceWindowGenerationFromSRT::initialize() {
	DPRINTF("Service SRT Initialize.\n");
    setPathLibras();
	extratorSRT = (ExtratorSRT*) extrator_factory->getExtrator(Extrator::SRT);
	extratorSRT->addListener(this);
	extratorSRT->setFilePath(path_srt);

    tradutor = new TradutorPortGlosa();
	if (this->sub_language == 1) {
        tradutor->addListener(this);
    }

    renderer = new Renderer(this->path_libras ,this->user_id);
	renderer->addListener(this);

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

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