main.cpp 11.4 KB
/* 
 * File:   main.cpp
 * Author: felipe
 *
 * Created on 15 de Outubro de 2009, 10:56
 */

/* 
 * File:   main.cpp
 * Author: Erickson
 *
 * Edit on 03 de Fevereiro de 2014
 */

#include "serviceWindowGenerationFromCC.h"
#include "serviceWindowGenerationFromSRT.h"
#include "serviceWindowGenerationFromREC.h"
#include "serviceWindowGenerationFromText.h"
#include "serviceException.h"

#include <sys/time.h>
#include <stdlib.h>

using namespace std;

void serviceCC();
void serviceSRT(char* path_in, char* path_srt, char* sublanguage, char* position, char* size, char* transparency, char* username);
void serviceOnlySRT(char* path_file, char* transparency, char* username);
void serviceREC(char* path_video, char* sublanguage, char* position, char* size, char* transparency, char* username, char* rate);
void serviceREC(char* path_video, char* sublanguage, char* position, char* size, char* transparency, char* username);

void serviceText(char* path_text, char* transparency, char* username, char* client_type);
void serviceREC2();
void help();
void fail(string msg);
void hasFailed();
void hasInvalid();

bool isFailed;
bool isInvalid;

int main(int argc, char* argv[]) {  

	struct timeval tv1, tv2;
	double t1, t2;    

	gettimeofday(&tv1, NULL);
	t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;


	printf("\n################## VLIBRAS : LAVID ##################\n\n");  
	DDPRINTF("Service Type: %s\n", argv[1]);

	switch((int) atoi(argv[1])){
		case 1:
			serviceCC();
			hasInvalid();
			break;
		case 2:
			if(argc <= 8){
				cout << "\nParametros inválidos. Tente novamente.\nService Type SRT: ./vlibras 2 INPUT_VIDEO INPUT_SRT LANGUAGE{1=Portuguese, 2=Glosa} POSITION{1=Top Left, 2=Top Right, 3=Bottom Right, 4=Bottom Left} SIZE{1=Small, 2=Medium, 3=Large} TRANSPARENCY{0=Opaque, 1=Transparent} ID" << endl;
				help();
				hasInvalid();
				break;
			}
			serviceSRT(argv[2], argv[3], argv[4], argv[5],
				argv[6], argv[7], argv[8]);
			break;
		case 3:
			if(argc <= 7){
				cout << "\nParametros inválidos. Tente novamente.\nService Type REC: ./vlibras 3 INPUT_VIDEO LANGUAGE{1=Portuguese, 2=Glosa} POSITION{1=Top Left, 2=Top Right, 3=Bottom Right, 4=Bottom Left} SIZE{1=Small, 2=Medium, 3=Large} TRANSPARENCY{0=Opaque, 1=Transparent} ID" << endl;
				help();
				hasInvalid();	
				break;
			}
			if (argc == 8)
			serviceREC(argv[2], argv[3], argv[4], argv[5],
				argv[6], argv[7]);
			else
				serviceREC(argv[2], argv[3], argv[4], argv[5],
				argv[6], argv[7], argv[8]);
			break;
		case 4:
			if(argc <= 5){
				cout << "\nParametros inválidos. Tente novamente.\nService Type Text: ./vlibras 4 INPUT_TEXT_FILE TRANSPARENCY{0=Opaque, 1=Transparent} ID CLIENT_TYPE{Desktop, Web}" << endl;
				help();
				hasInvalid();
				break;	
			}
			serviceText(argv[2], argv[3], argv[4], argv[5]);
			break;	    	 
		case 5:
			if(argc <= 4){
				cout << "\nParametros inválidos. Tente novamente.\nService Type SRT Only: ./vlibras 5 INPUT_SRT TRANSPARENCY{0=Opaque, 1=Transparent} ID" << endl;
				help();
				hasInvalid();
				break;
			}
			serviceOnlySRT(argv[2], argv[3], argv[4]);
			break;
		case 6:
			serviceREC2();
			hasInvalid();
			break;
		default:
			cout << "\nServiço inválido. Tente novamente." << endl;
			help();	
			hasInvalid();
			break;
	}

	if(isFailed)
		exit(1);
	else if(isInvalid)
		exit(127);

	gettimeofday(&tv2, NULL);
	t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
	cout << endl;
	DDPRINTF("Time: %lf\n", (t2-t1));
	DDPRINTF("VLibras concluído!\n\n");
	exit(0);

}

void serviceCC(){

	cout << "\n--> Serviço não disponível.\n" << endl;
	/**
	cout << "\nSERVICE CLOSED CAPTION\n" << endl;
	filename = createFileToResponse((char*)argv[6]); // FIXME: [6] é transparency

	char* input_file = argv[2];
	int position = (int) atoi(argv[4]);
	int size = (int) atoi(argv[5]);
	int transparency = (int) atoi(argv[6]);

	ServiceWindowGenerationFromCC *service;
	service = new ServiceWindowGenerationFromCC(
					input_file,position,size, transparency, 1);

	service->initialize();
	        
	while(service->isRunning()){   
	    sleep(5);
	}

	char* id = (char*) argv[7];		
	updateRequestStatus(filename.c_str(), id, "true");
	**/
}

void serviceREC(char* path_video, char* sublanguage,
	char* position, char* size, char* transparency, char* id, char* rate){

	ServiceWindowGenerationFromREC * service_rec;
	service_rec = new ServiceWindowGenerationFromREC(
						path_video,
						(int) atoi(sublanguage),
						(int) atoi(position),
						(int) atoi(size),
						(int) atoi(transparency), 
						id, 3, rate);
	
	try{
		service_rec->initialize();
	}catch(ServiceException ex){
		fail(ex.getMessage());
		hasFailed();
		return;
	}	
	while(service_rec->isRunning()){
        sleep(2);
	}
	delete service_rec;
}

void serviceREC(char* path_video, char* sublanguage,
	char* position, char* size, char* transparency, char* id){

	ServiceWindowGenerationFromREC * service_rec;
	service_rec = new ServiceWindowGenerationFromREC(
						path_video,
						(int) atoi(sublanguage),
						(int) atoi(position),
						(int) atoi(size),
						(int) atoi(transparency), 
						id, 3);
	
	try{
		service_rec->initialize();
	}catch(ServiceException ex){
		fail(ex.getMessage());
		hasFailed();
		return;
	}	
	while(service_rec->isRunning()){
        sleep(2);
	}
	delete service_rec;
}

void serviceSRT(char* path_in, char* path_srt, char* sublanguage,
	char* position, char* size, char* transparency, char* id){

	
	ServiceWindowGenerationFromSRT * service_srt;
	service_srt = new ServiceWindowGenerationFromSRT(
						path_in, path_srt, (int) atoi(sublanguage),
						(int) atoi(position), (int) atoi(size),
						(int) atoi(transparency), id, 2);
	
	try{
		service_srt->initialize();
	}catch(ServiceException ex){
		fail(ex.getMessage());
		hasFailed();
		return;
	}		    
	while(service_srt->isRunning()){
        sleep(5);
	}
	
	delete service_srt;

}

void serviceText(char* path_text, char* transparency, char* id, char* client_type){

	if (strcmp(client_type, (char*)"WEB") == 0) {
		ServiceWindowGenerationFromText *service_text;
		service_text = new ServiceWindowGenerationFromText(path_text, id, (int) atoi(transparency), 4, client_type);
		try{
			service_text->initialize();
		}catch(ServiceException ex){
			fail(ex.getMessage());
			hasFailed();
			return;
		}
		
		while (service_text->isAlive()) {
		    usleep(100000); //100ms
		}
		delete service_text;

	} else {
		
		ServiceWindowGenerationFromText *service_text;
        service_text = new ServiceWindowGenerationFromText(path_text, id, (int) atoi(transparency), 4, (char*)"IOS");
        try{
                service_text->initialize();
        }catch(ServiceException ex){
                fail(ex.getMessage());
                hasFailed();
                return;
        }
        
        while (service_text->isAlive()) {
            usleep(100000); //100ms
        }
        delete service_text;
	}

}

void serviceOnlySRT(char* path_file, char* transparency, char* id){


	ServiceWindowGenerationFromSRT * service_srt;
	service_srt = new ServiceWindowGenerationFromSRT(path_file, (int) atoi(transparency), id, 5);		
	try{
		service_srt->initialize();
	}catch(ServiceException ex){
		fail(ex.getMessage());
		hasFailed();
		return;
	}

    while (!service_srt->finished()) {
		usleep(100000);	//100ms
	}
	delete service_srt;

}

void serviceREC2(){

	cout << "\n--> Serviço não disponível.\n" << endl;

/**
	cout << "\nSERVICE RECOGNIZER FROM AUDIO - Delivers only LIBRAS Translation\n" << endl;

	char* path_in = (char*) argv[2];
	int transparency = (int) atoi(argv[3]);	
	char* id = (char*) argv[4];
	filename = createFileToRespWeb(id);
	
	printf("Main 1\n");

	ServiceWindowGenerationFromREC * service_rec;
	service_rec = new ServiceWindowGenerationFromREC(path_in, id, transparency, 6);
	
	service_rec->initialize();

	while(service_rec->isRunning()){
                sleep(5);
		printf("\nservice_rec->isRunning()...\n");
	}
	printf("\nService REC_FROM_AUDIO finished!\n");	
	updateRequestStatus(filename.c_str(), id, "true");
**/
}

void fail(string msg){
	printf("\n");
	DDDDPRINTF("Ops... Tivemos um problema! :(\n");
	DDDDPRINTF("Possível causa do erro: %s\n\n", msg.c_str());
}

void hasFailed(){
	isFailed = true; 
}

void hasInvalid(){
	isInvalid = true;
}


//Help do programa, explicando todos os parâmetros existentes...
void help() {
	cout     <<"\n####################################################################################\n"	           
		     <<"# SERVICE_TYPE: 1 - means Closed Caption       - doesn't use INPUT_SRT 	           #\n"
		     <<"#               2 - means With Subtitles (SRT) - requires INPUT_SRT    	           #\n"
		     <<"#               3 - means Recognize 	       - requires INPUT_VIDEO	           #\n"
		     <<"#               4 - means Text                 - requires INPUT_FILE_TEXT          #\n"
		     <<"#               5 - means Subtitles ONLY (SRT) - requires INPUT_SRT    	           #\n"
		     <<"#----------------------------------------------------------------------------------#\n\n"
		     /*<<"# INPUT_VIDEO: Path of the video file                                              #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# INPUT_SRT: Path of the SRT file (only for SERVICE_TYPE = 2)                      #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
                     <<"# INPUT_FILE_TEXT: Path of the text file (doesn't use INPUT_VIDEO and INPUT_SRT)   #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# LANGUAGE: 1 - means Portuguese				                   #\n"
             	     <<"#           2 - means Glosa			  	                           #\n"
             	     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# POSITION: 1 - means TOP_LEFT                                                     #\n"
		     <<"#           2 - means TOP_RIGHT                                                    #\n"
		     <<"#           3 - means BOTTOM_RIGHT                                                 #\n"
		     <<"#           4 - means BOTTOM_LEFT                                                  #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# SIZE: 1 - means SMALL                                                            #\n"
		     <<"#       2 - means MEDIUM                                                           #\n"
		     <<"#       3 - means LARGE                                                            #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# TRANSPARENCY: 0 - means that the Background is Opaque                            #\n"
		     <<"#               1 - means that the Background is Transparent                       #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# CLIENT TYPE: DESKTOP                                                             #\n"
		     <<"#              WEB                                                                 #\n"
		     <<"#----------------------------------------------------------------------------------#\n"
		     <<"# ID: relative to the unique ID on the Database                                    #\n"
		     <<"####################################################################################\n"*/;
			
}