-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain_worked.cpp
More file actions
414 lines (352 loc) · 12 KB
/
main_worked.cpp
File metadata and controls
414 lines (352 loc) · 12 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#ifdef NOEMSCRIPTEN
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <easywsclient.hpp>
#pragma comment( lib, "ws2_32" )
#include <WinSock2.h>
#else
#include <emscripten/emscripten.h>
#include <GLES2/gl2.h>
#include <GLFW/glfw3.h>
#endif
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "framecontainer.h"
#include <chrono>
const int VIDEO_PIXEL_WIDTH = 1920;
const int VIDEO_PIXEL_HEIGHT = 1080;
struct SYUVInfo
{
SYUVInfo(int pixWid = VIDEO_PIXEL_WIDTH, int pixHei = VIDEO_PIXEL_HEIGHT);
bool update(int pixWid, int pixHei);
bool setViewSize(int pixWid, int pixHei);
unsigned m_wid;
float m_semiWid;
unsigned m_hei;
float m_semiHei;
unsigned m_len;
unsigned m_yWid;
unsigned m_yHei;
unsigned m_uWid;
unsigned m_uHei;
unsigned m_yLen;
unsigned m_uLen;
float m_yuvRadio;
int m_viewWid;
int m_viewHei;
float m_renderScale;
};
SYUVInfo::SYUVInfo(int pixWid, int pixHei)
{
update(pixWid, pixHei);
setViewSize(pixWid, pixHei);
}
bool SYUVInfo::update(int pixWid, int pixHei)
{
m_wid = pixWid;
m_hei = pixHei;
m_semiWid = m_wid >> 1;
m_semiHei = m_hei >> 1;
m_yWid = pixWid;
m_yHei = pixHei;
m_uWid = (pixWid + 1) / 2;
m_uHei = (pixHei + 1) / 2;
m_yLen = m_yWid*m_yHei;
m_uLen = m_uWid*m_uHei;
m_len = m_yLen + m_uLen * 2;
m_yuvRadio = m_semiHei / m_semiWid;
return true;
}
bool SYUVInfo::setViewSize(int pixWid, int pixHei)
{
m_viewWid = pixWid;
m_viewHei = pixHei;
//adapt to render pixels
m_renderScale = std::min(float(m_viewWid) / m_wid, float(m_viewHei) / m_hei);
return true;
}
//*******
//
static FrameContainer *m_frames = nullptr;
//
static SYUVInfo* m_yuvInfo = nullptr;
extern "C" {
int initVideoStream()
{
m_yuvInfo = new SYUVInfo;
m_frames = new FrameContainer(m_yuvInfo->m_len, 8);
return 0;
}
int putVideoStream(uint8_t* src, int sz)
{
bool stat = m_frames->produce(src, sz);
if (!stat)
{
printf("skip#########\n");
}
return 1;
}
}
//***********
static GLuint m_textureY = 0, m_textureU = 0, m_textureV = 0;
static GLuint texUniY = 0, texUniU = 0, texUniV = 0;
GLFWwindow* window;
void bindVideoBuf(const uint8_t *yuvBuf)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureY);
char * ptr = (char *)yuvBuf;
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_yuvInfo->m_yWid, m_yuvInfo->m_yHei
, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, ptr);
glUniform1i(texUniY, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_textureU);
ptr += m_yuvInfo->m_yLen;
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_yuvInfo->m_uWid, m_yuvInfo->m_uHei
, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, ptr);
glUniform1i(texUniU, 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_textureV);
ptr += m_yuvInfo->m_uLen;
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_yuvInfo->m_uWid, m_yuvInfo->m_uHei
, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, ptr);
glUniform1i(texUniV, 2);
}
void initTexture()
{
glGenTextures(1, &m_textureY);
glBindTexture(GL_TEXTURE_2D, m_textureY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &m_textureU);
glBindTexture(GL_TEXTURE_2D, m_textureU);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &m_textureV);
glBindTexture(GL_TEXTURE_2D, m_textureV);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glEnable(GL_TEXTURE_2D);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource =
"attribute vec3 aPos;\n"
"attribute vec2 aTextureCoord;\n"
"varying vec2 vTextureCoord;\n"
"void main()\n"
"{\n"
" vTextureCoord = aTextureCoord;\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource =
"precision mediump float;\n"
"varying vec2 vTextureCoord;\n"
"uniform sampler2D tex_y;\n"
"uniform sampler2D tex_u;\n"
"uniform sampler2D tex_v;\n"
"void main()\n"
"{\n"
" vec3 yuv;\n"
" vec3 rgb;\n"
" yuv.x = texture2D(tex_y, vTextureCoord).r;\n"
" yuv.y = texture2D(tex_u, vTextureCoord).r - 0.5;\n"
" yuv.z = texture2D(tex_v, vTextureCoord).r - 0.5;\n"
" \n"
" rgb = mat3(1.0, 1.0, 1.0,\n"
" 0.0, -0.395, 2.032,\n"
" 1.140, -0.581, 0.0) * yuv;\n"
" \n"
" gl_FragColor = vec4(rgb, 1.0);\n"
//" gl_FragColor = texture(tex_y, vTextureCoord);\n"
"}\n\0";
extern "C"
int mainInit()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "WASM OpenGL", NULL, NULL);
if (window == NULL)
{
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
#ifdef NOEMSCRIPTEN
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("Failed to initialize GLAD\n");
return -1;
}
#endif // !EMSCRIPTEN
// build and compile our shader program
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s \n", infoLog);
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s \n", infoLog);
}
// link shaders
int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n %s \n", infoLog);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUseProgram(shaderProgram);
texUniY = glGetUniformLocation(shaderProgram, "tex_y");
texUniU = glGetUniformLocation(shaderProgram, "tex_u");
texUniV = glGetUniformLocation(shaderProgram, "tex_v");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
GLfloat vertices[] = {
-1.f, -1.f, 0.0f, 0.f, 1.f, // left-bottom
1.f, -1.f, 0.0f, 1.f, 1.f,// right-bottom
1.f, 1.f, 0.0f, 1.f, 0.f, // right-top
-1.f, -1.f, 0.0f, 0.f, 1.f, // left-bottom
1.f, 1.f, 0.0f, 1.f, 0.f, // right-top
-1.f, 1.f, 0.0f, 0.f, 0.f // left-top
};
GLuint VBO;
//glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
//glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
initTexture();
return 0;
}
extern "C"
int mainPaint()
{
// render
// ------
glClearColor(0.2f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
//glUseProgram(shaderProgram);
//glBindTexture(GL_TEXTURE_2D, m_textureY);
//printf("gldraw...%d\n", yuvBufUpdate);
int yuvBufUpdate = 0;
auto buf = m_frames->consume(&yuvBufUpdate);
if (buf)
{
if (yuvBufUpdate)
{
//printf("yuvBufUpdate bind...\n");
bindVideoBuf(buf);
}
m_frames->consumeFinished();
}
//glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 6);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
//emscripten_sleep(40);
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
#ifdef NOEMSCRIPTEN
int main()
{
initVideoStream();
mainInit();
using easywsclient::WebSocket;
INT rc;
WSADATA wsaData;
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc) {
printf("WSAStartup Failed.\n");
return 1;
}
WebSocket *ws = WebSocket::from_url("ws://127.0.0.1:12345");
if (!ws) return 1;
bool dblHit = true;
while (!glfwWindowShouldClose(window))
{
dblHit = !dblHit;
if (dblHit)
{
ws->poll();
ws->send("hello");
ws->dispatch([ws](const std::string & message) {
//printf(">>> %d\n", message.size());
putVideoStream((uint8_t*)message.data(), message.size());
//pdec->putVideoStream((uint8_t*)message.data(), message.size());
});
}
processInput(window);
mainPaint();
Sleep(20);
}
return 1;
}
#endif