serviceWindowGenerationFromText.cpp
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "serviceWindowGenerationFromText.h"
ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, int transp, char* id, char* client) {
this->path_input = pathFile;
this->transparency = transp;
this->user_id = id;
client_type = client;
running = true;
finish = false;
numero_legendas = INT_MAX;
legendas_enviadas = 0;
vetor_pts = new vector<int64_t >();
tradutor = new TradutorPortGlosa();
extrator_factory = new ExtratorFactory();
setPathContents();
running = true;
finish = false;
DPRINTF("Done!\n");
}
ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() {
free(vetor_pts);
delete(ppty_h);
if (tradutor) delete tradutor;
if (sincronizador) delete sincronizador;
if (extratorTXT)delete extratorTXT;
if (extrator_factory) delete extrator_factory;
DDDPRINTF("Service Text finalized!\n");
}
void ServiceWindowGenerationFromText::setPathContents() {
if(strcmp(client_type,DEVELOPER) == 0){
this->path_contents = PATH_DEVEL;
this->path_uploads = PATH_DEVEL_UPLOADS;
}else if(strcmp(client_type, PRODUCTION) == 0){
try{
ppty_h = new PropertyHandler(PATH_CONF_FILE, PropertyHandler::READ);
this->path_contents = (char*) ppty_h->get_attibute_value("STORAGE");
}catch(RuntimeException &ex){
printf("%s\n", ex.getMessage().c_str());
}
this->path_uploads = PATH_VBOX_UPLOADS;
}else{
throw new ServiceException("Invalid client!");
}
}
void ServiceWindowGenerationFromText::setPathLibras() {
char* final_path = new char[MAX_SIZE_PATH];
strcpy(final_path, this->path_uploads);
strcat(final_path, this->user_id);
strcat(final_path, "/video_libras.ts");
this->path_libras = final_path;
}
void ServiceWindowGenerationFromText::setBackground() {
if(this->transparency == 0) { //pega dicionario com BackGround opaco
sincronizador = new Synchronizer(BASEDIR, EXTENSAO_DICIONARIO,
this->path_libras, this->transparency);
} else {
if(this->transparency == 1) { //pega dicionario com BackGround transparente
sincronizador = new Synchronizer(BASEDIRTRANSP, EXTENSAO_DICIONARIO,
this->path_libras, this->transparency);
}
}
}
void ServiceWindowGenerationFromText::setSizeOfSubtitles(int sub_size) {
numero_legendas = sub_size;
if (legendas_enviadas >= numero_legendas)
sincronizador->stop();
}
void ServiceWindowGenerationFromText::notifyTranslator(unsigned char* text) {
tradutor->traduz(text);
}
void ServiceWindowGenerationFromText::notifyLine(unsigned char* line) {
notifyTranslator(line);
}
void ServiceWindowGenerationFromText::notifyEndOfSynchronization() {
transcodeVideoToMp4();
this->running = false;
}
void ServiceWindowGenerationFromText::notifyTranslation(vector<string> * glosas) {
for (int i = 0; i < glosas->size(); i++) {
locale loc;
string glosa_lower = "";
for (int k = 0; k < glosas->at(i).length(); k++){
glosa_lower += std::tolower(glosas->at(i).at(k), loc);
}
sincronizador->recebeglosa(glosa_lower, 1000);
}
sincronizador->stop();
legendas_enviadas++;
}
void ServiceWindowGenerationFromText::notifyEnd(int line_size) {
DPRINTF("Service Text recebeu: %d linhas.\n", line_size);
setSizeOfSubtitles(line_size);
}
bool ServiceWindowGenerationFromText::isRunning() {
return this->running;
}
bool ServiceWindowGenerationFromText::isFinished() {
return this->finish;
}
void ServiceWindowGenerationFromText::initialize() {
DPRINTF("Service Text Initialize.\n");
extratorTXT = (ExtratorTXT*) extrator_factory->getExtrator(Extrator::TXT);
extratorTXT->addListener(this);
extratorTXT->setFilePath(path_input);
setPathLibras();
tradutor->addListener(this);
setBackground();
uint64_t pcr_base = (uint64_t) 1000; //FIXME: macro
sincronizador->setPCRBase(pcr_base);
sincronizador->addListener(this);
sincronizador->Start();
try{
extratorTXT->initialize();
}catch(ExtratorException ex){
throw ServiceException(ex.getMessage());
}
this->Start();
}
void ServiceWindowGenerationFromText::transcodeVideoToMp4(){
DPRINTF("[AGUARDE] Transcodificando o vídeo de Libras...\n");
string command = "ffmpeg -i ";
command.append(path_libras)
.append(" -qscale 0 -strict experimental -vcodec libx264 -preset fast -r 30 ").append(" -v quiet ")
.append(path_contents).append(user_id).append(".mp4");
//printf("[INFO]: Transcodification command -> %s\n", command.c_str());
system(command.c_str());
}
void ServiceWindowGenerationFromText::Run(){
while(isRunning()){
usleep(200000);
}
finish = true;
}