renderer.cpp
3.66 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
#include "renderer.h"
Renderer::Renderer(char* filename) {
this->output = filename;
// serverInitialize();
running = true;
count_task = 0;
glosa_processed = 0;
core_socket = new StreamSocket();
listeners = new list<ListenerRenderer*>();
DPRINTF("Done!\n");
}
Renderer::~Renderer() {
listeners->clear();
delete listeners;
if(core_socket) delete core_socket;
DDDPRINTF("Renderer finalized!\n");
}
void Renderer::serverInitialize(){
string render = "./render.sh ";
render.append(output).append(" \"VLIBRAS\"").append(" 1920 1080").append(" 1").append(" 30");
string command = "cd ";
char* shPath;
shPath = getenv("RENDERER");
if(shPath != NULL)
command.append(shPath);
else
command.append(PATH_RENDERER);
command.append(" && ").append(render);
cout << command << endl;
system(command.c_str());
// sleep(1);
}
void Renderer::addListener(ListenerRenderer* listener) {
listeners->push_back(listener);
}
void Renderer::notifyListeners() {
for (list<ListenerRenderer*>::iterator i = listeners->begin(); i != listeners->end(); i++) {
(*i)->notifyEndOfRenderization();
}
}
void Renderer::receiveGlosa(std::string glosa, int64_t pts) {
glosa_copy = glosa;
stringstream ss;
ss << pts;
glosa_copy += "#"; // '#' para identificar que vem um pts
glosa_copy += ss.str();
count_task++;
}
void Renderer::sendGlosa() {
try{
int glosa_size = strlen(glosa_copy.c_str())+1;
char* glosa_buffer = new char[glosa_size];
strcpy(glosa_buffer, glosa_copy.c_str());
core_socket->write(glosa_buffer, glosa_size);
char* ok_core = new char[3];
do {
core_socket->read(ok_core, 3); //aguarda o unity confirmar o recebimento da glosa
}while(strcmp(ok_core, "OK") != 0);
glosa_processed++;
delete [] ok_core;
delete [] glosa_buffer;
}catch(lavidlib::RuntimeException &ex){
throw lavidlib::RuntimeException(ex.getMessage().c_str());
}catch(lavidlib::IOException &ex){
throw lavidlib::RuntimeException(ex.getMessage().c_str());
}
}
void Renderer::connectToUnity() {
try{
static InetAddress* addr = InetAddress::createByName(HOST);
core_socket->connect(addr, PORTNO);
}catch(lavidlib::UnknownHostException &ex){
throw RuntimeException(ex.getMessage().c_str());
}catch(lavidlib::SocketException &ex){
throw RuntimeException(ex.getMessage().c_str());
}
}
void Renderer::waitScreenShots() {
char* endgeneration = new char[strlen(END_FLAG)+1];
int connected;
try{
do{
connected = core_socket->read(endgeneration, sizeof(endgeneration));
}while(strcmp(endgeneration, END_FLAG) != 0 && connected != 0);
core_socket->close();
notifyListeners();
}catch(IOException &ex){
throw RuntimeException(ex.getMessage().c_str());
}
}
void Renderer::finalize() {
while(glosa_processed < count_task)
usleep(10000);
receiveGlosa(END_FLAG, (int64_t) -1);
this->running = false;
}
bool Renderer::isSending() {
if(glosa_processed < count_task)
return true;
else
return false;
}
void Renderer::Run() {
try{
while(running){
while(count_task <= glosa_processed){
usleep(10000);
}
if(!core_socket->isConnected())
connectToUnity();
sendGlosa();
}
waitScreenShots();
}catch(lavidlib::RuntimeException &ex){
DDDDPRINTF("Erro: %s\n", ex.getMessage().c_str());
throw RuntimeException(ex.getMessage().c_str());
}
}