Commit c9375703af04c228529097bb448beab24967a0d5
1 parent
2687f5ec
Exists in
master
and in
1 other branch
Adicionado o componente Renderer
Showing
10 changed files
with
291 additions
and
176 deletions
Show diff stats
... | ... | @@ -0,0 +1,14 @@ |
1 | +#ifndef LISTENER_RENDERER_H | |
2 | +#define LISTENER_RENDERER_H | |
3 | + | |
4 | +#include <string> | |
5 | +#include <stdint.h> | |
6 | + | |
7 | +using namespace std; | |
8 | + | |
9 | +class ListenerRenderer { | |
10 | +public: | |
11 | + virtual void notifyEndOfRenderization() = 0; | |
12 | +}; | |
13 | + | |
14 | +#endif /* LISTENER_RENDERER_H */ | |
0 | 15 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,57 @@ |
1 | +#ifndef RENDERER_H | |
2 | +#define RENDERER_H | |
3 | + | |
4 | +#include "jthread.h" | |
5 | +#include "dprintf.h" | |
6 | +#include "stdint.h" | |
7 | +#include "string.h" | |
8 | +#include <string> | |
9 | +#include <list> | |
10 | +#include <sstream> | |
11 | +#include <iostream> | |
12 | +#include "listenerRenderer.h" | |
13 | +#include <lavidlib/net/StreamSocket.h> | |
14 | +#include <lavidlib/net/InetAddress.h> | |
15 | +#include <lavidlib/base/RuntimeException.h> | |
16 | +#include <lavidlib/net/UnknownHostException.h> | |
17 | +#include <lavidlib/net/SocketException.h> | |
18 | + | |
19 | +#define END_FLAG "FINALIZE" | |
20 | +// #define HOST "150.165.205.127" | |
21 | +// #define PORTNO 5555 | |
22 | +#define HOST "127.0.0.1" | |
23 | +#define PORTNO 12345 | |
24 | + | |
25 | +using namespace lavidlib; | |
26 | +using namespace jthread; | |
27 | +using namespace std; | |
28 | + | |
29 | +class Renderer : public Thread { | |
30 | +public: | |
31 | + Renderer(); | |
32 | + ~Renderer(); | |
33 | + | |
34 | + bool isSending(); | |
35 | + void receiveGlosa(std::string glosa, int64_t pts); | |
36 | + void addListener(ListenerRenderer* listener); | |
37 | + | |
38 | + void finalize(); | |
39 | + void Run(); | |
40 | + | |
41 | +private: | |
42 | + StreamSocket* core_socket; | |
43 | + list<ListenerRenderer*> * listeners; | |
44 | + | |
45 | + bool running; | |
46 | + int count_task; | |
47 | + int glosa_processed; | |
48 | + | |
49 | + string glosa_copy; | |
50 | + | |
51 | + void notifyListeners(); | |
52 | + void sendGlosa(); | |
53 | + void connectToUnity(); | |
54 | + void waitScreenShots(); | |
55 | +}; | |
56 | + | |
57 | +#endif /* RENDERER_H */ | ... | ... |
... | ... | @@ -0,0 +1,116 @@ |
1 | +#include "renderer.h" | |
2 | + | |
3 | +Renderer::Renderer() { | |
4 | + core_socket = new StreamSocket(); | |
5 | + listeners = new list<ListenerRenderer*>(); | |
6 | + running = true; | |
7 | + count_task = 0; | |
8 | + glosa_processed = 0; | |
9 | + DPRINTF("Done!\n"); | |
10 | +} | |
11 | + | |
12 | +Renderer::~Renderer() { | |
13 | + listeners->clear(); | |
14 | + delete listeners; | |
15 | + if(core_socket) delete core_socket; | |
16 | + DDDPRINTF("Renderer finalized!\n"); | |
17 | +} | |
18 | + | |
19 | +void Renderer::addListener(ListenerRenderer* listener) { | |
20 | + listeners->push_back(listener); | |
21 | +} | |
22 | + | |
23 | +void Renderer::notifyListeners() { | |
24 | + for (list<ListenerRenderer*>::iterator i = listeners->begin(); i != listeners->end(); i++) { | |
25 | + (*i)->notifyEndOfRenderization(); | |
26 | + } | |
27 | +} | |
28 | + | |
29 | +void Renderer::receiveGlosa(std::string glosa, int64_t pts) { | |
30 | + glosa_copy = glosa; | |
31 | + stringstream ss; | |
32 | + ss << pts; | |
33 | + glosa_copy += "#"; // '#' para identificar que vem um pts | |
34 | + glosa_copy += ss.str(); | |
35 | + count_task++; | |
36 | +} | |
37 | + | |
38 | +void Renderer::sendGlosa() { | |
39 | + try{ | |
40 | + int glosa_size = strlen(glosa_copy.c_str())+1; | |
41 | + char* glosa_buffer = new char[glosa_size]; | |
42 | + strcpy(glosa_buffer, glosa_copy.c_str()); | |
43 | + core_socket->write(glosa_buffer, glosa_size); | |
44 | + | |
45 | + char* ok_core = new char[3]; | |
46 | + do { | |
47 | + core_socket->read(ok_core, 3); //aguarda o unity confirmar o recebimento da glosa | |
48 | + }while(strcmp(ok_core, "OK") != 0); | |
49 | + glosa_processed++; | |
50 | + | |
51 | + delete [] ok_core; | |
52 | + delete [] glosa_buffer; | |
53 | + | |
54 | + }catch(lavidlib::RuntimeException &ex){ | |
55 | + throw lavidlib::RuntimeException(ex.getMessage().c_str()); | |
56 | + }catch(lavidlib::IOException &ex){ | |
57 | + throw lavidlib::RuntimeException(ex.getMessage().c_str()); | |
58 | + } | |
59 | +} | |
60 | + | |
61 | +void Renderer::connectToUnity() { | |
62 | + try{ | |
63 | + static InetAddress* addr = InetAddress::createByName(HOST); | |
64 | + core_socket->connect(addr, PORTNO); | |
65 | + }catch(lavidlib::UnknownHostException &ex){ | |
66 | + throw RuntimeException(ex.getMessage().c_str()); | |
67 | + }catch(lavidlib::SocketException &ex){ | |
68 | + throw RuntimeException(ex.getMessage().c_str()); | |
69 | + } | |
70 | +} | |
71 | + | |
72 | +void Renderer::waitScreenShots() { | |
73 | + char* endgeneration = new char[strlen(END_FLAG)+1]; | |
74 | + int connected; | |
75 | + try{ | |
76 | + do{ | |
77 | + | |
78 | + connected = core_socket->read(endgeneration, sizeof(endgeneration)); | |
79 | + }while(strcmp(endgeneration, END_FLAG) != 0 && connected != 0); | |
80 | + core_socket->close(); | |
81 | + notifyListeners(); | |
82 | + }catch(IOException &ex){ | |
83 | + throw RuntimeException(ex.getMessage().c_str()); | |
84 | + } | |
85 | +} | |
86 | + | |
87 | +void Renderer::finalize() { | |
88 | + while(glosa_processed < count_task) | |
89 | + usleep(10000); | |
90 | + receiveGlosa(END_FLAG, (int64_t) -1); | |
91 | + this->running = false; | |
92 | +} | |
93 | + | |
94 | +bool Renderer::isSending() { | |
95 | + if(glosa_processed < count_task) | |
96 | + return true; | |
97 | + else | |
98 | + return false; | |
99 | +} | |
100 | + | |
101 | +void Renderer::Run() { | |
102 | + try{ | |
103 | + while(running){ | |
104 | + while(count_task <= glosa_processed){ | |
105 | + usleep(10000); | |
106 | + } | |
107 | + if(!core_socket->isConnected()) | |
108 | + connectToUnity(); | |
109 | + sendGlosa(); | |
110 | + } | |
111 | + waitScreenShots(); | |
112 | + }catch(lavidlib::RuntimeException &ex){ | |
113 | + DDDDPRINTF("Erro: %s\n", ex.getMessage().c_str()); | |
114 | + throw RuntimeException(ex.getMessage().c_str()); | |
115 | + } | |
116 | +} | |
0 | 117 | \ No newline at end of file | ... | ... |
servico/src/include/serviceWindowGeneration.h
... | ... | @@ -5,18 +5,17 @@ |
5 | 5 | #include <iostream> |
6 | 6 | #include <stdint.h> |
7 | 7 | #include <string> |
8 | +#include <sstream> | |
8 | 9 | #include <locale> |
9 | 10 | #include "jthread.h" |
10 | 11 | #include "dprintf.h" |
11 | 12 | #include "Mixer.h" |
13 | +#include "renderer.h" | |
14 | +#include "listenerRenderer.h" | |
12 | 15 | #include "listenerTradutor.h" |
13 | 16 | #include "tradutorPortGlosa.h" |
14 | 17 | #include "serviceException.h" |
15 | 18 | #include <json/json.h> |
16 | -#include <lavidlib/net/StreamSocket.h> | |
17 | -#include <lavidlib/net/InetAddress.h> | |
18 | -#include <lavidlib/net/UnknownHostException.h> | |
19 | -#include <lavidlib/net/SocketException.h> | |
20 | 19 | #include <lavidlib/base/RuntimeException.h> |
21 | 20 | |
22 | 21 | #define DEVELOPER "devel" |
... | ... | @@ -24,11 +23,8 @@ |
24 | 23 | #define PATH_DEVEL_CONTENTS "vlibras_user/vlibras-contents/videos/" |
25 | 24 | #define PATH_DEVEL_UPLOADS "vlibras_user/vlibras-contents/uploads/" |
26 | 25 | #define PATH_VBOX_UPLOADS "vlibras_user/.vlibras-conf/uploads/" |
27 | -#define PATH_CONF_FILE "vlibras_user/.vlibras-conf/param.json" | |
26 | +#define PATH_CONF_FILE "vlibras_user/.vlibras-conf/params.json" | |
28 | 27 | #define MAX_SIZE_PATH 256 |
29 | -#define END_NOTIFICATION "FINALIZE" | |
30 | -#define HOST "127.0.0.1" | |
31 | -#define PORTNO 12345 | |
32 | 28 | |
33 | 29 | using namespace Json; |
34 | 30 | using namespace Tradutor; |
... | ... | @@ -40,8 +36,8 @@ class ServiceWindowGeneration { |
40 | 36 | |
41 | 37 | protected: |
42 | 38 | TradutorPortGlosa* tradutor; |
39 | + Renderer* renderer; | |
43 | 40 | Mixer* mixer; |
44 | - StreamSocket* core_socket; | |
45 | 41 | |
46 | 42 | Value root; |
47 | 43 | Reader reader; |
... | ... | @@ -68,10 +64,6 @@ protected: |
68 | 64 | |
69 | 65 | virtual void setSizeOfSubtitles(int sub_size) = 0; |
70 | 66 | virtual void setPathContents() = 0; |
71 | - | |
72 | - virtual void connectToUnity() = 0; | |
73 | - virtual void sendGlosa(string glosa) = 0; | |
74 | - virtual void waitVideoGeneration() = 0; | |
75 | 67 | |
76 | 68 | public: |
77 | 69 | virtual void initialize() = 0; | ... | ... |
servico/src/include/serviceWindowGenerationFromRec.h
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | #define SERVICE_TYPE_REC 2 |
9 | 9 | #define SERVICE_TYPE_REC_ONLY_AUDIO 5 |
10 | 10 | |
11 | -class ServiceWindowGenerationFromRec : public ServiceWindowGeneration, public RecognizeListener, public ListenerTradutor, public Thread { | |
11 | +class ServiceWindowGenerationFromRec : public ServiceWindowGeneration, public RecognizeListener, public ListenerTradutor, public ListenerRenderer, public Thread { | |
12 | 12 | |
13 | 13 | private: |
14 | 14 | Recognize* rec; |
... | ... | @@ -16,11 +16,6 @@ private: |
16 | 16 | void addPTS(int64_t pts); |
17 | 17 | void setSizeOfSubtitles(int sub_size); |
18 | 18 | void setPathContents(); |
19 | - | |
20 | - void connectToUnity(); | |
21 | - void sendGlosa(string glosa); | |
22 | - void waitVideoGeneration(); | |
23 | - | |
24 | 19 | bool isRunning(); |
25 | 20 | public: |
26 | 21 | ServiceWindowGenerationFromRec(char* pathVideo, int sublanguage, int position, int size, |
... | ... | @@ -30,6 +25,7 @@ public: |
30 | 25 | ~ServiceWindowGenerationFromRec(); |
31 | 26 | |
32 | 27 | void notifyTextRecognized(unsigned char* text, int64_t pts); |
28 | + void notifyEndOfRenderization(); | |
33 | 29 | void notifyTranslation(vector<string>* glosas); |
34 | 30 | void notifyTranslator(unsigned char* text); |
35 | 31 | void notifyEnd(int sentences_size); | ... | ... |
servico/src/include/serviceWindowGenerationFromSRT.h
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | #define SERVICE_TYPE_SRT 1 |
9 | 9 | #define SERVICE_TYPE_SRT_ONLY 4 |
10 | 10 | |
11 | -class ServiceWindowGenerationFromSRT : public ServiceWindowGeneration, public ListenerSRT, public ListenerTradutor, public Thread { | |
11 | +class ServiceWindowGenerationFromSRT : public ServiceWindowGeneration, public ListenerSRT, public ListenerTradutor, public ListenerRenderer, public Thread { | |
12 | 12 | |
13 | 13 | private: |
14 | 14 | ExtratorFactory* extrator_factory; |
... | ... | @@ -19,11 +19,6 @@ private: |
19 | 19 | void addPTS(int64_t pts); |
20 | 20 | void setSizeOfSubtitles(int sub_size); |
21 | 21 | void setPathContents(); |
22 | - | |
23 | - void connectToUnity(); | |
24 | - void sendGlosa(string glosa); | |
25 | - void waitVideoGeneration(); | |
26 | - | |
27 | 22 | bool isRunning(); |
28 | 23 | public: |
29 | 24 | //construtor de serviço de video e legenda |
... | ... | @@ -34,6 +29,7 @@ public: |
34 | 29 | ~ServiceWindowGenerationFromSRT(); |
35 | 30 | |
36 | 31 | void notifySubtitle(unsigned char* subtitle, int64_t pts); |
32 | + void notifyEndOfRenderization(); | |
37 | 33 | void notifyTranslation(vector<string>* glosas); |
38 | 34 | void notifyTranslator(unsigned char* text); |
39 | 35 | void notifyEnd(int sub_size); | ... | ... |
servico/src/include/serviceWindowGenerationFromText.h
... | ... | @@ -7,7 +7,7 @@ |
7 | 7 | |
8 | 8 | #define MAX_TEXT_SIZE 2048 //FIXME: está restrito a 2K bytes de texto |
9 | 9 | |
10 | -class ServiceWindowGenerationFromText : public ServiceWindowGeneration, public ListenerTXT, public ListenerTradutor, public Thread { | |
10 | +class ServiceWindowGenerationFromText : public ServiceWindowGeneration, public ListenerTXT, public ListenerTradutor, public ListenerRenderer, public Thread { | |
11 | 11 | |
12 | 12 | private: |
13 | 13 | ExtratorFactory* extrator_factory; |
... | ... | @@ -15,17 +15,13 @@ private: |
15 | 15 | |
16 | 16 | void setSizeOfSubtitles(int sub_size); |
17 | 17 | void setPathContents(); |
18 | - | |
19 | - void connectToUnity(); | |
20 | - void sendGlosa(string glosa); | |
21 | - void waitVideoGeneration(); | |
22 | - | |
23 | 18 | bool isRunning(); |
24 | 19 | public: |
25 | 20 | ServiceWindowGenerationFromText(char* pathFile, int transparency, char* id, char* client); |
26 | 21 | ~ServiceWindowGenerationFromText(); |
27 | 22 | |
28 | 23 | void notifyLine(unsigned char* line); |
24 | + void notifyEndOfRenderization(); | |
29 | 25 | void notifyTranslation(vector<string>* glosas); |
30 | 26 | void notifyTranslator(unsigned char* text); |
31 | 27 | void notifyEnd(int line_size); | ... | ... |
servico/src/serviceWindowGenerationFromRec.cpp
... | ... | @@ -15,6 +15,7 @@ ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec( |
15 | 15 | vetor_pts = new vector<int64_t >(); |
16 | 16 | rec = new Recognize(pathVideo, id, rate); |
17 | 17 | tradutor = new TradutorPortGlosa(); |
18 | + renderer = new Renderer(); | |
18 | 19 | running = true; |
19 | 20 | finish = false; |
20 | 21 | DPRINTF("Done!\n"); |
... | ... | @@ -36,6 +37,7 @@ ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec( |
36 | 37 | vetor_pts = new vector<int64_t >(); |
37 | 38 | rec = new Recognize(path_input, id); |
38 | 39 | tradutor = new TradutorPortGlosa(); |
40 | + renderer = new Renderer(); | |
39 | 41 | try{ |
40 | 42 | setPathContents(); |
41 | 43 | }catch(RuntimeException ex){ |
... | ... | @@ -49,6 +51,7 @@ ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec( |
49 | 51 | ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){ |
50 | 52 | free(vetor_pts); |
51 | 53 | if (tradutor) delete tradutor; |
54 | + if (renderer) delete renderer; | |
52 | 55 | if (rec) delete rec; |
53 | 56 | if (mixer) delete mixer; |
54 | 57 | DDDPRINTF("Service Rec finished!\n"); |
... | ... | @@ -80,8 +83,11 @@ void ServiceWindowGenerationFromRec::setPathContents(){ |
80 | 83 | void ServiceWindowGenerationFromRec::setSizeOfSubtitles(int sub_size){ |
81 | 84 | numero_legendas = sub_size; |
82 | 85 | if (legendas_enviadas >= numero_legendas){ |
83 | - sendGlosa(END_NOTIFICATION); | |
84 | - waitVideoGeneration(); | |
86 | + try{ | |
87 | + renderer->finalize(); | |
88 | + }catch(lavidlib::RuntimeException &ex){ | |
89 | + throw ServiceException(ex.getMessage().c_str()); | |
90 | + } | |
85 | 91 | } |
86 | 92 | } |
87 | 93 | |
... | ... | @@ -100,23 +106,36 @@ void ServiceWindowGenerationFromRec::notifyTextRecognized(unsigned char* text, i |
100 | 106 | } |
101 | 107 | |
102 | 108 | void ServiceWindowGenerationFromRec::notifyTranslation(vector<string> * glosas) { |
109 | + string glosa = ""; | |
103 | 110 | for (int i = 0; i < glosas->size(); i++) { |
104 | 111 | locale loc; |
105 | 112 | string glosa_lower = ""; |
106 | 113 | for (int k = 0; k < glosas->at(i).length(); k++){ |
107 | 114 | glosa_lower += std::tolower(glosas->at(i).at(k), loc); |
108 | 115 | } |
109 | - //int64_t pts_notificado = vetor_pts->front(); | |
110 | - sendGlosa(glosa_lower); | |
116 | + glosa += glosa_lower; | |
117 | + glosa += " "; | |
118 | + | |
119 | + } | |
120 | + int64_t pts_notificado = vetor_pts->front(); | |
121 | + while(renderer->isSending()) | |
122 | + usleep(10000); | |
123 | + try{ | |
124 | + renderer->receiveGlosa(glosa, pts_notificado); | |
125 | + legendas_enviadas++; | |
126 | + }catch(lavidlib::RuntimeException &ex){ | |
127 | + throw ServiceException(ex.getMessage().c_str()); | |
111 | 128 | } |
112 | - | |
113 | 129 | vetor_pts->erase(vetor_pts->begin()); |
114 | 130 | legendas_enviadas++; |
115 | 131 | } |
116 | 132 | |
133 | +void ServiceWindowGenerationFromRec::notifyEndOfRenderization() { | |
134 | + running = false; | |
135 | +} | |
136 | + | |
117 | 137 | void ServiceWindowGenerationFromRec::notifyEnd(int sentences_size){ |
118 | 138 | DPRINTF("Service REC recebeu: %d sentenças.\n", sentences_size); |
119 | - cout << "chamou: " << sentences_size << endl; | |
120 | 139 | setSizeOfSubtitles(sentences_size); |
121 | 140 | } |
122 | 141 | |
... | ... | @@ -133,54 +152,19 @@ void ServiceWindowGenerationFromRec::initialize(){ |
133 | 152 | |
134 | 153 | rec->addListener(this); |
135 | 154 | tradutor->addListener(this); |
136 | - connectToUnity(); | |
137 | - | |
155 | + renderer->addListener(this); | |
156 | + | |
138 | 157 | try{ |
158 | + renderer->Start(); | |
139 | 159 | rec->initialize(); |
140 | 160 | } catch(RecognizeException ex){ |
141 | 161 | throw ServiceException(ex.getMessage()); |
142 | - } | |
162 | + } catch(RuntimeException &ex){ | |
163 | + throw ServiceException(ex.getMessage().c_str()); | |
164 | + } | |
143 | 165 | this->Start(); |
144 | 166 | } |
145 | 167 | |
146 | -void ServiceWindowGenerationFromRec::sendGlosa(string glosa) { | |
147 | - char* glosa_buffer = new char[strlen(glosa.c_str())+1]; | |
148 | - strcpy(glosa_buffer, glosa.c_str()); | |
149 | - int tamanho = strlen(glosa_buffer)+1; | |
150 | - //cout << "Enviando glosa: " << glosa_buffer << endl; | |
151 | - core_socket->write(glosa_buffer, tamanho); | |
152 | - delete [] glosa_buffer; | |
153 | - | |
154 | - char* ok_core = new char[4]; | |
155 | - core_socket->read(ok_core, 4); //aguarda o unity confirmar o recebimento da glosa | |
156 | - delete [] ok_core; | |
157 | -} | |
158 | - | |
159 | -void ServiceWindowGenerationFromRec::connectToUnity() { | |
160 | - core_socket = new StreamSocket(); | |
161 | - try{ | |
162 | - static InetAddress* addr = InetAddress::createByName(HOST); | |
163 | - core_socket->connect(addr, PORTNO); | |
164 | - }catch(UnknownHostException ex){ | |
165 | - throw ServiceException(ex.getMessage()); | |
166 | - }catch(SocketException ex){ | |
167 | - throw ServiceException(ex.getMessage()); | |
168 | - } | |
169 | -} | |
170 | - | |
171 | -void ServiceWindowGenerationFromRec::waitVideoGeneration() { | |
172 | - char* endgeneration = new char[strlen(END_NOTIFICATION) + 1]; | |
173 | - try{ | |
174 | - do{ | |
175 | - core_socket->read(endgeneration, sizeof(endgeneration)); | |
176 | - }while(strcmp(endgeneration, END_NOTIFICATION) != 0); | |
177 | - core_socket->close(); | |
178 | - this->running = false; | |
179 | - }catch(IOException ex){ | |
180 | - throw ServiceException(ex.getMessage()); | |
181 | - } | |
182 | -} | |
183 | - | |
184 | 168 | void ServiceWindowGenerationFromRec::Run(){ |
185 | 169 | while(isRunning()){ |
186 | 170 | usleep(200000); | ... | ... |
servico/src/serviceWindowGenerationFromSRT.cpp
... | ... | @@ -16,6 +16,7 @@ ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* pathVideo, |
16 | 16 | legendas_enviadas = 0; |
17 | 17 | vetor_pts = new vector<int64_t >(); |
18 | 18 | tradutor = new TradutorPortGlosa(); |
19 | + renderer = new Renderer(); | |
19 | 20 | extrator_factory = new ExtratorFactory(); |
20 | 21 | try{ |
21 | 22 | setPathContents(); |
... | ... | @@ -38,6 +39,7 @@ ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* pathSRT, in |
38 | 39 | legendas_enviadas = 0; |
39 | 40 | vetor_pts = new vector<int64_t >(); |
40 | 41 | tradutor = new TradutorPortGlosa(); |
42 | + renderer = new Renderer(); | |
41 | 43 | extrator_factory = new ExtratorFactory(); |
42 | 44 | try{ |
43 | 45 | setPathContents(); |
... | ... | @@ -53,6 +55,7 @@ ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() { |
53 | 55 | free(vetor_pts); |
54 | 56 | if (mixer) delete mixer; |
55 | 57 | if (tradutor) delete tradutor; |
58 | + if (renderer) delete renderer; | |
56 | 59 | if (extratorSRT)delete extratorSRT; |
57 | 60 | if (extrator_factory) delete extrator_factory; |
58 | 61 | DDDPRINTF("Service SRT finalized!\n"); |
... | ... | @@ -80,8 +83,11 @@ void ServiceWindowGenerationFromSRT::setPathContents() { |
80 | 83 | void ServiceWindowGenerationFromSRT::setSizeOfSubtitles(int sub_size) { |
81 | 84 | numero_legendas = sub_size; |
82 | 85 | if (legendas_enviadas >= numero_legendas){ |
83 | - sendGlosa(END_NOTIFICATION); | |
84 | - waitVideoGeneration(); | |
86 | + try{ | |
87 | + renderer->finalize(); | |
88 | + }catch(lavidlib::RuntimeException &ex){ | |
89 | + throw ServiceException(ex.getMessage().c_str()); | |
90 | + } | |
85 | 91 | } |
86 | 92 | } |
87 | 93 | |
... | ... | @@ -103,20 +109,34 @@ void ServiceWindowGenerationFromSRT::notifySubtitle(unsigned char *subtitle, int |
103 | 109 | } |
104 | 110 | |
105 | 111 | void ServiceWindowGenerationFromSRT::notifyTranslation(vector<string> * glosas) { |
112 | + string glosa = ""; | |
106 | 113 | for (int i = 0; i < glosas->size(); i++) { |
107 | 114 | locale loc; |
108 | 115 | string glosa_lower = ""; |
109 | 116 | for (int k = 0; k < glosas->at(i).length(); k++){ |
110 | 117 | glosa_lower += std::tolower(glosas->at(i).at(k), loc); |
111 | 118 | } |
112 | - sendGlosa(glosa_lower); | |
113 | - //int64_t pts_notificado = vetor_pts->front(); | |
119 | + glosa += glosa_lower; | |
120 | + glosa += " "; | |
121 | + | |
114 | 122 | } |
115 | - | |
123 | + int64_t pts_notificado = vetor_pts->front(); | |
124 | + while(renderer->isSending()) | |
125 | + usleep(10000); | |
126 | + try{ | |
127 | + renderer->receiveGlosa(glosa, pts_notificado); | |
128 | + legendas_enviadas++; | |
129 | + }catch(lavidlib::RuntimeException &ex){ | |
130 | + throw ServiceException(ex.getMessage().c_str()); | |
131 | + } | |
116 | 132 | vetor_pts->erase(vetor_pts->begin()); |
117 | 133 | legendas_enviadas++; |
118 | 134 | } |
119 | 135 | |
136 | +void ServiceWindowGenerationFromSRT::notifyEndOfRenderization() { | |
137 | + this->running = false; | |
138 | +} | |
139 | + | |
120 | 140 | void ServiceWindowGenerationFromSRT::notifyEnd(int sub_size) { |
121 | 141 | DPRINTF("Service SRT recebeu: %d legendas.\n", sub_size); |
122 | 142 | setSizeOfSubtitles(sub_size); |
... | ... | @@ -137,59 +157,19 @@ void ServiceWindowGenerationFromSRT::initialize() { |
137 | 157 | extratorSRT->setFilePath(path_srt); |
138 | 158 | |
139 | 159 | tradutor->addListener(this); |
140 | - | |
141 | - // if (service_type != SERVICE_TYPE_SRT) { | |
142 | - // uint64_t pcr_base = (uint64_t) 1000; //FIXME: macro | |
143 | - // } | |
144 | - | |
145 | - connectToUnity(); | |
146 | - | |
147 | - try{ | |
160 | + renderer->addListener(this); | |
161 | + | |
162 | + try{ | |
163 | + renderer->Start(); | |
148 | 164 | extratorSRT->initialize(); |
149 | 165 | }catch(ExtratorException ex){ |
150 | 166 | throw ServiceException(ex.getMessage()); |
167 | + }catch(RuntimeException &ex){ | |
168 | + throw ServiceException(ex.getMessage().c_str()); | |
151 | 169 | } |
152 | 170 | this->Start(); |
153 | 171 | } |
154 | 172 | |
155 | -void ServiceWindowGenerationFromSRT::sendGlosa(string glosa) { | |
156 | - char* glosa_buffer = new char[strlen(glosa.c_str())+1]; | |
157 | - strcpy(glosa_buffer, glosa.c_str()); | |
158 | - int tamanho = strlen(glosa_buffer)+1; | |
159 | - //cout << "Enviando glosa: " << glosa_buffer << endl; | |
160 | - core_socket->write(glosa_buffer, tamanho); | |
161 | - delete [] glosa_buffer; | |
162 | - | |
163 | - char* ok_core = new char[3]; | |
164 | - core_socket->read(ok_core, 3); //aguarda o unity confirmar o recebimento da glosa | |
165 | - delete [] ok_core; | |
166 | -} | |
167 | - | |
168 | -void ServiceWindowGenerationFromSRT::connectToUnity() { | |
169 | - core_socket = new StreamSocket(); | |
170 | - try{ | |
171 | - static InetAddress* addr = InetAddress::createByName(HOST); | |
172 | - core_socket->connect(addr, PORTNO); | |
173 | - }catch(UnknownHostException ex){ | |
174 | - throw ServiceException(ex.getMessage()); | |
175 | - }catch(SocketException ex){ | |
176 | - throw ServiceException(ex.getMessage()); | |
177 | - } | |
178 | -} | |
179 | - | |
180 | -void ServiceWindowGenerationFromSRT::waitVideoGeneration() { | |
181 | - char* endgeneration = new char[strlen(END_NOTIFICATION) + 1]; | |
182 | - try{ | |
183 | - do{ | |
184 | - core_socket->read(endgeneration, sizeof(endgeneration)); | |
185 | - }while(strcmp(endgeneration, END_NOTIFICATION) != 0); | |
186 | - core_socket->close(); | |
187 | - this->running = false; | |
188 | - }catch(IOException ex){ | |
189 | - throw ServiceException(ex.getMessage()); | |
190 | - } | |
191 | -} | |
192 | - | |
193 | 173 | void ServiceWindowGenerationFromSRT::Run() { |
194 | 174 | while(isRunning()){ |
195 | 175 | usleep(200000); | ... | ... |
servico/src/serviceWindowGenerationFromText.cpp
... | ... | @@ -11,6 +11,7 @@ ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, |
11 | 11 | legendas_enviadas = 0; |
12 | 12 | vetor_pts = new vector<int64_t >(); |
13 | 13 | tradutor = new TradutorPortGlosa(); |
14 | + renderer = new Renderer(); | |
14 | 15 | extrator_factory = new ExtratorFactory(); |
15 | 16 | try{ |
16 | 17 | setPathContents(); |
... | ... | @@ -25,6 +26,7 @@ ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, |
25 | 26 | ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() { |
26 | 27 | free(vetor_pts); |
27 | 28 | if (tradutor) delete tradutor; |
29 | + if (renderer) delete renderer; | |
28 | 30 | if (extratorTXT)delete extratorTXT; |
29 | 31 | if (extrator_factory) delete extrator_factory; |
30 | 32 | DDDPRINTF("Service Text finalized!\n"); |
... | ... | @@ -48,10 +50,13 @@ void ServiceWindowGenerationFromText::setPathContents() { |
48 | 50 | } |
49 | 51 | |
50 | 52 | void ServiceWindowGenerationFromText::setSizeOfSubtitles(int sub_size) { |
51 | - numero_legendas = sub_size; | |
53 | + numero_legendas = sub_size; | |
52 | 54 | if (legendas_enviadas >= numero_legendas){ |
53 | - sendGlosa(END_NOTIFICATION); | |
54 | - waitVideoGeneration(); | |
55 | + try{ | |
56 | + renderer->finalize(); | |
57 | + }catch(lavidlib::RuntimeException &ex){ | |
58 | + throw ServiceException(ex.getMessage().c_str()); | |
59 | + } | |
55 | 60 | } |
56 | 61 | } |
57 | 62 | |
... | ... | @@ -64,15 +69,30 @@ void ServiceWindowGenerationFromText::notifyLine(unsigned char* line) { |
64 | 69 | } |
65 | 70 | |
66 | 71 | void ServiceWindowGenerationFromText::notifyTranslation(vector<string> * glosas) { |
72 | + string glosa = ""; | |
67 | 73 | for (int i = 0; i < glosas->size(); i++) { |
68 | 74 | locale loc; |
69 | 75 | string glosa_lower = ""; |
70 | 76 | for (int k = 0; k < glosas->at(i).length(); k++){ |
71 | 77 | glosa_lower += std::tolower(glosas->at(i).at(k), loc); |
72 | 78 | } |
73 | - sendGlosa(glosa_lower); | |
79 | + glosa += glosa_lower; | |
80 | + glosa += " "; | |
81 | + } | |
82 | + while(renderer->isSending()){ | |
83 | + usleep(10000); | |
84 | + } // aguarda o renderizador processar a glosa anterior | |
85 | + | |
86 | + try{ | |
87 | + renderer->receiveGlosa(glosa, (int64_t) -1); | |
88 | + legendas_enviadas++; | |
89 | + }catch(lavidlib::RuntimeException &ex){ | |
90 | + throw ServiceException(ex.getMessage().c_str()); | |
74 | 91 | } |
75 | - legendas_enviadas++; | |
92 | +} | |
93 | + | |
94 | +void ServiceWindowGenerationFromText::notifyEndOfRenderization() { | |
95 | + this->running = false; | |
76 | 96 | } |
77 | 97 | |
78 | 98 | void ServiceWindowGenerationFromText::notifyEnd(int line_size) { |
... | ... | @@ -95,55 +115,19 @@ void ServiceWindowGenerationFromText::initialize() { |
95 | 115 | extratorTXT->setFilePath(path_input); |
96 | 116 | |
97 | 117 | tradutor->addListener(this); |
98 | - | |
99 | - connectToUnity(); | |
100 | - | |
118 | + renderer->addListener(this); | |
119 | + | |
101 | 120 | try{ |
121 | + renderer->Start(); | |
102 | 122 | extratorTXT->initialize(); |
103 | 123 | }catch(ExtratorException ex){ |
104 | - throw ServiceException(ex.getMessage()); | |
124 | + throw ServiceException(ex.getMessage().c_str()); | |
125 | + }catch(RuntimeException &ex){ | |
126 | + throw ServiceException(ex.getMessage().c_str()); | |
105 | 127 | } |
106 | 128 | this->Start(); |
107 | 129 | } |
108 | 130 | |
109 | -void ServiceWindowGenerationFromText::sendGlosa(string glosa) { | |
110 | - char* glosa_buffer = new char[strlen(glosa.c_str())+1]; | |
111 | - strcpy(glosa_buffer, glosa.c_str()); | |
112 | - int tamanho = strlen(glosa_buffer)+1; | |
113 | - //cout << "Enviando glosa: " << glosa_buffer << endl; | |
114 | - core_socket->write(glosa_buffer, tamanho); | |
115 | - delete [] glosa_buffer; | |
116 | - | |
117 | - char* ok_core = new char[3]; | |
118 | - core_socket->read(ok_core, 3); //aguarda o unity confirmar o recebimento da glosa | |
119 | - delete [] ok_core; | |
120 | -} | |
121 | - | |
122 | -void ServiceWindowGenerationFromText::connectToUnity() { | |
123 | - core_socket = new StreamSocket(); | |
124 | - try{ | |
125 | - static InetAddress* addr = InetAddress::createByName(HOST); | |
126 | - core_socket->connect(addr, PORTNO); | |
127 | - }catch(UnknownHostException ex){ | |
128 | - throw ServiceException(ex.getMessage()); | |
129 | - }catch(SocketException ex){ | |
130 | - throw ServiceException(ex.getMessage()); | |
131 | - } | |
132 | -} | |
133 | - | |
134 | -void ServiceWindowGenerationFromText::waitVideoGeneration() { | |
135 | - char* endgeneration = new char[strlen(END_NOTIFICATION) + 1]; | |
136 | - try{ | |
137 | - do{ | |
138 | - core_socket->read(endgeneration, sizeof(endgeneration)); | |
139 | - }while(strcmp(endgeneration, END_NOTIFICATION) != 0); | |
140 | - core_socket->close(); | |
141 | - this->running = false; | |
142 | - }catch(IOException ex){ | |
143 | - throw ServiceException(ex.getMessage()); | |
144 | - } | |
145 | -} | |
146 | - | |
147 | 131 | void ServiceWindowGenerationFromText::Run(){ |
148 | 132 | while(isRunning()){ |
149 | 133 | usleep(200000); | ... | ... |