main2.cpp 11.9 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 <sys/time.h>

using namespace std;


void serviceCC();
void serviceSRT();
void serviceOnlySRT();
void serviceREC();
void serviceText();
void serviceREC2();
void help();
void header();
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;
char* path_file;
char* username;
char* client_type;
int sublanguage;
int position;
int size;
int transp;
int service_type;

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;

	service_type = (int) atoi((char*)argv[1]);	

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

	switch(argc){
		case 4:
			path_file = (char*) argv[2];
			username = (char*) argv[3];
			DDPRINTF("Path File: %s\n\n", path_file);
			break;

		case 6:
			path_file = (char*) argv[2];
			transp = (int) atoi(argv[3]);
			username = (char*) argv[4];
			client_type = (char*) argv[5]; // {WEB, DESKTOP}
			DDPRINTF("Path File: %s\n", path_file);
			DDPRINTF("ID: %s\n", username);
			DDPRINTF("Client Type: %s\n", client_type);
			if (transp == 0){
				DDPRINTF("Background: Opaco\n\n");
			} else{
		    	DDPRINTF("Background: Transparente.\n\n");
			}

			break;
		default:
			cout << "\nParametros inválidos. Tente novamente." << endl;
			help();	

			return 1;

	}



	switch(service_type){
		case 1:
			serviceCC();
			return 1;
		case 2:
			serviceSRT();
			return 1;
		case 3:
			serviceREC();
			break;
		case 4:
			serviceText();
			break;	    	 
		case 5:
			serviceOnlySRT();
			break;
		case 6:
			serviceREC2();
			break;
		default:
			cout << "\nServiço inválido. Tente novamente." << endl;
			help();	
			return 1;
	}

	gettimeofday(&tv2, NULL);
	t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
	cout << endl;
	DPRINTF("Time: %lf\n", (t2-t1));
	DPRINTF("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(){

	filename = createFileToResponse(username);

	/**sublanguage = (int) atoi(argv[3]);
    int position = (int) atoi(argv[4]);
    int size = (int) atoi(argv[5]);    
	int transparency = (int) atoi(argv[6]);	**/

	ServiceWindowGenerationFromREC * service_rec;
	service_rec = new ServiceWindowGenerationFromREC(
						path_file, 1, 3, 1, 0, 3);
	
	service_rec->initialize();

	while(service_rec->isRunning()){
        sleep(2);
	}
	updateRequestStatus(filename.c_str(), username, "true");
	delete service_rec;
	//printf("\n\nAtualizei o status\n\n");

}

void serviceSRT(){

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

	cout << "\nSERVICE SUBTITLE (SRT)\n" << endl;

	char* username = (char*) argv[8];
	filename = createFileToRespWeb(username);
	
    char* path_in = (char*) argv[2];
    char* path_srt = (char*) argv[3];
	sublanguage = (int) atoi(argv[4]);
    int position = (int) atoi(argv[5]);
    int size = (int) atoi(argv[6]);    
	int transparency = (int) atoi(argv[7]);
	
	ServiceWindowGenerationFromSRT * service_srt;
	service_srt = new ServiceWindowGenerationFromSRT(
							path_in, path_srt, sublanguage, position, size, transparency, 2);
		    
	service_srt->initialize();
	while(service_srt->isRunning()){
        sleep(5);
	}
	char* id = (char*) argv[8];	
	updateRequestStatus(filename.c_str(), id, "true");


	
	**/
}

void serviceText(){
	filename = createFileToRespWeb(username);

	ServiceWindowGenerationFromText *service_text;
	service_text = new ServiceWindowGenerationFromText(path_file, username, transp, 4, client_type);
	service_text->initialize();
            service_text->Start();
	
	while (service_text->isAlive()) {
	    usleep(100000); //100ms
	}
	updateRequestStatusWeb(filename.c_str(), username, "true");
	delete service_text;

}

void serviceOnlySRT(){

          
            // LEONARDO: comentado para os testes de Eduardo!
	//filename = createFileToRespWeb(username);

	//cout << "SRT FILE: " << path_file << endl;

	ServiceWindowGenerationFromSRT * service_srt;
	service_srt = new ServiceWindowGenerationFromSRT(path_file, username, transp, 5);		
	service_srt->initialize();
	service_srt->Start();

	//while(service_srt->isRunning()){
    while (!service_srt->finished()) {
        	//sleep(1);
		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* username = (char*) argv[4];
	filename = createFileToRespWeb(username);
	
	printf("Main 1\n");

	ServiceWindowGenerationFromREC * service_rec;
	service_rec = new ServiceWindowGenerationFromREC(path_in, username, 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(), username, "true");
**/
}



//Help do programa, explicando todos os parâmetros existentes...
void help() {

	//cout << "\nCommand line: ./gtaaas SERVICE_TYPE INPUT_VIDEO INPUT_SRT INPUT_TEXT_FILE LANGUAGE POSITION SIZE TRANSPARENCY ID\n\n"
	cout << "\nService Type Text: ./gtaaas 4 INPUT_TEXT_FILE TRANSPARENCY ID CLIENT_TYPE"
	     << "\nService Type SRT Only: ./gtaaas 5 INPUT_SRT TRANSPARENCY ID CLIENT_TYPE\n\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"
		     <<"# 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";
			
}


/* Response file to Web  */
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);
        //DDPRINTF("Criando o arquivo: %s\n", filename.c_str());
        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);

}
/* End */
 

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);
	//printf("\ncreating file: %s\n", namearq.c_str());

	return namearq;

}

void updateRequestStatus(const char* filename, char* id, char* status){
  
	//printf("\nupdateRequestStatus: id = %s\n", id);

	//printf("\nupdating file: %s\n", filename);
	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);

}