| 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 <pjmedia/event.h> |
| 20 |
#include <pj/assert.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/os.h> |
| 23 |
|
| 24 |
#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ |
| 25 |
defined(PJMEDIA_VIDEO_DEV_HAS_SDL) && PJMEDIA_VIDEO_DEV_HAS_SDL != 0 |
| 26 |
|
| 27 |
#include <SDL.h> |
| 28 |
#include <SDL_syswm.h> |
| 29 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 30 |
# include "SDL_opengl.h" |
| 31 |
# define OPENGL_DEV_IDX 1 |
| 32 |
#endif |
| 33 |
|
| 34 |
#if !(SDL_VERSION_ATLEAST(1,3,0)) |
| 35 |
# error "SDL 1.3 or later is required" |
| 36 |
#endif |
| 37 |
|
| 38 |
#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 39 |
# include "TargetConditionals.h" |
| 40 |
# include <Foundation/Foundation.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#define THIS_FILE "sdl_dev.c" |
| 44 |
#define DEFAULT_CLOCK_RATE 90000 |
| 45 |
#define DEFAULT_WIDTH 640 |
| 46 |
#define DEFAULT_HEIGHT 480 |
| 47 |
#define DEFAULT_FPS 25 |
| 48 |
|
| 49 |
typedef struct sdl_fmt_info |
| 50 |
{ |
| 51 |
pjmedia_format_id fmt_id; |
| 52 |
Uint32 sdl_format; |
| 53 |
Uint32 Rmask; |
| 54 |
Uint32 Gmask; |
| 55 |
Uint32 Bmask; |
| 56 |
Uint32 Amask; |
| 57 |
} sdl_fmt_info; |
| 58 |
|
| 59 |
static sdl_fmt_info sdl_fmts[] = |
| 60 |
{ |
| 61 |
#if PJ_IS_BIG_ENDIAN |
| 62 |
{PJMEDIA_FORMAT_RGBA, (Uint32)SDL_PIXELFORMAT_RGBA8888, |
| 63 |
0xFF000000, 0xFF0000, 0xFF00, 0xFF} , |
| 64 |
{PJMEDIA_FORMAT_RGB24, (Uint32)SDL_PIXELFORMAT_RGB24, |
| 65 |
0xFF0000, 0xFF00, 0xFF, 0} , |
| 66 |
{PJMEDIA_FORMAT_BGRA, (Uint32)SDL_PIXELFORMAT_BGRA8888, |
| 67 |
0xFF00, 0xFF0000, 0xFF000000, 0xFF} , |
| 68 |
#else |
| 69 |
{PJMEDIA_FORMAT_RGBA, (Uint32)SDL_PIXELFORMAT_ABGR8888, |
| 70 |
0xFF, 0xFF00, 0xFF0000, 0xFF000000} , |
| 71 |
{PJMEDIA_FORMAT_RGB24, (Uint32)SDL_PIXELFORMAT_BGR24, |
| 72 |
0xFF, 0xFF00, 0xFF0000, 0} , |
| 73 |
{PJMEDIA_FORMAT_BGRA, (Uint32)SDL_PIXELFORMAT_ARGB8888, |
| 74 |
0xFF0000, 0xFF00, 0xFF, 0xFF000000} , |
| 75 |
#endif |
| 76 |
|
| 77 |
{PJMEDIA_FORMAT_DIB , (Uint32)SDL_PIXELFORMAT_RGB24, |
| 78 |
0xFF0000, 0xFF00, 0xFF, 0} , |
| 79 |
|
| 80 |
{PJMEDIA_FORMAT_YUY2, SDL_PIXELFORMAT_YUY2, 0, 0, 0, 0} , |
| 81 |
{PJMEDIA_FORMAT_UYVY, SDL_PIXELFORMAT_UYVY, 0, 0, 0, 0} , |
| 82 |
{PJMEDIA_FORMAT_YVYU, SDL_PIXELFORMAT_YVYU, 0, 0, 0, 0} , |
| 83 |
{PJMEDIA_FORMAT_I420, SDL_PIXELFORMAT_IYUV, 0, 0, 0, 0} , |
| 84 |
{PJMEDIA_FORMAT_YV12, SDL_PIXELFORMAT_YV12, 0, 0, 0, 0} , |
| 85 |
{PJMEDIA_FORMAT_I420JPEG, SDL_PIXELFORMAT_IYUV, 0, 0, 0, 0} , |
| 86 |
{PJMEDIA_FORMAT_I422JPEG, SDL_PIXELFORMAT_YV12, 0, 0, 0, 0} |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
struct sdl_dev_info |
| 91 |
{ |
| 92 |
pjmedia_vid_dev_info info; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
struct stream_list |
| 97 |
{ |
| 98 |
PJ_DECL_LIST_MEMBER(struct stream_list); |
| 99 |
struct sdl_stream *stream; |
| 100 |
}; |
| 101 |
|
| 102 |
#define INITIAL_MAX_JOBS 64 |
| 103 |
#define JOB_QUEUE_INC_FACTOR 2 |
| 104 |
|
| 105 |
typedef pj_status_t (*job_func_ptr)(void *data); |
| 106 |
|
| 107 |
typedef struct job { |
| 108 |
job_func_ptr func; |
| 109 |
void *data; |
| 110 |
unsigned flags; |
| 111 |
pj_status_t retval; |
| 112 |
} job; |
| 113 |
|
| 114 |
#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 115 |
@interface JQDelegate: NSObject |
| 116 |
{ |
| 117 |
@public |
| 118 |
job *pjob; |
| 119 |
} |
| 120 |
|
| 121 |
- (void)run_job; |
| 122 |
@end |
| 123 |
|
| 124 |
@implementation JQDelegate |
| 125 |
- (void)run_job |
| 126 |
{ |
| 127 |
pjob->retval = (*pjob->func)(pjob->data); |
| 128 |
} |
| 129 |
@end |
| 130 |
#endif |
| 131 |
|
| 132 |
typedef struct job_queue { |
| 133 |
pj_pool_t *pool; |
| 134 |
job **jobs; |
| 135 |
pj_sem_t **job_sem; |
| 136 |
pj_sem_t **old_sem; |
| 137 |
pj_mutex_t *mutex; |
| 138 |
pj_thread_t *thread; |
| 139 |
pj_sem_t *sem; |
| 140 |
|
| 141 |
unsigned size; |
| 142 |
unsigned head, tail; |
| 143 |
pj_bool_t is_full; |
| 144 |
pj_bool_t is_quitting; |
| 145 |
} job_queue; |
| 146 |
|
| 147 |
|
| 148 |
struct sdl_factory |
| 149 |
{ |
| 150 |
pjmedia_vid_dev_factory base; |
| 151 |
pj_pool_t *pool; |
| 152 |
pj_pool_factory *pf; |
| 153 |
|
| 154 |
unsigned dev_count; |
| 155 |
struct sdl_dev_info *dev_info; |
| 156 |
job_queue *jq; |
| 157 |
|
| 158 |
pj_thread_t *sdl_thread; |
| 159 |
pj_sem_t *sem; |
| 160 |
pj_mutex_t *mutex; |
| 161 |
struct stream_list streams; |
| 162 |
pj_bool_t is_quitting; |
| 163 |
pj_thread_desc thread_desc; |
| 164 |
pj_thread_t *ev_thread; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
struct sdl_stream |
| 169 |
{ |
| 170 |
pjmedia_vid_dev_stream base; |
| 171 |
pjmedia_vid_dev_param param; |
| 172 |
pj_pool_t *pool; |
| 173 |
|
| 174 |
pjmedia_vid_dev_cb vid_cb; |
| 175 |
void *user_data; |
| 176 |
|
| 177 |
struct sdl_factory *sf; |
| 178 |
const pjmedia_frame *frame; |
| 179 |
pj_bool_t is_running; |
| 180 |
pj_timestamp last_ts; |
| 181 |
struct stream_list list_entry; |
| 182 |
|
| 183 |
SDL_Window *window; |
| 184 |
SDL_Renderer *renderer; |
| 185 |
SDL_Texture *scr_tex; |
| 186 |
int pitch; |
| 187 |
SDL_Rect rect; |
| 188 |
SDL_Rect dstrect; |
| 189 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 190 |
SDL_GLContext *gl_context; |
| 191 |
GLuint texture; |
| 192 |
#endif |
| 193 |
|
| 194 |
pjmedia_video_apply_fmt_param vafp; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
static pj_status_t sdl_factory_init(pjmedia_vid_dev_factory *f); |
| 199 |
static pj_status_t sdl_factory_destroy(pjmedia_vid_dev_factory *f); |
| 200 |
static pj_status_t sdl_factory_refresh(pjmedia_vid_dev_factory *f); |
| 201 |
static unsigned sdl_factory_get_dev_count(pjmedia_vid_dev_factory *f); |
| 202 |
static pj_status_t sdl_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 203 |
unsigned index, |
| 204 |
pjmedia_vid_dev_info *info); |
| 205 |
static pj_status_t sdl_factory_default_param(pj_pool_t *pool, |
| 206 |
pjmedia_vid_dev_factory *f, |
| 207 |
unsigned index, |
| 208 |
pjmedia_vid_dev_param *param); |
| 209 |
static pj_status_t sdl_factory_create_stream( |
| 210 |
pjmedia_vid_dev_factory *f, |
| 211 |
pjmedia_vid_dev_param *param, |
| 212 |
const pjmedia_vid_dev_cb *cb, |
| 213 |
void *user_data, |
| 214 |
pjmedia_vid_dev_stream **p_vid_strm); |
| 215 |
|
| 216 |
static pj_status_t sdl_stream_get_param(pjmedia_vid_dev_stream *strm, |
| 217 |
pjmedia_vid_dev_param *param); |
| 218 |
static pj_status_t sdl_stream_get_cap(pjmedia_vid_dev_stream *strm, |
| 219 |
pjmedia_vid_dev_cap cap, |
| 220 |
void *value); |
| 221 |
static pj_status_t sdl_stream_set_cap(pjmedia_vid_dev_stream *strm, |
| 222 |
pjmedia_vid_dev_cap cap, |
| 223 |
const void *value); |
| 224 |
static pj_status_t sdl_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 225 |
const pjmedia_frame *frame); |
| 226 |
static pj_status_t sdl_stream_start(pjmedia_vid_dev_stream *strm); |
| 227 |
static pj_status_t sdl_stream_stop(pjmedia_vid_dev_stream *strm); |
| 228 |
static pj_status_t sdl_stream_destroy(pjmedia_vid_dev_stream *strm); |
| 229 |
|
| 230 |
static pj_status_t resize_disp(struct sdl_stream *strm, |
| 231 |
pjmedia_rect_size *new_disp_size); |
| 232 |
static pj_status_t sdl_destroy_all(void *data); |
| 233 |
|
| 234 |
|
| 235 |
static pj_status_t job_queue_create(pj_pool_t *pool, job_queue **pjq); |
| 236 |
static pj_status_t job_queue_post_job(job_queue *jq, job_func_ptr func, |
| 237 |
void *data, unsigned flags, |
| 238 |
pj_status_t *retval); |
| 239 |
static pj_status_t job_queue_destroy(job_queue *jq); |
| 240 |
|
| 241 |
|
| 242 |
static pjmedia_vid_dev_factory_op factory_op = |
| 243 |
{ |
| 244 |
&sdl_factory_init, |
| 245 |
&sdl_factory_destroy, |
| 246 |
&sdl_factory_get_dev_count, |
| 247 |
&sdl_factory_get_dev_info, |
| 248 |
&sdl_factory_default_param, |
| 249 |
&sdl_factory_create_stream, |
| 250 |
&sdl_factory_refresh |
| 251 |
}; |
| 252 |
|
| 253 |
static pjmedia_vid_dev_stream_op stream_op = |
| 254 |
{ |
| 255 |
&sdl_stream_get_param, |
| 256 |
&sdl_stream_get_cap, |
| 257 |
&sdl_stream_set_cap, |
| 258 |
&sdl_stream_start, |
| 259 |
NULL, |
| 260 |
&sdl_stream_put_frame, |
| 261 |
&sdl_stream_stop, |
| 262 |
&sdl_stream_destroy |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static void sdl_log_err(const char *op) |
| 269 |
{ |
| 270 |
PJ_LOG(1,(THIS_FILE, "%s error: %s", op, SDL_GetError())); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
pjmedia_vid_dev_factory* pjmedia_sdl_factory(pj_pool_factory *pf) |
| 280 |
{ |
| 281 |
struct sdl_factory *f; |
| 282 |
pj_pool_t *pool; |
| 283 |
|
| 284 |
pool = pj_pool_create(pf, "sdl video", 4000, 4000, NULL); |
| 285 |
f = PJ_POOL_ZALLOC_T(pool, struct sdl_factory); |
| 286 |
f->pf = pf; |
| 287 |
f->pool = pool; |
| 288 |
f->base.op = &factory_op; |
| 289 |
|
| 290 |
return &f->base; |
| 291 |
} |
| 292 |
|
| 293 |
static pj_status_t sdl_init(void * data) |
| 294 |
{ |
| 295 |
PJ_UNUSED_ARG(data); |
| 296 |
|
| 297 |
#if SDL_VERSION_ATLEAST(2,0,2) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1"); |
| 302 |
#endif |
| 303 |
|
| 304 |
if (SDL_Init(SDL_INIT_VIDEO)) { |
| 305 |
sdl_log_err("SDL_Init()"); |
| 306 |
return PJMEDIA_EVID_INIT; |
| 307 |
} |
| 308 |
|
| 309 |
return PJ_SUCCESS; |
| 310 |
} |
| 311 |
|
| 312 |
static struct sdl_stream* find_stream(struct sdl_factory *sf, |
| 313 |
Uint32 windowID, |
| 314 |
pjmedia_event *pevent) |
| 315 |
{ |
| 316 |
struct stream_list *it, *itBegin; |
| 317 |
struct sdl_stream *strm = NULL; |
| 318 |
|
| 319 |
itBegin = &sf->streams; |
| 320 |
for (it = itBegin->next; it != itBegin; it = it->next) { |
| 321 |
if (SDL_GetWindowID(it->stream->window) == windowID) |
| 322 |
{ |
| 323 |
strm = it->stream; |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (strm) |
| 329 |
pjmedia_event_init(pevent, PJMEDIA_EVENT_NONE, &strm->last_ts, |
| 330 |
strm); |
| 331 |
|
| 332 |
return strm; |
| 333 |
} |
| 334 |
|
| 335 |
static pj_status_t handle_event(void *data) |
| 336 |
{ |
| 337 |
struct sdl_factory *sf = (struct sdl_factory*)data; |
| 338 |
SDL_Event sevent; |
| 339 |
|
| 340 |
if (!pj_thread_is_registered()) |
| 341 |
pj_thread_register("sdl_ev", sf->thread_desc, &sf->ev_thread); |
| 342 |
|
| 343 |
while (SDL_PollEvent(&sevent)) { |
| 344 |
struct sdl_stream *strm = NULL; |
| 345 |
pjmedia_event pevent; |
| 346 |
|
| 347 |
pj_mutex_lock(sf->mutex); |
| 348 |
pevent.type = PJMEDIA_EVENT_NONE; |
| 349 |
switch(sevent.type) { |
| 350 |
case SDL_MOUSEBUTTONDOWN: |
| 351 |
strm = find_stream(sf, sevent.button.windowID, &pevent); |
| 352 |
pevent.type = PJMEDIA_EVENT_MOUSE_BTN_DOWN; |
| 353 |
break; |
| 354 |
case SDL_WINDOWEVENT: |
| 355 |
strm = find_stream(sf, sevent.window.windowID, &pevent); |
| 356 |
switch (sevent.window.event) { |
| 357 |
case SDL_WINDOWEVENT_RESIZED: |
| 358 |
pevent.type = PJMEDIA_EVENT_WND_RESIZED; |
| 359 |
pevent.data.wnd_resized.new_size.w = |
| 360 |
sevent.window.data1; |
| 361 |
pevent.data.wnd_resized.new_size.h = |
| 362 |
sevent.window.data2; |
| 363 |
break; |
| 364 |
case SDL_WINDOWEVENT_CLOSE: |
| 365 |
pevent.type = PJMEDIA_EVENT_WND_CLOSING; |
| 366 |
break; |
| 367 |
} |
| 368 |
break; |
| 369 |
default: |
| 370 |
break; |
| 371 |
} |
| 372 |
|
| 373 |
if (strm && pevent.type != PJMEDIA_EVENT_NONE) { |
| 374 |
pj_status_t status; |
| 375 |
|
| 376 |
pjmedia_event_publish(NULL, strm, &pevent, 0); |
| 377 |
|
| 378 |
switch (pevent.type) { |
| 379 |
case PJMEDIA_EVENT_WND_RESIZED: |
| 380 |
status = resize_disp(strm, &pevent.data.wnd_resized.new_size); |
| 381 |
if (status != PJ_SUCCESS) { |
| 382 |
PJ_PERROR(3, (THIS_FILE, status, |
| 383 |
"Failed resizing the display.")); |
| 384 |
} |
| 385 |
break; |
| 386 |
case PJMEDIA_EVENT_WND_CLOSING: |
| 387 |
if (pevent.data.wnd_closing.cancel) { |
| 388 |
|
| 389 |
break; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
sdl_stream_stop(&strm->base); |
| 397 |
sdl_destroy_all(strm); |
| 398 |
pjmedia_event_init(&pevent, PJMEDIA_EVENT_WND_CLOSED, |
| 399 |
&strm->last_ts, strm); |
| 400 |
pjmedia_event_publish(NULL, strm, &pevent, 0); |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
break; |
| 407 |
default: |
| 408 |
|
| 409 |
break; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
pj_mutex_unlock(sf->mutex); |
| 414 |
} |
| 415 |
|
| 416 |
return PJ_SUCCESS; |
| 417 |
} |
| 418 |
|
| 419 |
static int sdl_ev_thread(void *data) |
| 420 |
{ |
| 421 |
struct sdl_factory *sf = (struct sdl_factory*)data; |
| 422 |
|
| 423 |
while(1) { |
| 424 |
pj_status_t status; |
| 425 |
|
| 426 |
pj_mutex_lock(sf->mutex); |
| 427 |
if (pj_list_empty(&sf->streams)) { |
| 428 |
pj_mutex_unlock(sf->mutex); |
| 429 |
|
| 430 |
pj_sem_wait(sf->sem); |
| 431 |
} else |
| 432 |
pj_mutex_unlock(sf->mutex); |
| 433 |
|
| 434 |
if (sf->is_quitting) |
| 435 |
break; |
| 436 |
|
| 437 |
job_queue_post_job(sf->jq, handle_event, sf, 0, &status); |
| 438 |
|
| 439 |
pj_thread_sleep(50); |
| 440 |
} |
| 441 |
|
| 442 |
return 0; |
| 443 |
} |
| 444 |
|
| 445 |
static pj_status_t sdl_quit(void *data) |
| 446 |
{ |
| 447 |
PJ_UNUSED_ARG(data); |
| 448 |
SDL_Quit(); |
| 449 |
return PJ_SUCCESS; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
static pj_status_t sdl_factory_init(pjmedia_vid_dev_factory *f) |
| 454 |
{ |
| 455 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 456 |
struct sdl_dev_info *ddi; |
| 457 |
unsigned i, j; |
| 458 |
pj_status_t status; |
| 459 |
SDL_version version; |
| 460 |
|
| 461 |
pj_list_init(&sf->streams); |
| 462 |
|
| 463 |
status = job_queue_create(sf->pool, &sf->jq); |
| 464 |
if (status != PJ_SUCCESS) |
| 465 |
return PJMEDIA_EVID_INIT; |
| 466 |
|
| 467 |
job_queue_post_job(sf->jq, sdl_init, NULL, 0, &status); |
| 468 |
if (status != PJ_SUCCESS) |
| 469 |
return status; |
| 470 |
|
| 471 |
status = pj_mutex_create_recursive(sf->pool, "sdl_factory", |
| 472 |
&sf->mutex); |
| 473 |
if (status != PJ_SUCCESS) |
| 474 |
return status; |
| 475 |
|
| 476 |
status = pj_sem_create(sf->pool, NULL, 0, 1, &sf->sem); |
| 477 |
if (status != PJ_SUCCESS) |
| 478 |
return status; |
| 479 |
|
| 480 |
|
| 481 |
status = pj_thread_create(sf->pool, "sdl_thread", sdl_ev_thread, |
| 482 |
sf, 0, 0, &sf->sdl_thread); |
| 483 |
if (status != PJ_SUCCESS) |
| 484 |
return status; |
| 485 |
|
| 486 |
sf->dev_count = 1; |
| 487 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 488 |
sf->dev_count++; |
| 489 |
#endif |
| 490 |
sf->dev_info = (struct sdl_dev_info*) |
| 491 |
pj_pool_calloc(sf->pool, sf->dev_count, |
| 492 |
sizeof(struct sdl_dev_info)); |
| 493 |
|
| 494 |
ddi = &sf->dev_info[0]; |
| 495 |
pj_bzero(ddi, sizeof(*ddi)); |
| 496 |
pj_ansi_strxcpy(ddi->info.name, "SDL renderer", |
| 497 |
sizeof(ddi->info.name)); |
| 498 |
ddi->info.fmt_cnt = PJ_ARRAY_SIZE(sdl_fmts); |
| 499 |
|
| 500 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 501 |
ddi = &sf->dev_info[OPENGL_DEV_IDX]; |
| 502 |
pj_bzero(ddi, sizeof(*ddi)); |
| 503 |
pj_ansi_strxcpy(ddi->info.name, "SDL openGL renderer", |
| 504 |
sizeof(ddi->info.name)); |
| 505 |
ddi->info.fmt_cnt = 1; |
| 506 |
#endif |
| 507 |
|
| 508 |
for (i = 0; i < sf->dev_count; i++) { |
| 509 |
ddi = &sf->dev_info[i]; |
| 510 |
pj_ansi_strxcpy(ddi->info.driver, "SDL", |
| 511 |
sizeof(ddi->info.driver)); |
| 512 |
ddi->info.dir = PJMEDIA_DIR_RENDER; |
| 513 |
ddi->info.has_callback = PJ_FALSE; |
| 514 |
ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT | |
| 515 |
PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE; |
| 516 |
ddi->info.caps |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
| 517 |
ddi->info.caps |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS; |
| 518 |
|
| 519 |
for (j = 0; j < ddi->info.fmt_cnt; j++) { |
| 520 |
pjmedia_format *fmt = &ddi->info.fmt[j]; |
| 521 |
pjmedia_format_init_video(fmt, sdl_fmts[j].fmt_id, |
| 522 |
DEFAULT_WIDTH, DEFAULT_HEIGHT, |
| 523 |
DEFAULT_FPS, 1); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
SDL_VERSION(&version); |
| 528 |
PJ_LOG(4, (THIS_FILE, "SDL %d.%d initialized", |
| 529 |
version.major, version.minor)); |
| 530 |
|
| 531 |
return PJ_SUCCESS; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
static pj_status_t sdl_factory_destroy(pjmedia_vid_dev_factory *f) |
| 536 |
{ |
| 537 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 538 |
pj_pool_t *pool = sf->pool; |
| 539 |
pj_status_t status; |
| 540 |
|
| 541 |
pj_assert(pj_list_empty(&sf->streams)); |
| 542 |
|
| 543 |
sf->is_quitting = PJ_TRUE; |
| 544 |
if (sf->sdl_thread) { |
| 545 |
pj_sem_post(sf->sem); |
| 546 |
#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false); |
| 552 |
#endif |
| 553 |
pj_thread_join(sf->sdl_thread); |
| 554 |
pj_thread_destroy(sf->sdl_thread); |
| 555 |
} |
| 556 |
|
| 557 |
if (sf->mutex) { |
| 558 |
pj_mutex_destroy(sf->mutex); |
| 559 |
sf->mutex = NULL; |
| 560 |
} |
| 561 |
|
| 562 |
if (sf->sem) { |
| 563 |
pj_sem_destroy(sf->sem); |
| 564 |
sf->sem = NULL; |
| 565 |
} |
| 566 |
|
| 567 |
job_queue_post_job(sf->jq, sdl_quit, NULL, 0, &status); |
| 568 |
job_queue_destroy(sf->jq); |
| 569 |
|
| 570 |
sf->pool = NULL; |
| 571 |
pj_pool_release(pool); |
| 572 |
|
| 573 |
return PJ_SUCCESS; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
static pj_status_t sdl_factory_refresh(pjmedia_vid_dev_factory *f) |
| 578 |
{ |
| 579 |
PJ_UNUSED_ARG(f); |
| 580 |
return PJ_SUCCESS; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
static unsigned sdl_factory_get_dev_count(pjmedia_vid_dev_factory *f) |
| 585 |
{ |
| 586 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 587 |
return sf->dev_count; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
static pj_status_t sdl_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 592 |
unsigned index, |
| 593 |
pjmedia_vid_dev_info *info) |
| 594 |
{ |
| 595 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 596 |
|
| 597 |
PJ_ASSERT_RETURN(index < sf->dev_count, PJMEDIA_EVID_INVDEV); |
| 598 |
|
| 599 |
pj_memcpy(info, &sf->dev_info[index].info, sizeof(*info)); |
| 600 |
|
| 601 |
return PJ_SUCCESS; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
static pj_status_t sdl_factory_default_param(pj_pool_t *pool, |
| 606 |
pjmedia_vid_dev_factory *f, |
| 607 |
unsigned index, |
| 608 |
pjmedia_vid_dev_param *param) |
| 609 |
{ |
| 610 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 611 |
struct sdl_dev_info *di = &sf->dev_info[index]; |
| 612 |
|
| 613 |
PJ_ASSERT_RETURN(index < sf->dev_count, PJMEDIA_EVID_INVDEV); |
| 614 |
|
| 615 |
PJ_UNUSED_ARG(pool); |
| 616 |
|
| 617 |
pj_bzero(param, sizeof(*param)); |
| 618 |
param->dir = PJMEDIA_DIR_RENDER; |
| 619 |
param->rend_id = index; |
| 620 |
param->cap_id = PJMEDIA_VID_INVALID_DEV; |
| 621 |
|
| 622 |
|
| 623 |
param->flags = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 624 |
param->fmt.type = PJMEDIA_TYPE_VIDEO; |
| 625 |
param->clock_rate = DEFAULT_CLOCK_RATE; |
| 626 |
pj_memcpy(¶m->fmt, &di->info.fmt[0], sizeof(param->fmt)); |
| 627 |
|
| 628 |
return PJ_SUCCESS; |
| 629 |
} |
| 630 |
|
| 631 |
static sdl_fmt_info* get_sdl_format_info(pjmedia_format_id id) |
| 632 |
{ |
| 633 |
unsigned i; |
| 634 |
|
| 635 |
for (i = 0; i < PJ_ARRAY_SIZE(sdl_fmts); i++) { |
| 636 |
if (sdl_fmts[i].fmt_id == id) |
| 637 |
return &sdl_fmts[i]; |
| 638 |
} |
| 639 |
|
| 640 |
return NULL; |
| 641 |
} |
| 642 |
|
| 643 |
static pj_status_t sdl_destroy(void *data) |
| 644 |
{ |
| 645 |
struct sdl_stream *strm = (struct sdl_stream *)data; |
| 646 |
|
| 647 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 648 |
if (strm->texture) { |
| 649 |
glDeleteTextures(1, &strm->texture); |
| 650 |
strm->texture = 0; |
| 651 |
} |
| 652 |
if (strm->gl_context) { |
| 653 |
SDL_GL_DeleteContext(strm->gl_context); |
| 654 |
strm->gl_context = NULL; |
| 655 |
} |
| 656 |
#endif |
| 657 |
if (strm->scr_tex) { |
| 658 |
SDL_DestroyTexture(strm->scr_tex); |
| 659 |
strm->scr_tex = NULL; |
| 660 |
} |
| 661 |
if (strm->renderer) { |
| 662 |
SDL_DestroyRenderer(strm->renderer); |
| 663 |
strm->renderer = NULL; |
| 664 |
} |
| 665 |
return PJ_SUCCESS; |
| 666 |
} |
| 667 |
|
| 668 |
static pj_status_t sdl_destroy_all(void *data) |
| 669 |
{ |
| 670 |
struct sdl_stream *strm = (struct sdl_stream *)data; |
| 671 |
|
| 672 |
sdl_destroy(data); |
| 673 |
#if !defined(TARGET_OS_IPHONE) || TARGET_OS_IPHONE == 0 |
| 674 |
if (strm->window && |
| 675 |
!(strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW)) |
| 676 |
{ |
| 677 |
SDL_DestroyWindow(strm->window); |
| 678 |
} |
| 679 |
strm->window = NULL; |
| 680 |
#endif |
| 681 |
return PJ_SUCCESS; |
| 682 |
} |
| 683 |
|
| 684 |
static pj_status_t sdl_create_window(struct sdl_stream *strm, |
| 685 |
pj_bool_t use_app_win, |
| 686 |
Uint32 sdl_format, |
| 687 |
pjmedia_vid_dev_hwnd *hwnd) |
| 688 |
{ |
| 689 |
if (!strm->window) { |
| 690 |
Uint32 flags = 0; |
| 691 |
|
| 692 |
if (strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS) { |
| 693 |
if (!(strm->param.window_flags & PJMEDIA_VID_DEV_WND_BORDER)) |
| 694 |
flags |= SDL_WINDOW_BORDERLESS; |
| 695 |
if (strm->param.window_flags & PJMEDIA_VID_DEV_WND_RESIZABLE) |
| 696 |
flags |= SDL_WINDOW_RESIZABLE; |
| 697 |
} else { |
| 698 |
flags |= SDL_WINDOW_BORDERLESS; |
| 699 |
} |
| 700 |
|
| 701 |
if (!((strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) && |
| 702 |
strm->param.window_hide)) |
| 703 |
{ |
| 704 |
flags |= SDL_WINDOW_SHOWN; |
| 705 |
} else { |
| 706 |
flags &= ~SDL_WINDOW_SHOWN; |
| 707 |
flags |= SDL_WINDOW_HIDDEN; |
| 708 |
} |
| 709 |
|
| 710 |
if ((strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN) && |
| 711 |
strm->param.window_fullscreen) |
| 712 |
{ |
| 713 |
if (strm->param.window_fullscreen == PJMEDIA_VID_DEV_FULLSCREEN) |
| 714 |
flags |= SDL_WINDOW_FULLSCREEN; |
| 715 |
else |
| 716 |
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 717 |
} |
| 718 |
|
| 719 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 720 |
if (strm->param.rend_id == OPENGL_DEV_IDX) |
| 721 |
flags |= SDL_WINDOW_OPENGL; |
| 722 |
#endif |
| 723 |
if (use_app_win) { |
| 724 |
|
| 725 |
strm->window = SDL_CreateWindowFrom(hwnd->info.window); |
| 726 |
if (!strm->window) { |
| 727 |
sdl_log_err("SDL_CreateWindowFrom()"); |
| 728 |
return PJMEDIA_EVID_SYSERR; |
| 729 |
} |
| 730 |
} else { |
| 731 |
int x, y; |
| 732 |
|
| 733 |
x = y = SDL_WINDOWPOS_CENTERED; |
| 734 |
if (strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) { |
| 735 |
x = strm->param.window_pos.x; |
| 736 |
y = strm->param.window_pos.y; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
strm->window = SDL_CreateWindow("pjmedia-SDL video", |
| 741 |
x, y, |
| 742 |
strm->param.disp_size.w, |
| 743 |
strm->param.disp_size.h, |
| 744 |
flags); |
| 745 |
if (!strm->window) { |
| 746 |
sdl_log_err("SDL_CreateWindow()"); |
| 747 |
return PJMEDIA_EVID_SYSERR; |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
strm->renderer = SDL_CreateRenderer(strm->window, -1, 0); |
| 757 |
if (!strm->renderer) { |
| 758 |
sdl_log_err("SDL_CreateRenderer()"); |
| 759 |
return PJMEDIA_EVID_SYSERR; |
| 760 |
} |
| 761 |
|
| 762 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 763 |
if (strm->param.rend_id == OPENGL_DEV_IDX) { |
| 764 |
strm->gl_context = SDL_GL_CreateContext(strm->window); |
| 765 |
if (!strm->gl_context) { |
| 766 |
sdl_log_err("SDL_GL_CreateContext()"); |
| 767 |
return PJMEDIA_EVID_SYSERR; |
| 768 |
} |
| 769 |
SDL_GL_MakeCurrent(strm->window, strm->gl_context); |
| 770 |
|
| 771 |
|
| 772 |
glDisable(GL_DEPTH_TEST); |
| 773 |
glDisable(GL_CULL_FACE); |
| 774 |
glEnable(GL_TEXTURE_2D); |
| 775 |
|
| 776 |
|
| 777 |
glViewport(0, 0, strm->param.disp_size.w, strm->param.disp_size.h); |
| 778 |
glMatrixMode(GL_PROJECTION); |
| 779 |
glLoadIdentity(); |
| 780 |
|
| 781 |
glOrtho(0.0, (GLdouble)strm->param.disp_size.w, |
| 782 |
(GLdouble)strm->param.disp_size.h, 0.0, 0.0, 1.0); |
| 783 |
|
| 784 |
glMatrixMode(GL_MODELVIEW); |
| 785 |
glLoadIdentity(); |
| 786 |
|
| 787 |
|
| 788 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 789 |
glGenTextures(1, &strm->texture); |
| 790 |
|
| 791 |
if (!strm->texture) |
| 792 |
return PJMEDIA_EVID_SYSERR; |
| 793 |
} else |
| 794 |
#endif |
| 795 |
{ |
| 796 |
strm->scr_tex = SDL_CreateTexture(strm->renderer, sdl_format, |
| 797 |
SDL_TEXTUREACCESS_STREAMING, |
| 798 |
strm->rect.w, strm->rect.h); |
| 799 |
if (strm->scr_tex == NULL) { |
| 800 |
sdl_log_err("SDL_CreateTexture()"); |
| 801 |
return PJMEDIA_EVID_SYSERR; |
| 802 |
} |
| 803 |
|
| 804 |
strm->pitch = strm->rect.w * SDL_BYTESPERPIXEL(sdl_format); |
| 805 |
} |
| 806 |
|
| 807 |
return PJ_SUCCESS; |
| 808 |
} |
| 809 |
|
| 810 |
static pj_status_t sdl_create_rend(struct sdl_stream * strm, |
| 811 |
pjmedia_format *fmt) |
| 812 |
{ |
| 813 |
sdl_fmt_info *sdl_info; |
| 814 |
const pjmedia_video_format_info *vfi; |
| 815 |
pjmedia_video_format_detail *vfd; |
| 816 |
|
| 817 |
sdl_info = get_sdl_format_info(fmt->id); |
| 818 |
vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(), |
| 819 |
fmt->id); |
| 820 |
if (!vfi || !sdl_info) |
| 821 |
return PJMEDIA_EVID_BADFORMAT; |
| 822 |
|
| 823 |
strm->vafp.size = fmt->det.vid.size; |
| 824 |
strm->vafp.buffer = NULL; |
| 825 |
if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS) |
| 826 |
return PJMEDIA_EVID_BADFORMAT; |
| 827 |
|
| 828 |
vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE); |
| 829 |
strm->rect.x = strm->rect.y = 0; |
| 830 |
strm->rect.w = (Uint16)vfd->size.w; |
| 831 |
strm->rect.h = (Uint16)vfd->size.h; |
| 832 |
if (strm->param.disp_size.w == 0) |
| 833 |
strm->param.disp_size.w = strm->rect.w; |
| 834 |
if (strm->param.disp_size.h == 0) |
| 835 |
strm->param.disp_size.h = strm->rect.h; |
| 836 |
strm->dstrect.x = strm->dstrect.y = 0; |
| 837 |
strm->dstrect.w = (Uint16)strm->param.disp_size.w; |
| 838 |
strm->dstrect.h = (Uint16)strm->param.disp_size.h; |
| 839 |
|
| 840 |
sdl_destroy(strm); |
| 841 |
|
| 842 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 843 |
if (strm->param.rend_id == OPENGL_DEV_IDX) { |
| 844 |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); |
| 845 |
} |
| 846 |
#endif |
| 847 |
return sdl_create_window(strm, |
| 848 |
(strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW), |
| 849 |
sdl_info->sdl_format, |
| 850 |
&strm->param.window); |
| 851 |
} |
| 852 |
|
| 853 |
static pj_status_t sdl_create(void *data) |
| 854 |
{ |
| 855 |
struct sdl_stream *strm = (struct sdl_stream *)data; |
| 856 |
return sdl_create_rend(strm, &strm->param.fmt); |
| 857 |
} |
| 858 |
|
| 859 |
static pj_status_t resize_disp(struct sdl_stream *strm, |
| 860 |
pjmedia_rect_size *new_disp_size) |
| 861 |
{ |
| 862 |
pj_memcpy(&strm->param.disp_size, new_disp_size, |
| 863 |
sizeof(strm->param.disp_size)); |
| 864 |
|
| 865 |
if (strm->scr_tex) { |
| 866 |
strm->dstrect.x = strm->dstrect.y = 0; |
| 867 |
strm->dstrect.w = (Uint16)strm->param.disp_size.w; |
| 868 |
strm->dstrect.h = (Uint16)strm->param.disp_size.h; |
| 869 |
SDL_RenderSetViewport(strm->renderer, &strm->dstrect); |
| 870 |
} |
| 871 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 872 |
else if (strm->param.rend_id == OPENGL_DEV_IDX) { |
| 873 |
sdl_create_rend(strm, &strm->param.fmt); |
| 874 |
} |
| 875 |
#endif |
| 876 |
|
| 877 |
return PJ_SUCCESS; |
| 878 |
} |
| 879 |
|
| 880 |
static pj_status_t change_format(struct sdl_stream *strm, |
| 881 |
pjmedia_format *new_fmt) |
| 882 |
{ |
| 883 |
pj_status_t status; |
| 884 |
|
| 885 |
|
| 886 |
status = sdl_create_rend(strm, (new_fmt? new_fmt : |
| 887 |
&strm->param.fmt)); |
| 888 |
if (status == PJ_SUCCESS && new_fmt) |
| 889 |
pjmedia_format_copy(&strm->param.fmt, new_fmt); |
| 890 |
|
| 891 |
return status; |
| 892 |
} |
| 893 |
|
| 894 |
static pj_status_t put_frame(void *data) |
| 895 |
{ |
| 896 |
struct sdl_stream *stream = (struct sdl_stream *)data; |
| 897 |
const pjmedia_frame *frame = stream->frame; |
| 898 |
|
| 899 |
if (stream->scr_tex) { |
| 900 |
SDL_UpdateTexture(stream->scr_tex, NULL, frame->buf, stream->pitch); |
| 901 |
SDL_RenderClear(stream->renderer); |
| 902 |
SDL_RenderCopy(stream->renderer, stream->scr_tex, |
| 903 |
&stream->rect, &stream->dstrect); |
| 904 |
SDL_RenderPresent(stream->renderer); |
| 905 |
} |
| 906 |
#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 907 |
else if (stream->param.rend_id == OPENGL_DEV_IDX && stream->texture) { |
| 908 |
glBindTexture(GL_TEXTURE_2D, stream->texture); |
| 909 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 910 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 911 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 912 |
stream->rect.w, stream->rect.h, 0, |
| 913 |
GL_RGBA, GL_UNSIGNED_BYTE, frame->buf); |
| 914 |
glBegin(GL_TRIANGLE_STRIP); |
| 915 |
glTexCoord2f(0, 0); glVertex2i(0, 0); |
| 916 |
glTexCoord2f(1, 0); glVertex2i(stream->param.disp_size.w, 0); |
| 917 |
glTexCoord2f(0, 1); glVertex2i(0, stream->param.disp_size.h); |
| 918 |
glTexCoord2f(1, 1); |
| 919 |
glVertex2i(stream->param.disp_size.w, stream->param.disp_size.h); |
| 920 |
glEnd(); |
| 921 |
SDL_GL_SwapWindow(stream->window); |
| 922 |
} |
| 923 |
#endif |
| 924 |
|
| 925 |
return PJ_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
static pj_status_t sdl_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 930 |
const pjmedia_frame *frame) |
| 931 |
{ |
| 932 |
struct sdl_stream *stream = (struct sdl_stream*)strm; |
| 933 |
pj_status_t status; |
| 934 |
|
| 935 |
stream->last_ts.u64 = frame->timestamp.u64; |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
if (frame->size==0 || frame->buf==NULL) |
| 941 |
return PJ_SUCCESS; |
| 942 |
|
| 943 |
if (frame->size < stream->vafp.framebytes) |
| 944 |
return PJ_ETOOSMALL; |
| 945 |
|
| 946 |
if (!stream->is_running) |
| 947 |
return PJ_EINVALIDOP; |
| 948 |
|
| 949 |
stream->frame = frame; |
| 950 |
job_queue_post_job(stream->sf->jq, put_frame, strm, 0, &status); |
| 951 |
|
| 952 |
return status; |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
static pj_status_t sdl_factory_create_stream( |
| 957 |
pjmedia_vid_dev_factory *f, |
| 958 |
pjmedia_vid_dev_param *param, |
| 959 |
const pjmedia_vid_dev_cb *cb, |
| 960 |
void *user_data, |
| 961 |
pjmedia_vid_dev_stream **p_vid_strm) |
| 962 |
{ |
| 963 |
struct sdl_factory *sf = (struct sdl_factory*)f; |
| 964 |
pj_pool_t *pool; |
| 965 |
struct sdl_stream *strm; |
| 966 |
pj_status_t status; |
| 967 |
|
| 968 |
PJ_ASSERT_RETURN(param->dir == PJMEDIA_DIR_RENDER, PJ_EINVAL); |
| 969 |
|
| 970 |
|
| 971 |
pool = pj_pool_create(sf->pf, "sdl-dev", 1000, 1000, NULL); |
| 972 |
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 973 |
|
| 974 |
strm = PJ_POOL_ZALLOC_T(pool, struct sdl_stream); |
| 975 |
pj_memcpy(&strm->param, param, sizeof(*param)); |
| 976 |
strm->pool = pool; |
| 977 |
strm->sf = sf; |
| 978 |
pj_memcpy(&strm->vid_cb, cb, sizeof(*cb)); |
| 979 |
pj_list_init(&strm->list_entry); |
| 980 |
strm->list_entry.stream = strm; |
| 981 |
strm->user_data = user_data; |
| 982 |
|
| 983 |
|
| 984 |
job_queue_post_job(sf->jq, sdl_create, strm, 0, &status); |
| 985 |
if (status != PJ_SUCCESS) { |
| 986 |
goto on_error; |
| 987 |
} |
| 988 |
pj_mutex_lock(strm->sf->mutex); |
| 989 |
if (pj_list_empty(&strm->sf->streams)) |
| 990 |
pj_sem_post(strm->sf->sem); |
| 991 |
pj_list_insert_after(&strm->sf->streams, &strm->list_entry); |
| 992 |
pj_mutex_unlock(strm->sf->mutex); |
| 993 |
|
| 994 |
|
| 995 |
strm->base.op = &stream_op; |
| 996 |
*p_vid_strm = &strm->base; |
| 997 |
|
| 998 |
return PJ_SUCCESS; |
| 999 |
|
| 1000 |
on_error: |
| 1001 |
sdl_stream_destroy(&strm->base); |
| 1002 |
return status; |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
static pj_status_t sdl_stream_get_param(pjmedia_vid_dev_stream *s, |
| 1007 |
pjmedia_vid_dev_param *pi) |
| 1008 |
{ |
| 1009 |
struct sdl_stream *strm = (struct sdl_stream*)s; |
| 1010 |
|
| 1011 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 1012 |
|
| 1013 |
pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 1014 |
|
| 1015 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 1016 |
&pi->window) == PJ_SUCCESS) |
| 1017 |
{ |
| 1018 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
| 1019 |
} |
| 1020 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION, |
| 1021 |
&pi->window_pos) == PJ_SUCCESS) |
| 1022 |
{ |
| 1023 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION; |
| 1024 |
} |
| 1025 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE, |
| 1026 |
&pi->disp_size) == PJ_SUCCESS) |
| 1027 |
{ |
| 1028 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE; |
| 1029 |
} |
| 1030 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE, |
| 1031 |
&pi->window_hide) == PJ_SUCCESS) |
| 1032 |
{ |
| 1033 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE; |
| 1034 |
} |
| 1035 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS, |
| 1036 |
&pi->window_flags) == PJ_SUCCESS) |
| 1037 |
{ |
| 1038 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS; |
| 1039 |
} |
| 1040 |
if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN, |
| 1041 |
&pi->window_fullscreen) == PJ_SUCCESS) |
| 1042 |
{ |
| 1043 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN; |
| 1044 |
} |
| 1045 |
|
| 1046 |
return PJ_SUCCESS; |
| 1047 |
} |
| 1048 |
|
| 1049 |
struct strm_cap { |
| 1050 |
struct sdl_stream *strm; |
| 1051 |
pjmedia_vid_dev_cap cap; |
| 1052 |
union { |
| 1053 |
void *pval; |
| 1054 |
const void *cpval; |
| 1055 |
} pval; |
| 1056 |
}; |
| 1057 |
|
| 1058 |
static pj_status_t get_cap(void *data) |
| 1059 |
{ |
| 1060 |
struct strm_cap *scap = (struct strm_cap *)data; |
| 1061 |
struct sdl_stream *strm = scap->strm; |
| 1062 |
pjmedia_vid_dev_cap cap = scap->cap; |
| 1063 |
void *pval = scap->pval.pval; |
| 1064 |
|
| 1065 |
if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) |
| 1066 |
{ |
| 1067 |
SDL_SysWMinfo info; |
| 1068 |
SDL_VERSION(&info.version); |
| 1069 |
|
| 1070 |
if (SDL_GetWindowWMInfo(strm->window, &info)) { |
| 1071 |
pjmedia_vid_dev_hwnd *wnd = (pjmedia_vid_dev_hwnd *)pval; |
| 1072 |
if (0) { } |
| 1073 |
#if defined(SDL_VIDEO_DRIVER_WINDOWS) |
| 1074 |
else if (info.subsystem == SDL_SYSWM_WINDOWS) { |
| 1075 |
wnd->type = PJMEDIA_VID_DEV_HWND_TYPE_WINDOWS; |
| 1076 |
wnd->info.win.hwnd = (void *)info.info.win.window; |
| 1077 |
} |
| 1078 |
#endif |
| 1079 |
#if defined(SDL_VIDEO_DRIVER_X11) |
| 1080 |
else if (info.subsystem == SDL_SYSWM_X11) { |
| 1081 |
wnd->info.x11.window = (void *)info.info.x11.window; |
| 1082 |
wnd->info.x11.display = (void *)info.info.x11.display; |
| 1083 |
} |
| 1084 |
#endif |
| 1085 |
#if defined(SDL_VIDEO_DRIVER_COCOA) |
| 1086 |
else if (info.subsystem == SDL_SYSWM_COCOA) { |
| 1087 |
wnd->info.cocoa.window = (void *)info.info.cocoa.window; |
| 1088 |
} |
| 1089 |
#endif |
| 1090 |
#if defined(SDL_VIDEO_DRIVER_UIKIT) |
| 1091 |
else if (info.subsystem == SDL_SYSWM_UIKIT) { |
| 1092 |
wnd->info.ios.window = (void *)info.info.uikit.window; |
| 1093 |
} |
| 1094 |
#endif |
| 1095 |
else { |
| 1096 |
return PJMEDIA_EVID_INVCAP; |
| 1097 |
} |
| 1098 |
return PJ_SUCCESS; |
| 1099 |
} else |
| 1100 |
return PJMEDIA_EVID_INVCAP; |
| 1101 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) { |
| 1102 |
SDL_GetWindowPosition(strm->window, &((pjmedia_coord *)pval)->x, |
| 1103 |
&((pjmedia_coord *)pval)->y); |
| 1104 |
return PJ_SUCCESS; |
| 1105 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE) { |
| 1106 |
SDL_GetWindowSize(strm->window, (int *)&((pjmedia_rect_size *)pval)->w, |
| 1107 |
(int *)&((pjmedia_rect_size *)pval)->h); |
| 1108 |
return PJ_SUCCESS; |
| 1109 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) { |
| 1110 |
Uint32 flag = SDL_GetWindowFlags(strm->window); |
| 1111 |
*((pj_bool_t *)pval) = (flag & SDL_WINDOW_HIDDEN)? PJ_TRUE: PJ_FALSE; |
| 1112 |
return PJ_SUCCESS; |
| 1113 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS) { |
| 1114 |
Uint32 flag = SDL_GetWindowFlags(strm->window); |
| 1115 |
unsigned *wnd_flags = (unsigned *)pval; |
| 1116 |
if (!(flag & SDL_WINDOW_BORDERLESS)) |
| 1117 |
*wnd_flags |= PJMEDIA_VID_DEV_WND_BORDER; |
| 1118 |
if (flag & SDL_WINDOW_RESIZABLE) |
| 1119 |
*wnd_flags |= PJMEDIA_VID_DEV_WND_RESIZABLE; |
| 1120 |
return PJ_SUCCESS; |
| 1121 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN) { |
| 1122 |
Uint32 flag = SDL_GetWindowFlags(strm->window); |
| 1123 |
pjmedia_vid_dev_fullscreen_flag val = PJMEDIA_VID_DEV_WINDOWED; |
| 1124 |
if ((flag & SDL_WINDOW_FULLSCREEN_DESKTOP) == |
| 1125 |
SDL_WINDOW_FULLSCREEN_DESKTOP) |
| 1126 |
{ |
| 1127 |
val = PJMEDIA_VID_DEV_FULLSCREEN_DESKTOP; |
| 1128 |
} else if ((flag & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN) { |
| 1129 |
val = PJMEDIA_VID_DEV_FULLSCREEN; |
| 1130 |
} |
| 1131 |
*((pjmedia_vid_dev_fullscreen_flag*)pval) = val; |
| 1132 |
return PJ_SUCCESS; |
| 1133 |
} |
| 1134 |
|
| 1135 |
return PJMEDIA_EVID_INVCAP; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
static pj_status_t sdl_stream_get_cap(pjmedia_vid_dev_stream *s, |
| 1140 |
pjmedia_vid_dev_cap cap, |
| 1141 |
void *pval) |
| 1142 |
{ |
| 1143 |
struct sdl_stream *strm = (struct sdl_stream*)s; |
| 1144 |
struct strm_cap scap; |
| 1145 |
pj_status_t status; |
| 1146 |
|
| 1147 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1148 |
|
| 1149 |
scap.strm = strm; |
| 1150 |
scap.cap = cap; |
| 1151 |
scap.pval.pval = pval; |
| 1152 |
|
| 1153 |
job_queue_post_job(strm->sf->jq, get_cap, &scap, 0, &status); |
| 1154 |
|
| 1155 |
return status; |
| 1156 |
} |
| 1157 |
|
| 1158 |
static pj_status_t set_cap(void *data) |
| 1159 |
{ |
| 1160 |
struct strm_cap *scap = (struct strm_cap *)data; |
| 1161 |
struct sdl_stream *strm = scap->strm; |
| 1162 |
pjmedia_vid_dev_cap cap = scap->cap; |
| 1163 |
const void *pval = scap->pval.cpval; |
| 1164 |
|
| 1165 |
PJ_ASSERT_RETURN(data && strm, PJ_EINVAL); |
| 1166 |
|
| 1167 |
if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) { |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
Uint32 flag = SDL_GetWindowFlags(strm->window); |
| 1176 |
if (flag & SDL_WINDOW_HIDDEN) |
| 1177 |
SDL_ShowWindow(strm->window); |
| 1178 |
SDL_SetWindowPosition(strm->window, ((pjmedia_coord *)pval)->x, |
| 1179 |
((pjmedia_coord *)pval)->y); |
| 1180 |
if (flag & SDL_WINDOW_HIDDEN) |
| 1181 |
SDL_HideWindow(strm->window); |
| 1182 |
return PJ_SUCCESS; |
| 1183 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) { |
| 1184 |
if (*(pj_bool_t *)pval) |
| 1185 |
SDL_HideWindow(strm->window); |
| 1186 |
else |
| 1187 |
SDL_ShowWindow(strm->window); |
| 1188 |
return PJ_SUCCESS; |
| 1189 |
} else if (cap == PJMEDIA_VID_DEV_CAP_FORMAT) { |
| 1190 |
pj_status_t status; |
| 1191 |
|
| 1192 |
status = change_format(strm, (pjmedia_format *)pval); |
| 1193 |
if (status != PJ_SUCCESS) { |
| 1194 |
pj_status_t status_; |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
status_ = change_format(strm, &strm->param.fmt); |
| 1201 |
if (status_ != PJ_SUCCESS) { |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
status = PJMEDIA_EVID_ERR; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
return status; |
| 1211 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE) { |
| 1212 |
pjmedia_rect_size *new_size = (pjmedia_rect_size *)pval; |
| 1213 |
Uint32 flag = SDL_GetWindowFlags(strm->window); |
| 1214 |
pj_status_t status; |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
if (flag & SDL_WINDOW_FULLSCREEN_DESKTOP) |
| 1221 |
SDL_SetWindowFullscreen(strm->window, 0); |
| 1222 |
|
| 1223 |
SDL_SetWindowSize(strm->window, new_size->w, new_size->h); |
| 1224 |
status = resize_disp(strm, new_size); |
| 1225 |
|
| 1226 |
|
| 1227 |
if (flag & SDL_WINDOW_FULLSCREEN_DESKTOP) |
| 1228 |
SDL_SetWindowFullscreen(strm->window, SDL_WINDOW_FULLSCREEN_DESKTOP); |
| 1229 |
|
| 1230 |
return status; |
| 1231 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 1232 |
pjmedia_vid_dev_hwnd *hwnd = (pjmedia_vid_dev_hwnd*)pval; |
| 1233 |
pj_status_t status = PJ_SUCCESS; |
| 1234 |
sdl_fmt_info *sdl_info = get_sdl_format_info(strm->param.fmt.id); |
| 1235 |
|
| 1236 |
status = sdl_destroy_all(strm); |
| 1237 |
if (status != PJ_SUCCESS) |
| 1238 |
return status; |
| 1239 |
|
| 1240 |
status = sdl_create_window(strm, PJ_TRUE, sdl_info->sdl_format, hwnd); |
| 1241 |
PJ_PERROR(4, (THIS_FILE, status, |
| 1242 |
"Re-initializing SDL with native window %p", |
| 1243 |
hwnd->info.window)); |
| 1244 |
return status; |
| 1245 |
} else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN) { |
| 1246 |
Uint32 flag; |
| 1247 |
pjmedia_vid_dev_fullscreen_flag val = |
| 1248 |
*(pjmedia_vid_dev_fullscreen_flag*)pval; |
| 1249 |
|
| 1250 |
flag = SDL_GetWindowFlags(strm->window); |
| 1251 |
if (val == PJMEDIA_VID_DEV_FULLSCREEN_DESKTOP) |
| 1252 |
flag |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 1253 |
else if (val == PJMEDIA_VID_DEV_FULLSCREEN) |
| 1254 |
flag |= SDL_WINDOW_FULLSCREEN; |
| 1255 |
else |
| 1256 |
flag &= (~SDL_WINDOW_FULLSCREEN_DESKTOP); |
| 1257 |
|
| 1258 |
SDL_SetWindowFullscreen(strm->window, flag); |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
if ((flag & SDL_WINDOW_FULLSCREEN)==0 && |
| 1264 |
(flag & SDL_WINDOW_BORDERLESS)==0) |
| 1265 |
{ |
| 1266 |
SDL_SetWindowBordered(strm->window, SDL_FALSE); |
| 1267 |
SDL_SetWindowBordered(strm->window, SDL_TRUE); |
| 1268 |
} |
| 1269 |
|
| 1270 |
return PJ_SUCCESS; |
| 1271 |
} |
| 1272 |
|
| 1273 |
return PJMEDIA_EVID_INVCAP; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
static pj_status_t sdl_stream_set_cap(pjmedia_vid_dev_stream *s, |
| 1278 |
pjmedia_vid_dev_cap cap, |
| 1279 |
const void *pval) |
| 1280 |
{ |
| 1281 |
struct sdl_stream *strm = (struct sdl_stream*)s; |
| 1282 |
struct strm_cap scap; |
| 1283 |
pj_status_t status; |
| 1284 |
|
| 1285 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1286 |
|
| 1287 |
scap.strm = strm; |
| 1288 |
scap.cap = cap; |
| 1289 |
scap.pval.cpval = pval; |
| 1290 |
|
| 1291 |
job_queue_post_job(strm->sf->jq, set_cap, &scap, 0, &status); |
| 1292 |
|
| 1293 |
return status; |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
static pj_status_t sdl_stream_start(pjmedia_vid_dev_stream *strm) |
| 1298 |
{ |
| 1299 |
struct sdl_stream *stream = (struct sdl_stream*)strm; |
| 1300 |
|
| 1301 |
PJ_LOG(4, (THIS_FILE, "Starting sdl video stream")); |
| 1302 |
|
| 1303 |
stream->is_running = PJ_TRUE; |
| 1304 |
|
| 1305 |
return PJ_SUCCESS; |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
static pj_status_t sdl_stream_stop(pjmedia_vid_dev_stream *strm) |
| 1311 |
{ |
| 1312 |
struct sdl_stream *stream = (struct sdl_stream*)strm; |
| 1313 |
|
| 1314 |
PJ_LOG(4, (THIS_FILE, "Stopping sdl video stream")); |
| 1315 |
|
| 1316 |
stream->is_running = PJ_FALSE; |
| 1317 |
|
| 1318 |
return PJ_SUCCESS; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
static pj_status_t sdl_stream_destroy(pjmedia_vid_dev_stream *strm) |
| 1324 |
{ |
| 1325 |
struct sdl_stream *stream = (struct sdl_stream*)strm; |
| 1326 |
pj_status_t status; |
| 1327 |
|
| 1328 |
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 1329 |
|
| 1330 |
sdl_stream_stop(strm); |
| 1331 |
|
| 1332 |
job_queue_post_job(stream->sf->jq, sdl_destroy_all, strm, 0, &status); |
| 1333 |
if (status != PJ_SUCCESS) |
| 1334 |
return status; |
| 1335 |
|
| 1336 |
pj_mutex_lock(stream->sf->mutex); |
| 1337 |
if (!pj_list_empty(&stream->list_entry)) |
| 1338 |
pj_list_erase(&stream->list_entry); |
| 1339 |
pj_mutex_unlock(stream->sf->mutex); |
| 1340 |
|
| 1341 |
pj_pool_release(stream->pool); |
| 1342 |
|
| 1343 |
return PJ_SUCCESS; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
#if PJ_DARWINOS==0 |
| 1350 |
static int job_thread(void * data) |
| 1351 |
{ |
| 1352 |
job_queue *jq = (job_queue *)data; |
| 1353 |
|
| 1354 |
while (1) { |
| 1355 |
job *jb; |
| 1356 |
|
| 1357 |
|
| 1358 |
pj_sem_wait(jq->sem); |
| 1359 |
|
| 1360 |
|
| 1361 |
if (jq->is_quitting && jq->head == jq->tail && !jq->is_full) |
| 1362 |
break; |
| 1363 |
|
| 1364 |
jb = jq->jobs[jq->head]; |
| 1365 |
jb->retval = (*jb->func)(jb->data); |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
if (jq->is_full && ((jq->head + 1) % jq->size == jq->tail)) { |
| 1370 |
unsigned i, head; |
| 1371 |
pj_status_t status; |
| 1372 |
|
| 1373 |
if (jq->old_sem) { |
| 1374 |
for (i = 0; i < jq->size / JOB_QUEUE_INC_FACTOR; i++) { |
| 1375 |
pj_sem_destroy(jq->old_sem[i]); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
jq->old_sem = jq->job_sem; |
| 1379 |
|
| 1380 |
|
| 1381 |
jq->size *= JOB_QUEUE_INC_FACTOR; |
| 1382 |
pj_sem_destroy(jq->sem); |
| 1383 |
status = pj_sem_create(jq->pool, "thread_sem", 0, jq->size + 1, |
| 1384 |
&jq->sem); |
| 1385 |
if (status != PJ_SUCCESS) { |
| 1386 |
PJ_PERROR(3, (THIS_FILE, status, |
| 1387 |
"Failed growing SDL job queue size.")); |
| 1388 |
return 0; |
| 1389 |
} |
| 1390 |
jq->jobs = (job **)pj_pool_calloc(jq->pool, jq->size, |
| 1391 |
sizeof(job *)); |
| 1392 |
jq->job_sem = (pj_sem_t **) pj_pool_calloc(jq->pool, jq->size, |
| 1393 |
sizeof(pj_sem_t *)); |
| 1394 |
for (i = 0; i < jq->size; i++) { |
| 1395 |
status = pj_sem_create(jq->pool, "job_sem", 0, 1, |
| 1396 |
&jq->job_sem[i]); |
| 1397 |
if (status != PJ_SUCCESS) { |
| 1398 |
PJ_PERROR(3, (THIS_FILE, status, |
| 1399 |
"Failed growing SDL job queue size.")); |
| 1400 |
return 0; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
jq->is_full = PJ_FALSE; |
| 1404 |
head = jq->head; |
| 1405 |
jq->head = jq->tail = 0; |
| 1406 |
pj_sem_post(jq->old_sem[head]); |
| 1407 |
} else { |
| 1408 |
pj_sem_post(jq->job_sem[jq->head]); |
| 1409 |
jq->head = (jq->head + 1) % jq->size; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
return 0; |
| 1414 |
} |
| 1415 |
#endif |
| 1416 |
|
| 1417 |
static pj_status_t job_queue_create(pj_pool_t *pool, job_queue **pjq) |
| 1418 |
{ |
| 1419 |
unsigned i; |
| 1420 |
pj_status_t status; |
| 1421 |
|
| 1422 |
job_queue *jq = PJ_POOL_ZALLOC_T(pool, job_queue); |
| 1423 |
jq->pool = pool; |
| 1424 |
jq->size = INITIAL_MAX_JOBS; |
| 1425 |
status = pj_sem_create(pool, "thread_sem", 0, jq->size + 1, &jq->sem); |
| 1426 |
if (status != PJ_SUCCESS) |
| 1427 |
goto on_error; |
| 1428 |
jq->jobs = (job **)pj_pool_calloc(pool, jq->size, sizeof(job *)); |
| 1429 |
jq->job_sem = (pj_sem_t **) pj_pool_calloc(pool, jq->size, |
| 1430 |
sizeof(pj_sem_t *)); |
| 1431 |
for (i = 0; i < jq->size; i++) { |
| 1432 |
status = pj_sem_create(pool, "job_sem", 0, 1, &jq->job_sem[i]); |
| 1433 |
if (status != PJ_SUCCESS) |
| 1434 |
goto on_error; |
| 1435 |
} |
| 1436 |
|
| 1437 |
status = pj_mutex_create_recursive(pool, "job_mutex", &jq->mutex); |
| 1438 |
if (status != PJ_SUCCESS) |
| 1439 |
goto on_error; |
| 1440 |
|
| 1441 |
#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 1442 |
PJ_UNUSED_ARG(status); |
| 1443 |
#else |
| 1444 |
status = pj_thread_create(pool, "job_th", job_thread, jq, 0, 0, |
| 1445 |
&jq->thread); |
| 1446 |
if (status != PJ_SUCCESS) |
| 1447 |
goto on_error; |
| 1448 |
#endif |
| 1449 |
|
| 1450 |
*pjq = jq; |
| 1451 |
return PJ_SUCCESS; |
| 1452 |
|
| 1453 |
on_error: |
| 1454 |
job_queue_destroy(jq); |
| 1455 |
return status; |
| 1456 |
} |
| 1457 |
|
| 1458 |
static pj_status_t job_queue_post_job(job_queue *jq, job_func_ptr func, |
| 1459 |
void *data, unsigned flags, |
| 1460 |
pj_status_t *retval) |
| 1461 |
{ |
| 1462 |
job jb; |
| 1463 |
int tail; |
| 1464 |
|
| 1465 |
if (jq->is_quitting) { |
| 1466 |
jb.retval = PJ_EBUSY; |
| 1467 |
goto on_return; |
| 1468 |
} |
| 1469 |
|
| 1470 |
jb.func = func; |
| 1471 |
jb.data = data; |
| 1472 |
jb.flags = flags; |
| 1473 |
jb.retval = PJ_SUCCESS; |
| 1474 |
|
| 1475 |
#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 1476 |
PJ_UNUSED_ARG(tail); |
| 1477 |
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc]init]; |
| 1478 |
JQDelegate *jqd = [[JQDelegate alloc]init]; |
| 1479 |
jqd->pjob = &jb; |
| 1480 |
[jqd performSelectorOnMainThread:@selector(run_job) |
| 1481 |
withObject:nil waitUntilDone:YES]; |
| 1482 |
[jqd release]; |
| 1483 |
[apool release]; |
| 1484 |
#else |
| 1485 |
pj_mutex_lock(jq->mutex); |
| 1486 |
jq->jobs[jq->tail] = &jb; |
| 1487 |
tail = jq->tail; |
| 1488 |
jq->tail = (jq->tail + 1) % jq->size; |
| 1489 |
if (jq->tail == jq->head) { |
| 1490 |
jq->is_full = PJ_TRUE; |
| 1491 |
PJ_LOG(4, (THIS_FILE, "SDL job queue is full, increasing " |
| 1492 |
"the queue size.")); |
| 1493 |
pj_mutex_unlock(jq->mutex); |
| 1494 |
pj_sem_post(jq->sem); |
| 1495 |
|
| 1496 |
pj_sem_wait(jq->job_sem[tail]); |
| 1497 |
} else { |
| 1498 |
pj_mutex_unlock(jq->mutex); |
| 1499 |
pj_sem_post(jq->sem); |
| 1500 |
|
| 1501 |
pj_sem_wait(jq->job_sem[tail]); |
| 1502 |
} |
| 1503 |
#endif |
| 1504 |
|
| 1505 |
on_return: |
| 1506 |
if (retval) |
| 1507 |
*retval = jb.retval; |
| 1508 |
|
| 1509 |
return jb.retval; |
| 1510 |
} |
| 1511 |
|
| 1512 |
static pj_status_t job_queue_destroy(job_queue *jq) |
| 1513 |
{ |
| 1514 |
unsigned i; |
| 1515 |
|
| 1516 |
jq->is_quitting = PJ_TRUE; |
| 1517 |
|
| 1518 |
if (jq->thread) { |
| 1519 |
pj_sem_post(jq->sem); |
| 1520 |
pj_thread_join(jq->thread); |
| 1521 |
pj_thread_destroy(jq->thread); |
| 1522 |
} |
| 1523 |
|
| 1524 |
if (jq->sem) { |
| 1525 |
pj_sem_destroy(jq->sem); |
| 1526 |
jq->sem = NULL; |
| 1527 |
} |
| 1528 |
for (i = 0; i < jq->size; i++) { |
| 1529 |
if (jq->job_sem[i]) { |
| 1530 |
pj_sem_destroy(jq->job_sem[i]); |
| 1531 |
jq->job_sem[i] = NULL; |
| 1532 |
} |
| 1533 |
} |
| 1534 |
if (jq->old_sem) { |
| 1535 |
for (i = 0; i < jq->size / JOB_QUEUE_INC_FACTOR; i++) { |
| 1536 |
if (jq->old_sem[i]) { |
| 1537 |
pj_sem_destroy(jq->old_sem[i]); |
| 1538 |
jq->old_sem[i] = NULL; |
| 1539 |
} |
| 1540 |
} |
| 1541 |
} |
| 1542 |
if (jq->mutex) { |
| 1543 |
pj_mutex_destroy(jq->mutex); |
| 1544 |
jq->mutex = NULL; |
| 1545 |
} |
| 1546 |
|
| 1547 |
return PJ_SUCCESS; |
| 1548 |
} |
| 1549 |
|
| 1550 |
#ifdef _MSC_VER |
| 1551 |
# if defined(PJMEDIA_SDL_LIB) |
| 1552 |
# pragma comment( lib, PJMEDIA_SDL_LIB) |
| 1553 |
# elif SDL_VERSION_ATLEAST(2,0,0) |
| 1554 |
# pragma comment( lib, "sdl2.lib") |
| 1555 |
# elif SDL_VERSION_ATLEAST(1,3,0) |
| 1556 |
# pragma comment( lib, "sdl.lib") |
| 1557 |
# endif |
| 1558 |
# if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL |
| 1559 |
# pragma comment(lib, "OpenGL32.lib") |
| 1560 |
# endif |
| 1561 |
#endif |
| 1562 |
|
| 1563 |
|
| 1564 |
#endif |