serviceWindowGenerationFromText.cpp 2.51 KB
#include "serviceWindowGenerationFromText.h"

#define PATH_LIBRAS ".libras/video/libras.ts"
#define OUTPUT_VIDEO_WEB "../gtaaas-plugin-server/videos/libras.webm"

ServiceWindowGenerationFromText::ServiceWindowGenerationFromText (char* _path_file, int _transp, int _serviceType, char* _client_type) {
	path_file = _path_file;
	client_type = _client_type;
    setTransparency(_transp);
    setServiceType(_serviceType);
	setPathLibras(PATH_LIBRAS);
	alive = true;
}

ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() {
	//TODO
}

int ServiceWindowGenerationFromText::readTextFile() {
	string line;
	string all_text = "";
	std::ifstream file (path_file, std::ifstream::binary);
	int bytes = 0;
	if (file.is_open()) {
		while (file.good()) {
			getline (file,line);			
			all_text.append(line).append(" ");
		}
		file.close();
	}
	bytes = all_text.length();
	if (bytes > 1) { // 1 = " "
		char* tbuffer = new char[bytes+1];
		std::strcpy (tbuffer, all_text.c_str());
		text_buff = (unsigned char*) tbuffer;
		return bytes;
	}
	return -1;	

}

void ServiceWindowGenerationFromText::initialize() {
	printf("\nServiceWindowGenerationFromText::initialize()\n");
	if (readTextFile() > 1) {
	   ServiceWindowGeneration::initialize();
	   notificaTradutor((unsigned char*) text_buff);
	   stopServiceText();	   
	   this->Start();
	} else {
	   printf("error!\n");
	}
}

void ServiceWindowGenerationFromText::notificaTradutor(unsigned char *legenda) {
	tradutor->traduz(legenda);
}

bool ServiceWindowGenerationFromText::isAlive() {
	return alive;
}

void ServiceWindowGenerationFromText::Run() {	
	while (isRunning()) {
		printf("waiting for the end...\n");
	    sleep(2);
	}	
	if (strcmp(client_type, (char*)"WEB") == 0) {
		printf("\n\n[INFO]: Client Type -> WEB\n\n");
		printf("[INFO]: A transcodificação para .webm está ativada!\n");
		transcodeVideoToWebm();
	} else {
		printf("\n\n[INFO]: Client Type -> DESKTOP\n\n");
	}
	alive = false;
}

void ServiceWindowGenerationFromText::transcodeVideoToWebm() {

	FILE *video_webm;
	video_webm = fopen(OUTPUT_VIDEO_WEB, "r");
	if (video_webm != NULL) {
		string cmd = "rm ";
		cmd.append(OUTPUT_VIDEO_WEB);
		printf("[INFO]: Remove video libras.webm: %s\n", cmd.c_str());
		system(cmd.c_str());
		fclose(video_webm);
	}
	
	char* command = (char*) malloc (INT_MAX);
	strcpy(command, "avconv -i ");
	strcat(command, PATH_LIBRAS);
	strcat(command, " -vcodec libvpx -acodec libvorbis ");
	strcat(command, OUTPUT_VIDEO_WEB);
	printf("[INFO]: Transcodification command -> %s\n", command);
	system(command);	

}