serviceWindowGenerationFromRec.cpp
7.55 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "serviceWindowGenerationFromRec.h"
ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec(
char* pathVideo, int sublanguage, int pos, int size, int transp, char* id, int serviceType, char* rate){
this->path_input = pathVideo;
this->sub_language = sublanguage;
this->position = pos;
this->size = size;
this->transparency = transp;
this->user_id = id;
this->service_type = serviceType;
numero_legendas = INT_MAX;
legendas_enviadas = 0;
vetor_pts = new vector<int64_t >();
rec = new Recognize(pathVideo, id, rate);
tradutor = new TradutorPortGlosa();
running = true;
finish = false;
DPRINTF("Done!\n");
}
ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec(
char* pathVideo, int sublanguage, int pos, int size, int transp, char* id, char* client, int serviceType){
this->path_input = pathVideo;
this->sub_language = sublanguage;
this->position = pos;
this->size = size;
this->transparency = transp;
this->user_id = id;
this->client_type = client;
this->service_type = serviceType;
numero_legendas = INT_MAX;
legendas_enviadas = 0;
vetor_pts = new vector<int64_t >();
rec = new Recognize(path_input, id);
tradutor = new TradutorPortGlosa();
try{
setPathContents();
}catch(RuntimeException ex){
throw ServiceException(ex.getMessage());
}
running = true;
finish = false;
DPRINTF("Done!\n");
}
ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){
free(vetor_pts);
if (tradutor) delete tradutor;
if (rec) delete rec;
if (sincronizador) delete sincronizador;
if (mixer) delete mixer;
DDDPRINTF("Service Rec finished!\n");
}
void ServiceWindowGenerationFromRec::setPathContents(){
if(strcmp(client_type,DEVELOPER) == 0){
this->path_contents = PATH_DEVEL;
this->path_uploads = PATH_DEVEL_UPLOADS;
rec->setPathAudioContents(path_uploads);
}else if(strcmp(client_type, PRODUCTION) == 0){
ifstream conf_file(PATH_CONF_FILE, ifstream::binary);
parsingSuccessful = reader.parse(conf_file, root);
if(!parsingSuccessful){
conf_file.close();
throw ServiceException("Fail to parsing param.json");
}
string attr = root.get("storage", PATH_VBOX_UPLOADS).asString();
this->path_contents = new char[MAX_SIZE_PATH];
strcpy(this->path_contents, attr.c_str());
this->path_uploads = PATH_VBOX_UPLOADS;
rec->setPathAudioContents(path_uploads);
conf_file.close();
}else{
throw ServiceException("Invalid client!");
}
}
void ServiceWindowGenerationFromRec::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 ServiceWindowGenerationFromRec::setBackground(){
if(this->transparency == 0) { //pega dicionario com BackGround opaco
char* dicPath;
dicPath = getenv("DIC_LIBRAS");
if(dicPath != NULL)
sincronizador = new Synchronizer(dicPath, EXTENSAO_DICIONARIO, this->path_libras, this->transparency);
else
sincronizador = new Synchronizer(BASEDIR, EXTENSAO_DICIONARIO, this->path_libras, this->transparency);
} else if(this->transparency == 1) { //pega dicionario com BackGround transparente
char* dicTPath;
dicTPath = getenv("DICTRANSP_LIBRAS");
if(dicTPath != NULL)
sincronizador = new Synchronizer(dicTPath, EXTENSAO_DICIONARIO, this->path_libras, this->transparency);
else
sincronizador = new Synchronizer(BASEDIRTRANSP, EXTENSAO_DICIONARIO, this->path_libras, this->transparency);
}
}
void ServiceWindowGenerationFromRec::setSizeOfSubtitles(int sub_size){
numero_legendas = sub_size;
if (legendas_enviadas >= numero_legendas)
sincronizador->stop();
}
void ServiceWindowGenerationFromRec::addPTS(int64_t pts){
vetor_pts->push_back(pts);
}
void ServiceWindowGenerationFromRec::notifyTranslator(unsigned char* text){
tradutor->traduz(text);
}
//Metodo utilizado pelo reconhecedor para avisar sobre novas sentenças reconhecidas
void ServiceWindowGenerationFromRec::notifyTextRecognized(unsigned char* text, int64_t pts){
addPTS(pts);
notifyTranslator(text);
}
//Quando o sincronizador termina, ele invoca esse método para avisar
void ServiceWindowGenerationFromRec::notifyEndOfSynchronization() {
if (this->service_type == SERVICE_TYPE_REC) {
mixer = new Mixer();
mixer->initialize(this->path_input, this->path_libras,this->position,this->size,
this->transparency, this->user_id, this->path_uploads, this->path_contents);
createThumbnail();
}else{
transcodeVideoToMp4();
}
this->running = false;
}
void ServiceWindowGenerationFromRec::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);
}
int64_t pts_notificado = vetor_pts->front();
sincronizador->recebeglosa(glosa_lower, pts_notificado);
}
vetor_pts->erase(vetor_pts->begin());
legendas_enviadas++;
}
void ServiceWindowGenerationFromRec::notifyEnd(int sentences_size){
DPRINTF("Service REC recebeu: %d sentenças.\n", sentences_size);
setSizeOfSubtitles(sentences_size);
}
bool ServiceWindowGenerationFromRec::isRunning(){
return this->running;
}
bool ServiceWindowGenerationFromRec::isFinished(){
return this->finish;
}
void ServiceWindowGenerationFromRec::initialize(){
DPRINTF("Service REC Initialize.\n");
rec->addListener(this);
setPathLibras();
if(this->sub_language == 1)
tradutor->addListener(this);
if(this->service_type != SERVICE_TYPE_REC_ONLY_AUDIO){
vector<string> tokens;
char* pathtmp = this->path_input;
int size = strlen(pathtmp);
char vtemp [size];
strcpy(vtemp, pathtmp);
pathtmp = strtok(vtemp, ".");
while (pathtmp != NULL) {
tokens.push_back(string(pathtmp));
pathtmp = strtok(NULL, ".");
}
string buildstrpath = tokens[0] + "_libras" + EXTENSAO_DICIONARIO;
this->path_libras = new char[buildstrpath.size()];
strcpy(this->path_libras, buildstrpath.c_str());
//printf("O Serviço montou o seguinte path para o vídeo de Libras: %s\n", path_libras);
}else{
tradutor->addListener(this);
}
setBackground();
uint64_t pcr_base = (uint64_t) 1000; //FIXME: macro
sincronizador->setPCRBase(pcr_base);
sincronizador->addListener(this);
sincronizador->Start();
try{
rec->initialize();
} catch(RecognizeException ex){
throw ServiceException(ex.getMessage());
}
this->Start();
}
void ServiceWindowGenerationFromRec::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 ServiceWindowGenerationFromRec::createThumbnail(){
string command = "ffmpeg -ss 10 -i ";
command.append(path_contents).append(user_id).append(".mp4")
.append(" -vcodec png -vframes 1 -an -f rawvideo -y -vf scale=200:200 ").append(" -v quiet ")
.append(path_contents).append(user_id).append(".png");
//printf("[INFO]: Thumbnail command -> %s\n", command.c_str());
system(command.c_str());
}
void ServiceWindowGenerationFromRec::Run(){
while(isRunning()){
usleep(200000);
}
finish = true;
}