| 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 |
#include <pj/os.h> |
| 22 |
|
| 23 |
#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ |
| 24 |
defined(PJMEDIA_VIDEO_DEV_HAS_ANDROID_OPENGL) && \ |
| 25 |
PJMEDIA_VIDEO_DEV_HAS_ANDROID_OPENGL != 0 |
| 26 |
|
| 27 |
#include <pjmedia-videodev/opengl_dev.h> |
| 28 |
#include <jni.h> |
| 29 |
#include <android/native_window_jni.h> |
| 30 |
#include <EGL/egl.h> |
| 31 |
#include <GLES2/gl2.h> |
| 32 |
#include <GLES2/gl2ext.h> |
| 33 |
|
| 34 |
#define THIS_FILE "android_opengl.cpp" |
| 35 |
|
| 36 |
#define MAX_JOBS 1 |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#define STOP_IF_ERROR_RENDERING 8 |
| 41 |
|
| 42 |
typedef struct andgl_fmt_info |
| 43 |
{ |
| 44 |
pjmedia_format_id pjmedia_format; |
| 45 |
} andgl_fmt_info; |
| 46 |
|
| 47 |
|
| 48 |
static andgl_fmt_info andgl_fmts[] = |
| 49 |
{ |
| 50 |
{PJMEDIA_FORMAT_BGRA} |
| 51 |
}; |
| 52 |
|
| 53 |
typedef pj_status_t (*job_func_ptr)(void *data); |
| 54 |
|
| 55 |
typedef struct job { |
| 56 |
job_func_ptr func; |
| 57 |
void *data; |
| 58 |
unsigned flags; |
| 59 |
pj_status_t retval; |
| 60 |
} job; |
| 61 |
|
| 62 |
typedef struct job_queue { |
| 63 |
job *jobs[MAX_JOBS]; |
| 64 |
pj_sem_t *job_sem[MAX_JOBS]; |
| 65 |
pj_mutex_t *mutex; |
| 66 |
pj_thread_t *thread; |
| 67 |
pj_sem_t *sem; |
| 68 |
|
| 69 |
unsigned size; |
| 70 |
unsigned head, tail; |
| 71 |
pj_bool_t is_quitting; |
| 72 |
} job_queue; |
| 73 |
|
| 74 |
|
| 75 |
struct andgl_stream |
| 76 |
{ |
| 77 |
pjmedia_vid_dev_stream base; |
| 78 |
pjmedia_vid_dev_param param; |
| 79 |
pj_pool_t *pool; |
| 80 |
|
| 81 |
pjmedia_vid_dev_cb vid_cb; |
| 82 |
void *user_data; |
| 83 |
|
| 84 |
pj_timestamp frame_ts; |
| 85 |
unsigned ts_inc; |
| 86 |
pjmedia_rect_size vid_size; |
| 87 |
|
| 88 |
job_queue *jq; |
| 89 |
pj_bool_t is_running; |
| 90 |
pj_int32_t err_rend; |
| 91 |
const pjmedia_frame *frame; |
| 92 |
|
| 93 |
gl_buffers *gl_buf; |
| 94 |
EGLDisplay display; |
| 95 |
EGLSurface surface; |
| 96 |
EGLContext context; |
| 97 |
ANativeWindow *window; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static pj_status_t andgl_stream_get_param(pjmedia_vid_dev_stream *strm, |
| 103 |
pjmedia_vid_dev_param *param); |
| 104 |
static pj_status_t andgl_stream_get_cap(pjmedia_vid_dev_stream *strm, |
| 105 |
pjmedia_vid_dev_cap cap, |
| 106 |
void *value); |
| 107 |
static pj_status_t andgl_stream_set_cap(pjmedia_vid_dev_stream *strm, |
| 108 |
pjmedia_vid_dev_cap cap, |
| 109 |
const void *value); |
| 110 |
static pj_status_t andgl_stream_start(pjmedia_vid_dev_stream *strm); |
| 111 |
static pj_status_t andgl_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 112 |
const pjmedia_frame *frame); |
| 113 |
static pj_status_t andgl_stream_stop(pjmedia_vid_dev_stream *strm); |
| 114 |
static pj_status_t andgl_stream_destroy(pjmedia_vid_dev_stream *strm); |
| 115 |
|
| 116 |
static pj_status_t init_opengl(void * data); |
| 117 |
static pj_status_t deinit_opengl(void * data); |
| 118 |
|
| 119 |
|
| 120 |
static pj_status_t job_queue_create(pj_pool_t *pool, job_queue **pjq); |
| 121 |
static pj_status_t job_queue_post_job(job_queue *jq, job_func_ptr func, |
| 122 |
void *data, unsigned flags, |
| 123 |
pj_status_t *retval); |
| 124 |
static pj_status_t job_queue_destroy(job_queue *jq); |
| 125 |
|
| 126 |
|
| 127 |
static pjmedia_vid_dev_stream_op stream_op = |
| 128 |
{ |
| 129 |
&andgl_stream_get_param, |
| 130 |
&andgl_stream_get_cap, |
| 131 |
&andgl_stream_set_cap, |
| 132 |
&andgl_stream_start, |
| 133 |
NULL, |
| 134 |
&andgl_stream_put_frame, |
| 135 |
&andgl_stream_stop, |
| 136 |
&andgl_stream_destroy |
| 137 |
}; |
| 138 |
|
| 139 |
int pjmedia_vid_dev_opengl_imp_get_cap(void) |
| 140 |
{ |
| 141 |
return PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
| 142 |
} |
| 143 |
|
| 144 |
static andgl_fmt_info* get_andgl_format_info(pjmedia_format_id id) |
| 145 |
{ |
| 146 |
unsigned i; |
| 147 |
|
| 148 |
for (i = 0; i < PJ_ARRAY_SIZE(andgl_fmts); i++) { |
| 149 |
if (andgl_fmts[i].pjmedia_format == id) |
| 150 |
return &andgl_fmts[i]; |
| 151 |
} |
| 152 |
|
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
#define EGL_ERR(str) \ |
| 157 |
{ \ |
| 158 |
PJ_LOG(3, (THIS_FILE, "Unable to %s %d", str, eglGetError())); \ |
| 159 |
status = PJMEDIA_EVID_SYSERR; \ |
| 160 |
goto on_return; \ |
| 161 |
} |
| 162 |
|
| 163 |
static pj_status_t init_opengl(void * data) |
| 164 |
{ |
| 165 |
struct andgl_stream *strm = (struct andgl_stream *)data; |
| 166 |
const EGLint attr[] = |
| 167 |
{ |
| 168 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_SURFACE_TYPE, |
| 169 |
EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, |
| 170 |
EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 8, EGL_NONE |
| 171 |
}; |
| 172 |
EGLint context_attr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
| 173 |
EGLConfig config; |
| 174 |
EGLint numConfigs; |
| 175 |
EGLint format; |
| 176 |
EGLint width; |
| 177 |
EGLint height; |
| 178 |
pj_status_t status; |
| 179 |
|
| 180 |
strm->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 181 |
if (strm->display == EGL_NO_DISPLAY || |
| 182 |
!eglInitialize(strm->display, NULL, NULL)) |
| 183 |
{ |
| 184 |
EGL_ERR("initialize OpenGL display"); |
| 185 |
} |
| 186 |
|
| 187 |
if (!eglChooseConfig(strm->display, attr, &config, 1, &numConfigs) || |
| 188 |
(!eglGetConfigAttrib(strm->display, config, EGL_NATIVE_VISUAL_ID, |
| 189 |
&format))) |
| 190 |
{ |
| 191 |
EGL_ERR("configure OpenGL display"); |
| 192 |
} |
| 193 |
|
| 194 |
if (ANativeWindow_setBuffersGeometry(strm->window, strm->param.disp_size.w, |
| 195 |
strm->param.disp_size.h, format) != 0) |
| 196 |
{ |
| 197 |
EGL_ERR("set window geometry"); |
| 198 |
} |
| 199 |
|
| 200 |
strm->surface = eglCreateWindowSurface(strm->display, config, |
| 201 |
strm->window, 0); |
| 202 |
if (strm->surface == EGL_NO_SURFACE) |
| 203 |
EGL_ERR("create window surface"); |
| 204 |
|
| 205 |
strm->context = eglCreateContext(strm->display, config, EGL_NO_CONTEXT, |
| 206 |
context_attr); |
| 207 |
if (strm->context == EGL_NO_CONTEXT) |
| 208 |
EGL_ERR("create OpenGL context"); |
| 209 |
|
| 210 |
if (!eglMakeCurrent(strm->display, strm->surface, strm->surface, |
| 211 |
strm->context)) |
| 212 |
{ |
| 213 |
EGL_ERR("make OpenGL as current context"); |
| 214 |
} |
| 215 |
|
| 216 |
if (!eglQuerySurface(strm->display, strm->surface, EGL_WIDTH, &width) || |
| 217 |
!eglQuerySurface(strm->display, strm->surface, EGL_HEIGHT, &height)) |
| 218 |
{ |
| 219 |
EGL_ERR("query surface"); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
pjmedia_vid_dev_opengl_create_buffers(strm->pool, PJ_TRUE, &strm->gl_buf); |
| 224 |
|
| 225 |
|
| 226 |
status = pjmedia_vid_dev_opengl_init_buffers(strm->gl_buf); |
| 227 |
|
| 228 |
on_return: |
| 229 |
if (status != PJ_SUCCESS) |
| 230 |
deinit_opengl(strm); |
| 231 |
|
| 232 |
return status; |
| 233 |
} |
| 234 |
|
| 235 |
#undef EGL_ERR |
| 236 |
|
| 237 |
static pj_status_t render(void * data) |
| 238 |
{ |
| 239 |
struct andgl_stream *stream = (struct andgl_stream *)data; |
| 240 |
|
| 241 |
if (stream->display == EGL_NO_DISPLAY || stream->err_rend == 0) |
| 242 |
return PJ_SUCCESS; |
| 243 |
|
| 244 |
pjmedia_vid_dev_opengl_draw(stream->gl_buf, stream->vid_size.w, |
| 245 |
stream->vid_size.h, stream->frame->buf); |
| 246 |
|
| 247 |
if (!eglSwapBuffers(stream->display, stream->surface)) { |
| 248 |
if (eglGetError() == EGL_BAD_SURFACE && stream->err_rend > 0) { |
| 249 |
stream->err_rend--; |
| 250 |
if (stream->err_rend == 0) { |
| 251 |
PJ_LOG(3, (THIS_FILE, "Stopping OpenGL rendering due to " |
| 252 |
"consecutive errors. If app is in bg," |
| 253 |
"it's advisable to stop the stream.")); |
| 254 |
} |
| 255 |
} |
| 256 |
return eglGetError(); |
| 257 |
} |
| 258 |
|
| 259 |
return PJ_SUCCESS; |
| 260 |
} |
| 261 |
|
| 262 |
static pj_status_t deinit_opengl(void * data) |
| 263 |
{ |
| 264 |
struct andgl_stream *stream = (struct andgl_stream *)data; |
| 265 |
|
| 266 |
if (stream->gl_buf) { |
| 267 |
pjmedia_vid_dev_opengl_destroy_buffers(stream->gl_buf); |
| 268 |
stream->gl_buf = NULL; |
| 269 |
} |
| 270 |
|
| 271 |
if (stream->display != EGL_NO_DISPLAY) { |
| 272 |
eglMakeCurrent(stream->display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 273 |
EGL_NO_CONTEXT); |
| 274 |
if (stream->context != EGL_NO_CONTEXT) |
| 275 |
eglDestroyContext(stream->display, stream->context); |
| 276 |
if (stream->surface != EGL_NO_SURFACE) |
| 277 |
eglDestroySurface(stream->display, stream->surface); |
| 278 |
eglTerminate(stream->display); |
| 279 |
} |
| 280 |
|
| 281 |
stream->display = EGL_NO_DISPLAY; |
| 282 |
stream->surface = EGL_NO_SURFACE; |
| 283 |
stream->context = EGL_NO_CONTEXT; |
| 284 |
|
| 285 |
return PJ_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
pj_status_t |
| 290 |
pjmedia_vid_dev_opengl_imp_create_stream(pj_pool_t *pool, |
| 291 |
pjmedia_vid_dev_param *param, |
| 292 |
const pjmedia_vid_dev_cb *cb, |
| 293 |
void *user_data, |
| 294 |
pjmedia_vid_dev_stream **p_vid_strm) |
| 295 |
{ |
| 296 |
struct andgl_stream *strm; |
| 297 |
const pjmedia_video_format_detail *vfd; |
| 298 |
pj_status_t status = PJ_SUCCESS; |
| 299 |
|
| 300 |
strm = PJ_POOL_ZALLOC_T(pool, struct andgl_stream); |
| 301 |
pj_memcpy(&strm->param, param, sizeof(*param)); |
| 302 |
strm->pool = pool; |
| 303 |
pj_memcpy(&strm->vid_cb, cb, sizeof(*cb)); |
| 304 |
strm->user_data = user_data; |
| 305 |
strm->display = EGL_NO_DISPLAY; |
| 306 |
|
| 307 |
vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE); |
| 308 |
strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1); |
| 309 |
|
| 310 |
|
| 311 |
status = andgl_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_FORMAT, |
| 312 |
¶m->fmt); |
| 313 |
if (status != PJ_SUCCESS) |
| 314 |
goto on_error; |
| 315 |
|
| 316 |
status = job_queue_create(pool, &strm->jq); |
| 317 |
if (status != PJ_SUCCESS) |
| 318 |
goto on_error; |
| 319 |
|
| 320 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 321 |
status = andgl_stream_set_cap(&strm->base, |
| 322 |
PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 323 |
¶m->window); |
| 324 |
} |
| 325 |
|
| 326 |
if (status != PJ_SUCCESS) { |
| 327 |
PJ_LOG(3, (THIS_FILE, "Failed to initialize OpenGL with the specified" |
| 328 |
" output window")); |
| 329 |
goto on_error; |
| 330 |
} |
| 331 |
|
| 332 |
PJ_LOG(4, (THIS_FILE, "Android OpenGL ES renderer successfully created")); |
| 333 |
|
| 334 |
|
| 335 |
strm->base.op = &stream_op; |
| 336 |
*p_vid_strm = &strm->base; |
| 337 |
|
| 338 |
return PJ_SUCCESS; |
| 339 |
|
| 340 |
on_error: |
| 341 |
andgl_stream_destroy((pjmedia_vid_dev_stream *)strm); |
| 342 |
|
| 343 |
return status; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
static pj_status_t andgl_stream_get_param(pjmedia_vid_dev_stream *s, |
| 348 |
pjmedia_vid_dev_param *pi) |
| 349 |
{ |
| 350 |
struct andgl_stream *strm = (struct andgl_stream*)s; |
| 351 |
|
| 352 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 353 |
|
| 354 |
pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 355 |
|
| 356 |
if (andgl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 357 |
&pi->window) == PJ_SUCCESS) |
| 358 |
{ |
| 359 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
| 360 |
} |
| 361 |
|
| 362 |
return PJ_SUCCESS; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
static pj_status_t andgl_stream_get_cap(pjmedia_vid_dev_stream *s, |
| 367 |
pjmedia_vid_dev_cap cap, |
| 368 |
void *pval) |
| 369 |
{ |
| 370 |
struct andgl_stream *strm = (struct andgl_stream*)s; |
| 371 |
|
| 372 |
PJ_UNUSED_ARG(strm); |
| 373 |
|
| 374 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 375 |
|
| 376 |
if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 377 |
pjmedia_vid_dev_hwnd *wnd = (pjmedia_vid_dev_hwnd *)pval; |
| 378 |
wnd->info.android.window = strm->window; |
| 379 |
return PJ_SUCCESS; |
| 380 |
} else { |
| 381 |
return PJMEDIA_EVID_INVCAP; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
static pj_status_t andgl_stream_set_cap(pjmedia_vid_dev_stream *s, |
| 387 |
pjmedia_vid_dev_cap cap, |
| 388 |
const void *pval) |
| 389 |
{ |
| 390 |
struct andgl_stream *strm = (struct andgl_stream*)s; |
| 391 |
|
| 392 |
PJ_UNUSED_ARG(strm); |
| 393 |
|
| 394 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 395 |
|
| 396 |
if (cap==PJMEDIA_VID_DEV_CAP_FORMAT) { |
| 397 |
const pjmedia_video_format_info *vfi; |
| 398 |
pjmedia_video_format_detail *vfd; |
| 399 |
pjmedia_format *fmt = (pjmedia_format *)pval; |
| 400 |
andgl_fmt_info *ifi; |
| 401 |
pj_status_t status = PJ_SUCCESS; |
| 402 |
|
| 403 |
if (!(ifi = get_andgl_format_info(fmt->id))) |
| 404 |
return PJMEDIA_EVID_BADFORMAT; |
| 405 |
|
| 406 |
vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(), |
| 407 |
fmt->id); |
| 408 |
if (!vfi) |
| 409 |
return PJMEDIA_EVID_BADFORMAT; |
| 410 |
|
| 411 |
|
| 412 |
if (strm->window) |
| 413 |
job_queue_post_job(strm->jq, deinit_opengl, strm, 0, NULL); |
| 414 |
|
| 415 |
pjmedia_format_copy(&strm->param.fmt, fmt); |
| 416 |
|
| 417 |
vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE); |
| 418 |
pj_memcpy(&strm->vid_size, &vfd->size, sizeof(vfd->size)); |
| 419 |
pj_memcpy(&strm->param.disp_size, &vfd->size, sizeof(vfd->size)); |
| 420 |
|
| 421 |
if (strm->window) |
| 422 |
job_queue_post_job(strm->jq, init_opengl, strm, 0, &status); |
| 423 |
|
| 424 |
PJ_PERROR(4,(THIS_FILE, status, |
| 425 |
"Re-initializing OpenGL due to format change")); |
| 426 |
return status; |
| 427 |
|
| 428 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 429 |
pj_status_t status = PJ_SUCCESS; |
| 430 |
pjmedia_vid_dev_hwnd *wnd = (pjmedia_vid_dev_hwnd *)pval; |
| 431 |
ANativeWindow *native_wnd = (ANativeWindow *)wnd->info.android.window; |
| 432 |
|
| 433 |
if (strm->window == native_wnd) |
| 434 |
return PJ_SUCCESS; |
| 435 |
|
| 436 |
|
| 437 |
job_queue_post_job(strm->jq, deinit_opengl, strm, 0, NULL); |
| 438 |
if (strm->window) |
| 439 |
ANativeWindow_release(strm->window); |
| 440 |
|
| 441 |
strm->window = strm->param.window.info.android.window = native_wnd; |
| 442 |
if (strm->window) { |
| 443 |
job_queue_post_job(strm->jq, init_opengl, strm, 0, &status); |
| 444 |
} |
| 445 |
|
| 446 |
PJ_PERROR(4,(THIS_FILE, status, |
| 447 |
"Re-initializing OpenGL with native window %p", |
| 448 |
strm->window)); |
| 449 |
return status; |
| 450 |
} |
| 451 |
|
| 452 |
return PJMEDIA_EVID_INVCAP; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
static pj_status_t andgl_stream_start(pjmedia_vid_dev_stream *strm) |
| 457 |
{ |
| 458 |
struct andgl_stream *stream = (struct andgl_stream*)strm; |
| 459 |
|
| 460 |
stream->err_rend = STOP_IF_ERROR_RENDERING; |
| 461 |
if (!stream->err_rend) stream->err_rend = 0xFFFF; |
| 462 |
stream->is_running = PJ_TRUE; |
| 463 |
PJ_LOG(4, (THIS_FILE, "Starting Android opengl stream")); |
| 464 |
|
| 465 |
return PJ_SUCCESS; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
static pj_status_t andgl_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 470 |
const pjmedia_frame *frame) |
| 471 |
{ |
| 472 |
struct andgl_stream *stream = (struct andgl_stream*)strm; |
| 473 |
pj_status_t status; |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if (frame->size==0 || frame->buf==NULL) |
| 479 |
return PJ_SUCCESS; |
| 480 |
|
| 481 |
if (!stream->is_running || stream->display == EGL_NO_DISPLAY) |
| 482 |
return PJ_EINVALIDOP; |
| 483 |
|
| 484 |
stream->frame = frame; |
| 485 |
job_queue_post_job(stream->jq, render, strm, 0, &status); |
| 486 |
|
| 487 |
return status; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
static pj_status_t andgl_stream_stop(pjmedia_vid_dev_stream *strm) |
| 492 |
{ |
| 493 |
struct andgl_stream *stream = (struct andgl_stream*)strm; |
| 494 |
|
| 495 |
stream->is_running = PJ_FALSE; |
| 496 |
PJ_LOG(4, (THIS_FILE, "Stopping Android opengl stream")); |
| 497 |
|
| 498 |
return PJ_SUCCESS; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static pj_status_t andgl_stream_destroy(pjmedia_vid_dev_stream *strm) |
| 504 |
{ |
| 505 |
struct andgl_stream *stream = (struct andgl_stream*)strm; |
| 506 |
|
| 507 |
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 508 |
|
| 509 |
andgl_stream_stop(strm); |
| 510 |
|
| 511 |
job_queue_post_job(stream->jq, deinit_opengl, strm, 0, NULL); |
| 512 |
|
| 513 |
if (stream->window) { |
| 514 |
ANativeWindow_release(stream->window); |
| 515 |
stream->window = NULL; |
| 516 |
} |
| 517 |
|
| 518 |
if (stream->jq) { |
| 519 |
job_queue_destroy(stream->jq); |
| 520 |
stream->jq = NULL; |
| 521 |
} |
| 522 |
|
| 523 |
pj_pool_release(stream->pool); |
| 524 |
|
| 525 |
return PJ_SUCCESS; |
| 526 |
} |
| 527 |
|
| 528 |
static int job_thread(void * data) |
| 529 |
{ |
| 530 |
job_queue *jq = (job_queue *)data; |
| 531 |
|
| 532 |
while (1) { |
| 533 |
job *jb; |
| 534 |
|
| 535 |
|
| 536 |
pj_sem_wait(jq->sem); |
| 537 |
|
| 538 |
|
| 539 |
if (jq->is_quitting && jq->head == jq->tail) |
| 540 |
break; |
| 541 |
|
| 542 |
jb = jq->jobs[jq->head]; |
| 543 |
jb->retval = (*jb->func)(jb->data); |
| 544 |
pj_sem_post(jq->job_sem[jq->head]); |
| 545 |
jq->head = (jq->head + 1) % jq->size; |
| 546 |
} |
| 547 |
|
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
static pj_status_t job_queue_create(pj_pool_t *pool, job_queue **pjq) |
| 552 |
{ |
| 553 |
unsigned i; |
| 554 |
pj_status_t status; |
| 555 |
|
| 556 |
job_queue *jq = PJ_POOL_ZALLOC_T(pool, job_queue); |
| 557 |
jq->size = MAX_JOBS; |
| 558 |
status = pj_sem_create(pool, "thread_sem", 0, jq->size + 1, &jq->sem); |
| 559 |
if (status != PJ_SUCCESS) |
| 560 |
goto on_error; |
| 561 |
|
| 562 |
for (i = 0; i < jq->size; i++) { |
| 563 |
status = pj_sem_create(pool, "job_sem", 0, 1, &jq->job_sem[i]); |
| 564 |
if (status != PJ_SUCCESS) |
| 565 |
goto on_error; |
| 566 |
} |
| 567 |
|
| 568 |
status = pj_mutex_create_recursive(pool, "job_mutex", &jq->mutex); |
| 569 |
if (status != PJ_SUCCESS) |
| 570 |
goto on_error; |
| 571 |
|
| 572 |
status = pj_thread_create(pool, "job_th", job_thread, jq, 0, 0, |
| 573 |
&jq->thread); |
| 574 |
if (status != PJ_SUCCESS) |
| 575 |
goto on_error; |
| 576 |
|
| 577 |
*pjq = jq; |
| 578 |
return PJ_SUCCESS; |
| 579 |
|
| 580 |
on_error: |
| 581 |
job_queue_destroy(jq); |
| 582 |
return status; |
| 583 |
} |
| 584 |
|
| 585 |
static pj_status_t job_queue_post_job(job_queue *jq, job_func_ptr func, |
| 586 |
void *data, unsigned flags, |
| 587 |
pj_status_t *retval) |
| 588 |
{ |
| 589 |
job jb; |
| 590 |
int tail; |
| 591 |
|
| 592 |
if (jq->is_quitting) |
| 593 |
return PJ_EBUSY; |
| 594 |
|
| 595 |
jb.func = func; |
| 596 |
jb.data = data; |
| 597 |
jb.flags = flags; |
| 598 |
|
| 599 |
pj_mutex_lock(jq->mutex); |
| 600 |
jq->jobs[jq->tail] = &jb; |
| 601 |
tail = jq->tail; |
| 602 |
jq->tail = (jq->tail + 1) % jq->size; |
| 603 |
|
| 604 |
pj_sem_post(jq->sem); |
| 605 |
|
| 606 |
pj_sem_wait(jq->job_sem[tail]); |
| 607 |
pj_mutex_unlock(jq->mutex); |
| 608 |
|
| 609 |
if (retval) *retval = jb.retval; |
| 610 |
|
| 611 |
return PJ_SUCCESS; |
| 612 |
} |
| 613 |
|
| 614 |
static pj_status_t job_queue_destroy(job_queue *jq) |
| 615 |
{ |
| 616 |
unsigned i; |
| 617 |
|
| 618 |
jq->is_quitting = PJ_TRUE; |
| 619 |
|
| 620 |
if (jq->thread) { |
| 621 |
pj_sem_post(jq->sem); |
| 622 |
pj_thread_join(jq->thread); |
| 623 |
pj_thread_destroy(jq->thread); |
| 624 |
} |
| 625 |
|
| 626 |
if (jq->sem) { |
| 627 |
pj_sem_destroy(jq->sem); |
| 628 |
jq->sem = NULL; |
| 629 |
} |
| 630 |
for (i = 0; i < jq->size; i++) { |
| 631 |
if (jq->job_sem[i]) { |
| 632 |
pj_sem_destroy(jq->job_sem[i]); |
| 633 |
jq->job_sem[i] = NULL; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
if (jq->mutex) { |
| 638 |
pj_mutex_destroy(jq->mutex); |
| 639 |
jq->mutex = NULL; |
| 640 |
} |
| 641 |
|
| 642 |
return PJ_SUCCESS; |
| 643 |
} |
| 644 |
|
| 645 |
#endif |