blob: 00e1b5c2abb6f153dad837240bc05770022f2f92 [file] [log] [blame]
Benny Prijonoc45d9512010-12-10 11:04:30 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2010 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjmedia/converter.h>
20#include <pjmedia-videodev/videodev_imp.h>
21#include <pj/assert.h>
22#include <pj/log.h>
23#include <pj/os.h>
24
25#if PJMEDIA_VIDEO_DEV_HAS_SDL
26
Sauw Ming6e6c2152010-12-14 13:03:10 +000027#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
Sauw Ming21bd3fd2011-04-06 11:30:18 +000028# include <Foundation/Foundation.h>
Sauw Ming6e6c2152010-12-14 13:03:10 +000029#endif
30
Benny Prijonoc45d9512010-12-10 11:04:30 +000031#include <SDL.h>
Sauw Ming21bd3fd2011-04-06 11:30:18 +000032#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
33# include "SDL_opengl.h"
34# define OPENGL_DEV_IDX 1
35#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +000036
37#define THIS_FILE "sdl_dev.c"
38#define DEFAULT_CLOCK_RATE 90000
39#define DEFAULT_WIDTH 640
40#define DEFAULT_HEIGHT 480
41#define DEFAULT_FPS 25
42
43
44typedef struct sdl_fmt_info
45{
46 pjmedia_format_id fmt_id;
47 Uint32 sdl_format;
48 Uint32 Rmask;
49 Uint32 Gmask;
50 Uint32 Bmask;
51 Uint32 Amask;
52} sdl_fmt_info;
53
54static sdl_fmt_info sdl_fmts[] =
55{
56#if PJ_IS_BIG_ENDIAN
57 {PJMEDIA_FORMAT_RGBA, 0, 0xFF000000, 0xFF0000, 0xFF00, 0xFF} ,
58 {PJMEDIA_FORMAT_RGB24, 0, 0xFF0000, 0xFF00, 0xFF, 0} ,
59 {PJMEDIA_FORMAT_BGRA, 0, 0xFF00, 0xFF0000, 0xFF000000, 0xFF} ,
60#else
61 {PJMEDIA_FORMAT_RGBA, 0, 0xFF, 0xFF00, 0xFF0000, 0xFF000000} ,
62 {PJMEDIA_FORMAT_RGB24, 0, 0xFF, 0xFF00, 0xFF0000, 0} ,
63 {PJMEDIA_FORMAT_BGRA, 0, 0xFF0000, 0xFF00, 0xFF, 0xFF000000} ,
64#endif
65
66 {PJMEDIA_FORMAT_DIB , 0, 0xFF0000, 0xFF00, 0xFF, 0} ,
67
68 {PJMEDIA_FORMAT_YUY2, SDL_YUY2_OVERLAY, 0, 0, 0, 0} ,
69 {PJMEDIA_FORMAT_UYVY, SDL_UYVY_OVERLAY, 0, 0, 0, 0} ,
70 {PJMEDIA_FORMAT_YVYU, SDL_YVYU_OVERLAY, 0, 0, 0, 0} ,
71 {PJMEDIA_FORMAT_I420, SDL_IYUV_OVERLAY, 0, 0, 0, 0} ,
72 {PJMEDIA_FORMAT_YV12, SDL_YV12_OVERLAY, 0, 0, 0, 0} ,
73 {PJMEDIA_FORMAT_I420JPEG, SDL_IYUV_OVERLAY, 0, 0, 0, 0} ,
74 {PJMEDIA_FORMAT_I422JPEG, SDL_YV12_OVERLAY, 0, 0, 0, 0} ,
75};
76
77/* sdl_ device info */
78struct sdl_dev_info
79{
80 pjmedia_vid_dev_info info;
81};
82
83/* sdl_ factory */
84struct sdl_factory
85{
86 pjmedia_vid_dev_factory base;
87 pj_pool_t *pool;
88 pj_pool_factory *pf;
89
90 unsigned dev_count;
91 struct sdl_dev_info *dev_info;
92};
93
Sauw Ming21bd3fd2011-04-06 11:30:18 +000094#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
95@interface SDLDelegate: NSObject
96{
97 @public
98 struct sdl_stream *strm;
99}
100
101- (int)sdl_thread;
102- (int)handle_event;
103- (pj_status_t)put_frame;
104@end
105#endif
106
Benny Prijonoc45d9512010-12-10 11:04:30 +0000107/* Video stream. */
108struct sdl_stream
109{
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000110 pjmedia_vid_dev_stream base; /**< Base stream */
111 pjmedia_vid_param param; /**< Settings */
112 pj_pool_t *pool; /**< Memory pool. */
Benny Prijonoc45d9512010-12-10 11:04:30 +0000113
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000114 pjmedia_vid_cb vid_cb; /**< Stream callback. */
115 void *user_data; /**< Application data. */
Benny Prijonoc45d9512010-12-10 11:04:30 +0000116
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000117 pj_thread_t *sdl_thread; /**< SDL thread. */
118 pj_bool_t is_quitting;
119 pj_bool_t is_running;
120 pj_bool_t render_exited;
121 pj_status_t status;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000122
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000123 SDL_Rect rect; /**< Display rectangle. */
124 SDL_Surface *screen; /**< Display screen. */
125 SDL_Surface *surf; /**< RGB surface. */
126 SDL_Overlay *overlay; /**< YUV overlay. */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000127#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
128 GLuint texture;
Sauw Ming02548292011-04-11 21:38:53 +0000129 void *tex_buf;
130 pj_size_t tex_buf_size;
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000131#endif
132#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
133 NSAutoreleasePool *apool;
134 SDLDelegate *delegate;
135 const pjmedia_frame *frame;
136#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +0000137
138 /* For frame conversion */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000139 pjmedia_converter *conv;
140 pjmedia_conversion_param conv_param;
141 pjmedia_frame conv_buf;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000142
143 pjmedia_video_apply_fmt_param vafp;
144};
145
146
147/* Prototypes */
148static pj_status_t sdl_factory_init(pjmedia_vid_dev_factory *f);
149static pj_status_t sdl_factory_destroy(pjmedia_vid_dev_factory *f);
150static unsigned sdl_factory_get_dev_count(pjmedia_vid_dev_factory *f);
151static pj_status_t sdl_factory_get_dev_info(pjmedia_vid_dev_factory *f,
152 unsigned index,
153 pjmedia_vid_dev_info *info);
154static pj_status_t sdl_factory_default_param(pj_pool_t *pool,
155 pjmedia_vid_dev_factory *f,
156 unsigned index,
157 pjmedia_vid_param *param);
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000158static pj_status_t sdl_factory_create_stream(
159 pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000160 pjmedia_vid_param *param,
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000161 const pjmedia_vid_cb *cb,
162 void *user_data,
163 pjmedia_vid_dev_stream **p_vid_strm);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000164
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000165static pj_status_t sdl_stream_get_param(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000166 pjmedia_vid_param *param);
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000167static pj_status_t sdl_stream_get_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000168 pjmedia_vid_dev_cap cap,
169 void *value);
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000170static pj_status_t sdl_stream_set_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000171 pjmedia_vid_dev_cap cap,
172 const void *value);
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000173static pj_status_t sdl_stream_put_frame(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000174 const pjmedia_frame *frame);
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000175static pj_status_t sdl_stream_start(pjmedia_vid_dev_stream *strm);
176static pj_status_t sdl_stream_stop(pjmedia_vid_dev_stream *strm);
177static pj_status_t sdl_stream_destroy(pjmedia_vid_dev_stream *strm);
Benny Prijonoda1d3f72011-04-13 14:21:10 +0000178
179#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
Sauw Ming02548292011-04-11 21:38:53 +0000180static void draw_gl(struct sdl_stream *stream, void *tex_buf);
Benny Prijonoda1d3f72011-04-13 14:21:10 +0000181#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +0000182
183/* Operations */
184static pjmedia_vid_dev_factory_op factory_op =
185{
186 &sdl_factory_init,
187 &sdl_factory_destroy,
188 &sdl_factory_get_dev_count,
189 &sdl_factory_get_dev_info,
190 &sdl_factory_default_param,
191 &sdl_factory_create_stream
192};
193
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000194static pjmedia_vid_dev_stream_op stream_op =
Benny Prijonoc45d9512010-12-10 11:04:30 +0000195{
196 &sdl_stream_get_param,
197 &sdl_stream_get_cap,
198 &sdl_stream_set_cap,
199 &sdl_stream_start,
200 NULL,
201 &sdl_stream_put_frame,
202 &sdl_stream_stop,
203 &sdl_stream_destroy
204};
205
206
207/****************************************************************************
208 * Factory operations
209 */
210/*
211 * Init sdl_ video driver.
212 */
213pjmedia_vid_dev_factory* pjmedia_sdl_factory(pj_pool_factory *pf)
214{
215 struct sdl_factory *f;
216 pj_pool_t *pool;
217
218 pool = pj_pool_create(pf, "sdl video", 1000, 1000, NULL);
219 f = PJ_POOL_ZALLOC_T(pool, struct sdl_factory);
220 f->pf = pf;
221 f->pool = pool;
222 f->base.op = &factory_op;
223
224 return &f->base;
225}
226
227
228/* API: init factory */
229static pj_status_t sdl_factory_init(pjmedia_vid_dev_factory *f)
230{
231 struct sdl_factory *sf = (struct sdl_factory*)f;
232 struct sdl_dev_info *ddi;
233 unsigned i;
234
235 sf->dev_count = 1;
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000236#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
237 sf->dev_count++;
238#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +0000239 sf->dev_info = (struct sdl_dev_info*)
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000240 pj_pool_calloc(sf->pool, sf->dev_count,
241 sizeof(struct sdl_dev_info));
Benny Prijonoc45d9512010-12-10 11:04:30 +0000242
243 ddi = &sf->dev_info[0];
244 pj_bzero(ddi, sizeof(*ddi));
245 strncpy(ddi->info.name, "SDL renderer", sizeof(ddi->info.name));
246 ddi->info.name[sizeof(ddi->info.name)-1] = '\0';
247 strncpy(ddi->info.driver, "SDL", sizeof(ddi->info.driver));
248 ddi->info.driver[sizeof(ddi->info.driver)-1] = '\0';
249 ddi->info.dir = PJMEDIA_DIR_RENDER;
250 ddi->info.has_callback = PJ_FALSE;
251 ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT |
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000252 PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000253
254 ddi->info.fmt_cnt = PJ_ARRAY_SIZE(sdl_fmts);
255 ddi->info.fmt = (pjmedia_format*)
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000256 pj_pool_calloc(sf->pool, ddi->info.fmt_cnt,
257 sizeof(pjmedia_format));
Benny Prijonoc45d9512010-12-10 11:04:30 +0000258 for (i = 0; i < ddi->info.fmt_cnt; i++) {
259 pjmedia_format *fmt = &ddi->info.fmt[i];
260 pjmedia_format_init_video(fmt, sdl_fmts[i].fmt_id,
261 DEFAULT_WIDTH, DEFAULT_HEIGHT,
262 DEFAULT_FPS, 1);
263 }
264
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000265#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
266 ddi = &sf->dev_info[OPENGL_DEV_IDX];
267 pj_bzero(ddi, sizeof(*ddi));
268 strncpy(ddi->info.name, "SDL openGL renderer", sizeof(ddi->info.name));
269 ddi->info.name[sizeof(ddi->info.name)-1] = '\0';
270 strncpy(ddi->info.driver, "SDL", sizeof(ddi->info.driver));
271 ddi->info.driver[sizeof(ddi->info.driver)-1] = '\0';
272 ddi->info.dir = PJMEDIA_DIR_RENDER;
273 ddi->info.has_callback = PJ_FALSE;
274 ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
275
276 ddi->info.fmt_cnt = PJ_ARRAY_SIZE(sdl_fmts);
277 ddi->info.fmt_cnt = 1;
278 ddi->info.fmt = (pjmedia_format*)
279 pj_pool_calloc(sf->pool, ddi->info.fmt_cnt,
280 sizeof(pjmedia_format));
281 for (i = 0; i < ddi->info.fmt_cnt; i++) {
282 pjmedia_format *fmt = &ddi->info.fmt[i];
283 pjmedia_format_init_video(fmt, sdl_fmts[i].fmt_id,
284 DEFAULT_WIDTH, DEFAULT_HEIGHT,
285 DEFAULT_FPS, 1);
286 }
287#endif
288
Benny Prijonoc45d9512010-12-10 11:04:30 +0000289 PJ_LOG(4, (THIS_FILE, "SDL initialized"));
290
291 return PJ_SUCCESS;
292}
293
294/* API: destroy factory */
295static pj_status_t sdl_factory_destroy(pjmedia_vid_dev_factory *f)
296{
297 struct sdl_factory *sf = (struct sdl_factory*)f;
298 pj_pool_t *pool = sf->pool;
299
300 sf->pool = NULL;
301 pj_pool_release(pool);
302
303 return PJ_SUCCESS;
304}
305
306/* API: get number of devices */
307static unsigned sdl_factory_get_dev_count(pjmedia_vid_dev_factory *f)
308{
309 struct sdl_factory *sf = (struct sdl_factory*)f;
310 return sf->dev_count;
311}
312
313/* API: get device info */
314static pj_status_t sdl_factory_get_dev_info(pjmedia_vid_dev_factory *f,
315 unsigned index,
316 pjmedia_vid_dev_info *info)
317{
318 struct sdl_factory *sf = (struct sdl_factory*)f;
319
320 PJ_ASSERT_RETURN(index < sf->dev_count, PJMEDIA_EVID_INVDEV);
321
322 pj_memcpy(info, &sf->dev_info[index].info, sizeof(*info));
323
324 return PJ_SUCCESS;
325}
326
327/* API: create default device parameter */
328static pj_status_t sdl_factory_default_param(pj_pool_t *pool,
329 pjmedia_vid_dev_factory *f,
330 unsigned index,
331 pjmedia_vid_param *param)
332{
333 struct sdl_factory *sf = (struct sdl_factory*)f;
334 struct sdl_dev_info *di = &sf->dev_info[index];
335
336 PJ_ASSERT_RETURN(index < sf->dev_count, PJMEDIA_EVID_INVDEV);
337
338 PJ_UNUSED_ARG(pool);
339
340 pj_bzero(param, sizeof(*param));
Nanang Izzuddin98610702011-03-01 17:40:17 +0000341 if (di->info.dir == PJMEDIA_DIR_CAPTURE_RENDER) {
Benny Prijonoc45d9512010-12-10 11:04:30 +0000342 param->dir = PJMEDIA_DIR_CAPTURE_RENDER;
343 param->cap_id = index;
344 param->rend_id = index;
345 } else if (di->info.dir & PJMEDIA_DIR_CAPTURE) {
346 param->dir = PJMEDIA_DIR_CAPTURE;
347 param->cap_id = index;
348 param->rend_id = PJMEDIA_VID_INVALID_DEV;
349 } else if (di->info.dir & PJMEDIA_DIR_RENDER) {
350 param->dir = PJMEDIA_DIR_RENDER;
351 param->rend_id = index;
352 param->cap_id = PJMEDIA_VID_INVALID_DEV;
353 } else {
354 return PJMEDIA_EVID_INVDEV;
355 }
356
357 /* Set the device capabilities here */
358 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
359 param->fmt.type = PJMEDIA_TYPE_VIDEO;
360 param->clock_rate = DEFAULT_CLOCK_RATE;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000361 pjmedia_format_init_video(&param->fmt, sdl_fmts[0].fmt_id,
362 DEFAULT_WIDTH, DEFAULT_HEIGHT,
363 DEFAULT_FPS, 1);
364
365 return PJ_SUCCESS;
366}
367
368static sdl_fmt_info* get_sdl_format_info(pjmedia_format_id id)
369{
370 unsigned i;
371
372 for (i = 0; i < sizeof(sdl_fmts)/sizeof(sdl_fmts[0]); i++) {
373 if (sdl_fmts[i].fmt_id == id)
374 return &sdl_fmts[i];
375 }
376
377 return NULL;
378}
379
Sauw Ming093c5692011-04-13 20:06:12 +0000380static void destroy_sdl(struct sdl_stream *strm)
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000381{
Sauw Ming093c5692011-04-13 20:06:12 +0000382 if (strm->surf) {
383 SDL_FreeSurface(strm->surf);
384 strm->surf = NULL;
385 }
386 if (strm->overlay) {
387 SDL_FreeYUVOverlay(strm->overlay);
388 strm->overlay = NULL;
389 }
390#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
391 if (strm->texture) {
392 glDeleteTextures(1, &strm->texture);
393 strm->texture = 0;
394 }
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000395#endif
Sauw Ming093c5692011-04-13 20:06:12 +0000396}
397
398static pj_status_t init_sdl(struct sdl_stream *strm, pjmedia_format *fmt)
399{
400 sdl_fmt_info *sdl_info = get_sdl_format_info(fmt->id);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000401 const pjmedia_video_format_info *vfi;
402 pjmedia_video_format_detail *vfd;
403
404 vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(),
Sauw Ming093c5692011-04-13 20:06:12 +0000405 fmt->id);
406 if (!vfi || !sdl_info)
407 return PJMEDIA_EVID_BADFORMAT;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000408
Sauw Ming093c5692011-04-13 20:06:12 +0000409 strm->vafp.size = fmt->det.vid.size;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000410 strm->vafp.buffer = NULL;
Sauw Ming093c5692011-04-13 20:06:12 +0000411 if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS)
412 return PJMEDIA_EVID_BADFORMAT;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000413
Sauw Ming093c5692011-04-13 20:06:12 +0000414 vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000415 strm->rect.x = strm->rect.y = 0;
416 strm->rect.w = (Uint16)vfd->size.w;
417 strm->rect.h = (Uint16)vfd->size.h;
418
Sauw Ming093c5692011-04-13 20:06:12 +0000419 /* Initialize the display */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000420 strm->screen = SDL_SetVideoMode(strm->rect.w, strm->rect.h, 0, (
421#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
422 strm->param.rend_id == OPENGL_DEV_IDX?
423 SDL_OPENGL:
424#endif
425 SDL_RESIZABLE | SDL_SWSURFACE));
Sauw Ming093c5692011-04-13 20:06:12 +0000426 if (strm->screen == NULL)
427 return PJMEDIA_EVID_SYSERR;
428
Benny Prijonoc45d9512010-12-10 11:04:30 +0000429 SDL_WM_SetCaption("pjmedia-SDL video", NULL);
430
Sauw Ming093c5692011-04-13 20:06:12 +0000431 destroy_sdl(strm);
432
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000433#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
434 if (strm->param.rend_id == OPENGL_DEV_IDX) {
Sauw Ming093c5692011-04-13 20:06:12 +0000435 /* Init some OpenGL settings */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000436 glDisable(GL_DEPTH_TEST);
437 glDisable(GL_CULL_FACE);
438 glEnable(GL_TEXTURE_2D);
439
Sauw Ming093c5692011-04-13 20:06:12 +0000440 /* Init the viewport */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000441 glViewport(0, 0, strm->rect.w, strm->rect.h);
442 glMatrixMode(GL_PROJECTION);
443 glLoadIdentity();
444
445 glOrtho(0.0, (GLdouble)strm->rect.w, (GLdouble)strm->rect.h,
446 0.0, 0.0, 1.0);
447
448 glMatrixMode(GL_MODELVIEW);
449 glLoadIdentity();
450
Sauw Ming093c5692011-04-13 20:06:12 +0000451 /* Create a texture */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000452 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
453 glGenTextures(1, &strm->texture);
Sauw Ming02548292011-04-11 21:38:53 +0000454
455#if defined(PJ_WIN32) && PJ_WIN32 != 0
Sauw Ming093c5692011-04-13 20:06:12 +0000456 /**
457 * On Win32 platform, the OpenGL drawing must be in the same
458 * thread that calls SDL_SetVideoMode(), hence we need a buffer
459 * for the frame from sdl_stream_put_frame()
460 */
Sauw Ming02548292011-04-11 21:38:53 +0000461 if (strm->vafp.framebytes > strm->tex_buf_size) {
462 strm->tex_buf_size = strm->vafp.framebytes;
463 strm->tex_buf = pj_pool_alloc(strm->pool, strm->vafp.framebytes);
464 }
465#endif
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000466 } else
467#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +0000468 if (vfi->color_model == PJMEDIA_COLOR_MODEL_RGB) {
469 strm->surf = SDL_CreateRGBSurface(SDL_SWSURFACE,
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000470 strm->rect.w, strm->rect.h,
471 vfi->bpp,
472 sdl_info->Rmask,
473 sdl_info->Gmask,
474 sdl_info->Bmask,
475 sdl_info->Amask);
Sauw Ming093c5692011-04-13 20:06:12 +0000476 if (strm->surf == NULL)
477 return PJMEDIA_EVID_SYSERR;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000478 } else if (vfi->color_model == PJMEDIA_COLOR_MODEL_YUV) {
479 strm->overlay = SDL_CreateYUVOverlay(strm->rect.w, strm->rect.h,
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000480 sdl_info->sdl_format,
481 strm->screen);
Sauw Ming093c5692011-04-13 20:06:12 +0000482 if (strm->overlay == NULL)
483 return PJMEDIA_EVID_SYSERR;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000484 }
485
Sauw Ming093c5692011-04-13 20:06:12 +0000486 return PJ_SUCCESS;
487}
488
489#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
490@implementation SDLDelegate
491- (int)sdl_thread
492{
493#else
494static int sdlthread(void * data)
495{
496 struct sdl_stream *strm = (struct sdl_stream*)data;
497#endif
498
499 /* Initialize the SDL library */
500 if (SDL_Init(SDL_INIT_VIDEO)) {
501 PJ_LOG(4, (THIS_FILE, "Cannot initialize SDL"));
502 strm->status = PJMEDIA_EVID_INIT;
503 goto on_return;
504 }
505
506#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
507 if (strm->param.rend_id == OPENGL_DEV_IDX) {
508 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
509 }
510#endif
511
512 strm->status = init_sdl(strm, &strm->param.fmt);
513 if (strm->status != PJ_SUCCESS)
514 goto on_return;
515
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000516#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
517on_return:
518 if (strm->status != PJ_SUCCESS) {
Sauw Ming093c5692011-04-13 20:06:12 +0000519 destory_sdl(strm);
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000520 SDL_Quit();
521 strm->screen = NULL;
522 }
523
524 return strm->status;
525}
526
527- (int)handle_event
528{
529 sdl_fmt_info *sdl_info = get_sdl_format_info(strm->param.fmt.id);
530 const pjmedia_video_format_info *vfi;
531 pjmedia_video_format_detail *vfd;
532
533 vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(),
534 strm->param.fmt.id);
535 vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE);
536#else
537 while(!strm->is_quitting)
538#endif
539 {
Benny Prijonoc45d9512010-12-10 11:04:30 +0000540 SDL_Event sevent;
541 pjmedia_vid_event pevent;
542
Sauw Ming02548292011-04-11 21:38:53 +0000543#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
544#if defined(PJ_WIN32) && PJ_WIN32 != 0
545 if (strm->param.rend_id == OPENGL_DEV_IDX) {
546 draw_gl(strm, strm->tex_buf);
547 }
548#endif
549#endif
Sauw Ming4a20bc82011-03-01 15:55:34 +0000550 /**
551 * The event polling must be placed in the same thread that
552 * call SDL_SetVideoMode(). Please consult the official doc of
553 * SDL_PumpEvents().
554 */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000555 while (SDL_PollEvent(&sevent)) {
Benny Prijonoc45d9512010-12-10 11:04:30 +0000556 pj_bzero(&pevent, sizeof(pevent));
557
558 switch(sevent.type) {
559 case SDL_USEREVENT:
Sauw Ming4a20bc82011-03-01 15:55:34 +0000560 {
561 pjmedia_format *fmt;
562
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000563 if (sevent.user.code == PJMEDIA_EVENT_NONE) {
564 strm->is_quitting = PJ_TRUE;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000565 goto on_return;
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000566 }
Sauw Ming4a20bc82011-03-01 15:55:34 +0000567
568 pj_assert(sevent.user.code == PJMEDIA_VID_DEV_CAP_FORMAT);
569
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000570 fmt = (pjmedia_format *)sevent.user.data1;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000571
Sauw Ming093c5692011-04-13 20:06:12 +0000572 /* Stop the stream */
Sauw Ming4a20bc82011-03-01 15:55:34 +0000573 sdl_stream_stop((pjmedia_vid_dev_stream *)strm);
574
Sauw Ming093c5692011-04-13 20:06:12 +0000575 /* Re-initialize SDL */
576 strm->status = init_sdl(strm, fmt);
Sauw Ming4a20bc82011-03-01 15:55:34 +0000577
Sauw Ming093c5692011-04-13 20:06:12 +0000578 if (strm->status == PJ_SUCCESS) {
579 pjmedia_format_copy(&strm->param.fmt, fmt);
580 /* Restart the stream */
581 sdl_stream_start((pjmedia_vid_dev_stream *)strm);
582 }
Sauw Ming4a20bc82011-03-01 15:55:34 +0000583
Sauw Ming4a20bc82011-03-01 15:55:34 +0000584 break;
585 }
586
Benny Prijonoc45d9512010-12-10 11:04:30 +0000587 case SDL_MOUSEBUTTONDOWN:
588 pevent.event_type = PJMEDIA_EVENT_MOUSEBUTTONDOWN;
589 if (strm->vid_cb.on_event_cb)
590 if ((*strm->vid_cb.on_event_cb)(&strm->base,
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000591 strm->user_data,
592 &pevent) != PJ_SUCCESS)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000593 {
594 /* Application wants us to ignore this event */
595 break;
596 }
597 if (strm->is_running)
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000598 pjmedia_vid_dev_stream_stop(&strm->base);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000599 else
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000600 pjmedia_vid_dev_stream_start(&strm->base);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000601 break;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000602
Benny Prijonoc45d9512010-12-10 11:04:30 +0000603 case SDL_VIDEORESIZE:
604 pevent.event_type = PJMEDIA_EVENT_WINDOW_RESIZE;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000605 pevent.event_desc.resize.new_size.w = sevent.resize.w;
606 pevent.event_desc.resize.new_size.h = sevent.resize.h;
607 if (strm->vid_cb.on_event_cb) {
608 /**
609 * To process PJMEDIA_EVENT_WINDOW_RESIZE event,
610 * application should do this in the on_event_cb
611 * callback:
612 * 1. change the input frame size given to SDL
613 * to the new size.
614 * 2. call pjmedia_vid_dev_stream_set_cap()
615 * using PJMEDIA_VID_DEV_CAP_FORMAT capability
616 * and the new format size
617 */
618 (*strm->vid_cb.on_event_cb)(&strm->base,
619 strm->user_data,
620 &pevent);
621 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000622 break;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000623
Benny Prijonoc45d9512010-12-10 11:04:30 +0000624 case SDL_QUIT:
625 pevent.event_type = PJMEDIA_EVENT_WINDOW_CLOSE;
626 /**
627 * To process PJMEDIA_EVENT_WINDOW_CLOSE event,
628 * application should do this in the on_event_cb callback:
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000629 * 1. stop further calls to
630 * #pjmedia_vid_dev_stream_put_frame()
Benny Prijonoc45d9512010-12-10 11:04:30 +0000631 * 2. return PJ_SUCCESS
632 * Upon returning from the callback, SDL will destroy its
633 * own stream.
634 *
635 * Returning non-PJ_SUCCESS will cause SDL to ignore
636 * the event
637 */
638 if (strm->vid_cb.on_event_cb) {
639 strm->is_quitting = PJ_TRUE;
640 if ((*strm->vid_cb.on_event_cb)(&strm->base,
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000641 strm->user_data,
642 &pevent) != PJ_SUCCESS)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000643 {
644 /* Application wants us to ignore this event */
645 strm->is_quitting = PJ_FALSE;
646 break;
647 }
648
649 /* Destroy the stream */
650 sdl_stream_destroy(&strm->base);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000651 goto on_return;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000652 }
653
654 /**
655 * Default event-handler when there is no user-specified
656 * callback: close the renderer window. We cannot destroy
657 * the stream here since there is no callback to notify
658 * the application.
659 */
660 sdl_stream_stop(&strm->base);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000661 goto on_return;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000662
Benny Prijonoc45d9512010-12-10 11:04:30 +0000663 default:
664 break;
665 }
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000666 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000667 }
668
Sauw Ming6e6c2152010-12-14 13:03:10 +0000669#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000670 return 0;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000671#endif
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000672on_return:
Sauw Ming093c5692011-04-13 20:06:12 +0000673 destroy_sdl(strm);
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000674 SDL_Quit();
675 strm->screen = NULL;
676
Sauw Ming6e6c2152010-12-14 13:03:10 +0000677 return strm->status;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000678}
679
Benny Prijonoda1d3f72011-04-13 14:21:10 +0000680#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
Sauw Ming02548292011-04-11 21:38:53 +0000681static void draw_gl(struct sdl_stream *stream, void *tex_buf)
682{
683 if (stream->texture) {
684 glBindTexture(GL_TEXTURE_2D, stream->texture);
685 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
686 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
687 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
688 stream->rect.w, stream->rect.h, 0,
689 GL_RGBA, GL_UNSIGNED_BYTE, tex_buf);
690 glBegin(GL_TRIANGLE_STRIP);
691 glTexCoord2f(0, 0); glVertex2i(0, 0);
692 glTexCoord2f(1, 0); glVertex2i(stream->rect.w, 0);
693 glTexCoord2f(0, 1); glVertex2i(0, stream->rect.h);
694 glTexCoord2f(1, 1); glVertex2i(stream->rect.w, stream->rect.h);
695 glEnd();
696 SDL_GL_SwapBuffers();
697 }
698}
Benny Prijonoda1d3f72011-04-13 14:21:10 +0000699#endif
Sauw Ming02548292011-04-11 21:38:53 +0000700
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000701#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
702- (pj_status_t)put_frame
703{
704 const pjmedia_frame *frame = strm->frame;
705#else
706/* API: Put frame from stream */
707static pj_status_t sdl_stream_put_frame(pjmedia_vid_dev_stream *strm,
708 const pjmedia_frame *frame)
709{
710#endif
711 struct sdl_stream *stream = (struct sdl_stream*)strm;
712 pj_status_t status = PJ_SUCCESS;
713
714 if (!stream->is_running) {
715 stream->render_exited = PJ_TRUE;
716 goto on_return;
717 }
718
Sauw Mingf7b429c2011-04-08 04:04:25 +0000719 if (frame->size==0 || frame->buf==NULL ||
720 frame->size < stream->vafp.framebytes)
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000721 goto on_return;
722
723 if (stream->surf) {
724 if (SDL_MUSTLOCK(stream->surf)) {
725 if (SDL_LockSurface(stream->surf) < 0) {
726 PJ_LOG(3, (THIS_FILE, "Unable to lock SDL surface"));
727 status = PJMEDIA_EVID_NOTREADY;
728 goto on_return;
729 }
730 }
731
Sauw Mingf7b429c2011-04-08 04:04:25 +0000732 pj_memcpy(stream->surf->pixels, frame->buf,
733 stream->vafp.framebytes);
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000734
735 if (SDL_MUSTLOCK(stream->surf)) {
736 SDL_UnlockSurface(stream->surf);
737 }
738 SDL_BlitSurface(stream->surf, NULL, stream->screen, NULL);
739 SDL_UpdateRect(stream->screen, 0, 0, 0, 0);
740 } else if (stream->overlay) {
741 int i, sz, offset;
742
743 if (SDL_LockYUVOverlay(stream->overlay) < 0) {
744 PJ_LOG(3, (THIS_FILE, "Unable to lock SDL overlay"));
745 status = PJMEDIA_EVID_NOTREADY;
746 goto on_return;
747 }
748
749 for (i = 0, offset = 0; i < stream->overlay->planes; i++) {
750 sz = stream->vafp.plane_bytes[i];
751 pj_memcpy(stream->overlay->pixels[i],
752 (char *)frame->buf + offset, sz);
753 offset += sz;
754 }
755
756 SDL_UnlockYUVOverlay(stream->overlay);
757 SDL_DisplayYUVOverlay(stream->overlay, &stream->rect);
758 }
759#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
Sauw Ming02548292011-04-11 21:38:53 +0000760 else if (stream->param.rend_id == OPENGL_DEV_IDX) {
761#if defined(PJ_WIN32) && PJ_WIN32 != 0
Sauw Ming093c5692011-04-13 20:06:12 +0000762 pj_memcpy(stream->tex_buf, frame->buf, stream->vafp.framebytes);
Sauw Ming02548292011-04-11 21:38:53 +0000763#else
764 draw_gl(stream, frame->buf);
765#endif
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000766 }
767#endif
768
769on_return:
770 return status;
771}
772#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
773@end
774
775static pj_status_t sdl_stream_put_frame(pjmedia_vid_dev_stream *strm,
776 const pjmedia_frame *frame)
777{
778 struct sdl_stream *stream = (struct sdl_stream*)strm;
779 stream->frame = frame;
780 [stream->delegate performSelectorOnMainThread:@selector(put_frame)
781 withObject:nil waitUntilDone:YES];
782
783 return PJ_SUCCESS;
784}
785
786static int sdlthread(void * data)
787{
788 struct sdl_stream *strm = (struct sdl_stream*)data;
789
790 while(!strm->is_quitting) {
791 [strm->delegate performSelectorOnMainThread:@selector(handle_event)
792 withObject:nil waitUntilDone:YES];
793 }
794
795 return 0;
796}
797
798#endif
799
Benny Prijonoc45d9512010-12-10 11:04:30 +0000800/* API: create stream */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000801static pj_status_t sdl_factory_create_stream(
802 pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000803 pjmedia_vid_param *param,
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000804 const pjmedia_vid_cb *cb,
805 void *user_data,
806 pjmedia_vid_dev_stream **p_vid_strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000807{
808 struct sdl_factory *sf = (struct sdl_factory*)f;
809 pj_pool_t *pool;
810 struct sdl_stream *strm;
811 pj_status_t status;
812
813 /* Create and Initialize stream descriptor */
814 pool = pj_pool_create(sf->pf, "sdl-dev", 1000, 1000, NULL);
815 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
816
817 strm = PJ_POOL_ZALLOC_T(pool, struct sdl_stream);
818 pj_memcpy(&strm->param, param, sizeof(*param));
819 strm->pool = pool;
820 pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
821 strm->user_data = user_data;
822
823 /* Create capture stream here */
824 if (param->dir & PJMEDIA_DIR_CAPTURE) {
825 }
826
827 /* Create render stream here */
828 if (param->dir & PJMEDIA_DIR_RENDER) {
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000829 strm->status = PJ_SUCCESS;
830#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
831 pj_assert(![NSThread isMainThread]);
832 strm->apool = [[NSAutoreleasePool alloc] init];
833 strm->delegate = [[SDLDelegate alloc]init];
834 strm->delegate->strm = strm;
Sauw Ming093c5692011-04-13 20:06:12 +0000835 /* On Mac OS X, we need to call SDL functions in the main thread */
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000836 [strm->delegate performSelectorOnMainThread:@selector(sdl_thread)
837 withObject:nil waitUntilDone:YES];
838 if ((status = strm->status) != PJ_SUCCESS) {
839 goto on_error;
840 }
841#endif
842 status = pj_thread_create(pool, "sdl_thread", sdlthread,
843 strm, 0, 0, &strm->sdl_thread);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000844
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000845 if (status != PJ_SUCCESS) {
846 goto on_error;
847 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000848
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000849 while(strm->status == PJ_SUCCESS && !strm->surf && !strm->overlay
850#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
851 && !strm->texture
852#endif
853 )
854 {
855 pj_thread_sleep(10);
856 }
857 if ((status = strm->status) != PJ_SUCCESS) {
858 goto on_error;
859 }
860
861 pjmedia_format_copy(&strm->conv_param.src, &param->fmt);
862 pjmedia_format_copy(&strm->conv_param.dst, &param->fmt);
863 /*
864 status = pjmedia_converter_create(NULL, pool, &strm->conv_param,
865 &strm->conv);
866 */
Benny Prijonoc45d9512010-12-10 11:04:30 +0000867 }
868
869 /* Apply the remaining settings */
870 if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) {
871 sdl_stream_set_cap(&strm->base,
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000872 PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW,
873 &param->window);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000874 }
875
876 /* Done */
877 strm->base.op = &stream_op;
878 *p_vid_strm = &strm->base;
879
880 return PJ_SUCCESS;
881
882on_error:
883 sdl_stream_destroy(&strm->base);
884 return status;
885}
886
887/* API: Get stream info. */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000888static pj_status_t sdl_stream_get_param(pjmedia_vid_dev_stream *s,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000889 pjmedia_vid_param *pi)
890{
891 struct sdl_stream *strm = (struct sdl_stream*)s;
892
893 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
894
895 pj_memcpy(pi, &strm->param, sizeof(*pi));
896
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000897 /*
898 if (sdl_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW,
899 &pi->fmt.info_size) == PJ_SUCCESS)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000900 {
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000901 pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000902 }
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000903 */
Benny Prijonoc45d9512010-12-10 11:04:30 +0000904 return PJ_SUCCESS;
905}
906
907/* API: get capability */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000908static pj_status_t sdl_stream_get_cap(pjmedia_vid_dev_stream *s,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000909 pjmedia_vid_dev_cap cap,
910 void *pval)
911{
912 struct sdl_stream *strm = (struct sdl_stream*)s;
913
914 PJ_UNUSED_ARG(strm);
915
916 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
917
Sauw Ming4a20bc82011-03-01 15:55:34 +0000918 if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000919 {
920 return PJ_SUCCESS;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000921 } else if (cap == PJMEDIA_VID_DEV_CAP_FORMAT) {
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000922 return PJ_SUCCESS;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000923 } else {
924 return PJMEDIA_EVID_INVCAP;
925 }
926}
927
928/* API: set capability */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000929static pj_status_t sdl_stream_set_cap(pjmedia_vid_dev_stream *s,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000930 pjmedia_vid_dev_cap cap,
931 const void *pval)
932{
933 struct sdl_stream *strm = (struct sdl_stream*)s;
934
935 PJ_UNUSED_ARG(strm);
936
937 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
938
Sauw Ming4a20bc82011-03-01 15:55:34 +0000939 if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) {
Benny Prijonoc45d9512010-12-10 11:04:30 +0000940 return PJ_SUCCESS;
Sauw Ming4a20bc82011-03-01 15:55:34 +0000941 } else if (cap == PJMEDIA_VID_DEV_CAP_FORMAT) {
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000942 SDL_Event sevent;
943
944 strm->status = PJ_TRUE;
945 sevent.type = SDL_USEREVENT;
946 sevent.user.code = PJMEDIA_VID_DEV_CAP_FORMAT;
947 sevent.user.data1 = (void *)pval;
948 SDL_PushEvent(&sevent);
949
950 while (strm->status == PJ_TRUE)
951 pj_thread_sleep(10);
952
953 if (strm->status != PJ_SUCCESS) {
954 pj_status_t status = strm->status;
955
956 /**
957 * Failed to change the output format. Try to revert
958 * to its original format.
959 */
960 strm->status = PJ_TRUE;
961 sevent.user.data1 = &strm->param.fmt;
962 SDL_PushEvent(&sevent);
963
964 while (strm->status == PJ_TRUE)
965 pj_thread_sleep(10);
966
967 if (strm->status != PJ_SUCCESS) {
968 /**
969 * This means that we failed to revert to our
970 * original state!
971 */
972 status = PJMEDIA_EVID_ERR;
973 }
974
975 strm->status = status;
Sauw Ming21bd3fd2011-04-06 11:30:18 +0000976 }
977
978 return strm->status;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000979 }
980
981 return PJMEDIA_EVID_INVCAP;
982}
983
Benny Prijonoc45d9512010-12-10 11:04:30 +0000984/* API: Start stream. */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000985static pj_status_t sdl_stream_start(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000986{
987 struct sdl_stream *stream = (struct sdl_stream*)strm;
988
989 PJ_LOG(4, (THIS_FILE, "Starting sdl video stream"));
990
991 stream->is_running = PJ_TRUE;
992 stream->render_exited = PJ_FALSE;
993
994 return PJ_SUCCESS;
995}
996
997/* API: Stop stream. */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +0000998static pj_status_t sdl_stream_stop(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000999{
1000 struct sdl_stream *stream = (struct sdl_stream*)strm;
1001 unsigned i;
1002
1003 PJ_LOG(4, (THIS_FILE, "Stopping sdl video stream"));
1004
1005 /* Wait for renderer put_frame() to finish */
1006 stream->is_running = PJ_FALSE;
1007 for (i=0; !stream->render_exited && i<100; ++i)
1008 pj_thread_sleep(10);
1009
1010 return PJ_SUCCESS;
1011}
1012
1013
1014/* API: Destroy stream. */
Nanang Izzuddina9c1cf42011-02-24 07:47:55 +00001015static pj_status_t sdl_stream_destroy(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +00001016{
1017 struct sdl_stream *stream = (struct sdl_stream*)strm;
1018 SDL_Event sevent;
1019
1020 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
1021
Benny Prijonoc45d9512010-12-10 11:04:30 +00001022 sdl_stream_stop(strm);
1023
Sauw Ming21bd3fd2011-04-06 11:30:18 +00001024 if (!stream->is_quitting) {
1025 sevent.type = SDL_USEREVENT;
1026 sevent.user.code = PJMEDIA_EVENT_NONE;
1027 SDL_PushEvent(&sevent);
1028 if (stream->sdl_thread)
1029 pj_thread_join(stream->sdl_thread);
Benny Prijonoc45d9512010-12-10 11:04:30 +00001030 }
1031
Sauw Ming21bd3fd2011-04-06 11:30:18 +00001032#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
1033 if (stream->delegate)
1034 [stream->delegate release];
1035 if (stream->apool)
1036 [stream->apool release];
1037#endif
Benny Prijonoc45d9512010-12-10 11:04:30 +00001038 pj_pool_release(stream->pool);
Sauw Ming21bd3fd2011-04-06 11:30:18 +00001039
Benny Prijonoc45d9512010-12-10 11:04:30 +00001040
1041 return PJ_SUCCESS;
1042}
1043
Nanang Izzuddine43ee722010-12-10 20:55:13 +00001044#ifdef _MSC_VER
1045# pragma comment( lib, "sdl.lib")
Sauw Ming21bd3fd2011-04-06 11:30:18 +00001046# if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
1047# pragma comment(lib, "OpenGL32.lib")
1048# endif
Nanang Izzuddine43ee722010-12-10 20:55:13 +00001049#endif
1050
Benny Prijonoc45d9512010-12-10 11:04:30 +00001051#endif /* PJMEDIA_VIDEO_DEV_HAS_SDL */