summaryrefslogtreecommitdiff
path: root/sys/glsink/gstglsink.c
blob: 54d2938fa38485f7843ae2cbc3c87dece50f07e3 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
<ds-work> your element belongs to a scheduler, which calls some functions from the same thread
<ds-work> all the other functions could be called from any random thread
<gernot> ds-work: which are the "some" function in that case ? 
<gernot> It is quite costly to do glXGetCurrentContext for every function call.
<ds-work> _chain, -get, _loop
*/

#include <config.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <sys/time.h>

/*#define GST_DEBUG_FORCE_DISABLE*/

#include "gstglsink.h"

/* elementfactory information */
static GstElementDetails gst_glsink_details = {
  "OpenGL Sink/GLX",
  "Sink/GLVideo",
  "An OpenGL based video sink - uses OpenGL and GLX to draw video, utilizing different acceleration options",
  "Gernot Ziegler <gz@lysator.liu.se>"
};

/* default template - initiated with class struct to allow gst-register to work
   with X running */
GST_PAD_TEMPLATE_FACTORY (gst_glsink_sink_template_factory,
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW ( "glsink_rgbsink", "video/x-raw-rgb",
    "framerate", GST_PROPS_FLOAT_RANGE(0, G_MAXFLOAT),
    "width", GST_PROPS_INT_RANGE(0, G_MAXINT),
    "height", GST_PROPS_INT_RANGE(0, G_MAXINT)),
  GST_CAPS_NEW ( "glsink_yuvsink", "video/x-raw-yuv",
    "framerate", GST_PROPS_FLOAT_RANGE(0, G_MAXFLOAT),
    "width", GST_PROPS_INT_RANGE(0, G_MAXINT),
    "height", GST_PROPS_INT_RANGE(0, G_MAXINT))
)

/* glsink signals and args */
enum {
  LAST_SIGNAL
};


enum {
  ARG_0,
  ARG_WIDTH,
  ARG_HEIGHT,
  ARG_FRAMES_DISPLAYED,
  ARG_FRAME_TIME,
  ARG_HOOK,
  ARG_MUTE,
  ARG_REPAINT,
  ARG_DEMO,
  ARG_DUMP
};

/* GLsink class */
#define GST_TYPE_GLSINK		(gst_glsink_get_type())
#define GST_GLSINK(obj)		(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_GLSINK,GstGLSink))
#define GST_GLSINK_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_GLSINK,GstGLSink))
#define GST_IS_GLSINK(obj)		(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_GLSINK))
#define GST_IS_GLSINK_CLASS(obj)	(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_GLSINK))

typedef struct _GstGLSink GstGLSink;
typedef struct _GstGLSinkClass GstGLSinkClass;

struct _GstGLSink {
  GstElement element;

  GstPad *sinkpad;

  gint frames_displayed;
  guint64 frame_time;
  gint width, height;
  gboolean muted;
  gint demo; // some kind of fun demo mode to let GL show its 3D capabilities
  gboolean dumpvideo; // dump the video down to .ppm:s 
  GstBuffer *last_image; /* not thread safe ? */
  
  GstClock *clock;

  /* bufferpool stuff */
  GstBufferPool *bufferpool;
  GMutex *cache_lock;
  GList *cache;
  
  /* plugins */
  GstImagePlugin* plugin;
  GstImageConnection *conn;
  
  /* allow anybody to hook in here */
  GstImageInfo *hook;
};

struct _GstGLSinkClass {
  GstElementClass parent_class;

  /* plugins */
  GList *plugins;
};


static GType 			gst_glsink_get_type		(void);
static void		gst_glsink_base_init	(gpointer g_class);
static void			gst_glsink_class_init	(GstGLSinkClass *klass);
static void			gst_glsink_init		(GstGLSink *sink);
/* static void 			gst_glsink_dispose 		(GObject *object); */

static void                     gst_glsink_chain                (GstPad *pad, GstData *_data);
static void			gst_glsink_set_clock		(GstElement *element, GstClock *clock);
static GstElementStateReturn	gst_glsink_change_state 	(GstElement *element);
static GstPadLinkReturn	gst_glsink_sinkconnect	(GstPad *pad, GstCaps *caps);
static GstCaps *		gst_glsink_getcaps		(GstPad *pad, GstCaps *caps);
static GstBufferPool*		gst_glsink_get_bufferpool	(GstPad *pad);

static void			gst_glsink_set_property	(GObject *object, guint prop_id, 
								 const GValue *value, GParamSpec *pspec);
static void			gst_glsink_get_property	(GObject *object, guint prop_id, 
								 GValue *value, GParamSpec *pspec);

static void			gst_glsink_release_conn	(GstGLSink *sink);
static void			gst_glsink_append_cache	(GstGLSink *sink, GstImageData *image);
static gboolean			gst_glsink_set_caps		(GstGLSink *sink, GstCaps *caps);
/* bufferpool stuff */
static GstBuffer*		gst_glsink_buffer_new 	(GstBufferPool *pool, 
								 gint64 location, 
								 guint size, gpointer user_data);
static void			gst_glsink_buffer_free	(GstBufferPool *pool, 
		                                                 GstBuffer *buffer, 
								 gpointer user_data);

/* prototypes from plugins */
extern GstImagePlugin* 		get_gl_rgbimage_plugin		(void);
extern GstImagePlugin* 		get_gl_nvimage_plugin		(void);
/* default output */
extern void			gst_glxwindow_new 		(GstGLSink *sink);
extern void			gst_glxwindow_hook_context 		(GstImageInfo *info);
extern void			gst_glxwindow_unhook_context 		(GstImageInfo *info);


static GstPadTemplate *sink_template;

static GstElementClass *parent_class = NULL;
/* static guint gst_glsink_signals[LAST_SIGNAL] = { 0 }; */

static GType
gst_glsink_get_type (void)
{
  static GType videosink_type = 0;

  if (!videosink_type) {
    static const GTypeInfo videosink_info = {
      sizeof(GstGLSinkClass),
      gst_glsink_base_init,
      NULL,
      (GClassInitFunc) gst_glsink_class_init,
      NULL,
      NULL,
      sizeof(GstGLSink),
      0,
      (GInstanceInitFunc) gst_glsink_init,
    };
    videosink_type = g_type_register_static(GST_TYPE_ELEMENT, "GstGLSink", &videosink_info, 0);
  }
  return videosink_type;
}

static void
gst_glsink_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  
  gst_element_class_set_details (element_class, &gst_glsink_details);

  gst_element_class_add_pad_template (
    element_class, 
    GST_PAD_TEMPLATE_GET (gst_glsink_sink_template_factory));
}

static void
gst_glsink_class_init (GstGLSinkClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass*) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  g_object_class_install_property (gobject_class, ARG_WIDTH,
    g_param_spec_int ("width", "Width", "The video width",
                      G_MININT, G_MAXINT, 0, G_PARAM_READABLE)); /* CHECKME */
  g_object_class_install_property (gobject_class, ARG_HEIGHT,
    g_param_spec_int ("height", "Height", "The video height",
                      G_MININT, G_MAXINT, 0, G_PARAM_READABLE)); /* CHECKME */
  g_object_class_install_property (gobject_class, ARG_FRAMES_DISPLAYED,
    g_param_spec_int ("frames_displayed", "Frames Displayed", "The number of frames displayed so far",
                      G_MININT,G_MAXINT, 0, G_PARAM_READWRITE)); /* CHECKME */
  g_object_class_install_property (gobject_class, ARG_FRAME_TIME,
    g_param_spec_int ("frame_time", "Frame time", "The interval between frames",
                      G_MININT, G_MAXINT, 0, G_PARAM_READWRITE)); /* CHECKME */
  g_object_class_install_property (gobject_class, ARG_HOOK,
    g_param_spec_pointer ("hook", "Hook", "The object receiving the output", G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class, ARG_MUTE,
    g_param_spec_boolean ("mute", "Mute", "mute the output ?", FALSE, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, ARG_REPAINT,
    g_param_spec_boolean ("repaint", "Repaint", "repaint the current frame", FALSE, G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class, ARG_DEMO,
    g_param_spec_int ("demo", "Demo", "demo mode (shows 3D capabilities)",0, 1, 0, G_PARAM_READWRITE)); 
  g_object_class_install_property (gobject_class, ARG_DUMP,
    g_param_spec_boolean ("dump", "Dump", "stores sequence of frames in .ppm files", FALSE, G_PARAM_READWRITE));

  gobject_class->set_property = gst_glsink_set_property;
  gobject_class->get_property = gst_glsink_get_property;

  /*gobject_class->dispose = gst_glsink_dispose; */
  
  gstelement_class->change_state = gst_glsink_change_state;
  gstelement_class->set_clock = gst_glsink_set_clock;
  
  /* plugins */
  klass->plugins = NULL;
  klass->plugins = g_list_append (klass->plugins, get_gl_rgbimage_plugin ());
  klass->plugins = g_list_append (klass->plugins, get_gl_nvimage_plugin ());
}


/*
  GLSink has its own Buffer management - this allows special plugins to create special memory areas for
  buffer upload 
*/
static void
gst_glsink_init (GstGLSink *sink)
{
  sink->sinkpad = gst_pad_new_from_template (sink_template, "sink");
  gst_element_add_pad (GST_ELEMENT (sink), sink->sinkpad);
  gst_pad_set_chain_function (sink->sinkpad, gst_glsink_chain);
  gst_pad_set_link_function (sink->sinkpad, gst_glsink_sinkconnect);
  gst_pad_set_getcaps_function (sink->sinkpad, gst_glsink_getcaps);
  gst_pad_set_bufferpool_function (sink->sinkpad, gst_glsink_get_bufferpool);

  sink->last_image = NULL;
  sink->width = 0;
  sink->height = 0;
  sink->muted = FALSE;
  sink->clock = NULL;  
  GST_FLAG_SET(sink, GST_ELEMENT_THREAD_SUGGESTED);
  GST_FLAG_SET (sink, GST_ELEMENT_EVENT_AWARE);
    
  /* create bufferpool and image cache */
  GST_DEBUG ("glsink: creating bufferpool");
  sink->bufferpool = gst_buffer_pool_new (
		    NULL,
		    NULL,
		    (GstBufferPoolBufferNewFunction)gst_glsink_buffer_new,
		    NULL,
		    (GstBufferPoolBufferFreeFunction)gst_glsink_buffer_free,
		    sink);
  sink->cache_lock = g_mutex_new();
  sink->cache = NULL; 
  
  /* plugins */
  sink->plugin = NULL;
  sink->conn = NULL;
  
  /* do initialization of default hook here */
  gst_glxwindow_new (sink); 
  //printf("GLSink_init: Current context %p\n", glXGetCurrentContext());
  gst_glxwindow_unhook_context(sink->hook);
}

/** frees the temporary connection that tests the window system capabilities */
static void
gst_glsink_release_conn (GstGLSink *sink)
{
  if (sink->conn == NULL) return;
  
  /* free last image if any */
  if (sink->last_image != NULL)
  {
    gst_buffer_unref (sink->last_image);
    sink->last_image = NULL;
  }
  /* free cache */
  g_mutex_lock (sink->cache_lock);
  while (sink->cache)
  {
    sink->plugin->free_image ((GstImageData *) sink->cache->data);
    sink->cache = g_list_delete_link (sink->cache, sink->cache);
  }
  g_mutex_unlock (sink->cache_lock);
  
  /* release connection */
  sink->conn->free_conn (sink->conn);
  sink->conn = NULL;
}

static void 		
gst_glsink_append_cache (GstGLSink *sink, GstImageData *image)
{
  g_mutex_lock (sink->cache_lock);
  sink->cache = g_list_prepend (sink->cache, image);
  g_mutex_unlock (sink->cache_lock);
}

/* 
   Create a new buffer to hand up the chain.
   This allows the plugins to make its own decoding buffers
 */
static GstBuffer*
gst_glsink_buffer_new (GstBufferPool *pool, gint64 location, 
		          guint size, gpointer user_data)
{
  GstGLSink *sink;
  GstBuffer *buffer;
  GstImageData *image;
  
  sink = GST_GLSINK (user_data);
  
  /* If cache is non-empty, get buffer from there */
  if (sink->cache != NULL) {
    g_mutex_lock (sink->cache_lock);
    image = (GstImageData *) sink->cache->data;
    sink->cache = g_list_delete_link (sink->cache, sink->cache);
    g_mutex_unlock (sink->cache_lock);
  } else {
    /* otherwise, get one from the plugin */
    image = sink->plugin->get_image (sink->hook, sink->conn);
  }
  
  buffer = gst_buffer_new ();
  GST_BUFFER_DATA (buffer) = image->data;
  GST_BUFFER_SIZE (buffer) = image->size;
  GST_BUFFER_POOL_PRIVATE (buffer) = image;
  
  return buffer;
}

/*
  Free a buffer that the chain doesn't need anymore. 
*/
static void
gst_glsink_buffer_free (GstBufferPool *pool, GstBuffer *buffer, gpointer user_data)
{
  GstGLSink *sink = GST_GLSINK (gst_buffer_pool_get_user_data (GST_BUFFER_BUFFERPOOL (buffer)));

  gst_glsink_append_cache (sink, (GstImageData *) GST_BUFFER_POOL_PRIVATE (buffer));

  /* set to NULL so the data is not freed */
  GST_BUFFER_DATA (buffer) = NULL;

  gst_buffer_default_free (buffer);
}

static GstBufferPool*
gst_glsink_get_bufferpool (GstPad *pad)
{
  GstGLSink *sink = GST_GLSINK (gst_pad_get_parent (pad));

  return sink->bufferpool;
}

/* 
   Set the caps that the application desires. 
   Go through the plugin list, finding the plugin that first fits the given parameters 
*/
static gboolean
gst_glsink_set_caps (GstGLSink *sink, GstCaps *caps)
{
  g_warning("in glsink set caps!\n");
  printf("Getting GLstring, context is %p\n",  glXGetCurrentContext());
    
  GList *list = ((GstGLSinkClass *) G_OBJECT_GET_CLASS (sink))->plugins;
  GstImageConnection *conn = NULL;
  while (list)    
  {
  printf("AGetting GLstring, context is %p\n",  glXGetCurrentContext());
    GstImagePlugin *plugin = (GstImagePlugin *) list->data;
    if ((conn = plugin->set_caps (sink->hook, caps)) != NULL)
    {
      //gst_glsink_release_conn (sink);
  printf("BGetting GLstring, context is %p\n",  glXGetCurrentContext());
      sink->conn = conn;
  printf("CGetting GLstring, context is %p\n",  glXGetCurrentContext());
      sink->plugin = plugin;
      sink->conn->open_conn (sink->conn, sink->hook);
      return TRUE;
    }
    list = g_list_next (list);
  }
  return FALSE;
}

/**
Link the input video sink internally.
*/
static GstPadLinkReturn
gst_glsink_sinkconnect (GstPad *pad, GstCaps *caps)
{
  g_warning("in glsink sinkconnect!\n");
  GstGLSink *sink;
  guint32 fourcc, print_format, result;

  sink = GST_GLSINK (gst_pad_get_parent (pad));

  /* we are not going to act on variable caps */
  if (!GST_CAPS_IS_FIXED (caps))
    return GST_PAD_LINK_DELAYED;
  
  gst_glxwindow_hook_context(sink->hook);
  /* try to set the caps on the output */
  result = gst_glsink_set_caps (sink, caps);
  gst_glxwindow_unhook_context(sink->hook);

  if (result == FALSE)
    {
      return GST_PAD_LINK_REFUSED;
    }
  
  /* remember width & height */
  gst_caps_get_int (caps, "width", &sink->width);
  gst_caps_get_int (caps, "height", &sink->height);

  gst_caps_get_fourcc_int (caps, "format", &fourcc);
  print_format = GULONG_FROM_LE (fourcc);
  GST_DEBUG ("glsink: setting %08x (%4.4s) %dx%d\n", 
		  fourcc, (gchar*)&print_format, sink->width, sink->height);

  /* emit signal */
  g_object_freeze_notify (G_OBJECT (sink));
  g_object_notify (G_OBJECT (sink), "width");
  g_object_notify (G_OBJECT (sink), "height");
  g_object_thaw_notify (G_OBJECT (sink));

  return GST_PAD_LINK_OK;
}
static GstCaps *
gst_glsink_getcaps (GstPad *pad, GstCaps *caps)
{
  g_warning("in glsink get caps!\n");
  /* what is the "caps" parameter good for? */
  GstGLSink *sink = GST_GLSINK (gst_pad_get_parent (pad));
  GstCaps *ret = NULL;
  GList *list = ((GstGLSinkClass *) G_OBJECT_GET_CLASS (sink))->plugins;
  
  gst_glxwindow_hook_context(sink->hook);
  while (list)    
  {
    ret = gst_caps_append (ret, ((GstImagePlugin *) list->data)->get_caps (sink->hook));
    list = g_list_next (list);
  }

  gst_glxwindow_unhook_context(sink->hook);
  return ret;
}

static void
gst_glsink_set_clock (GstElement *element, GstClock *clock)
{
  GstGLSink *sink = GST_GLSINK (element);
  
  sink->clock = clock;
}
static void
gst_glsink_chain (GstPad *pad, GstData *_data)
{
  //g_warning("in glsink_chain!\n");
  GstBuffer *buf = GST_BUFFER (_data);
  GstGLSink *sink;

  GstBuffer *buffer;

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));
  g_return_if_fail (buf != NULL);

  sink = GST_GLSINK (gst_pad_get_parent (pad));

  if (GST_IS_EVENT (buf)) {
    GstEvent *event = GST_EVENT (buf);

    switch (GST_EVENT_TYPE (event)) {
      default:
        gst_pad_event_default (pad, event);
    }
    return;
  }
  GST_DEBUG ("glsink: clock wait: %llu %u", 
		  GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_SIZE (buf));

#if 0
  GstClockTime time = GST_BUFFER_TIMESTAMP (buf);
  static int frame_drops = 0;

  if (sink->clock && time != -1) {
    if (time < gst_clock_get_time(sink->clock))
      {
	g_warning("Frame drop (%d consecutive) !!", frame_drops);
	/* we are going to drop late buffers */
	gst_buffer_unref (buf);
	frame_drops++;
	return;
      }
    frame_drops = 0; // we made it - reset time

    GstClockReturn ret;
    GstClockID id = gst_clock_new_single_shot_id (sink->clock, GST_BUFFER_TIMESTAMP (buf));

    ret = gst_element_clock_wait (GST_ELEMENT (sink), id, NULL);
    gst_clock_id_free (id);

    /* we are going to drop early buffers */
    if (ret == GST_CLOCK_EARLY) {
      gst_buffer_unref (buf);
      return;
    }
  }
#endif

  /* call the notify _before_ displaying so the handlers can react */
  sink->frames_displayed++;
  g_object_notify (G_OBJECT (sink), "frames_displayed");

  if (!sink->muted)
  {
    if (glXGetCurrentContext() == NULL)
      {
	printf("Rehooking window !\n");
	gst_glxwindow_hook_context(sink->hook);

#if 1
	GST_DEBUG("Initializing OpenGL parameters\n");
	/* initialize OpenGL drawing */
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glClearDepth(1.0f);
	glClearColor(0, 0, 0, 0);
	
	glEnable(GL_AUTO_NORMAL); // let OpenGL generate the Normals
	
	glDisable(GL_BLEND);
	glDisable(GL_CULL_FACE);
	glPolygonMode(GL_FRONT, GL_FILL);
	glPolygonMode(GL_BACK, GL_FILL);
	
	glShadeModel(GL_SMOOTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
	
	GstGLImageInfo *window = (GstGLImageInfo *)sink->hook;
	int w=window->width, h = window->height;

	glViewport(0, 0, (GLint) w, (GLint) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	GLfloat aspect = (GLfloat) w / (GLfloat) h;
	glFrustum(-aspect, aspect, -1.0, 1.0, 5.0, 500.0);

#endif
	gst_glxwindow_unhook_context(sink->hook);
	gst_glxwindow_hook_context(sink->hook);
	glMatrixMode(GL_MODELVIEW);
#if 0 
	sink->hook->free_info(sink->hook);
	printf("Reallocating window brutally !\n");
	gst_glxwindow_new(sink);
#endif
      }
    
    /* free last_image, if any */
    if (sink->last_image != NULL)
      gst_buffer_unref (sink->last_image);
    if (sink->bufferpool && GST_BUFFER_BUFFERPOOL (buf) == sink->bufferpool) {
      
      // awful hack ! But I currently have no other solution without changing the API
      sink->hook->demo = sink->demo;
      sink->hook->dumpvideo = sink->dumpvideo;

      sink->plugin->put_image (sink->hook, (GstImageData *) GST_BUFFER_POOL_PRIVATE (buf));
      sink->last_image = buf;
    } else {
      buffer = gst_buffer_new_from_pool (gst_glsink_get_bufferpool (sink->sinkpad), 
		                         0, GST_BUFFER_SIZE (buf));
      memcpy (GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (buf), 
	      GST_BUFFER_SIZE (buf) > GST_BUFFER_SIZE (buffer) ? 
	        GST_BUFFER_SIZE (buffer) : GST_BUFFER_SIZE (buf));

      sink->plugin->put_image (sink->hook, (GstImageData *) GST_BUFFER_POOL_PRIVATE (buffer));

      sink->last_image = buffer;
      gst_buffer_unref (buf);
    }

    //gst_glxwindow_unhook_context(sink->hook);
  }
  
}


static void
gst_glsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  //g_warning("in set_property!\n");
  GstGLSink *sink;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_GLSINK (object));

  sink = GST_GLSINK (object);

  switch (prop_id) {
    case ARG_FRAMES_DISPLAYED:
      sink->frames_displayed = g_value_get_int (value);
      g_object_notify (object, "frames_displayed");
      break;
    case ARG_FRAME_TIME:
      sink->frame_time = g_value_get_int (value);
      break;
    case ARG_HOOK:
      if (sink->hook)
      {
	sink->hook->free_info (sink->hook);
      }
      sink->hook = g_value_get_pointer (value);
      break;
    case ARG_MUTE:
      sink->muted = g_value_get_boolean (value);
      g_object_notify (object, "mute");
      break;
    case ARG_DEMO:
      sink->demo = g_value_get_int (value);
      g_object_notify (object, "demo");
      break;
    case ARG_DUMP:
      sink->dumpvideo = g_value_get_boolean (value);
      g_object_notify (object, "dump");
      break;
    case ARG_REPAINT:
      if (sink->last_image != NULL) {
	sink->plugin->put_image (sink->hook, (GstImageData *) GST_BUFFER_POOL_PRIVATE (sink->last_image));
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_glsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  //g_warning("in get_property!\n");
  GstGLSink *sink;

  /* it's not null if we got it, but it might not be ours */
  sink = GST_GLSINK(object);

  switch (prop_id) {
    case ARG_WIDTH:
      g_value_set_int (value, sink->width);
      break;
    case ARG_HEIGHT:
      g_value_set_int (value, sink->height);
      break;
    case ARG_FRAMES_DISPLAYED:
      g_value_set_int (value, sink->frames_displayed);
      break;
    case ARG_FRAME_TIME:
      g_value_set_int (value, sink->frame_time/1000000);
      break;
    case ARG_MUTE:
      g_value_set_boolean (value, sink->muted);
      break;
    case ARG_DEMO:
      g_value_set_int (value, sink->demo);
      break;
    case ARG_DUMP:
      g_value_set_boolean (value, sink->dumpvideo);
      break;
    default: 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


static GstElementStateReturn
gst_glsink_change_state (GstElement *element)
{
  //g_warning("in change_state!\n");
  GstGLSink *sink;

  sink = GST_GLSINK (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
      break;
    case GST_STATE_READY_TO_PAUSED:
      {
	//g_warning("Going GST_STATE_READY_TO_PAUSED: %p", sink->conn);
      }
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      {
	//g_warning("Going GST_STATE_PAUSED_TO_PLAYING: %p", sink->conn);
      }
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
      if (sink->conn)
        sink->conn->close_conn (sink->conn, sink->hook);
      if (sink->last_image) {
	gst_buffer_unref (sink->last_image);
        sink->last_image = NULL;
      }
      break;
    case GST_STATE_READY_TO_NULL:
      gst_glsink_release_conn (sink);
      break;
  }

  parent_class->change_state (element);

  return GST_STATE_SUCCESS;
}

static gboolean
plugin_init (GstPlugin *plugin)
{
  /* Loading the library containing GstVideoSink, our parent object */
  if (!gst_library_load ("gstvideo"))
    return FALSE;

  /* this is needed later on in the _real_ init (during a gst-launch) */
  sink_template = gst_pad_template_new (
		  "sink",
                  GST_PAD_SINK,
  		  GST_PAD_ALWAYS,
		  NULL);

  if (!gst_element_register (plugin, "glsink", GST_RANK_NONE, GST_TYPE_GLSINK))
    return FALSE;

  return TRUE;
}

GST_PLUGIN_DEFINE (
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "glsink",
  "An OpenGL based video sink - uses OpenGL and GLX to draw video, utilizing different acceleration options",
  plugin_init,
  VERSION,
  GST_LICENSE,
  GST_COPYRIGHT,
  GST_PACKAGE,
  GST_ORIGIN
);