]> git.sesse.net Git - vlc/blob - modules/video_output/qte/qte.cpp
Added some debug info.
[vlc] / modules / video_output / qte / qte.cpp
1 /*****************************************************************************
2  * qte.cpp : QT Embedded plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 1998-2002 VideoLAN
5  * $Id: qte.cpp,v 1.8 2002/12/24 19:25:54 jpsaman Exp $
6  *
7  * Authors: Gerald Hansink <gerald.hansink@ordain.nl>
8  *          Jean-Paul Saman <jpsaman@wxs.nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * notes:
31  * - written for ipaq, so hardcoded assumptions specific for ipaq...
32  * - runs full screen
33  * - no "mouse events" handling
34  * - etc.
35  *****************************************************************************/
36
37 extern "C"
38 {
39 #include <errno.h>                                                 /* ENOMEM */
40 #include <stdlib.h>                                                /* free() */
41 #include <string.h>                                                /* strerror() */
42
43 #include <vlc/vlc.h>
44 #include <vlc/intf.h>
45 #include <vlc/vout.h>
46
47 #ifdef HAVE_MACHINE_PARAM_H
48     /* BSD */
49 #   include <machine/param.h>
50 #   include <sys/types.h>                                  /* typedef ushort */
51 #   include <sys/ipc.h>
52 #endif
53
54 #ifndef WIN32
55 #   include <netinet/in.h>                            /* BSD: struct in_addr */
56 #endif
57
58 #ifdef HAVE_SYS_SHM_H
59 #   include <sys/shm.h>                                /* shmget(), shmctl() */
60 #endif
61 } /* extern "C" */
62
63 #include <qapplication.h>
64 #include <qpainter.h>
65
66 #ifdef Q_WS_QWS
67 # define USE_DIRECT_PAINTER
68 # include <qdirectpainter_qws.h>
69 # include <qgfxraster_qws.h>
70 #endif
71
72 extern "C"
73 {
74 #include "netutils.h"                                 /* network_ChannelJoin */
75 #include "qte.h"
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 #define ALT_FS_TEXT N_("alternate fullscreen method")
81 #define ALT_FS_LONGTEXT N_( \
82     "There are two ways to make a fullscreen window, unfortunately each one " \
83     "has its drawbacks.\n" \
84     "1) Let the window manager handle your fullscreen window (default). But " \
85     "things like taskbars will likely show on top of the video.\n" \
86     "2) Completly bypass the window manager, but then nothing will be able " \
87     "to show on top of the video.")
88 #define DISPLAY_TEXT N_("QT Embedded display name")
89 #define DISPLAY_LONGTEXT N_( \
90     "Specify the Qt Embedded hardware display you want to use. By default vlc will " \
91     "use the value of the DISPLAY environment variable.")
92 #define DRAWABLE_TEXT N_("QT Embedded drawable")
93 #define DRAWABLE_LONGTEXT N_( \
94     "Specify a QT Embedded drawable to use instead of opening a new window. This " \
95     "option is DANGEROUS, use with care.")
96
97 /*****************************************************************************
98  * Local prototypes
99  *****************************************************************************/
100 static int  Open      ( vlc_object_t * );
101 static void Close     ( vlc_object_t * );
102 static void Render    ( vout_thread_t *, picture_t * );
103 static void Display   ( vout_thread_t *, picture_t * );
104 static int  Manage    ( vout_thread_t * );
105 static int  Init      ( vout_thread_t * );
106 static void End       ( vout_thread_t * );
107
108 static int  CreateQtWindow ( vout_thread_t * );
109 static void DestroyQtWindow( vout_thread_t * );
110
111 static int  NewPicture     ( vout_thread_t *, picture_t * );
112 static void FreePicture    ( vout_thread_t *, picture_t * );
113
114 static void ToggleFullScreen      ( vout_thread_t * );
115
116 static void RunQtThread( event_thread_t *p_event );
117 } /* extern "C" */
118
119 /*****************************************************************************
120 * Exported prototypes
121 *****************************************************************************/
122 extern "C"
123 {
124
125 vlc_module_begin();
126 //    add_category_hint( N_("QT Embedded"), NULL );
127 //    add_string( "qte-display", "landscape", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT);
128 //    add_bool( "qte-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT);
129 //    add_integer( "qte-drawable", -1, NULL, NULL, NULL); //DRAWABLE_TEXT, DRAWABLE_LONGTEXT );
130     set_description( _("QT Embedded module") );
131     set_capability( "video output", 30 );
132     set_callbacks( Open, Close);
133 vlc_module_end();
134
135 } /* extern "C" */
136
137 /*****************************************************************************
138  * Seeking function TODO: put this in a generic location !
139  *****************************************************************************/
140 static inline void vout_Seek( off_t i_seek )
141 {
142 }
143
144 /*****************************************************************************
145  * Open: allocate video thread output method
146  *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
148 {
149     vout_thread_t * p_vout = (vout_thread_t *)p_this;
150
151     /* Allocate structure */
152     p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
153
154     if( p_vout->p_sys == NULL )
155     {
156         msg_Err( p_vout, "out of memory" );
157         return( 1 );
158     }
159
160 //    memset(p_vout->p_sys, 0, sizeof( struct vout_sys_t ));
161     p_vout->pf_init    = Init;
162     p_vout->pf_end     = End;
163     p_vout->pf_manage  = NULL; //Manage;
164     p_vout->pf_render  = NULL; //Render;
165     p_vout->pf_display = Display;
166
167     CreateQtWindow(p_vout);
168     return( 0 );
169 }
170
171 /*****************************************************************************
172  * CloseVideo: destroy Sys video thread output method
173  *****************************************************************************
174  * Terminate an output method created by Open
175  *****************************************************************************/
176 static void Close ( vlc_object_t *p_this )
177 {
178     vout_thread_t * p_vout = (vout_thread_t *)p_this;
179
180     msg_Err( p_vout, "Close" );
181     if( p_vout->p_sys->p_event )
182     {
183         vlc_object_detach( p_vout->p_sys->p_event );
184
185         /* Kill RunQtThread */
186         p_vout->p_sys->p_event->b_die = VLC_TRUE;
187         DestroyQtWindow(p_vout);
188
189         vlc_thread_join( p_vout->p_sys->p_event );
190         vlc_object_destroy( p_vout->p_sys->p_event );
191     }
192
193     if( p_vout->p_sys )
194     {
195         free( p_vout->p_sys );
196         p_vout->p_sys = NULL;
197     }
198 }
199
200 /*****************************************************************************
201  * Init: initialize video thread output method
202  *****************************************************************************
203  * This function create the buffers needed by the output thread. It is called
204  * at the beginning of the thread, but also each time the window is resized.
205  *****************************************************************************/
206 static int Init( vout_thread_t *p_vout )
207 {
208     int         i_index;
209     picture_t*  p_pic;
210     int         dd = QPixmap::defaultDepth();
211
212     I_OUTPUTPICTURES = 0;
213
214     p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
215     p_vout->output.i_rmask  = 0xf800;
216     p_vout->output.i_gmask  = 0x07e0;
217     p_vout->output.i_bmask  = 0x001f;
218
219     /* We may need to convert the chroma, but at least we keep the
220      * aspect ratio */
221     p_vout->output.i_width  = p_vout->p_sys->i_width;
222     p_vout->output.i_height = p_vout->p_sys->i_height;
223     p_vout->output.i_aspect = p_vout->render.i_aspect;
224
225     /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
226     while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
227     {
228         p_pic = NULL;
229
230         /* Find an empty picture slot */
231         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
232         {
233             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
234             {
235                 p_pic = p_vout->p_picture + i_index;
236                 break;
237             }
238         }
239
240         /* Allocate the picture */
241         if( p_pic == NULL ||  NewPicture( p_vout, p_pic ) )
242         {
243             break;
244         }
245
246         p_pic->i_status = DESTROYED_PICTURE;
247         p_pic->i_type   = DIRECT_PICTURE;
248
249         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
250
251         I_OUTPUTPICTURES++;
252     }
253
254     return( 0 );
255 }
256
257
258 /*****************************************************************************
259  * Render: render previously calculated output
260  *****************************************************************************/
261 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
262 {
263     ;
264 }
265
266 /*****************************************************************************
267  * Display: displays previously rendered output
268  *****************************************************************************
269  * This function sends the currently rendered image to screen.
270  *****************************************************************************/
271 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
272 {
273     unsigned int x, y, w, h;
274
275     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
276                        &x, &y, &w, &h );
277
278     msg_Err(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
279                                         p_vout->p_sys->i_width, p_vout->p_sys->i_height, x, y, w, h );
280
281     if(p_vout->p_sys->pcVoutWidget)
282     {
283 // shameless borrowed from opie mediaplayer....
284 #ifndef USE_DIRECT_PAINTER
285         QPainter p(p_vout->p_sys->pcVoutWidget);
286
287         /* rotate frame */
288         int dd      = QPixmap::defaultDepth();
289         int bytes   = ( dd == 16 ) ? 2 : 4;
290         int rw = h, rh = w;
291
292         QImage rotatedFrame( rw, rh, bytes << 3 );
293
294         ushort* in  = (ushort*)p_pic->p_sys->pQImage->bits();
295         ushort* out = (ushort*)rotatedFrame.bits();
296
297         int spl = rotatedFrame.bytesPerLine() / bytes;
298         for (int x=0; x<h; x++)
299         {
300             if ( bytes == 2 )
301             {
302                 ushort* lout = out++ + (w - 1)*spl;
303                 for (int y=0; y<w; y++)
304                 {
305                     *lout=*in++;
306                     lout-=spl;
307                 }
308             }
309             else
310             {
311                 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
312                 for (int y=0; y<w; y++)
313                 {
314                     *lout=*((ulong*)in)++;
315                     lout-=spl;
316                 }
317             }
318         }
319
320         p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
321 #else
322         QDirectPainter p(p_vout->p_sys->pcVoutWidget);
323
324         // just copy the image to the frame buffer...
325         memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
326 #endif
327     }
328     msg_Err(p_vout, "-qte::Display" );
329
330 }
331
332 /*****************************************************************************
333  * Manage: handle X11 events
334  *****************************************************************************
335  * This function should be called regularly by video output thread. It manages
336  * X11 events and allows window resizing. It returns a non null value on
337  * error.
338  *****************************************************************************/
339 static int Manage( vout_thread_t *p_vout )
340 {
341     return 0;
342 }
343
344 /*****************************************************************************
345  * End: terminate video thread output method
346  *****************************************************************************
347  * Destroy the buffers created by vout_Init. It is called at the end of
348  * the thread, but also each time the window is resized.
349  *****************************************************************************/
350 static void End( vout_thread_t *p_vout )
351 {
352     int i_index;
353
354     /* Free the direct buffers we allocated */
355     for( i_index = I_OUTPUTPICTURES ; i_index ; )
356     {
357         i_index--;
358         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
359     }
360 }
361
362
363 /*****************************************************************************
364  * NewPicture: allocate a picture
365  *****************************************************************************
366  * Returns 0 on success, -1 otherwise
367  *****************************************************************************/
368 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
369 {
370     int dd = QPixmap::defaultDepth();
371
372     msg_Dbg(p_vout, "+NewPicture::dd = %d",dd );
373
374     p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
375
376     if( p_pic->p_sys == NULL )
377     {
378         return -1;
379     }
380
381     switch(p_vout->output.i_chroma)
382     {
383     case VLC_FOURCC('R','V','1','6'):
384         if(dd == 16)
385         {
386             p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
387                                                p_vout->output.i_height,
388                                                dd );
389
390             if(p_pic->p_sys->pQImage == NULL)
391             {
392                 return -1;
393             }
394
395             p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
396
397             p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
398
399             p_pic->p->i_lines = p_vout->output.i_height;
400             p_pic->p->i_pixel_pitch   = 2;
401             p_pic->p->i_visible_pitch = 0;
402 //            p_pic->p->i_pixel_bytes = 2;
403 //            p_pic->p->b_margin      = 0;
404             p_pic->i_planes         = 1;
405         }
406         else
407         {
408             return -1;
409         }
410         break;
411     case VLC_FOURCC('R','V','3','2'):
412         if(dd == 32)
413         {
414             p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
415                                                p_vout->output.i_height,
416                                                dd );
417
418             if(p_pic->p_sys->pQImage == NULL)
419             {
420                 return -1;
421             }
422
423             p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
424
425             p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
426
427             p_pic->p->i_lines = p_vout->output.i_height;
428             p_pic->p->i_pixel_pitch   = 4;
429             p_pic->p->i_visible_pitch = 0;
430 //            p_pic->p->i_pixel_bytes = 4;
431 //            p_pic->p->b_margin      = 0;
432             p_pic->i_planes         = 1;
433         }
434         else
435         {
436             return -1;
437         }
438         break;
439     default:
440         return -1;
441         break;
442     }
443
444     msg_Dbg(p_vout, "-NewPicture: %d %d %d",p_vout->output.i_width,
445                                  p_vout->output.i_height,
446                                  p_vout->output.i_chroma );
447
448     return 0;
449 }
450
451 /*****************************************************************************
452  * FreePicture: destroy a picture allocated with NewPicture
453  *****************************************************************************/
454 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
455 {
456     delete p_pic->p_sys->pQImage;
457 }
458
459 /*****************************************************************************
460  * ToggleFullScreen: Enable or disable full screen mode
461  *****************************************************************************
462  * This function will switch between fullscreen and window mode.
463  *
464  *****************************************************************************/
465 static void ToggleFullScreen ( vout_thread_t *p_vout )
466 {
467 }
468
469
470 /*****************************************************************************
471  * CreateQtWindow: create qte applicaton / window
472  *****************************************************************************
473  * Create a window according to video output given size, and set other
474  * properties according to the display properties.
475  *****************************************************************************/
476 static int CreateQtWindow( vout_thread_t *p_vout )
477 {
478     /* for displaying the vout in a qt window we need the QtApplication */
479     p_vout->p_sys->pcVoutWidget = NULL;
480     msg_Dbg( p_vout, "creating RunQtThread" );
481
482     p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
483     p_vout->p_sys->p_event->p_vout = p_vout;
484
485     /* Initializations */
486     p_vout->p_sys->i_width  = 320;
487     p_vout->p_sys->i_height = 240;
488
489     /* create thread to exec the qpe application */
490     if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
491                                                         RunQtThread,
492                             VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
493     {
494         msg_Err( p_vout, "cannot create QT Embedded Thread" );
495         vlc_object_destroy( p_vout->p_sys->p_event );
496         p_vout->p_sys->p_event = NULL;
497         goto error;
498     }
499
500     if( p_vout->p_sys->p_event->b_error )
501     {
502         msg_Err( p_vout, "RunQtThread failed" );
503         goto error;
504     }
505
506     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
507     msg_Dbg( p_vout, "RunQtThread running" );
508
509     // just wait until the crew is complete...
510     while(p_vout->p_sys->pcVoutWidget == NULL)
511     {
512         msleep(1);
513     }
514
515     return VLC_SUCCESS;
516
517  error:
518     Close( VLC_OBJECT(p_vout) );
519     return VLC_EGENERIC;
520 }
521
522
523 /*****************************************************************************
524  * DestroyQtWindow: destroy the window
525  *****************************************************************************/
526 static void DestroyQtWindow( vout_thread_t *p_vout )
527 {
528     // quit qt application loop
529     if(p_vout->p_sys->pcQApplication)
530     {
531         if(p_vout->p_sys->bOwnsQApp)
532         {
533             p_vout->p_sys->pcQApplication->quit();
534         }
535         else
536         {
537             p_vout->p_sys->bRunning = FALSE;
538         }
539
540         while(p_vout->p_sys->pcVoutWidget)
541         {
542             msleep(1);
543         }
544     }
545 }
546
547 /*****************************************************************************
548  * main loop of qtapplication
549  *****************************************************************************/
550 static void RunQtThread(event_thread_t *p_event)
551 {
552     int     argc    = 0;
553
554     msg_Dbg( p_event->p_vout, "+qte::RunQtThread" );
555
556     if(qApp == NULL)
557     {
558         QApplication* pApp = new QApplication(argc, NULL);
559         if(pApp)
560         {
561             p_event->p_vout->p_sys->pcQApplication = pApp;
562             p_event->p_vout->p_sys->bOwnsQApp = TRUE;
563         }
564         msg_Dbg( p_event->p_vout, "RunQtThread application created" );
565     }
566     else
567     {
568         p_event->p_vout->p_sys->pcQApplication = qApp;
569         msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
570     }
571
572     /* signal the creation of the window */
573     vlc_thread_ready( p_event );
574     msg_Dbg( p_event->p_vout, "+qte::RunQtThread ready" );
575
576     if (p_event->p_vout->p_sys->pcQApplication)
577     {
578         QWidget vo(0, "qte");
579         vo.showFullScreen();
580         vo.show();
581         p_event->p_vout->p_sys->pcVoutWidget = &vo;
582         p_event->p_vout->p_sys->bRunning = TRUE;
583
584         if(p_event->p_vout->p_sys->bOwnsQApp)
585         {
586             // run the main loop of qtapplication until someone says: 'quit'
587             p_event->p_vout->p_sys->pcQApplication->exec();
588         }
589         else
590         {
591             while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
592                         {
593                    /* Check if we are asked to exit */
594                if( p_event->b_die )
595                    break;
596
597                                 msleep(100);
598                         }
599         }
600     }
601
602     p_event->p_vout->p_sys->pcVoutWidget = NULL;
603
604     if(p_event->p_vout->p_sys->bOwnsQApp)
605     {
606         delete p_event->p_vout->p_sys->pcQApplication;
607         p_event->p_vout->p_sys->pcQApplication = NULL;
608     }
609
610     msg_Dbg( p_event->p_vout, "-qte::RunQtThread terminating" );
611 }
612