source: mediastreamer2/src/videoout.c @ 1367:eb5d88eb5a20

Last change on this file since 1367:eb5d88eb5a20 was 1367:eb5d88eb5a20, checked in by Guillaume Beraudo <guillaume.beraudo@…>, 2 years ago

First version: non plannar, SDL 1.3 HG, FFMPEG GIT LGPL.

  • Latest SDL 1.3 required (fix crashes on setVideoMode)
  • Latest FFMPEG required (previous ones doesn't compile with SDL HG)
  • Plannar captured images under investigation.
File size: 27.5 KB
Line 
1/*
2mediastreamer2 library - modular sound and video processing and streaming
3Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include "mediastreamer-config.h"
22#endif
23
24#include "mediastreamer2/msfilter.h"
25#include "mediastreamer2/msvideo.h"
26
27/*required for dllexport of win_display_desc */
28#define INVIDEOUT_C 1
29#include "mediastreamer2/msvideoout.h"
30
31#ifdef __APPLE__
32#include <CoreFoundation/CFRunLoop.h>
33#endif
34
35struct _MSDisplay;
36
37typedef enum _MSDisplayEventType{
38        MS_DISPLAY_RESIZE_EVENT
39}MSDisplayEventType;
40
41typedef struct _MSDisplayEvent{
42        MSDisplayEventType evtype;
43        int w,h;
44}MSDisplayEvent;
45
46typedef struct _MSDisplayDesc{
47        /*init requests setup of the display window at the proper size, given
48        in frame_buffer argument. Memory buffer (data,strides) must be fulfilled
49        at return. init() might be called several times upon screen resize*/
50        bool_t (*init)(struct _MSDisplay *, struct _MSFilter *f, MSPicture *frame_buffer, MSPicture *selfview_buffer);
51        void (*lock)(struct _MSDisplay *);/*lock before writing to the framebuffer*/
52        void (*unlock)(struct _MSDisplay *);/*unlock after writing to the framebuffer*/
53        void (*update)(struct _MSDisplay *, int new_image, int new_selfview); /*display the picture to the screen*/
54        void (*uninit)(struct _MSDisplay *);
55        int (*pollevent)(struct _MSDisplay *, MSDisplayEvent *ev);
56        long default_window_id;
57}MSDisplayDesc;
58
59typedef struct _MSDisplay{
60        MSDisplayDesc *desc;
61        long window_id; /*window id if the display should use an existing window*/
62        void *data;
63        bool_t use_external_window;
64} MSDisplay;
65
66
67#define ms_display_init(d,f,fbuf,fbuf_selfview) (d)->desc->init(d,f,fbuf,fbuf_selfview)
68#define ms_display_lock(d)      if ((d)->desc->lock) (d)->desc->lock(d)
69#define ms_display_unlock(d)    if ((d)->desc->unlock) (d)->desc->unlock(d)
70#define ms_display_update(d, A, B)      if ((d)->desc->update) (d)->desc->update(d, A, B)
71
72int ms_display_poll_event(MSDisplay *d, MSDisplayEvent *ev);
73
74extern MSDisplayDesc ms_sdl_display_desc;
75
76#if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(MEDIASTREAMER_STATIC)
77#if defined(MEDIASTREAMER2_EXPORTS) && defined(INVIDEOUT_C)
78   #define MSVAR_DECLSPEC    __declspec(dllexport)
79#else
80   #define MSVAR_DECLSPEC    __declspec(dllimport)
81#endif
82#else
83   #define MSVAR_DECLSPEC    extern
84#endif
85
86#ifdef __cplusplus
87extern "C"{
88#endif
89
90/*plugins can set their own display using this method:*/
91void ms_display_desc_set_default(MSDisplayDesc *desc);
92
93MSDisplayDesc * ms_display_desc_get_default(void);
94void ms_display_desc_set_default_window_id(MSDisplayDesc *desc, long id);
95
96MSVAR_DECLSPEC MSDisplayDesc ms_win_display_desc;
97
98MSDisplay *ms_display_new(MSDisplayDesc *desc);
99void ms_display_set_window_id(MSDisplay *d, long window_id);
100void ms_display_destroy(MSDisplay *d);
101
102#define MS_VIDEO_OUT_SET_DISPLAY        MS_FILTER_METHOD(MS_VIDEO_OUT_ID,0,MSDisplay*)
103#define MS_VIDEO_OUT_HANDLE_RESIZING    MS_FILTER_METHOD_NO_ARG(MS_VIDEO_OUT_ID,1)
104#define MS_VIDEO_OUT_SET_CORNER         MS_FILTER_METHOD(MS_VIDEO_OUT_ID,2,int)
105#define MS_VIDEO_OUT_AUTO_FIT           MS_FILTER_METHOD(MS_VIDEO_OUT_ID,3,int)
106#define MS_VIDEO_OUT_ENABLE_MIRRORING   MS_FILTER_METHOD(MS_VIDEO_OUT_ID,4,int)
107#define MS_VIDEO_OUT_GET_NATIVE_WINDOW_ID MS_FILTER_METHOD(MS_VIDEO_OUT_ID,5,unsigned long)
108#define MS_VIDEO_OUT_GET_CORNER MS_FILTER_METHOD(MS_VIDEO_OUT_ID,6,int)
109#define MS_VIDEO_OUT_SET_SCALE_FACTOR   MS_FILTER_METHOD(MS_VIDEO_OUT_ID,7,float)
110#define MS_VIDEO_OUT_GET_SCALE_FACTOR   MS_FILTER_METHOD(MS_VIDEO_OUT_ID,8,float)
111#define MS_VIDEO_OUT_SET_SELFVIEW_POS   MS_FILTER_METHOD(MS_VIDEO_OUT_ID,9,float[3])
112#define MS_VIDEO_OUT_GET_SELFVIEW_POS   MS_FILTER_METHOD(MS_VIDEO_OUT_ID,10,float[3])
113#define MS_VIDEO_OUT_SET_BACKGROUND_COLOR       MS_FILTER_METHOD(MS_VIDEO_OUT_ID,11,int[3])
114#define MS_VIDEO_OUT_GET_BACKGROUND_COLOR       MS_FILTER_METHOD(MS_VIDEO_OUT_ID,12,int[3])
115
116#ifdef __cplusplus
117}
118#endif
119
120#define SCALE_FACTOR 4.0f
121#define SELVIEW_POS_INACTIVE -100.0
122
123static int video_out_set_vsize(MSFilter *f,void *arg);
124
125int ms_display_poll_event(MSDisplay *d, MSDisplayEvent *ev){
126        if (d->desc->pollevent)
127                return d->desc->pollevent(d,ev);
128        else return -1;
129}
130
131
132static int gcd(int m, int n)
133{
134   if(n == 0)
135     return m;
136   else
137     return gcd(n, m % n);
138}
139   
140static void reduce(int *num, int *denom)
141{
142   int divisor = gcd(*num, *denom);
143   *num /= divisor;
144   *denom /= divisor;
145}
146
147#include <SDL/SDL.h>
148#include <SDL/SDL_video.h>
149
150typedef struct _SdlDisplay{
151        MSFilter *filter;
152        bool_t sdl_initialized;
153        ms_mutex_t sdl_mutex;
154        SDL_Surface *sdl_screen;
155        SDL_Overlay *lay;
156
157        float sv_scalefactor;
158        MSVideoSize screen_size;
159} SdlDisplay;
160
161#ifdef HAVE_X11_XLIB_H
162
163#include <SDL/SDL_syswm.h>
164
165static long sdl_get_native_window_id(){
166        SDL_SysWMinfo info;
167        SDL_VERSION(&info.version);
168        if ( SDL_GetWMInfo(&info) ) {
169                if ( info.subsystem == SDL_SYSWM_X11 ) {
170                        return (long) info.info.x11.wmwindow;
171                }
172        }
173        return 0;
174}
175
176static void sdl_show_window(bool_t show){
177        SDL_SysWMinfo info;
178        SDL_VERSION(&info.version);
179        if ( SDL_GetWMInfo(&info) ) {
180                if ( info.subsystem == SDL_SYSWM_X11 ) {
181                        Display *display;
182                        Window window;
183               
184                        info.info.x11.lock_func();
185                        display = info.info.x11.display;
186                        window = info.info.x11.wmwindow;
187                        if (show)
188                                XMapWindow(display,window);
189                        else
190                                XUnmapWindow(display,window);
191                        info.info.x11.unlock_func();
192                }
193        }
194}
195
196#else
197
198static void sdl_show_window(bool_t show){
199        ms_warning("SDL window show/hide not implemented");
200}
201
202static long sdl_get_native_window_id(){
203        ms_warning("sdl_get_native_window_id not implemented");
204        return 0;
205}
206
207#endif
208
209static void sdl_display_uninit(MSDisplay *obj);
210
211static int sdl_create_window(SdlDisplay *wd, int w, int h){
212        static bool_t once=TRUE;
213        uint32_t flags = SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_RESIZABLE;
214        const SDL_VideoInfo *info;
215        info =SDL_GetVideoInfo();
216        if (info->wm_available) {
217                ms_message("Using window manager");
218        }
219        if (info->hw_available) {
220                ms_message("hw surface available (%dk memory)", info->video_mem);
221                flags |= SDL_HWSURFACE;
222        }
223        else
224                flags |= SDL_SWSURFACE;
225       
226        if (info->blit_hw) {
227                ms_message("hw surface available (%dk memory)", info->video_mem);
228                flags |= SDL_ASYNCBLIT;
229        }
230        if (info->blit_hw_CC)
231                ms_message("Colorkey blits between hw surfaces: accelerated");
232        if (info->blit_hw_A)
233                ms_message("Alpha blits between hw surfaces:  accelerated");
234        if (info->blit_sw)
235                ms_message("Copy blits from sw to hw surfaces:  accelerated");
236        if (info->blit_hw_CC)
237                ms_message("Colorkey blits between sw to hw surfaces: accelerated");
238        if (info->blit_hw_A)
239                ms_message("Alpha blits between sw to hw surfaces: accelerated");
240
241        ms_debug("Setting SDL video mode");
242        wd->sdl_screen = SDL_SetVideoMode(w,h, 0,flags);
243        if (wd->sdl_screen == NULL ) {
244                ms_warning("no hardware for video mode: %s\n",
245                                   SDL_GetError());
246        }
247        wd->screen_size.width = w;
248        wd->screen_size.height = h;
249        if (wd->sdl_screen->flags & SDL_HWSURFACE) ms_message("SDL surface created in hardware");
250        if (once) {
251                SDL_WM_SetCaption("Video window", NULL);
252                once=FALSE;
253        }
254
255        wd->lay=SDL_CreateYUVOverlay(w , h ,SDL_YV12_OVERLAY,wd->sdl_screen);
256        if (wd->lay==NULL){
257                ms_warning("Couldn't create yuv overlay: %s\n",
258                                                SDL_GetError());
259                return -1;
260        }else{
261                ms_message("Number of planes: %i", wd->lay->planes);
262                SDL_LockYUVOverlay(wd->lay); // necessary for getting accurate plane addresses since SDL 1.3
263                ms_message("%i x %i YUV overlay created: hw_accel=%i, pitches=%i,%i,%i",wd->lay->w,wd->lay->h,wd->lay->hw_overlay,
264                        wd->lay->pitches[0],wd->lay->pitches[1],wd->lay->pitches[2]);
265                ms_message("planes= %p %p %p  %i %i",wd->lay->pixels[0],wd->lay->pixels[1],wd->lay->pixels[2],
266                        wd->lay->pixels[1]-wd->lay->pixels[0],wd->lay->pixels[2]-wd->lay->pixels[1]);
267                SDL_UnlockYUVOverlay(wd->lay);
268        }
269        SDL_ShowCursor(0);//Hide the mouse cursor if was displayed
270        return 0;
271}
272
273static void free_overlay_and_surface(SdlDisplay* wd) {
274        if (wd->lay!=NULL) {
275                ms_message("Freeing overlay");
276                SDL_FreeYUVOverlay(wd->lay);
277        }
278        if (wd->sdl_screen!=NULL) {
279                ms_message("Freeing surface");
280                SDL_FreeSurface(wd->sdl_screen);
281        }
282        wd->lay=NULL;
283        wd->sdl_screen=NULL;
284}
285
286static bool_t sdl_display_init(MSDisplay *obj, MSFilter *f, MSPicture *fbuf, MSPicture *fbuf_selfview){
287        SdlDisplay *wd = (SdlDisplay*)obj->data;
288        int i;
289        if (wd==NULL){
290                char driver[128];
291                /* Initialize the SDL library */
292                ms_message("Initialize SDL video");
293                wd=(SdlDisplay*)ms_new0(SdlDisplay,1);
294                wd->filter = f;
295                obj->data=wd;
296               
297                if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
298                        ms_error("Couldn't initialize SDL: %s", SDL_GetError());
299                        return FALSE;
300                }
301                wd->sdl_initialized=TRUE;
302                if (SDL_VideoDriverName(driver, sizeof(driver))){
303                        ms_message("Video driver: %s", driver);
304                }
305                ms_mutex_init(&wd->sdl_mutex,NULL);
306                ms_mutex_lock(&wd->sdl_mutex);
307               
308        }else {
309                ms_message("Cleaning WD");
310                ms_mutex_lock(&wd->sdl_mutex);
311                free_overlay_and_surface(wd);
312        }
313        wd->filter = f;
314               
315        i=sdl_create_window(wd, fbuf->w, fbuf->h);
316        if (i==0){
317                fbuf->planes[0]=wd->lay->pixels[0];
318                fbuf->planes[1]=wd->lay->pixels[2];
319                fbuf->planes[2]=wd->lay->pixels[1];
320                fbuf->planes[3]=NULL;
321                fbuf->strides[0]=wd->lay->pitches[0];
322                fbuf->strides[1]=wd->lay->pitches[2];
323                fbuf->strides[2]=wd->lay->pitches[1];
324                fbuf->strides[3]=0;
325                fbuf->w=wd->lay->w;
326                fbuf->h=wd->lay->h;
327                sdl_show_window(TRUE);
328                obj->window_id=sdl_get_native_window_id();
329                ms_mutex_unlock(&wd->sdl_mutex);
330                return TRUE;
331        }
332        ms_mutex_unlock(&wd->sdl_mutex);
333        return FALSE;
334}
335
336static void sdl_display_lock(MSDisplay *obj){
337        SdlDisplay *wd = (SdlDisplay*)obj->data;
338        ms_mutex_lock(&wd->sdl_mutex);
339        SDL_LockYUVOverlay(wd->lay);
340        ms_mutex_unlock(&wd->sdl_mutex);
341}
342
343static void sdl_display_unlock(MSDisplay *obj){
344        SdlDisplay *wd = (SdlDisplay*)obj->data;
345        ms_mutex_lock(&wd->sdl_mutex);
346        SDL_UnlockYUVOverlay(wd->lay);
347        ms_mutex_unlock(&wd->sdl_mutex);
348}
349
350static void sdl_display_update(MSDisplay *obj, int new_image, int new_selfview){
351        SdlDisplay *wd = (SdlDisplay*)obj->data;
352        SDL_Rect rect;
353        int ratiow;
354        int ratioh;
355        int w;
356        int h;
357       
358        rect.x=0;
359        rect.y=0;
360        ms_mutex_lock(&wd->sdl_mutex);
361
362        ratiow=wd->lay->w;
363        ratioh=wd->lay->h;
364        reduce(&ratiow, &ratioh);
365        w = wd->screen_size.width/ratiow*ratiow;
366        h = wd->screen_size.height/ratioh*ratioh;
367
368        if (h*ratiow>w*ratioh)
369        {
370                w = w;
371                h = w*ratioh/ratiow;
372        }
373        else
374        {
375                h = h;
376                w = h*ratiow/ratioh;
377        }
378
379        if (h*wd->lay->w!=w*wd->lay->h)
380                ms_error("wrong ratio");
381
382        rect.x = (wd->screen_size.width-w)/2;
383        rect.y = (wd->screen_size.height-h)/2;
384        rect.w = w;
385        rect.h = h;
386       
387        if (SDL_DisplayYUVOverlay(wd->lay,&rect) != 0) 
388                ms_error("Error while displaying overlay");
389
390        ms_mutex_unlock(&wd->sdl_mutex);
391}
392
393static int sdl_poll_event(MSDisplay *obj, MSDisplayEvent *ev){
394        SdlDisplay *wd = (SdlDisplay*)obj->data;
395        SDL_Event event;
396        if (wd->sdl_screen==NULL) return -1;
397        ms_mutex_lock(&wd->sdl_mutex);
398        if (SDL_PollEvent(&event)){
399                ms_mutex_unlock(&wd->sdl_mutex);
400                switch(event.type){
401                        case SDL_VIDEORESIZE:
402                                ev->evtype=MS_DISPLAY_RESIZE_EVENT;
403                                ev->w=event.resize.w;
404                                ev->h=event.resize.h;
405                                wd->screen_size.width = event.resize.w;
406                                wd->screen_size.height = event.resize.h;
407                                return 1;
408                        break;
409                        default:
410                                return 0;
411                        break;
412                }
413        }else ms_mutex_unlock(&wd->sdl_mutex);
414        return -1;
415}
416
417static void sdl_display_uninit(MSDisplay *obj){
418        SdlDisplay *wd = (SdlDisplay*)obj->data;
419
420        if (wd!=NULL) {
421                free_overlay_and_surface(wd);
422                ms_free(wd);
423                wd=NULL;
424        }
425        ms_message("WD Fred");
426
427#ifdef __linux
428        /*purge the event queue before leaving*/
429        SDL_Event event;
430        int i;
431        for(i=0;SDL_PollEvent(&event) && i<100;++i){
432        }
433#endif
434        sdl_show_window(FALSE);
435        SDL_Quit();
436}
437
438MSDisplayDesc ms_sdl_display_desc={
439        .init=sdl_display_init,
440        .lock=sdl_display_lock,
441        .unlock=sdl_display_unlock,
442        .update=sdl_display_update,
443        .uninit=sdl_display_uninit,
444        .pollevent=sdl_poll_event,
445};
446
447
448MSDisplay *ms_display_new(MSDisplayDesc *desc){
449        MSDisplay *obj=(MSDisplay *)ms_new0(MSDisplay,1);
450        obj->desc=desc;
451        obj->data=NULL;
452        return obj;
453}
454
455void ms_display_set_window_id(MSDisplay *d, long id){
456        d->window_id=id;
457        d->use_external_window=TRUE;
458}
459
460void ms_display_destroy(MSDisplay *obj){
461        obj->desc->uninit(obj);
462        ms_free(obj);
463}
464
465static MSDisplayDesc *default_display_desc=&ms_sdl_display_desc;
466
467void ms_display_desc_set_default(MSDisplayDesc *desc){
468        default_display_desc=desc;
469}
470
471MSDisplayDesc * ms_display_desc_get_default(void){
472        return default_display_desc;
473}
474
475void ms_display_desc_set_default_window_id(MSDisplayDesc *desc, long id){
476        desc->default_window_id=id;
477}
478
479typedef struct VideoOut
480{
481        struct Rational {int num; int den;} ratio;
482        MSPicture fbuf;
483        MSPicture fbuf_selfview;
484        MSPicture local_pic;
485        MSRect local_rect;
486        mblk_t *local_msg;
487        MSVideoSize prevsize;
488        int corner; /*for selfview*/
489        float scale_factor; /*for selfview*/
490        float sv_posx,sv_posy;
491        int background_color[3];
492
493        MSScalerContext *sws1;
494        MSScalerContext *sws2;
495        MSDisplay *display;
496        bool_t own_display;
497        bool_t ready;
498        bool_t autofit;
499        bool_t mirror;
500
501#ifdef __APPLE__
502        bool_t need_update;
503        CFRunLoopTimerRef timer;
504        CFRunLoopTimerContext timer_context;
505#endif
506} VideoOut;
507
508static void set_corner(VideoOut *s, int corner)
509{
510        s->corner=corner;
511        s->local_pic.w=((int)(s->fbuf.w/s->scale_factor)) & ~0x1;
512        s->local_pic.h=((int)(s->fbuf.h/s->scale_factor)) & ~0x1;
513        s->local_rect.w=s->local_pic.w;
514        s->local_rect.h=s->local_pic.h;
515        if (corner==1) {
516                /* top left corner */
517                s->local_rect.x=0;
518                s->local_rect.y=0;
519        } else if (corner==2) {
520                /* top right corner */
521                s->local_rect.x=s->fbuf.w-s->local_pic.w;
522                s->local_rect.y=0;
523        } else if (corner==3) {
524                /* bottom left corner */
525                s->local_rect.x=0;
526                s->local_rect.y=s->fbuf.h-s->local_pic.h;
527        } else {
528                /* default: bottom right corner */
529                /* corner can be set to -1: to disable the self view... */
530                s->local_rect.x=s->fbuf.w-s->local_pic.w;
531                s->local_rect.y=s->fbuf.h-s->local_pic.h;
532        }
533        s->fbuf_selfview.w=(s->fbuf.w/1) & ~0x1;
534        s->fbuf_selfview.h=(s->fbuf.h/1) & ~0x1;
535}
536
537
538
539static void set_vsize(VideoOut *s, MSVideoSize *sz){
540        s->fbuf.w=sz->width & ~0x1;
541        s->fbuf.h=sz->height & ~0x1;
542        set_corner(s,s->corner);
543        ms_message("Video size set to %ix%i",s->fbuf.w,s->fbuf.h);
544}
545
546static void video_out_init(MSFilter  *f){
547        ms_message("video_out_init");
548        VideoOut *obj=(VideoOut*)ms_new0(VideoOut,1);
549        MSVideoSize def_size;
550        obj->ratio.num=11;
551        obj->ratio.den=9;
552        def_size.width=MS_VIDEO_SIZE_CIF_W;
553        def_size.height=MS_VIDEO_SIZE_CIF_H;
554        obj->prevsize.width=0;
555        obj->prevsize.height=0;
556        obj->local_msg=NULL;
557        obj->corner=0;
558        obj->scale_factor=SCALE_FACTOR;
559        obj->sv_posx=obj->sv_posy=SELVIEW_POS_INACTIVE;
560        obj->background_color[0]=obj->background_color[1]=obj->background_color[2]=0;
561        obj->sws1=NULL;
562        obj->sws2=NULL;
563        obj->display=NULL;
564        obj->own_display=FALSE;
565        obj->ready=FALSE;
566        obj->autofit=FALSE;
567        obj->mirror=FALSE;
568        set_vsize(obj,&def_size);
569        f->data=obj;
570}
571
572
573static void video_out_uninit(MSFilter *f){
574        VideoOut *obj=(VideoOut*)f->data;
575        if (obj->display!=NULL && obj->own_display)
576                ms_display_destroy(obj->display);
577        if (obj->sws1!=NULL){
578                ms_scaler_context_free(obj->sws1);
579                obj->sws1=NULL;
580        }
581        if (obj->sws2!=NULL){
582                ms_scaler_context_free(obj->sws2);
583                obj->sws2=NULL;
584        }
585        if (obj->local_msg!=NULL) {
586                freemsg(obj->local_msg);
587                obj->local_msg=NULL;
588        }
589        ms_free(obj);
590}
591
592static void video_out_prepare(MSFilter *f){
593        VideoOut *obj=(VideoOut*)f->data;
594        if (obj->display==NULL){
595                if (default_display_desc==NULL){
596                        ms_error("No default display built in !");
597                        return;
598                }
599                obj->display=ms_display_new(default_display_desc);
600                obj->own_display=TRUE;
601        }
602        if (!ms_display_init(obj->display,f,&obj->fbuf,&obj->fbuf_selfview)){
603                if (obj->own_display) ms_display_destroy(obj->display);
604                obj->display=NULL;
605        }
606        if (obj->sws1!=NULL){
607                ms_scaler_context_free(obj->sws1);
608                obj->sws1=NULL;
609        }
610        if (obj->sws2!=NULL){
611                 ms_scaler_context_free(obj->sws2);
612                obj->sws2=NULL;
613        }
614        if (obj->local_msg!=NULL) {
615                freemsg(obj->local_msg);
616                obj->local_msg=NULL;
617        }
618        set_corner(obj,obj->corner);
619        obj->ready=TRUE;
620}
621
622static int video_out_handle_resizing(MSFilter *f, void *data){
623        /* to be removed */
624        return -1;
625}
626
627static int _video_out_handle_resizing(MSFilter *f, void *data){
628        VideoOut *s=(VideoOut*)f->data;
629        MSDisplay *disp=s->display;
630        int ret = -1;
631        if (disp!=NULL){
632                MSDisplayEvent ev;
633                ret = ms_display_poll_event(disp,&ev);
634                if (ret>0){
635                        if (ev.evtype==MS_DISPLAY_RESIZE_EVENT){
636                                MSVideoSize sz;
637                                sz.width=ev.w;
638                                sz.height=ev.h;
639                                ms_filter_lock(f);
640                                if (s->ready){
641                                        set_vsize(s,&sz);
642                                        s->ready=FALSE;
643                                }
644                                ms_filter_unlock(f);
645                        }
646                        return 1;
647                }
648        }
649        return ret;
650}
651
652static void poll_for_resizing_lock_filter_and_enventually_prepare(MSFilter *f) {
653        VideoOut *obj=(VideoOut*)f->data;
654        int i;
655
656        for(i=0;i<100;++i){
657                int ret = _video_out_handle_resizing(f, NULL);
658                if (ret<0)
659                        break;
660        }
661        ms_filter_lock(f);
662        if (!obj->ready) video_out_prepare(f);
663}
664
665
666#ifdef __APPLE__
667static void apple_loop_cb(CFRunLoopTimerRef timer, void *info) {
668        MSFilter *f = (MSFilter *) info;
669        VideoOut *obj=(VideoOut*)f->data;
670        poll_for_resizing_lock_filter_and_enventually_prepare(f);
671
672        if (obj->need_update) {
673                ms_display_update(obj->display, 1, 1);
674                obj->need_update=false;
675        }
676
677        ms_filter_unlock(f);
678}
679#endif
680
681static void video_out_postprocess(MSFilter *f){
682#ifdef __APPLE__
683        VideoOut* obj = (VideoOut*) f->data;
684        CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), obj->timer, kCFRunLoopCommonModes);
685        obj->timer = NULL;
686#endif
687}
688
689static void video_out_preprocess(MSFilter *f){
690#ifndef __APPLE__
691        video_out_prepare(f);
692#else
693        VideoOut* obj = (VideoOut*) f->data;
694        if (obj->timer != NULL) ms_error("Non null timer found");
695
696        CFTimeInterval interval=0.01f; // 10 milliseconds
697        obj->timer_context.version=0;
698        obj->timer_context.info=f;
699        obj->timer_context.retain=NULL;
700        obj->timer_context.release=NULL;
701        obj->timer_context.copyDescription=NULL;
702
703        obj->timer = CFRunLoopTimerCreate (NULL,
704                CFAbsoluteTimeGetCurrent() + interval,
705                interval,
706                0,
707                0,
708                apple_loop_cb,
709                &(obj->timer_context)
710);
711        CFRunLoopAddTimer (CFRunLoopGetCurrent(), obj->timer, kCFRunLoopCommonModes);
712#endif
713}
714
715
716static void video_out_process(MSFilter *f){
717        VideoOut *obj=(VideoOut*)f->data;
718        mblk_t *inm;
719        int update=0;
720        int update_selfview=0;
721
722#ifndef __APPLE__
723        poll_for_resizing_lock_filter_and_enventually_prepare(f);
724#endif
725        if (!obj->ready){
726                ms_filter_unlock(f);
727                if (f->inputs[0]!=NULL)
728                        ms_queue_flush(f->inputs[0]);
729                if (f->inputs[1]!=NULL)
730                        ms_queue_flush(f->inputs[1]);
731                return;
732        }
733        /*get most recent message and draw it*/
734        if (f->inputs[1]!=NULL && (inm=ms_queue_peek_last(f->inputs[1]))!=0) {
735                if (obj->corner==-1){
736                        if (obj->local_msg!=NULL) {
737                                freemsg(obj->local_msg);
738                                obj->local_msg=NULL;
739                        }
740                }else if (obj->fbuf_selfview.planes[0]!=NULL) {
741                        MSPicture src;
742                        if (ms_yuv_buf_init_from_mblk(&src,inm)==0){
743                               
744                                if (obj->sws2==NULL){
745                                        obj->sws2=ms_scaler_create_context(src.w,src.h,MS_YUV420P,
746                                                                                         obj->fbuf_selfview.w,obj->fbuf_selfview.h,MS_YUV420P,
747                                                                                         MS_SCALER_METHOD_BILINEAR);
748                                }
749                                ms_display_lock(obj->display);
750                                if (ms_scaler_process(obj->sws2,src.planes,src.strides,obj->fbuf_selfview.planes, obj->fbuf_selfview.strides)<0){
751                                        ms_error("Error in ms_sws_scale().");
752                                }
753                                if (!mblk_get_precious_flag(inm)) ms_yuv_buf_mirror(&obj->fbuf_selfview);
754                                ms_display_unlock(obj->display);
755                                update_selfview=1;
756                        }
757                }else{
758                        MSPicture src;
759                        if (ms_yuv_buf_init_from_mblk(&src,inm)==0){
760                               
761                                if (obj->sws2==NULL){
762                                        obj->sws2=ms_scaler_create_context(src.w,src.h,MS_YUV420P,
763                                                                obj->local_pic.w,obj->local_pic.h,MS_YUV420P,
764                                                                MS_SCALER_METHOD_BILINEAR);
765                                }
766                                if (obj->local_msg==NULL){
767                                        obj->local_msg=ms_yuv_buf_alloc(&obj->local_pic,
768                                                obj->local_pic.w,obj->local_pic.h);
769                                }
770                                if (obj->local_pic.planes[0]!=NULL)
771                                {
772                                        if (ms_scaler_process(obj->sws2,src.planes,src.strides,obj->local_pic.planes, obj->local_pic.strides)<0){
773                                                ms_error("Error in ms_sws_scale().");
774                                        }
775                                        if (!mblk_get_precious_flag(inm)) ms_yuv_buf_mirror(&obj->local_pic);
776                                        update=1;
777                                }
778                        }
779                }
780                ms_queue_flush(f->inputs[1]);
781        }
782       
783        if (f->inputs[0]!=NULL && (inm=ms_queue_peek_last(f->inputs[0]))!=0) {
784                MSPicture src;
785                if (ms_yuv_buf_init_from_mblk(&src,inm)==0){
786                        MSVideoSize cur,newsize;
787                        cur.width=obj->fbuf.w;
788                        cur.height=obj->fbuf.h;
789                        newsize.width=src.w;
790                        newsize.height=src.h;
791                        if (obj->autofit && !ms_video_size_equal(newsize,obj->prevsize) ) {
792                                MSVideoSize qvga_size;
793                                qvga_size.width=MS_VIDEO_SIZE_QVGA_W;
794                                qvga_size.height=MS_VIDEO_SIZE_QVGA_H;
795                                obj->prevsize=newsize;
796                                ms_message("received size is %ix%i",newsize.width,newsize.height);
797                                /*don't resize less than QVGA, it is too small*/
798                                if (ms_video_size_greater_than(qvga_size,newsize)){
799                                        newsize.width=MS_VIDEO_SIZE_QVGA_W;
800                                        newsize.height=MS_VIDEO_SIZE_QVGA_H;
801                                }
802                                if (!ms_video_size_equal(newsize,cur)){
803                                        set_vsize(obj,&newsize);
804                                        ms_message("autofit: new size is %ix%i",newsize.width,newsize.height);
805                                        #ifndef __APPLE__
806                                        video_out_prepare(f);
807                                        #else
808                                        obj->ready=false;
809                                        ms_queue_flush(f->inputs[0]);
810                                        ms_filter_unlock(f);
811                                        return;
812                                        #endif
813                                }
814                        }
815                        if (obj->sws1==NULL){
816                                obj->sws1=ms_scaler_create_context(src.w,src.h,MS_YUV420P,
817                                obj->fbuf.w,obj->fbuf.h,MS_YUV420P,
818                                MS_SCALER_METHOD_BILINEAR);
819                        }
820                        ms_display_lock(obj->display);
821                        if (ms_scaler_process(obj->sws1,src.planes,src.strides,obj->fbuf.planes, obj->fbuf.strides)<0){
822                                ms_error("Error in ms_sws_scale().");
823                        }
824                        if (obj->mirror && !mblk_get_precious_flag(inm)) ms_yuv_buf_mirror(&obj->fbuf);
825                        ms_display_unlock(obj->display);
826                }
827                update=1;
828                ms_queue_flush(f->inputs[0]);
829        }
830
831        /*copy resized local view into main buffer, at bottom left corner:*/
832        if (obj->local_msg!=NULL){
833                MSPicture corner=obj->fbuf;
834                MSVideoSize roi;
835                roi.width=obj->local_pic.w;
836                roi.height=obj->local_pic.h;
837                corner.w=obj->local_pic.w;
838                corner.h=obj->local_pic.h;
839                corner.planes[0]+=obj->local_rect.x+(obj->local_rect.y*corner.strides[0]);
840                corner.planes[1]+=(obj->local_rect.x/2)+((obj->local_rect.y/2)*corner.strides[1]);
841                corner.planes[2]+=(obj->local_rect.x/2)+((obj->local_rect.y/2)*corner.strides[2]);
842                corner.planes[3]=0;
843                ms_display_lock(obj->display);
844                ms_yuv_buf_copy(obj->local_pic.planes,obj->local_pic.strides,
845                                corner.planes,corner.strides,roi);
846                ms_display_unlock(obj->display);
847        }
848
849        if (update == 1 || update_selfview == 1) {
850                #ifdef __APPLE__
851                obj->need_update = true;
852                #else
853                ms_display_update(obj->display, update, update_selfview);
854                #endif
855        }
856
857        ms_filter_unlock(f);
858}
859
860static int video_out_set_vsize(MSFilter *f,void *arg){
861        VideoOut *s=(VideoOut*)f->data;
862        ms_filter_lock(f);
863        set_vsize(s,(MSVideoSize*)arg);
864        ms_filter_unlock(f);
865        return 0;
866}
867
868static int video_out_set_display(MSFilter *f,void *arg){
869        VideoOut *s=(VideoOut*)f->data;
870        s->display=(MSDisplay*)arg;
871        return 0;
872}
873
874static int video_out_auto_fit(MSFilter *f, void *arg){
875        VideoOut *s=(VideoOut*)f->data;
876        s->autofit=*(int*)arg;
877        return 0;
878}
879
880static int video_out_set_corner(MSFilter *f,void *arg){
881        VideoOut *s=(VideoOut*)f->data;
882        s->sv_posx=s->sv_posy=SELVIEW_POS_INACTIVE;
883        ms_filter_lock(f);
884        set_corner(s, *(int*)arg);
885        if (s->display){
886                ms_display_lock(s->display);
887                {
888                int w=s->fbuf.w;
889                int h=s->fbuf.h;
890                int ysize=w*h;
891                int usize=ysize/4;
892               
893                memset(s->fbuf.planes[0], 0, ysize);
894                memset(s->fbuf.planes[1], 0, usize);
895                memset(s->fbuf.planes[2], 0, usize);
896                s->fbuf.planes[3]=NULL;
897                }
898                ms_display_unlock(s->display);
899        }
900        ms_filter_unlock(f);
901        return 0;
902}
903
904static int video_out_get_corner(MSFilter *f,void *arg){
905        VideoOut *s=(VideoOut*)f->data;
906        *((int*)arg)=s->corner;
907        return 0;
908}
909
910static int video_out_set_scalefactor(MSFilter *f,void *arg){
911        VideoOut *s=(VideoOut*)f->data;
912        s->scale_factor = *(float*)arg;
913        if (s->scale_factor<0.5f)
914                s->scale_factor = 0.5f;
915        ms_filter_lock(f);
916        set_corner(s, s->corner);
917        if (s->display){
918                ms_display_lock(s->display);
919                {
920                int w=s->fbuf.w;
921                int h=s->fbuf.h;
922                int ysize=w*h;
923                int usize=ysize/4;
924               
925                memset(s->fbuf.planes[0], 0, ysize);
926                memset(s->fbuf.planes[1], 0, usize);
927                memset(s->fbuf.planes[2], 0, usize);
928                s->fbuf.planes[3]=NULL;
929                }
930                ms_display_unlock(s->display);
931        }
932        ms_filter_unlock(f);
933        return 0;
934}
935
936static int video_out_get_scalefactor(MSFilter *f,void *arg){
937        VideoOut *s=(VideoOut*)f->data;
938        *((float*)arg)=(float)s->scale_factor;
939        return 0;
940}
941
942
943static int video_out_enable_mirroring(MSFilter *f,void *arg){
944        VideoOut *s=(VideoOut*)f->data;
945        s->mirror=*(int*)arg;
946        return 0;
947}
948
949static int video_out_get_native_window_id(MSFilter *f, void*arg){
950        VideoOut *s=(VideoOut*)f->data;
951        unsigned long *id=(unsigned long*)arg;
952        *id=0;
953        if (s->display){
954                *id=s->display->window_id;
955                return 0;
956        }
957        return -1;
958}
959
960static int video_out_set_selfview_pos(MSFilter *f,void *arg){
961        VideoOut *s=(VideoOut*)f->data;
962        s->sv_posx=((float*)arg)[0];
963        s->sv_posy=((float*)arg)[1];
964        s->scale_factor=(float)100.0/((float*)arg)[2];
965        return 0;
966}
967
968static int video_out_get_selfview_pos(MSFilter *f,void *arg){
969        VideoOut *s=(VideoOut*)f->data;
970        ((float*)arg)[0]=s->sv_posx;
971        ((float*)arg)[1]=s->sv_posy;
972        ((float*)arg)[2]=(float)100.0/s->scale_factor;
973        return 0;
974}
975
976static int video_out_set_background_color(MSFilter *f,void *arg){
977        VideoOut *s=(VideoOut*)f->data;
978        s->background_color[0]=((int*)arg)[0];
979        s->background_color[1]=((int*)arg)[1];
980        s->background_color[2]=((int*)arg)[2];
981        return 0;
982}
983
984static int video_out_get_background_color(MSFilter *f,void *arg){
985        VideoOut *s=(VideoOut*)f->data;
986        ((int*)arg)[0]=s->background_color[0];
987        ((int*)arg)[1]=s->background_color[1];
988        ((int*)arg)[2]=s->background_color[2];
989        return 0;
990}
991
992static MSFilterMethod methods[]={
993        {       MS_FILTER_SET_VIDEO_SIZE        ,       video_out_set_vsize },
994        {       MS_VIDEO_OUT_SET_DISPLAY        ,       video_out_set_display},
995        {       MS_VIDEO_OUT_SET_CORNER         ,       video_out_set_corner},
996        {       MS_VIDEO_OUT_AUTO_FIT           ,       video_out_auto_fit},
997        {       MS_VIDEO_OUT_HANDLE_RESIZING    ,       video_out_handle_resizing},
998        {       MS_VIDEO_OUT_ENABLE_MIRRORING   ,       video_out_enable_mirroring},
999        {       MS_VIDEO_OUT_GET_NATIVE_WINDOW_ID,      video_out_get_native_window_id},
1000        {       MS_VIDEO_OUT_GET_CORNER         ,       video_out_get_corner},
1001        {       MS_VIDEO_OUT_SET_SCALE_FACTOR   ,       video_out_set_scalefactor},
1002        {       MS_VIDEO_OUT_GET_SCALE_FACTOR   ,       video_out_get_scalefactor},
1003        {       MS_VIDEO_OUT_SET_SELFVIEW_POS    ,      video_out_set_selfview_pos},
1004        {       MS_VIDEO_OUT_GET_SELFVIEW_POS    ,  video_out_get_selfview_pos},
1005        {       MS_VIDEO_OUT_SET_BACKGROUND_COLOR    ,  video_out_set_background_color},
1006        {       MS_VIDEO_OUT_GET_BACKGROUND_COLOR    ,  video_out_get_background_color},
1007/* methods for compatibility with the MSVideoDisplay interface*/
1008        {       MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE , video_out_set_corner },
1009        {       MS_VIDEO_DISPLAY_ENABLE_AUTOFIT                 , video_out_auto_fit },
1010        {       MS_VIDEO_DISPLAY_ENABLE_MIRRORING               , video_out_enable_mirroring },
1011        {       MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID   , video_out_get_native_window_id },
1012        {       MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_SCALEFACTOR     , video_out_set_scalefactor },
1013        {       MS_VIDEO_DISPLAY_SET_BACKGROUND_COLOR    ,  video_out_set_background_color},
1014       
1015        {       0       ,NULL}
1016};
1017
1018MSFilterDesc ms_video_out_desc={
1019        .id=MS_VIDEO_OUT_ID,
1020        .name="MSVideoOut",
1021        .text=N_("A SDL-based video display"),
1022        .category=MS_FILTER_OTHER,
1023        .ninputs=2,
1024        .noutputs=0,
1025        .init=video_out_init,
1026        .preprocess=video_out_preprocess,
1027        .process=video_out_process,
1028        .postprocess=video_out_postprocess,
1029        .uninit=video_out_uninit,
1030        .methods=methods
1031};
1032
1033
1034MS_FILTER_DESC_EXPORT(ms_video_out_desc)
1035
Note: See TracBrowser for help on using the repository browser.