main.cpp 12.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>

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);
void serviceText(char* path_text, char* transparency, char* username, char* client_type);
void serviceREC2();
void help();
void fail(string msg);
void hasFailed();
string createFileToResponse(char* id);
void updateRequestStatus(const char* filename, char* id, char* status);
string createFileToRespWeb(char* id);
void updateRequestStatusWeb(const char* filename, char* id, char* status);

string filename;
bool isFailed;

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################## GTAAAS : LAVID ##################\n\n");  
	DDPRINTF("Service Type: %s\n", argv[1]);

	switch((int) atoi(argv[1])){
		case 1:
			serviceCC();
			return 1;
		case 2:
			if(argc <= 8){
				cout << "\nParametros inválidos. Tente novamente.\nService Type SRT: ./gtaaas 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();
				return 1;	
			}
			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: ./gtaaas 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();
				return 1;	
			}
			serviceREC(argv[2], argv[3], argv[4], argv[5],
				argv[6], argv[7]);
			break;
		case 4:
			if(argc <= 5){
				cout << "\nParametros inválidos. Tente novamente.\nService Type Text: ./gtaaas 4 INPUT_TEXT_FILE TRANSPARENCY{0=Opaque, 1=Transparent} ID CLIENT_TYPE{Desktop, Web}" << endl;
				help();
				return 1;	
			}
			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: ./gtaaas 5 INPUT_SRT TRANSPARENCY{0=Opaque, 1=Transparent} ID" << endl;
				help();
				return 1;	
			}
			serviceOnlySRT(argv[2], argv[3], argv[4]);
			break;
		case 6:
			serviceREC2();
			return 1;
		default:
			cout << "\nServiço inválido. Tente novamente." << endl;
			help();	
			return 1;
	}

	if(!isFailed){
		gettimeofday(&tv2, NULL);
		t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
		cout << endl;
		DDPRINTF("Time: %lf\n", (t2-t1));
		DDPRINTF("GTaaaS concluído!\n\n");
	}
    return 1;
}

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){

	filename = createFileToResponse(id);

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

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

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

}

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

	//filename = createFileToRespWeb(id);

	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
	}
	//updateRequestStatusWeb(filename.c_str(), id, "true");
	delete service_text;

}

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

    //filename = createFileToRespWeb(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;

	// ./gtaaas 6 <path_video> <transp> <user>
/**
	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; 
}


//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"*/;
			
}


string createFileToRespWeb(char* id) {

	FILE* file;
        string filename = "web-content/";
        filename += id;
        filename += ".xml";
        file = fopen(filename.c_str(), "w+");
        string content_file = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<request>\n\t<id>";
        content_file += id;
        content_file += "</id>\n\t<finished>false</finished>\n</request>";
        const char *textchar = content_file.c_str();
        fwrite(textchar, 1, content_file.size(), file);
        fclose(file);
        return filename;

}

void updateRequestStatusWeb(const char* filename, char* id, char* status){

        //printf("\nupdateRequestStatusWeb: id = %s\n", id);

        FILE *file = fopen(filename, "w+");
        string content = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<request>\n\t<id>";
        content += id;
        content += "</id>\n\t<finished>";
        content += status;
        content += "</finished>\n</request>";
        const char *textchar = content.c_str();
        fwrite(textchar, 1, content.size(), file);
        fclose(file);

}

string createFileToResponse(char *id){
	
	FILE *arquivofinish;
	string namearq = "gtaaas_user/gtaaas_web/public/uploads/videos/";
	namearq += id;
	namearq += "/";
	namearq += id;
	namearq += ".xml";

	arquivofinish = fopen(namearq.c_str(), "w+");
	string arqtext = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<request>\n\t<id>";
	arqtext += id;
	arqtext += "</id>\n\t<finished>false</finished>\n</request>";
	const char *textchar = arqtext.c_str();
	fwrite(textchar, 1, arqtext.size(), arquivofinish);
	fclose(arquivofinish);

	return namearq;

}

void updateRequestStatus(const char* filename, char* id, char* status){
  
	FILE *arquivofinish = fopen(filename, "w+");
	string arqtext = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<request>\n\t<id>";
	arqtext += id;
	arqtext += "</id>\n\t<finished>";
	arqtext += status;
	arqtext += "</finished>\n</request>";
	const char *textchar = arqtext.c_str();
	fwrite(textchar, 1, arqtext.size(), arquivofinish);
	fclose(arquivofinish);

}