| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pjmedia-videodev/videodev_imp.h> |
| 19 |
#include <pj/assert.h> |
| 20 |
#include <pj/log.h> |
| 21 |
|
| 22 |
#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ |
| 23 |
defined(PJMEDIA_VIDEO_DEV_HAS_OPENGL) && \ |
| 24 |
PJMEDIA_VIDEO_DEV_HAS_OPENGL != 0 |
| 25 |
|
| 26 |
#include <pjmedia-videodev/opengl_dev.h> |
| 27 |
#ifdef PJMEDIA_VIDEO_DEV_HAS_OPENGL_ES |
| 28 |
# if PJ_ANDROID |
| 29 |
# include <GLES2/gl2.h> |
| 30 |
# include <GLES2/gl2ext.h> |
| 31 |
# undef GL_RGBA |
| 32 |
# define GL_RGBA GL_BGRA_EXT |
| 33 |
# define GL_BGRA GL_BGRA_EXT |
| 34 |
# else |
| 35 |
# include <OpenGLES/ES2/gl.h> |
| 36 |
# include <OpenGLES/ES2/glext.h> |
| 37 |
# endif |
| 38 |
#else |
| 39 |
# include <GL/gl.h> |
| 40 |
# include <GL/glext.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#define THIS_FILE "opengl_dev.c" |
| 44 |
#define DEFAULT_CLOCK_RATE 90000 |
| 45 |
#define DEFAULT_WIDTH 480 |
| 46 |
#define DEFAULT_HEIGHT 360 |
| 47 |
#define DEFAULT_FPS 15 |
| 48 |
|
| 49 |
#if PJ_ANDROID |
| 50 |
# define LOG(a) PJ_LOG(3, (THIS_FILE, a)) |
| 51 |
#else |
| 52 |
# define LOG(a) |
| 53 |
#endif |
| 54 |
|
| 55 |
enum { |
| 56 |
ATTRIB_VERTEX, |
| 57 |
ATTRIB_TEXTUREPOSITON, |
| 58 |
NUM_ATTRIBUTES |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
static const GLchar *vertSrc = " \ |
| 63 |
attribute vec4 position; \ |
| 64 |
attribute vec4 inTexCoord; \ |
| 65 |
varying vec2 texCoord; \ |
| 66 |
void main() \ |
| 67 |
{ \ |
| 68 |
gl_Position = position; \ |
| 69 |
texCoord = inTexCoord.xy; \ |
| 70 |
} \ |
| 71 |
"; |
| 72 |
static const GLchar *fragSrc = " \ |
| 73 |
varying highp vec2 texCoord; \ |
| 74 |
uniform sampler2D videoFrame; \ |
| 75 |
void main() \ |
| 76 |
{ \ |
| 77 |
gl_FragColor = texture2D(videoFrame, texCoord); \ |
| 78 |
} \ |
| 79 |
"; |
| 80 |
|
| 81 |
|
| 82 |
struct gl_buffers { |
| 83 |
GLuint frameBuf; |
| 84 |
GLuint rendBuf; |
| 85 |
GLuint rendTex; |
| 86 |
GLuint directProg; |
| 87 |
|
| 88 |
int rendBufW; |
| 89 |
int rendBufH; |
| 90 |
pj_bool_t direct; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
static pjmedia_format_id opengl_fmts[] = {PJMEDIA_FORMAT_BGRA}; |
| 95 |
|
| 96 |
|
| 97 |
struct opengl_dev_info |
| 98 |
{ |
| 99 |
pjmedia_vid_dev_info info; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
struct opengl_factory |
| 104 |
{ |
| 105 |
pjmedia_vid_dev_factory base; |
| 106 |
pj_pool_t *pool; |
| 107 |
pj_pool_factory *pf; |
| 108 |
|
| 109 |
unsigned dev_count; |
| 110 |
struct opengl_dev_info *dev_info; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
static pj_status_t opengl_factory_init(pjmedia_vid_dev_factory *f); |
| 115 |
static pj_status_t opengl_factory_destroy(pjmedia_vid_dev_factory *f); |
| 116 |
static pj_status_t opengl_factory_refresh(pjmedia_vid_dev_factory *f); |
| 117 |
static unsigned opengl_factory_get_dev_count(pjmedia_vid_dev_factory *f); |
| 118 |
static pj_status_t opengl_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 119 |
unsigned index, |
| 120 |
pjmedia_vid_dev_info *info); |
| 121 |
static pj_status_t opengl_factory_default_param(pj_pool_t *pool, |
| 122 |
pjmedia_vid_dev_factory *f, |
| 123 |
unsigned index, |
| 124 |
pjmedia_vid_dev_param *param); |
| 125 |
static pj_status_t |
| 126 |
opengl_factory_create_stream(pjmedia_vid_dev_factory *f, |
| 127 |
pjmedia_vid_dev_param *param, |
| 128 |
const pjmedia_vid_dev_cb *cb, |
| 129 |
void *user_data, |
| 130 |
pjmedia_vid_dev_stream **p_vid_strm); |
| 131 |
|
| 132 |
|
| 133 |
static pjmedia_vid_dev_factory_op factory_op = |
| 134 |
{ |
| 135 |
&opengl_factory_init, |
| 136 |
&opengl_factory_destroy, |
| 137 |
&opengl_factory_get_dev_count, |
| 138 |
&opengl_factory_get_dev_info, |
| 139 |
&opengl_factory_default_param, |
| 140 |
&opengl_factory_create_stream, |
| 141 |
&opengl_factory_refresh |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
GLint compile_shader(GLenum target, GLsizei count, const GLchar **sources, |
| 149 |
GLuint *shader) |
| 150 |
{ |
| 151 |
GLint status; |
| 152 |
|
| 153 |
*shader = glCreateShader(target); |
| 154 |
glShaderSource(*shader, count, sources, NULL); |
| 155 |
glCompileShader(*shader); |
| 156 |
|
| 157 |
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); |
| 158 |
|
| 159 |
return status; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
GLint create_program(const GLchar *vertSource, const GLchar *fragSource, |
| 164 |
GLsizei attribNameCnt, const GLchar **attribNames, |
| 165 |
const GLint *attribLocations, GLuint *program) |
| 166 |
{ |
| 167 |
GLuint vertShader = 0, fragShader = 0, prog = 0, i; |
| 168 |
GLint status; |
| 169 |
|
| 170 |
|
| 171 |
prog = glCreateProgram(); |
| 172 |
*program = prog; |
| 173 |
|
| 174 |
|
| 175 |
status = compile_shader(GL_VERTEX_SHADER, 1, &vertSource, &vertShader); |
| 176 |
if (status == 0) { |
| 177 |
LOG("Unable to compile vertex shader"); |
| 178 |
return status; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
status = compile_shader(GL_FRAGMENT_SHADER, 1, &fragSource, &fragShader); |
| 183 |
if (status == 0) { |
| 184 |
LOG("Unable to compile fragment shader"); |
| 185 |
return status; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
glAttachShader(prog, vertShader); |
| 190 |
|
| 191 |
|
| 192 |
glAttachShader(prog, fragShader); |
| 193 |
|
| 194 |
|
| 195 |
for (i = 0; i < attribNameCnt; i++) { |
| 196 |
glBindAttribLocation(prog, attribLocations[i], attribNames[i]); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
glLinkProgram(prog); |
| 201 |
glGetProgramiv(prog, GL_LINK_STATUS, &status); |
| 202 |
if (status == 0) { |
| 203 |
LOG("Unable to link program"); |
| 204 |
return status; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if (vertShader) |
| 209 |
glDeleteShader(vertShader); |
| 210 |
if (fragShader) |
| 211 |
glDeleteShader(fragShader); |
| 212 |
|
| 213 |
return status; |
| 214 |
} |
| 215 |
|
| 216 |
void pjmedia_vid_dev_opengl_create_buffers(pj_pool_t *pool, pj_bool_t direct, |
| 217 |
gl_buffers **glb) |
| 218 |
{ |
| 219 |
gl_buffers *glbuf = PJ_POOL_ZALLOC_T(pool, gl_buffers); |
| 220 |
|
| 221 |
*glb = glbuf; |
| 222 |
glDisable(GL_DEPTH_TEST); |
| 223 |
|
| 224 |
if (!(glbuf->direct = direct)) { |
| 225 |
glGenFramebuffers(1, &glbuf->frameBuf); |
| 226 |
glBindFramebuffer(GL_FRAMEBUFFER, glbuf->frameBuf); |
| 227 |
|
| 228 |
glGenRenderbuffers(1, &glbuf->rendBuf); |
| 229 |
glBindRenderbuffer(GL_RENDERBUFFER, glbuf->rendBuf); |
| 230 |
} |
| 231 |
|
| 232 |
glGenTextures(1, &glbuf->rendTex); |
| 233 |
} |
| 234 |
|
| 235 |
pj_status_t pjmedia_vid_dev_opengl_init_buffers(gl_buffers *glb) |
| 236 |
{ |
| 237 |
|
| 238 |
GLint attribLocation[NUM_ATTRIBUTES] = { ATTRIB_VERTEX, |
| 239 |
ATTRIB_TEXTUREPOSITON }; |
| 240 |
GLchar *attribName[NUM_ATTRIBUTES] = { "position", "texCoord" }; |
| 241 |
|
| 242 |
if (!glb->direct ) { |
| 243 |
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, |
| 244 |
&glb->rendBufW); |
| 245 |
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, |
| 246 |
&glb->rendBufH); |
| 247 |
|
| 248 |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 249 |
GL_RENDERBUFFER, glb->rendBuf); |
| 250 |
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 251 |
LOG("Unable to create frame buffer"); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
create_program(vertSrc, fragSrc, NUM_ATTRIBUTES, |
| 257 |
(const GLchar **)&attribName[0], attribLocation, |
| 258 |
&glb->directProg); |
| 259 |
|
| 260 |
if (!glb->directProg) { |
| 261 |
LOG("Unable to create program"); |
| 262 |
return -2; |
| 263 |
} |
| 264 |
|
| 265 |
return PJ_SUCCESS; |
| 266 |
} |
| 267 |
|
| 268 |
pj_status_t pjmedia_vid_dev_opengl_draw(gl_buffers *glb, unsigned int width, |
| 269 |
unsigned int height, void *pixels) |
| 270 |
{ |
| 271 |
static const GLfloat squareVertices[] = { |
| 272 |
-1.0f, -1.0f, |
| 273 |
1.0f, -1.0f, |
| 274 |
-1.0f, 1.0f, |
| 275 |
1.0f, 1.0f, |
| 276 |
}; |
| 277 |
GLfloat textureVertices[] = { |
| 278 |
0, 1, 1, 1, 0, 0, 1, 0 |
| 279 |
}; |
| 280 |
|
| 281 |
glBindTexture(GL_TEXTURE_2D, glb->rendTex); |
| 282 |
|
| 283 |
|
| 284 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 285 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 286 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 287 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 288 |
|
| 289 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, |
| 290 |
0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)pixels); |
| 291 |
|
| 292 |
glFlush(); |
| 293 |
|
| 294 |
|
| 295 |
glBindFramebuffer(GL_FRAMEBUFFER, (glb->direct? 0: glb->frameBuf)); |
| 296 |
|
| 297 |
|
| 298 |
glViewport(0, 0, (glb->direct? width: glb->rendBufW), |
| 299 |
(glb->direct? height: glb->rendBufH)); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
glUseProgram(glb->directProg); |
| 304 |
|
| 305 |
|
| 306 |
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices); |
| 307 |
glEnableVertexAttribArray(ATTRIB_VERTEX); |
| 308 |
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, |
| 309 |
textureVertices); |
| 310 |
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON); |
| 311 |
|
| 312 |
|
| 313 |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 314 |
|
| 315 |
|
| 316 |
if (!glb->direct) |
| 317 |
glBindRenderbuffer(GL_RENDERBUFFER, glb->rendBuf); |
| 318 |
|
| 319 |
return PJ_SUCCESS; |
| 320 |
} |
| 321 |
|
| 322 |
void pjmedia_vid_dev_opengl_destroy_buffers(gl_buffers *glb) |
| 323 |
{ |
| 324 |
if (glb->frameBuf) { |
| 325 |
glDeleteFramebuffers(1, &glb->frameBuf); |
| 326 |
glb->frameBuf = 0; |
| 327 |
} |
| 328 |
|
| 329 |
if (glb->rendBuf) { |
| 330 |
glDeleteRenderbuffers(1, &glb->rendBuf); |
| 331 |
glb->rendBuf = 0; |
| 332 |
} |
| 333 |
|
| 334 |
if (glb->rendTex) { |
| 335 |
glDeleteTextures(1, &glb->rendTex); |
| 336 |
glb->rendTex = 0; |
| 337 |
} |
| 338 |
|
| 339 |
if (glb->directProg) { |
| 340 |
glDeleteProgram(glb->directProg); |
| 341 |
glb->directProg = 0; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
pjmedia_vid_dev_factory* pjmedia_opengl_factory(pj_pool_factory *pf) |
| 352 |
{ |
| 353 |
struct opengl_factory *f; |
| 354 |
pj_pool_t *pool; |
| 355 |
|
| 356 |
pool = pj_pool_create(pf, "opengl rend", 512, 512, NULL); |
| 357 |
f = PJ_POOL_ZALLOC_T(pool, struct opengl_factory); |
| 358 |
f->pf = pf; |
| 359 |
f->pool = pool; |
| 360 |
f->base.op = &factory_op; |
| 361 |
|
| 362 |
return &f->base; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
static pj_status_t opengl_factory_init(pjmedia_vid_dev_factory *f) |
| 367 |
{ |
| 368 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 369 |
struct opengl_dev_info *qdi; |
| 370 |
unsigned l; |
| 371 |
|
| 372 |
|
| 373 |
qf->dev_info = (struct opengl_dev_info*) |
| 374 |
pj_pool_calloc(qf->pool, 1, sizeof(struct opengl_dev_info)); |
| 375 |
|
| 376 |
qf->dev_count = 0; |
| 377 |
qdi = &qf->dev_info[qf->dev_count++]; |
| 378 |
pj_bzero(qdi, sizeof(*qdi)); |
| 379 |
pj_ansi_strxcpy(qdi->info.name, "OpenGL renderer", sizeof(qdi->info.name)); |
| 380 |
pj_ansi_strxcpy(qdi->info.driver, "OpenGL", sizeof(qdi->info.driver)); |
| 381 |
qdi->info.dir = PJMEDIA_DIR_RENDER; |
| 382 |
qdi->info.has_callback = PJ_FALSE; |
| 383 |
qdi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 384 |
qdi->info.fmt_cnt = PJ_ARRAY_SIZE(opengl_fmts); |
| 385 |
qdi->info.caps |= pjmedia_vid_dev_opengl_imp_get_cap(); |
| 386 |
|
| 387 |
for (l = 0; l < PJ_ARRAY_SIZE(opengl_fmts); l++) { |
| 388 |
pjmedia_format *fmt = &qdi->info.fmt[l]; |
| 389 |
pjmedia_format_init_video(fmt, opengl_fmts[l], DEFAULT_WIDTH, |
| 390 |
DEFAULT_HEIGHT, DEFAULT_FPS, 1); |
| 391 |
} |
| 392 |
|
| 393 |
PJ_LOG(4, (THIS_FILE, "OpenGL device initialized")); |
| 394 |
|
| 395 |
return PJ_SUCCESS; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
static pj_status_t opengl_factory_destroy(pjmedia_vid_dev_factory *f) |
| 400 |
{ |
| 401 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 402 |
pj_pool_t *pool = qf->pool; |
| 403 |
|
| 404 |
qf->pool = NULL; |
| 405 |
pj_pool_release(pool); |
| 406 |
|
| 407 |
return PJ_SUCCESS; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
static pj_status_t opengl_factory_refresh(pjmedia_vid_dev_factory *f) |
| 412 |
{ |
| 413 |
PJ_UNUSED_ARG(f); |
| 414 |
return PJ_SUCCESS; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
static unsigned opengl_factory_get_dev_count(pjmedia_vid_dev_factory *f) |
| 419 |
{ |
| 420 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 421 |
return qf->dev_count; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
static pj_status_t opengl_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 426 |
unsigned index, |
| 427 |
pjmedia_vid_dev_info *info) |
| 428 |
{ |
| 429 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 430 |
|
| 431 |
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 432 |
|
| 433 |
pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info)); |
| 434 |
|
| 435 |
return PJ_SUCCESS; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
static pj_status_t opengl_factory_default_param(pj_pool_t *pool, |
| 440 |
pjmedia_vid_dev_factory *f, |
| 441 |
unsigned index, |
| 442 |
pjmedia_vid_dev_param *param) |
| 443 |
{ |
| 444 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 445 |
struct opengl_dev_info *di = &qf->dev_info[index]; |
| 446 |
|
| 447 |
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 448 |
|
| 449 |
PJ_UNUSED_ARG(pool); |
| 450 |
|
| 451 |
pj_bzero(param, sizeof(*param)); |
| 452 |
if (di->info.dir & PJMEDIA_DIR_RENDER) { |
| 453 |
param->dir = PJMEDIA_DIR_RENDER; |
| 454 |
param->rend_id = index; |
| 455 |
param->cap_id = PJMEDIA_VID_INVALID_DEV; |
| 456 |
} else { |
| 457 |
return PJMEDIA_EVID_INVDEV; |
| 458 |
} |
| 459 |
|
| 460 |
param->flags = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 461 |
param->clock_rate = DEFAULT_CLOCK_RATE; |
| 462 |
pj_memcpy(¶m->fmt, &di->info.fmt[0], sizeof(param->fmt)); |
| 463 |
|
| 464 |
return PJ_SUCCESS; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
static pj_status_t |
| 469 |
opengl_factory_create_stream(pjmedia_vid_dev_factory *f, |
| 470 |
pjmedia_vid_dev_param *param, |
| 471 |
const pjmedia_vid_dev_cb *cb, |
| 472 |
void *user_data, |
| 473 |
pjmedia_vid_dev_stream **p_vid_strm) |
| 474 |
{ |
| 475 |
struct opengl_factory *qf = (struct opengl_factory*)f; |
| 476 |
pj_pool_t *pool; |
| 477 |
const pjmedia_video_format_info *vfi; |
| 478 |
|
| 479 |
PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL); |
| 480 |
PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO && |
| 481 |
param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO && |
| 482 |
(param->dir == PJMEDIA_DIR_CAPTURE || |
| 483 |
param->dir == PJMEDIA_DIR_RENDER), |
| 484 |
PJ_EINVAL); |
| 485 |
|
| 486 |
vfi = pjmedia_get_video_format_info(NULL, param->fmt.id); |
| 487 |
if (!vfi) |
| 488 |
return PJMEDIA_EVID_BADFORMAT; |
| 489 |
|
| 490 |
|
| 491 |
pool = pj_pool_create(qf->pf, "opengl-dev", 4000, 4000, NULL); |
| 492 |
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 493 |
|
| 494 |
return pjmedia_vid_dev_opengl_imp_create_stream(pool, param, cb, |
| 495 |
user_data, p_vid_strm); |
| 496 |
} |
| 497 |
|
| 498 |
#endif |