opengl_dev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright (C) 2013-2014 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <pjmedia-videodev/videodev_imp.h>
  19. #include <pj/assert.h>
  20. #include <pj/log.h>
  21. #if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \
  22. defined(PJMEDIA_VIDEO_DEV_HAS_OPENGL) && \
  23. PJMEDIA_VIDEO_DEV_HAS_OPENGL != 0
  24. #include <pjmedia-videodev/opengl_dev.h>
  25. #ifdef PJMEDIA_VIDEO_DEV_HAS_OPENGL_ES
  26. # if PJ_ANDROID
  27. # include <GLES2/gl2.h>
  28. # include <GLES2/gl2ext.h>
  29. # undef GL_RGBA
  30. # define GL_RGBA GL_BGRA_EXT
  31. # define GL_BGRA GL_BGRA_EXT
  32. # else
  33. # include <OpenGLES/ES2/gl.h>
  34. # include <OpenGLES/ES2/glext.h>
  35. # endif
  36. #else
  37. # include <GL/gl.h>
  38. # include <GL/glext.h>
  39. #endif
  40. #define THIS_FILE "opengl_dev.c"
  41. #define DEFAULT_CLOCK_RATE 90000
  42. #define DEFAULT_WIDTH 480
  43. #define DEFAULT_HEIGHT 360
  44. #define DEFAULT_FPS 15
  45. #if PJ_ANDROID
  46. # define LOG(a) PJ_LOG(3, (THIS_FILE, a))
  47. #else
  48. # define LOG(a)
  49. #endif
  50. enum {
  51. ATTRIB_VERTEX,
  52. ATTRIB_TEXTUREPOSITON,
  53. NUM_ATTRIBUTES
  54. };
  55. /* Vertex and fragment shaders */
  56. static const GLchar *vertSrc = " \
  57. attribute vec4 position; \
  58. attribute vec4 inTexCoord; \
  59. varying vec2 texCoord; \
  60. void main() \
  61. { \
  62. gl_Position = position; \
  63. texCoord = inTexCoord.xy; \
  64. } \
  65. ";
  66. static const GLchar *fragSrc = " \
  67. varying highp vec2 texCoord; \
  68. uniform sampler2D videoFrame; \
  69. void main() \
  70. { \
  71. gl_FragColor = texture2D(videoFrame, texCoord); \
  72. } \
  73. ";
  74. /* OpenGL buffers structure. */
  75. struct gl_buffers {
  76. GLuint frameBuf;
  77. GLuint rendBuf;
  78. GLuint rendTex;
  79. GLuint directProg;
  80. int rendBufW;
  81. int rendBufH;
  82. pj_bool_t direct;
  83. };
  84. /* Supported formats */
  85. static pjmedia_format_id opengl_fmts[] = {PJMEDIA_FORMAT_BGRA};
  86. /* opengl device info */
  87. struct opengl_dev_info
  88. {
  89. pjmedia_vid_dev_info info;
  90. };
  91. /* opengl factory */
  92. struct opengl_factory
  93. {
  94. pjmedia_vid_dev_factory base;
  95. pj_pool_t *pool;
  96. pj_pool_factory *pf;
  97. unsigned dev_count;
  98. struct opengl_dev_info *dev_info;
  99. };
  100. /* Prototypes */
  101. static pj_status_t opengl_factory_init(pjmedia_vid_dev_factory *f);
  102. static pj_status_t opengl_factory_destroy(pjmedia_vid_dev_factory *f);
  103. static pj_status_t opengl_factory_refresh(pjmedia_vid_dev_factory *f);
  104. static unsigned opengl_factory_get_dev_count(pjmedia_vid_dev_factory *f);
  105. static pj_status_t opengl_factory_get_dev_info(pjmedia_vid_dev_factory *f,
  106. unsigned index,
  107. pjmedia_vid_dev_info *info);
  108. static pj_status_t opengl_factory_default_param(pj_pool_t *pool,
  109. pjmedia_vid_dev_factory *f,
  110. unsigned index,
  111. pjmedia_vid_dev_param *param);
  112. static pj_status_t
  113. opengl_factory_create_stream(pjmedia_vid_dev_factory *f,
  114. pjmedia_vid_dev_param *param,
  115. const pjmedia_vid_dev_cb *cb,
  116. void *user_data,
  117. pjmedia_vid_dev_stream **p_vid_strm);
  118. /* Operations */
  119. static pjmedia_vid_dev_factory_op factory_op =
  120. {
  121. &opengl_factory_init,
  122. &opengl_factory_destroy,
  123. &opengl_factory_get_dev_count,
  124. &opengl_factory_get_dev_info,
  125. &opengl_factory_default_param,
  126. &opengl_factory_create_stream,
  127. &opengl_factory_refresh
  128. };
  129. /****************************************************************************
  130. * OpenGL utility functions
  131. */
  132. /* Compile a shader from the provided source(s) */
  133. GLint compile_shader(GLenum target, GLsizei count, const GLchar **sources,
  134. GLuint *shader)
  135. {
  136. GLint status;
  137. *shader = glCreateShader(target);
  138. glShaderSource(*shader, count, sources, NULL);
  139. glCompileShader(*shader);
  140. glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
  141. return status;
  142. }
  143. /* Create program, compile shader, link program, bind attributes */
  144. GLint create_program(const GLchar *vertSource, const GLchar *fragSource,
  145. GLsizei attribNameCnt, const GLchar **attribNames,
  146. const GLint *attribLocations, GLuint *program)
  147. {
  148. GLuint vertShader = 0, fragShader = 0, prog = 0, i;
  149. GLint status;
  150. /* Create shader program */
  151. prog = glCreateProgram();
  152. *program = prog;
  153. /* Create and compile vertex shader */
  154. status = compile_shader(GL_VERTEX_SHADER, 1, &vertSource, &vertShader);
  155. if (status == 0) {
  156. LOG("Unable to compile vertex shader");
  157. return status;
  158. }
  159. /* Create and compile fragment shader */
  160. status = compile_shader(GL_FRAGMENT_SHADER, 1, &fragSource, &fragShader);
  161. if (status == 0) {
  162. LOG("Unable to compile fragment shader");
  163. return status;
  164. }
  165. /* Attach vertex shader to program */
  166. glAttachShader(prog, vertShader);
  167. /* Attach fragment shader to program */
  168. glAttachShader(prog, fragShader);
  169. /* Bind attribute locations prior to linking */
  170. for (i = 0; i < attribNameCnt; i++) {
  171. glBindAttribLocation(prog, attribLocations[i], attribNames[i]);
  172. }
  173. /* Link program */
  174. glLinkProgram(prog);
  175. glGetProgramiv(prog, GL_LINK_STATUS, &status);
  176. if (status == 0) {
  177. LOG("Unable to link program");
  178. return status;
  179. }
  180. /* Release vertex and fragment shaders */
  181. if (vertShader)
  182. glDeleteShader(vertShader);
  183. if (fragShader)
  184. glDeleteShader(fragShader);
  185. return status;
  186. }
  187. void pjmedia_vid_dev_opengl_create_buffers(pj_pool_t *pool, pj_bool_t direct,
  188. gl_buffers **glb)
  189. {
  190. gl_buffers *glbuf = PJ_POOL_ZALLOC_T(pool, gl_buffers);
  191. *glb = glbuf;
  192. glDisable(GL_DEPTH_TEST);
  193. if (!(glbuf->direct = direct)) {
  194. glGenFramebuffers(1, &glbuf->frameBuf);
  195. glBindFramebuffer(GL_FRAMEBUFFER, glbuf->frameBuf);
  196. glGenRenderbuffers(1, &glbuf->rendBuf);
  197. glBindRenderbuffer(GL_RENDERBUFFER, glbuf->rendBuf);
  198. }
  199. glGenTextures(1, &glbuf->rendTex);
  200. }
  201. pj_status_t pjmedia_vid_dev_opengl_init_buffers(gl_buffers *glb)
  202. {
  203. /* Attributes */
  204. GLint attribLocation[NUM_ATTRIBUTES] = { ATTRIB_VERTEX,
  205. ATTRIB_TEXTUREPOSITON };
  206. GLchar *attribName[NUM_ATTRIBUTES] = { "position", "texCoord" };
  207. if (!glb->direct ) {
  208. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH,
  209. &glb->rendBufW);
  210. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT,
  211. &glb->rendBufH);
  212. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  213. GL_RENDERBUFFER, glb->rendBuf);
  214. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  215. LOG("Unable to create frame buffer");
  216. return -1;
  217. }
  218. }
  219. create_program(vertSrc, fragSrc, NUM_ATTRIBUTES,
  220. (const GLchar **)&attribName[0], attribLocation,
  221. &glb->directProg);
  222. if (!glb->directProg) {
  223. LOG("Unable to create program");
  224. return -2;
  225. }
  226. return PJ_SUCCESS;
  227. }
  228. pj_status_t pjmedia_vid_dev_opengl_draw(gl_buffers *glb, unsigned int width,
  229. unsigned int height, void *pixels)
  230. {
  231. static const GLfloat squareVertices[] = {
  232. -1.0f, -1.0f,
  233. 1.0f, -1.0f,
  234. -1.0f, 1.0f,
  235. 1.0f, 1.0f,
  236. };
  237. GLfloat textureVertices[] = {
  238. 0, 1, 1, 1, 0, 0, 1, 0
  239. };
  240. glBindTexture(GL_TEXTURE_2D, glb->rendTex);
  241. /* Set texture parameters */
  242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  244. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  245. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  246. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height,
  247. 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)pixels);
  248. glFlush();
  249. /* Do we render directly to the screen? */
  250. glBindFramebuffer(GL_FRAMEBUFFER, (glb->direct? 0: glb->frameBuf));
  251. /* Set the view port to the entire view */
  252. glViewport(0, 0, (glb->direct? width: glb->rendBufW),
  253. (glb->direct? height: glb->rendBufH));
  254. /* Draw the texture on the screen with OpenGL ES 2 */
  255. /* Use program */
  256. glUseProgram(glb->directProg);
  257. /* Update attribute values */
  258. glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
  259. glEnableVertexAttribArray(ATTRIB_VERTEX);
  260. glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0,
  261. textureVertices);
  262. glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);
  263. /* Update uniform values if there are any */
  264. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  265. /* Present */
  266. if (!glb->direct)
  267. glBindRenderbuffer(GL_RENDERBUFFER, glb->rendBuf);
  268. return PJ_SUCCESS;
  269. }
  270. void pjmedia_vid_dev_opengl_destroy_buffers(gl_buffers *glb)
  271. {
  272. if (glb->frameBuf) {
  273. glDeleteFramebuffers(1, &glb->frameBuf);
  274. glb->frameBuf = 0;
  275. }
  276. if (glb->rendBuf) {
  277. glDeleteRenderbuffers(1, &glb->rendBuf);
  278. glb->rendBuf = 0;
  279. }
  280. if (glb->rendTex) {
  281. glDeleteTextures(1, &glb->rendTex);
  282. glb->rendTex = 0;
  283. }
  284. if (glb->directProg) {
  285. glDeleteProgram(glb->directProg);
  286. glb->directProg = 0;
  287. }
  288. }
  289. /****************************************************************************
  290. * Factory operations
  291. */
  292. /*
  293. * Init opengl video driver.
  294. */
  295. pjmedia_vid_dev_factory* pjmedia_opengl_factory(pj_pool_factory *pf)
  296. {
  297. struct opengl_factory *f;
  298. pj_pool_t *pool;
  299. pool = pj_pool_create(pf, "opengl rend", 512, 512, NULL);
  300. f = PJ_POOL_ZALLOC_T(pool, struct opengl_factory);
  301. f->pf = pf;
  302. f->pool = pool;
  303. f->base.op = &factory_op;
  304. return &f->base;
  305. }
  306. /* API: init factory */
  307. static pj_status_t opengl_factory_init(pjmedia_vid_dev_factory *f)
  308. {
  309. struct opengl_factory *qf = (struct opengl_factory*)f;
  310. struct opengl_dev_info *qdi;
  311. unsigned l;
  312. /* Initialize input and output devices here */
  313. qf->dev_info = (struct opengl_dev_info*)
  314. pj_pool_calloc(qf->pool, 1, sizeof(struct opengl_dev_info));
  315. qf->dev_count = 0;
  316. qdi = &qf->dev_info[qf->dev_count++];
  317. pj_bzero(qdi, sizeof(*qdi));
  318. pj_ansi_strxcpy(qdi->info.name, "OpenGL renderer", sizeof(qdi->info.name));
  319. pj_ansi_strxcpy(qdi->info.driver, "OpenGL", sizeof(qdi->info.driver));
  320. qdi->info.dir = PJMEDIA_DIR_RENDER;
  321. qdi->info.has_callback = PJ_FALSE;
  322. qdi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
  323. qdi->info.fmt_cnt = PJ_ARRAY_SIZE(opengl_fmts);
  324. qdi->info.caps |= pjmedia_vid_dev_opengl_imp_get_cap();
  325. for (l = 0; l < PJ_ARRAY_SIZE(opengl_fmts); l++) {
  326. pjmedia_format *fmt = &qdi->info.fmt[l];
  327. pjmedia_format_init_video(fmt, opengl_fmts[l], DEFAULT_WIDTH,
  328. DEFAULT_HEIGHT, DEFAULT_FPS, 1);
  329. }
  330. PJ_LOG(4, (THIS_FILE, "OpenGL device initialized"));
  331. return PJ_SUCCESS;
  332. }
  333. /* API: destroy factory */
  334. static pj_status_t opengl_factory_destroy(pjmedia_vid_dev_factory *f)
  335. {
  336. struct opengl_factory *qf = (struct opengl_factory*)f;
  337. pj_pool_t *pool = qf->pool;
  338. qf->pool = NULL;
  339. pj_pool_release(pool);
  340. return PJ_SUCCESS;
  341. }
  342. /* API: refresh the list of devices */
  343. static pj_status_t opengl_factory_refresh(pjmedia_vid_dev_factory *f)
  344. {
  345. PJ_UNUSED_ARG(f);
  346. return PJ_SUCCESS;
  347. }
  348. /* API: get number of devices */
  349. static unsigned opengl_factory_get_dev_count(pjmedia_vid_dev_factory *f)
  350. {
  351. struct opengl_factory *qf = (struct opengl_factory*)f;
  352. return qf->dev_count;
  353. }
  354. /* API: get device info */
  355. static pj_status_t opengl_factory_get_dev_info(pjmedia_vid_dev_factory *f,
  356. unsigned index,
  357. pjmedia_vid_dev_info *info)
  358. {
  359. struct opengl_factory *qf = (struct opengl_factory*)f;
  360. PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
  361. pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info));
  362. return PJ_SUCCESS;
  363. }
  364. /* API: create default device parameter */
  365. static pj_status_t opengl_factory_default_param(pj_pool_t *pool,
  366. pjmedia_vid_dev_factory *f,
  367. unsigned index,
  368. pjmedia_vid_dev_param *param)
  369. {
  370. struct opengl_factory *qf = (struct opengl_factory*)f;
  371. struct opengl_dev_info *di = &qf->dev_info[index];
  372. PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
  373. PJ_UNUSED_ARG(pool);
  374. pj_bzero(param, sizeof(*param));
  375. if (di->info.dir & PJMEDIA_DIR_RENDER) {
  376. param->dir = PJMEDIA_DIR_RENDER;
  377. param->rend_id = index;
  378. param->cap_id = PJMEDIA_VID_INVALID_DEV;
  379. } else {
  380. return PJMEDIA_EVID_INVDEV;
  381. }
  382. param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
  383. param->clock_rate = DEFAULT_CLOCK_RATE;
  384. pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
  385. return PJ_SUCCESS;
  386. }
  387. /* API: create stream */
  388. static pj_status_t
  389. opengl_factory_create_stream(pjmedia_vid_dev_factory *f,
  390. pjmedia_vid_dev_param *param,
  391. const pjmedia_vid_dev_cb *cb,
  392. void *user_data,
  393. pjmedia_vid_dev_stream **p_vid_strm)
  394. {
  395. struct opengl_factory *qf = (struct opengl_factory*)f;
  396. pj_pool_t *pool;
  397. const pjmedia_video_format_info *vfi;
  398. PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
  399. PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
  400. param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
  401. (param->dir == PJMEDIA_DIR_CAPTURE ||
  402. param->dir == PJMEDIA_DIR_RENDER),
  403. PJ_EINVAL);
  404. vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
  405. if (!vfi)
  406. return PJMEDIA_EVID_BADFORMAT;
  407. /* Create and Initialize stream descriptor */
  408. pool = pj_pool_create(qf->pf, "opengl-dev", 4000, 4000, NULL);
  409. PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
  410. return pjmedia_vid_dev_opengl_imp_create_stream(pool, param, cb,
  411. user_data, p_vid_strm);
  412. }
  413. #endif /* PJMEDIA_VIDEO_DEV_HAS_OPENGL */