Commit 252c7e28d0b08ef7c609e29530828fdee3c3471e
1 parent
70e7acfe
Exists in
master
and in
4 other branches
Integration with renderer for the new feature
Showing
2 changed files
with
220 additions
and
2 deletions
Show diff stats
@@ -0,0 +1,208 @@ | @@ -0,0 +1,208 @@ | ||
1 | +#!/usr/bin/env python | ||
2 | +# -*- coding: utf-8 -*- | ||
3 | + | ||
4 | +""" | ||
5 | +Author: Erickson Silva | ||
6 | +E-Mail: erickson.silva@lavid.ufpb.br | ||
7 | + | ||
8 | +Author: Jonathan Lincoln Brilhante | ||
9 | +E-Mail: jonathan.lincoln.brilhante@gmail.com | ||
10 | + | ||
11 | +Author: Wesnydy Lima Ribeiro | ||
12 | +E-Mail: wesnydy@lavid.ufpb.br | ||
13 | +""" | ||
14 | + | ||
15 | +import os | ||
16 | +import pika | ||
17 | +import PikaManager | ||
18 | +import signal | ||
19 | +import socket | ||
20 | +import subprocess | ||
21 | + | ||
22 | +from pyvirtualdisplay import Display | ||
23 | +from thread import start_new_thread | ||
24 | +from time import sleep | ||
25 | + | ||
26 | +#Temporary | ||
27 | +from shutil import rmtree | ||
28 | + | ||
29 | +# Manager of queues connections. | ||
30 | +manager = PikaManager.PikaManager("localhost") | ||
31 | + | ||
32 | +TCP_IP = '0.0.0.0' | ||
33 | +TCP_PORT = 5555 | ||
34 | + | ||
35 | +CONTROL_MESSAGE = "FINALIZE" | ||
36 | +DEFAULT_PTS = "#-1" | ||
37 | + | ||
38 | +PATH_LIBRAS = os.getenv("VLIBRAS_VIDEO_LIBRAS") | ||
39 | +VIDEO_CREATOR = os.getenv("VLIBRAS_VIDEO_CREATOR") | ||
40 | + | ||
41 | +#Temporary | ||
42 | +PATH_SCREENS = "/storage/frames/" | ||
43 | + | ||
44 | +# Status of renderer to process new requests. Answer one request at a time. | ||
45 | +worker_available = True | ||
46 | +# pyvirtualdisplay instance | ||
47 | +display = None | ||
48 | +# ffmpeg process instance | ||
49 | +ffmpeg = None | ||
50 | + | ||
51 | +def start_video_creator(id): | ||
52 | + """ | ||
53 | + Start video creator server. | ||
54 | + | ||
55 | + Parameters | ||
56 | + ---------- | ||
57 | + id : string | ||
58 | + Identification of request. | ||
59 | + """ | ||
60 | + global display, ffmpeg | ||
61 | + # logger.info("Starting video creator server") | ||
62 | + display = Display(visible=1, size=(800,600)) | ||
63 | + display.start() | ||
64 | + subprocess.call( | ||
65 | + [ | ||
66 | + VIDEO_CREATOR, | ||
67 | + id, | ||
68 | + "0", | ||
69 | + "30", | ||
70 | + "20", | ||
71 | + "25", | ||
72 | + "-screen-fullscreen", "1", | ||
73 | + "-screen-quality", "Fantastic", | ||
74 | + "-force-opengl" | ||
75 | + ], | ||
76 | + shell=False | ||
77 | + ) | ||
78 | + ffmpeg.send_signal(signal.SIGQUIT) | ||
79 | + ffmpeg.communicate() | ||
80 | + display.stop() | ||
81 | + | ||
82 | +def start_ffmpeg(id): | ||
83 | + """ | ||
84 | + Start FFmpeg to capture the video creator display. | ||
85 | + | ||
86 | + Parameters | ||
87 | + ---------- | ||
88 | + id : string | ||
89 | + Identification of request. | ||
90 | + """ | ||
91 | + global ffmpeg, display | ||
92 | + # logger.info("Starting ffmpeg") | ||
93 | + libras_video = os.path.join(PATH_LIBRAS, id + ".mp4") | ||
94 | + ffmpeg = subprocess.Popen( | ||
95 | + [ | ||
96 | + "ffmpeg", | ||
97 | + "-y", | ||
98 | + "-loglevel", "quiet", | ||
99 | + "-video_size", "800x600", | ||
100 | + "-r", "30", | ||
101 | + "-f", "x11grab", | ||
102 | + "-draw_mouse", "0", | ||
103 | + "-i", str(display.cmd_param[-1]) + ".0+nomouse", | ||
104 | + "-vcodec", "libx264", | ||
105 | + "-pix_fmt", "yuv420p", | ||
106 | + "-an", | ||
107 | + libras_video | ||
108 | + ], | ||
109 | + shell=False | ||
110 | + ) | ||
111 | + | ||
112 | +def open_socket_connection(): | ||
113 | + """ | ||
114 | + Create a new socket TCP connection with video creator server. | ||
115 | + | ||
116 | + Returns | ||
117 | + ------- | ||
118 | + socket object | ||
119 | + Connection with video creator server. | ||
120 | + """ | ||
121 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
122 | + # logger.info("Opening connection with video creator") | ||
123 | + while True: | ||
124 | + try: | ||
125 | + s.connect((TCP_IP, TCP_PORT)) | ||
126 | + break | ||
127 | + except: | ||
128 | + sleep(2) | ||
129 | + return s | ||
130 | + | ||
131 | +def send_to_video_creator(id, message): | ||
132 | + # Stablishes connection with video creator server. | ||
133 | + socket = open_socket_connection() | ||
134 | + # Send gloss to video creator. | ||
135 | + socket.send(message.encode("utf-8")+DEFAULT_PTS) | ||
136 | + # Receive a response from the video creator | ||
137 | + socket.recv(3) | ||
138 | + # Send the control message to video creator | ||
139 | + socket.send(CONTROL_MESSAGE+DEFAULT_PTS) | ||
140 | + # Start ffmpeg to capture the video creator display. | ||
141 | + start_ffmpeg(id) | ||
142 | + # Close the connection with video creator server. | ||
143 | + socket.close() | ||
144 | + | ||
145 | +def run(ch, method, properties, body): | ||
146 | + """ | ||
147 | + Execute the worker. | ||
148 | + | ||
149 | + Parameters | ||
150 | + ---------- | ||
151 | + ch : object | ||
152 | + Channel of communication. | ||
153 | + method : function | ||
154 | + Callback method. | ||
155 | + properties : object | ||
156 | + Message containing a set of 14 properties. | ||
157 | + body : string | ||
158 | + Json string containing the necessary arguments for workers. | ||
159 | + """ | ||
160 | + global worker_available | ||
161 | + | ||
162 | + if worker_available: | ||
163 | + worker_available = False | ||
164 | + start_new_thread(send_to_video_creator, (properties.correlation_id, body)) | ||
165 | + start_video_creator(properties.correlation_id) | ||
166 | + | ||
167 | + body = properties.correlation_id + ".mp4" | ||
168 | + manager.send_to_queue("videos", body, properties) | ||
169 | + | ||
170 | + clean(properties.correlation_id) | ||
171 | + worker_available = True | ||
172 | + else: | ||
173 | + ch.basic_reject(delivery_tag=method.delivery_tag, requeue=True) | ||
174 | + | ||
175 | +#Temporary | ||
176 | +def clean(id): | ||
177 | + # logger.info("Cleaning screens files") | ||
178 | + path = os.path.join(PATH_SCREENS, id) | ||
179 | + rmtree(path, ignore_errors=True) | ||
180 | + | ||
181 | +def keep_alive(conn_send, conn_receive): | ||
182 | + """ | ||
183 | + Keep the connection alive. | ||
184 | + | ||
185 | + Parameters | ||
186 | + ---------- | ||
187 | + conn_send : object | ||
188 | + Connection of writer. | ||
189 | + conn_receive : object | ||
190 | + Connection of receiver. | ||
191 | + """ | ||
192 | + while True: | ||
193 | + sleep(30) | ||
194 | + try: | ||
195 | + conn_send.process_data_events() | ||
196 | + conn_receive.process_data_events() | ||
197 | + except: | ||
198 | + continue | ||
199 | + | ||
200 | +# start_new_thread(keep_alive, (manager.get_conn_send(), manager.get_conn_receive())) | ||
201 | + | ||
202 | +print("Renderer listening...") | ||
203 | +while True: | ||
204 | + try: | ||
205 | + manager.receive_from_queue("translations", run) | ||
206 | + except KeyboardInterrupt: | ||
207 | + manager.close_connections() | ||
208 | + os._exit(0) |
core/translator.py
@@ -12,6 +12,7 @@ Author: Wesnydy Lima Ribeiro | @@ -12,6 +12,7 @@ Author: Wesnydy Lima Ribeiro | ||
12 | E-Mail: wesnydy@lavid.ufpb.br | 12 | E-Mail: wesnydy@lavid.ufpb.br |
13 | """ | 13 | """ |
14 | 14 | ||
15 | +import json | ||
15 | import os | 16 | import os |
16 | import pika | 17 | import pika |
17 | import PikaManager | 18 | import PikaManager |
@@ -37,9 +38,18 @@ def run(ch, method, properties, body): | @@ -37,9 +38,18 @@ def run(ch, method, properties, body): | ||
37 | body : string | 38 | body : string |
38 | Json string containing the necessary arguments for workers. | 39 | Json string containing the necessary arguments for workers. |
39 | """ | 40 | """ |
41 | + body = json.loads(body) | ||
40 | print ("Translating...") | 42 | print ("Translating...") |
41 | - gloss = traduzir(body) | ||
42 | - manager.send_to_queue("glosses", gloss, properties) | 43 | + try: |
44 | + gloss = traduzir(body["text"].encode("utf8")) | ||
45 | + if body["type"] == "gloss": | ||
46 | + manager.send_to_queue("glosses", gloss, properties) | ||
47 | + elif body["type"] == "video": | ||
48 | + manager.send_to_queue("translations", gloss, properties) | ||
49 | + else: | ||
50 | + print("Unexpected type of request") | ||
51 | + except KeyError: | ||
52 | + print ("Can't find text") | ||
43 | print ("Ok") | 53 | print ("Ok") |
44 | 54 | ||
45 | def keep_alive(conn_send, conn_receive): | 55 | def keep_alive(conn_send, conn_receive): |