glxvideo.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* indent-tabs-mode: t
 * vi: set noexpandtab:
 * :noTabs=false:
 */
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006	Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msvideo.h"
#include "layouts.h"

#include <X11/Xlib.h>

#include "opengles_display.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glx.h>

static void glxvideo_unprepare(MSFilter *f);

static bool_t createX11GLWindow(Display* display, MSVideoSize size, GLXContext* ctx, Window* win);

typedef struct GLXVideo
{
	MSVideoSize vsize;
	MSVideoSize wsize; /*wished window size */
	Display *display;
	Window window_id;
Simon Morlat's avatar
Simon Morlat committed
49
	Window subwindow;
50 51
	GLXContext glContext;
	struct opengles_display *glhelper;
Simon Morlat's avatar
Simon Morlat committed
52
	int corner;
53 54 55
	bool_t show;
	bool_t own_window;
	bool_t ready;
56 57
	bool_t mirror;
	bool_t autofit;
58 59 60
} GLXVideo;


61 62 63 64 65 66 67
static bool_t x11_error = FALSE;

static int x11error_handler(Display *d, XErrorEvent*ev) {
	ms_error("X11 error reported.");
	x11_error = TRUE;
	return 0;
}
68

69
static Display *init_display(void){
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	const char *display;
	Display *ret;
	display=getenv("DISPLAY");
	if (display==NULL) display=":0";
	ret=XOpenDisplay(display);
	if (ret==NULL){
		ms_error("Could not open display %s",display);
	}
	return ret;
}

static void glxvideo_init(MSFilter	*f){
	GLXVideo *obj=(GLXVideo*)ms_new0(GLXVideo,1);
	MSVideoSize def_size;
	def_size.width=MS_VIDEO_SIZE_CIF_W;
	def_size.height=MS_VIDEO_SIZE_CIF_H;
	obj->display=init_display();
Simon Morlat's avatar
Simon Morlat committed
87
	obj->own_window=TRUE;
88 89 90 91
	obj->ready=FALSE;
	obj->vsize=def_size; /* the size of the main video*/
	obj->wsize=def_size; /* the size of the window*/
	obj->show=TRUE;
92
	obj->autofit=TRUE;
93
	f->data=obj;
94 95

	XSetErrorHandler(x11error_handler);
96 97 98 99 100 101 102
}


static void glxvideo_uninit(MSFilter *f){
	GLXVideo *obj=(GLXVideo*)f->data;

	glxvideo_unprepare(f);
Simon Morlat's avatar
Simon Morlat committed
103 104 105 106 107
	
	if (obj->glContext) {
		glXDestroyContext(obj->display, obj->glContext);
		obj->glContext = NULL;
	}
108
	if (obj->own_window){
Simon Morlat's avatar
Simon Morlat committed
109 110 111 112 113
		if (obj->window_id)
			XDestroyWindow(obj->display,obj->window_id);
	}else{
		if (obj->subwindow)
			XDestroyWindow(obj->display,obj->subwindow);
114 115 116 117 118 119 120 121 122 123
	}
	if (obj->display){
		XCloseDisplay(obj->display);
		obj->display=NULL;
	}
	ms_free(obj);
}

static void glxvideo_prepare(MSFilter *f){
	GLXVideo *s=(GLXVideo*)f->data;
Simon Morlat's avatar
Simon Morlat committed
124
	Window window=s->window_id;
125 126 127
	XWindowAttributes wa;

	if (s->display==NULL) return;
Simon Morlat's avatar
Simon Morlat committed
128 129
	if (s->window_id==(unsigned long)-1) return;

130 131 132
	/* Make sure X11 window is ready to use*/
	XSync(s->display, False);

Simon Morlat's avatar
Simon Morlat committed
133 134 135
	if (createX11GLWindow(s->display, s->wsize, &s->glContext, &window)) {
		GLenum err;
		s->subwindow=window;
136 137 138
		if (s->own_window){
			s->window_id = window;
		}
Simon Morlat's avatar
Simon Morlat committed
139 140 141 142 143 144 145 146 147 148 149
		s->glhelper = ogl_display_new();
		glXMakeCurrent( s->display, s->subwindow, s->glContext );
		err = glewInit();
		if (err != GLEW_OK) {
			ms_error("Failed to initialize GLEW");
			return;
		} else if (!GLEW_VERSION_2_0) {
			ms_error("Need OpenGL 2.0+");
			return;
		} else {
			ogl_display_init(s->glhelper, s->wsize.width, s->wsize.height);
150
		}
Simon Morlat's avatar
Simon Morlat committed
151 152 153
	}
	if (s->window_id==0) return;
	if (s->own_window==FALSE){
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		/*we need to register for resize events*/
		XSelectInput(s->display,s->window_id,StructureNotifyMask);
	}

	XGetWindowAttributes(s->display,s->window_id,&wa);
	ms_message("glxvideo_prepare(): Window has size %ix%i, received video is %ix%i",wa.width,wa.height,s->vsize.width,s->vsize.height);

	if (wa.width<MS_LAYOUT_MIN_SIZE || wa.height<MS_LAYOUT_MIN_SIZE){
		return;
	}

	s->wsize.width=wa.width;
	s->wsize.height=wa.height;
	s->ready=TRUE;
}

static void glxvideo_unprepare(MSFilter *f){
	GLXVideo *s=(GLXVideo*)f->data;
	s->ready=FALSE;
}

static void glxvideo_preprocess(MSFilter *f){
	GLXVideo *obj=(GLXVideo*)f->data;
	if (obj->show) {
		if (obj->ready) glxvideo_unprepare(f);
		glxvideo_prepare(f);
	}
}


static void glxvideo_process(MSFilter *f){
	GLXVideo *obj=(GLXVideo*)f->data;
	mblk_t *inm;
	MSPicture src={0};
188
	bool_t precious=FALSE;
189
	XWindowAttributes wa;
Simon Morlat's avatar
Simon Morlat committed
190 191 192
	
	ms_filter_lock(f);
	
193
	if (obj->window_id==0 || obj->window_id==(Window)-1 || x11_error==TRUE) goto end;
194 195 196 197 198 199
	XGetWindowAttributes(obj->display,obj->window_id,&wa);
	if (wa.width!=obj->wsize.width || wa.height!=obj->wsize.height){
		ms_warning("Resized to %ix%i", wa.width,wa.height);
		obj->wsize.width=wa.width;
		obj->wsize.height=wa.height;
		ogl_display_init(obj->glhelper, wa.width, wa.height);
Simon Morlat's avatar
Simon Morlat committed
200 201 202
		if (obj->subwindow!=obj->window_id){
			XResizeWindow(obj->display,obj->subwindow, wa.width,wa.height);
		}
203
	}
204

Simon Morlat's avatar
Simon Morlat committed
205
	
206 207 208 209 210 211 212 213
	if (!obj->show) {
		goto end;
	}
	if (!obj->ready) glxvideo_prepare(f);
	if (!obj->ready){
		goto end;
	}

Simon Morlat's avatar
Simon Morlat committed
214
	glXMakeCurrent( obj->display, obj->subwindow, obj->glContext );
215 216
	if (f->inputs[0]!=NULL && (inm=ms_queue_peek_last(f->inputs[0]))!=0) {
		if (ms_yuv_buf_init_from_mblk(&src,inm)==0){
217 218 219 220 221 222 223 224 225
			MSVideoSize newsize;
			newsize.width=src.w;
			newsize.height=src.h;
			precious=mblk_get_precious_flag(inm);
			if (!ms_video_size_equal(newsize,obj->vsize) ) {
				ms_message("received size is %ix%i",newsize.width,newsize.height);
				obj->vsize=newsize;
				if (obj->autofit){
					MSVideoSize new_window_size;
226
					static const MSVideoSize min_size=MS_VIDEO_SIZE_QVGA;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
					/*don't resize less than QVGA, it is too small*/
					if (min_size.width*min_size.height>newsize.width*newsize.height){
						new_window_size.width=newsize.width*2;
						new_window_size.height=newsize.height*2;
					}else new_window_size=newsize;
					obj->wsize=new_window_size;
					ms_message("autofit: new window size should be %ix%i",new_window_size.width,new_window_size.height);
					XResizeWindow(obj->display,obj->window_id,new_window_size.width,new_window_size.height);
					XSync(obj->display,FALSE);
				}
				glxvideo_unprepare(f);
				glxvideo_prepare(f);
				if (!obj->ready) goto end;
			}
			if (obj->mirror && !precious) ms_yuv_buf_mirror(&src);
			ogl_display_set_yuv_to_display(obj->glhelper, inm);
243 244 245
		}
	}
	if (f->inputs[1]!=NULL && (inm=ms_queue_peek_last(f->inputs[1]))!=0) {
Simon Morlat's avatar
Simon Morlat committed
246 247 248 249 250 251
		if (obj->corner!=-1){
			if (ms_yuv_buf_init_from_mblk(&src,inm)==0){
				if (!mblk_get_precious_flag(inm)) ms_yuv_buf_mirror(&src);
				ogl_display_set_preview_yuv_to_display(obj->glhelper, inm);
			}
		}else ogl_display_set_preview_yuv_to_display(obj->glhelper,NULL);
252
	}
253
	ogl_display_render(obj->glhelper, 0);
Simon Morlat's avatar
Simon Morlat committed
254
	glXSwapBuffers ( obj->display, obj->subwindow );
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

	end:
		ms_filter_unlock(f);
		if (f->inputs[0]!=NULL)
			ms_queue_flush(f->inputs[0]);
		if (f->inputs[1]!=NULL)
			ms_queue_flush(f->inputs[1]);
}

static int glxvideo_set_vsize(MSFilter *f,void *arg){
	GLXVideo *s=(GLXVideo*)f->data;
	ms_filter_lock(f);
	s->wsize=*(MSVideoSize*)arg;
	ms_filter_unlock(f);
	return 0;
}

static int glxvideo_show_video(MSFilter *f, void *arg){
	GLXVideo *s=(GLXVideo*)f->data;
	bool_t show=*(bool_t*)arg;
	s->show=show?TRUE:FALSE;
	if (s->show==FALSE) {
		ms_filter_lock(f);
		glxvideo_unprepare(f);
		ms_filter_unlock(f);
	}

	return 0;
}

static int glxvideo_zoom(MSFilter *f, void *arg){
	GLXVideo *s=(GLXVideo*)f->data;

	ms_filter_lock(f);
289
	ogl_display_zoom(s->glhelper, arg);
290

291 292 293 294 295 296 297 298 299 300 301 302
	ms_filter_unlock(f);
	return 0;
}

static int glxvideo_get_native_window_id(MSFilter *f, void*arg){
	GLXVideo *s=(GLXVideo*)f->data;
	unsigned long *id=(unsigned long*)arg;
	*id=s->window_id;
	return 0;
}

static int glxvideo_set_native_window_id(MSFilter *f, void*arg){
Simon Morlat's avatar
Simon Morlat committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	GLXVideo *s=(GLXVideo*)f->data;
	unsigned long id=*(unsigned long*)arg;
	ms_filter_lock(f);
	
	if (s->window_id!=id) {
		if (s->display && s->subwindow!=0 && s->subwindow!=s->window_id){
			/*if the parent window is unset, and using a subwindow, destroy the subwindow*/
			XDestroyWindow(s->display, s->subwindow);
			XSync(s->display,FALSE);/*required to force immediate removal of the subwindow from its parent.
				Otherwise, if the parent is destroyed by a mouse click, it will automatically destroy child window and there will be a 
				double destroy of the subwindow resulting in "bad match" x error.*/
			ms_message("Subwindow destroyed");
			s->subwindow=0;
		}
		glxvideo_unprepare(f);
	}
	s->window_id=id;
	s->own_window=FALSE;
	
	ms_filter_unlock(f);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	return 0;
}

static bool_t createX11GLWindow(Display* display, MSVideoSize size, GLXContext* ctx, Window* win) {
	static int visual_attribs[] =
	{
	GLX_X_RENDERABLE	, True,
	GLX_DRAWABLE_TYPE	, GLX_WINDOW_BIT,
	GLX_RENDER_TYPE , GLX_RGBA_BIT,
	GLX_X_VISUAL_TYPE	, GLX_TRUE_COLOR,
	GLX_RED_SIZE	  , 8,
	GLX_GREEN_SIZE	, 8,
	GLX_BLUE_SIZE	 , 8,
	GLX_DOUBLEBUFFER	, True,
	//GLX_SAMPLE_BUFFERS  , 1,
	//GLX_SAMPLES	   , 4,
	None
	};
	int glx_major, glx_minor;
342 343 344 345 346 347 348 349 350
	int fbcount;
	GLXFBConfig *fbc;
	int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
	int i;
	GLXFBConfig bestFbc;
	XVisualInfo *vi;
	XSetWindowAttributes swa;
	Colormap cmap;

351 352 353 354 355 356 357
	// FBConfigs were added in GLX version 1.3.
	if ( !glXQueryVersion( display, &glx_major, &glx_minor ) ||		 ( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) ) {
		ms_error( "Invalid GLX version" );
		return FALSE;
	}

	ms_message( "Getting matching framebuffer configs" );
358
	fbc = glXChooseFBConfig( display, DefaultScreen( display ),
359 360 361 362 363 364 365 366
							visual_attribs, &fbcount );
	if ( !fbc ) {
		ms_error( "Failed to retrieve a framebuffer config" );
		return FALSE;
	}
	ms_message( "Found %d matching FB configs.", fbcount );
	// Pick the FB config/visual with the most samples per pixel
	ms_message( "Getting XVisualInfos" );
367

368 369 370 371 372 373
	for ( i = 0; i < fbcount; i++ ) {
		XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
		if ( vi ) {
			int samp_buf, samples;
			glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
			glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES	 , &samples  );
374

375
			ms_message( "  Matching fbconfig %d, visual ID 0x%lu: SAMPLE_BUFFERS = %d,"
376
		  " SAMPLES = %d",
377
		  i, vi -> visualid, samp_buf, samples );
378

379 380 381 382 383 384 385
			if ( best_fbc < 0 || (samp_buf && samples > best_num_samp) )
				best_fbc = i, best_num_samp = samples;
			if ( worst_fbc < 0 || (!samp_buf || samples < worst_num_samp) )
				worst_fbc = i, worst_num_samp = samples;
		}
		XFree( vi );
	}
386 387
	bestFbc = fbc[ best_fbc ];

388 389 390 391
	// Be sure to free the FBConfig list allocated by glXChooseFBConfig()
	XFree( fbc );

	// Get a visual
392
	vi = glXGetVisualFromFBConfig( display, bestFbc );
393
	ms_message( "Chosen visual ID = 0x%lu", vi->visualid );
394

395
	ms_message( "Creating colormap" );
Simon Morlat's avatar
Simon Morlat committed
396 397 398 399
	cmap = XCreateColormap( display,
						RootWindow( display, vi->screen ),
						vi->visual, AllocNone );
	swa.colormap = cmap;
400 401 402
	swa.background_pixmap = None ;
	swa.border_pixel	= 0;
	swa.event_mask	  = StructureNotifyMask;
Simon Morlat's avatar
Simon Morlat committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	if (*win==0){
		
		ms_message( "Creating window" );
		*win = XCreateWindow( display, RootWindow( display, vi->screen ),
						200, 200, size.width, size.height, 0, vi->depth, InputOutput,
						vi->visual,
						CWBorderPixel|CWColormap|CWEventMask, &swa );
		if ( !(*win) ) {
			ms_error( "Failed to create window." );
			return FALSE;
		}
		XStoreName( display, *win, "Video" );

		ms_message( "Mapping window" );
		XMapWindow( display, *win );
	}else{
419
		ms_message( "Creating sub window in window %p", (void *)*win );
Simon Morlat's avatar
Simon Morlat committed
420 421 422 423 424 425 426 427 428 429 430
		*win = XCreateWindow( display, *win,
						0, 0, size.width, size.height, 0, vi->depth, InputOutput,
						vi->visual,
						CWBorderPixel|CWColormap|CWEventMask, &swa );
		if ( !(*win) ) {
			ms_error( "Failed to create sub window." );
			return FALSE;
		}
		ms_message( "Mapping window subwindow" );
		XClearWindow(display, *win);
		XMapWindow( display, *win );
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	}
	// Done with the visual info data
	XFree( vi );
	// Get the default screen's GLX extension list
	*ctx = glXCreateNewContext( display, bestFbc, GLX_RGBA_TYPE, 0, True );

	// Sync to ensure any errors generated are processed.
	XSync( display, False );

	if (!(*ctx)) {
		ms_error("GL context creation failed");
		return FALSE;
	}

	return TRUE;
}

448 449 450 451 452 453 454 455 456 457 458 459
static int glxvideo_enable_mirroring(MSFilter *f,void *arg){
	GLXVideo *s=(GLXVideo*)f->data;
	s->mirror=(bool_t)*(int*)arg;
	return 0;
}

static int glxvideo_enable_autofit(MSFilter *f,void *arg){
	GLXVideo *s=(GLXVideo*)f->data;
	s->autofit=(bool_t)*(int*)arg;
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
460 461 462 463 464 465
static int glxvideo_set_local_view_mode(MSFilter *f, void *arg){
	GLXVideo *s=(GLXVideo*)f->data;
	s->corner=*(int*)arg;
	return 0;
}

466
static MSFilterMethod methods[]={
467 468
	{	MS_FILTER_SET_VIDEO_SIZE		, glxvideo_set_vsize },
	{	MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID	, glxvideo_get_native_window_id },
469
	{	MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID	, glxvideo_set_native_window_id },
470 471 472 473
	{	MS_VIDEO_DISPLAY_SHOW_VIDEO		, glxvideo_show_video },
	{	MS_VIDEO_DISPLAY_ZOOM			, glxvideo_zoom },
	{	MS_VIDEO_DISPLAY_ENABLE_MIRRORING	, glxvideo_enable_mirroring},
	{	MS_VIDEO_DISPLAY_ENABLE_AUTOFIT		, glxvideo_enable_autofit	},
Simon Morlat's avatar
Simon Morlat committed
474
	{	MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE	, glxvideo_set_local_view_mode},
475 476 477 478 479 480 481
	{	0	,NULL}
};


MSFilterDesc ms_glxvideo_desc={
	.id=MS_GLXVIDEO_ID,
	.name="MSGLXVideo",
482
	.text="A video display using GL (glx)",
483 484 485 486 487 488 489 490 491 492 493 494
	.category=MS_FILTER_OTHER,
	.ninputs=2,
	.noutputs=0,
	.init=glxvideo_init,
	.preprocess=glxvideo_preprocess,
	.process=glxvideo_process,
	.uninit=glxvideo_uninit,
	.methods=methods
};


MS_FILTER_DESC_EXPORT(ms_glxvideo_desc)