summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/qt7/videowidget.mm
blob: c281e1622f2cd6d88466899590d69c40678f9e59 (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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/*  This file is part of the KDE project.

    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 or 3 of the License.

    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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtCore/qglobal.h>
#ifdef QT_MAC_USE_COCOA
#import <QTKit/QTMovieLayer.h>
#endif

#include "videowidget.h"
#include "backendheader.h"
#include "quicktimevideoplayer.h"
#include "medianode.h"
#include "medianodeevent.h"
#include "mediaobject.h"

#include <QtOpenGL/QGLWidget>
#include <QtCore/QTime>
#include <QtCore/QEvent>
#include <QtCore/QCoreApplication>

#import <AppKit/NSImage.h>
#import <QTKit/QTMovieView.h>

/////////////////////////////////////////////////////////////////////////////////////////

#ifdef QT_MAC_USE_COCOA // Rendering to a QTMovieView can only be done in Cocoa

#define VIDEO_TRANSPARENT(m) -(void)m:(NSEvent *)e{[[self superview] m:e];}

@interface SharedQTMovieView : QTMovieView
{
@private
    Phonon::QT7::QuickTimeVideoPlayer *m_player;
    QList<QWidget *> *m_parents;
    QWidget *m_window;
    QRect *m_drawRect;
    bool m_newImageReady;
    bool m_usingWindow;
}

- (SharedQTMovieView *) init;
- (void) registerParent:(QWidget *)parent;
- (void) unregisterParent:(QWidget *)parent;
- (void) setDrawRect:(QRect &)rect;
- (void) drawVideoFrame:(Phonon::QT7::VideoFrame &)frame forWidget:(QWidget *)widget shareImages:(bool)share;
- (void) useOffscreenWindow:(bool)offscreen;
- (void) applyDrawRectOnSelf;
@end

/////////////////////////////////////////////////////////////////////////////////////////

@implementation SharedQTMovieView

- (SharedQTMovieView *) init
{
    self = [super initWithFrame:NSZeroRect];
	if (self){
        [self setControllerVisible:NO];
        m_parents = new QList<QWidget *>();
        m_drawRect = new QRect(0, 0, 1, 1);
        [self applyDrawRectOnSelf];
        m_usingWindow = false;
	}
	return self;
}

- (void) dealloc
{
    Phonon::QT7::PhononAutoReleasePool pool;
    delete m_window;
    delete m_drawRect;
    delete m_parents;
    [super dealloc];
}

- (void) applyDrawRectOnSelf
{
    NSRect nsrect;
    nsrect.origin.x = m_drawRect->x();
    nsrect.origin.y = m_drawRect->y();
    nsrect.size.width = m_drawRect->width();
    nsrect.size.height = m_drawRect->height();
    [self setFrame:nsrect];
}

- (void) setDrawRect:(QRect &)rect
{
    *m_drawRect = rect;
    if (!m_usingWindow)
        [self applyDrawRectOnSelf];
}

- (void) waitForFrame
{
    if (m_usingWindow){
        QTMovie *movie = [self movie];
        if (movie){
            // CIImages are expected, but not received.
            // Try to wait a couple of seconds for them:
            m_newImageReady = false;
            float rate = [movie rate];
            if (!rate)
                [movie setRate:1];
            QTime t; t.start();
            while (!m_newImageReady && t.elapsed() < 2000)
                ;
            [movie setRate:rate];
        }
    }
}

- (void) useOffscreenWindow:(bool)offscreen
{
    if (offscreen == m_usingWindow)
        return;
    if (offscreen){
        if (!m_window){
            m_window = new QWidget;
            m_window->setWindowOpacity(0.0);
            m_window->show();
            m_window->hide();
        }
        m_usingWindow = true;
        [self setDelegate:self];
        [self waitForFrame];
        foreach(QWidget *w, *m_parents)
            w->repaint();
        qApp->processEvents();
        [self removeFromSuperview];
        [(NSView *)m_window->winId() addSubview:self];
    } else if (!m_parents->isEmpty()){
        m_usingWindow = false;
        [self removeFromSuperview];
        [(NSView*)m_parents->first()->winId() addSubview:self];
        [self setDelegate:0];
        [self setDrawRect:*m_drawRect];
    }
}

- (void) drawVideoFrame:(Phonon::QT7::VideoFrame &)frame forWidget:(QWidget *)widget shareImages:(bool)share;
{
    // Detect if the video that produces the frame has changed:
    Phonon::QT7::QuickTimeVideoPlayer *player = frame.videoPlayer();
    if (player && player->qtMovie() != [self movie]){
        m_player = player;
        [self setMovie:player->qtMovie()];
        [self waitForFrame];
    }

    [self useOffscreenWindow:(share || m_parents->size() > 1)];
    if (m_usingWindow)
        widget->update();
}

// Override this method so that the movie doesn't stop if
// the window becomes invisible
- (void)viewWillMoveToWindow:(NSWindow *)newWindow
{
    Q_UNUSED(newWindow);
}

- (CIImage *) view:(QTMovieView *)view willDisplayImage:(CIImage *)img
{
	// This method is called from QTMovieView just
	// before the image will be drawn.
    Q_UNUSED(view);
    m_player->setPrimaryRenderingCIImage(img);
    m_newImageReady = true;
	return img;
}

- (void) registerParent:(QWidget *)parent
{
    if (m_parents->contains(parent))
        return;
    m_parents->append(parent);
    if (m_parents->size() == 1){
        Phonon::QT7::PhononAutoReleasePool pool;
        m_usingWindow = true;
        [self applyDrawRectOnSelf];
        [self useOffscreenWindow:NO];
    }
}

- (void) unregisterParent:(QWidget *)parent
{
    m_parents->removeAll(parent);
    if (m_parents->size() == 1)
        [self applyDrawRectOnSelf];
}

VIDEO_TRANSPARENT(mouseDown);
VIDEO_TRANSPARENT(mouseDragged);
VIDEO_TRANSPARENT(mouseUp);
VIDEO_TRANSPARENT(mouseMoved);
VIDEO_TRANSPARENT(mouseEntered);
VIDEO_TRANSPARENT(mouseExited);
VIDEO_TRANSPARENT(rightMouseDown);
VIDEO_TRANSPARENT(rightMouseDragged);
VIDEO_TRANSPARENT(rightMouseUp);
VIDEO_TRANSPARENT(otherMouseDown);
VIDEO_TRANSPARENT(otherMouseDragged);
VIDEO_TRANSPARENT(otherMouseUp);
VIDEO_TRANSPARENT(keyDown);
VIDEO_TRANSPARENT(keyUp);
VIDEO_TRANSPARENT(scrollWheel)

@end

#endif // QT_MAC_USE_COCOA

/////////////////////////////////////////////////////////////////////////////////////////

QT_BEGIN_NAMESPACE

namespace Phonon
{
namespace QT7
{

class IVideoRenderDrawWidget
{
public:
	virtual ~IVideoRenderDrawWidget(){}
	virtual void setVideoFrame(VideoFrame &) = 0;
	virtual void setDrawFrameRect(const QRect &) = 0;
	virtual void updateVideoOutputCount(int){}
	virtual void setMovieIsPaused(bool){}
};

/////////////////////////////////////////////////////////////////////////////////////////

QGLWidget *PhononSharedQGLWidget(){
	static QGLWidget *glWidget = 0;
	if (!glWidget)
		glWidget = new QGLWidget();
	return glWidget;
}

/////////////////////////////////////////////////////////////////////////////////////////

class RenderOpenGL : public QGLWidget, public IVideoRenderDrawWidget
{
public:
    VideoFrame m_currentFrame;
    QRect m_drawFrameRect;

    RenderOpenGL(QWidget *parent, const QGLFormat &format, const QSize &size) : QGLWidget(format, parent, PhononSharedQGLWidget())
    {
        resize(size);
        setAutoFillBackground(false);
        show();
    }

    void initializeGL()
    {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    void resizeGL(int w, int h)
    {
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, GLsizei(w), GLsizei(h));
        glOrtho(0, GLsizei(w), 0, GLsizei(h), -1, 1);
        updateGL();
    }

    void paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        m_currentFrame.drawCVTexture(m_drawFrameRect);
    }

    void setVideoFrame(VideoFrame &frame)
    {
        m_currentFrame = frame;
        makeCurrent();
        paintGL();
        swapBuffers();
    }

    void setDrawFrameRect(const QRect &rect)
    {
        m_drawFrameRect = rect;
    }
};

/////////////////////////////////////////////////////////////////////////////////////////

class RenderQTMovieView : public QWidget, public IVideoRenderDrawWidget
{
public:
#if defined(QT_MAC_USE_COCOA)
	QRect m_drawRect;
	VideoFrame m_videoFrame;
    SharedQTMovieView *m_currentView;
    bool m_setDrawRectPending;
    bool m_share;

    RenderQTMovieView(bool share, QWidget *parent, const QSize &size=QSize()) : QWidget(parent), m_currentView(0)
    {
        m_setDrawRectPending = true;
        m_share = share;
        setAutoFillBackground(false);
        if (share){
            // In 'share' mode, this widget will only make sure
            // that CIIImages are produced, and not actually
            // draw anything:
            hide();
        } else {
            resize(size);
            show();
        }
    }

    ~RenderQTMovieView()
    {
        [m_currentView unregisterParent:this];
    }

    void showEvent(QShowEvent *)
    {
        if (m_share)
            return;
        [m_currentView registerParent:this];
    }

    void hideEvent(QHideEvent *)
    {
        if (m_share)
            return;
        [m_currentView unregisterParent:this];
    }

    void paintEvent(QPaintEvent *)
    {
        if (m_share)
            return;
		QPainter p(this);
		p.fillRect(rect(), Qt::black);
        m_videoFrame.drawCIImage(m_drawRect);
    }

	void updateVideoOutputCount(int count)
	{
		Q_UNUSED(count);
	}

	void setMovieIsPaused(bool paused)
	{
        Q_UNUSED(paused);
	}

    void setVideoFrame(VideoFrame &frame)
    {
        m_videoFrame = frame;

        if (!m_videoFrame.isEmpty()){
            Phonon::QT7::QuickTimeVideoPlayer *player = m_videoFrame.videoPlayer();
            if (!player->m_primaryRenderingTarget){
                // First movie view. Create the shared resource:
                SharedQTMovieView *view = [[[SharedQTMovieView alloc] init] autorelease];
                player->setPrimaryRenderingTarget(view);
            }

            SharedQTMovieView *view = static_cast<SharedQTMovieView *>(player->m_primaryRenderingTarget);
            if (!m_share && view != m_currentView){
                [m_currentView unregisterParent:this];
                m_currentView = view;
                [m_currentView registerParent:this];
            }

            [view drawVideoFrame:m_videoFrame forWidget:this shareImages:m_share || m_videoFrame.hasColorAdjustments()];

            if (m_setDrawRectPending){
                m_setDrawRectPending = false;
                [view setDrawRect:m_drawRect];
            }
        }
    }

    void setDrawFrameRect(const QRect &rect)
    {
        m_drawRect = rect;
        Phonon::QT7::QuickTimeVideoPlayer *player = m_videoFrame.videoPlayer();
        if (player && player->m_primaryRenderingTarget){
            SharedQTMovieView *view = static_cast<SharedQTMovieView *>(player->m_primaryRenderingTarget);
            [view setDrawRect:m_drawRect];
        } else
            m_setDrawRectPending = true;
    }

#else // QT_MAC_USE_COCOA == false
	RenderQTMovieView(bool, QWidget *, const QSize& = QSize()){}
	void setVideoFrame(VideoFrame &){}
	void setDrawFrameRect(const QRect &){}
#endif
};

/////////////////////////////////////////////////////////////////////////////////////////

class RenderQTMovieLayer : public QWidget, public IVideoRenderDrawWidget
{
public:
#ifdef QT_MAC_USE_COCOA
    QTMovieLayer *m_movieLayer;

    RenderQTMovieLayer(QWidget *parent, const QSize&) : QWidget(parent)
    {
		PhononAutoReleasePool pool;
        setAutoFillBackground(false);
        m_movieLayer = 0;
        [(NSView *)winId() setWantsLayer:YES];
    }

    void setVideoFrame(VideoFrame &frame)
    {
        QuickTimeVideoPlayer *player = frame.videoPlayer();
        if (!player || player->qtMovie() == [m_movieLayer movie])
            return;

        if (m_movieLayer)
            [m_movieLayer setMovie:player->qtMovie()];
        else {
            m_movieLayer = [QTMovieLayer layerWithMovie:player->qtMovie()];
            [(NSView *)winId() setLayer:m_movieLayer];
        }
    }

    void setDrawFrameRect(const QRect &rect)
    {
        m_movieLayer.frame.origin.x = rect.x();
        m_movieLayer.frame.origin.y = rect.y();
        m_movieLayer.frame.size.width = rect.width();
        m_movieLayer.frame.size.height = rect.height();
    }

#else // QT_MAC_USE_COCOA == false
	RenderQTMovieLayer(QWidget *, const QSize&){}
	void setVideoFrame(VideoFrame &){}
	void setDrawFrameRect(const QRect &){}
#endif
};

/////////////////////////////////////////////////////////////////////////////////////////

class VideoRenderWidget : public QWidget
{
public:
    enum RenderSystem {	RS_NoRendering	= 0,
						RS_QGLWidget	= 1,
						RS_QPainter		= 2,
						RS_CIImage		= 3,
						RS_CVTexture	= 4,
						RS_QImage		= 5,
						RS_QTMovieView	= 6,
						RS_QTMovieLayer = 7
	} m_renderSystem;

    VideoFrame m_currentFrame;
    QRect m_movieFrameRect;
    QRect m_drawFrameRect;
    Phonon::VideoWidget::ScaleMode m_scaleMode;
    Phonon::VideoWidget::AspectRatio m_aspect;
	IVideoRenderDrawWidget *m_renderDrawWidget;

    qreal m_brightness;
    qreal m_contrast;
    qreal m_hue;
    qreal m_saturation;
    qreal m_opacity;

    VideoRenderWidget() : QWidget(0),
        m_scaleMode(Phonon::VideoWidget::FitInView), m_aspect(Phonon::VideoWidget::AspectRatioAuto)
    {
		PhononAutoReleasePool pool;
        m_brightness = 0;
        m_contrast = 0;
        m_hue = 0;
        m_saturation = 0;
        m_opacity = 1;
		m_renderDrawWidget = 0;
		m_renderSystem = RS_NoRendering;

        setAutoFillBackground(false);
        updateDrawFrameRect();
    }

    RenderSystem selectBestRenderSystem(){
        if (!isVisible())
            return RS_NoRendering;
        else if (window() && window()->testAttribute(Qt::WA_DontShowOnScreen))
            return RS_QPainter;
        else {
#ifdef QUICKTIME_C_API_AVAILABLE
            return RS_QGLWidget;
#else
            return RS_QTMovieView;
#endif
        }
    }

    void setRenderSystem(RenderSystem renderSystem){
		PhononAutoReleasePool pool;
		static QString userSystem = qgetenv("PHONON_RENDER_SYSTEM");
	    if (!userSystem.isEmpty())
			renderSystem = RenderSystem(userSystem.toInt());

        if (m_renderSystem == renderSystem)
            return;

        m_renderSystem = renderSystem;
        if (m_renderDrawWidget){
            delete m_renderDrawWidget;
			m_renderDrawWidget = 0;
		}

        switch (m_renderSystem){
            case RS_QGLWidget:{
			    QGLFormat format = QGLFormat::defaultFormat();
			    format.setSwapInterval(1); // Vertical sync (avoid tearing)
			    m_renderDrawWidget = new RenderOpenGL(this, format, size());
                break;}
            case RS_QTMovieView:{
			    m_renderDrawWidget = new RenderQTMovieView(false, this, size());
                break;}
            case RS_QTMovieLayer:{
			    m_renderDrawWidget = new RenderQTMovieLayer(this, size());
				break;}
            case RS_QPainter:
			case RS_CIImage:
			case RS_CVTexture:
			case RS_QImage:
#ifndef QUICKTIME_C_API_AVAILABLE
                // On cocoa-64, let QTMovieView produce
                // video frames for us:
				m_renderDrawWidget = new RenderQTMovieView(true, this);
#endif
				break;
            case RS_NoRendering:
                break;
        }

		if (m_renderDrawWidget){
            m_renderDrawWidget->setVideoFrame(m_currentFrame);
            m_renderDrawWidget->setDrawFrameRect(m_drawFrameRect);
        }
    }

    QSize sizeHint() const
    {
        return m_movieFrameRect.size();
    }

    bool event(QEvent *event)
    {
        switch (event->type()){
            // Try to detect if one of this objects
            // anchestors might have changed:
            case QEvent::Resize:{
                PhononAutoReleasePool pool;
                updateDrawFrameRect();
                if (m_renderDrawWidget)
                    dynamic_cast<QWidget *>(m_renderDrawWidget)->resize(size());
                break; }
            case QEvent::Paint:{
                PhononAutoReleasePool pool;
                float opacity = parentWidget() ? parentWidget()->windowOpacity() : 1;
                switch (m_renderSystem){
                    case RS_QPainter:{
                        QPainter p(this);
                        p.fillRect(rect(), Qt::black);
                        if (p.paintEngine()->type() == QPaintEngine::OpenGL)
                            m_currentFrame.drawCVTexture(m_drawFrameRect, opacity);
                        else
							m_currentFrame.drawQImage(&p, m_drawFrameRect);
                        break; }
					case RS_CIImage:
                        m_currentFrame.drawCIImage(m_drawFrameRect, opacity);
						break;
                    case RS_CVTexture:
	                   m_currentFrame.drawCVTexture(m_drawFrameRect, opacity);
					   break;
					case RS_QImage:{
                        QPainter p(this);
                        p.fillRect(rect(), Qt::black);
						m_currentFrame.drawQImage(&p, m_drawFrameRect);
                        break; }
                    case RS_QGLWidget:
                    case RS_QTMovieView:
                    case RS_QTMovieLayer:
                        // draw in separate widget
                        break;
                    case RS_NoRendering:
                        QPainter p(this);
                        p.fillRect(rect(), Qt::black);
                        break;
                }
                break; }
            default:
                break;
        }

        return QWidget::event(event);
    }

    void setVideoFrame(VideoFrame &frame)
    {
		PhononAutoReleasePool pool;
        m_currentFrame = frame;
        m_currentFrame.setColors(m_brightness, m_contrast, m_hue, m_saturation);

		if (m_renderDrawWidget)
			m_renderDrawWidget->setVideoFrame(m_currentFrame);

        setRenderSystem(selectBestRenderSystem());
        switch (m_renderSystem){
            case RS_QGLWidget:
            case RS_QTMovieView:
            case RS_QTMovieLayer:
            case RS_NoRendering:
                break;
			case RS_CIImage:
			case RS_CVTexture:
			case RS_QImage:
            case RS_QPainter:
                repaint();
                break;
        }
    }

    void updateVideoFrame()
    {
        setVideoFrame(m_currentFrame);
    }

    void setMovieRect(const QRect &mrect)
    {
        if (mrect == m_movieFrameRect)
            return;
        m_movieFrameRect = mrect;
        updateDrawFrameRect();
        updateGeometry();
        if (isVisible())
            qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
    }

    void setScaleMode(Phonon::VideoWidget::ScaleMode scaleMode)
    {
        m_scaleMode = scaleMode;
        updateDrawFrameRect();
        updateVideoFrame();
        repaint();
    }

    void setAspectRatio(Phonon::VideoWidget::AspectRatio aspect)
    {
        m_aspect = aspect;
        updateDrawFrameRect();
        updateVideoFrame();
        repaint();
    }

	void updateVideoOutputCount(int count)
	{
		if (m_renderDrawWidget)
			m_renderDrawWidget->updateVideoOutputCount(count);
	}

	void setMovieIsPaused(bool paused)
	{
		if (m_renderDrawWidget)
			m_renderDrawWidget->setMovieIsPaused(paused);
	}

    void updateDrawFrameRect()
    {
        if (m_movieFrameRect.width() <= 0 || m_movieFrameRect.height() <= 0)
            m_movieFrameRect = QRect(0, 0, 640, 480);

        // Set m_drawFrameRect to be the size of the smallest possible
        // rect conforming to the aspect and containing the whole frame:
        switch(m_aspect){
        case Phonon::VideoWidget::AspectRatioWidget:
            m_drawFrameRect = rect();
            break;
        case Phonon::VideoWidget::AspectRatio4_3:
            m_drawFrameRect = scaleToAspect(m_movieFrameRect, 4, 3);
            break;
        case Phonon::VideoWidget::AspectRatio16_9:
            m_drawFrameRect = scaleToAspect(m_movieFrameRect, 16, 9);
            break;
        case Phonon::VideoWidget::AspectRatioAuto:
        default:
            m_drawFrameRect = m_movieFrameRect;
            break;
        }

        // Scale m_drawFrameRect to fill the widget
        // without breaking aspect:
        int widgetWidth = rect().width();
        int widgetHeight = rect().height();
        int frameWidth = widgetWidth;
        int frameHeight = m_drawFrameRect.height() * float(widgetWidth) / float(m_drawFrameRect.width());

        switch(m_scaleMode){
        case Phonon::VideoWidget::ScaleAndCrop:
            if (frameHeight < widgetHeight){
                frameWidth *= float(widgetHeight) / float(frameHeight);
                frameHeight = widgetHeight;
            }
            break;
        case Phonon::VideoWidget::FitInView:
        default:
            if (frameHeight > widgetHeight){
                frameWidth *= float(widgetHeight) / float(frameHeight);
                frameHeight = widgetHeight;
            }
            break;
        }

        m_drawFrameRect.setSize(QSize(frameWidth, frameHeight));
        m_drawFrameRect.moveTo((widgetWidth - frameWidth) / 2.0f, (widgetHeight - frameHeight) / 2.0f);

		if (m_renderDrawWidget)
			m_renderDrawWidget->setDrawFrameRect(m_drawFrameRect);
    }

    QRect scaleToAspect(QRect srcRect, int w, int h)
    {
        int width = srcRect.width();
        int height = srcRect.width() * (float(h) / float(w));
        if (height > srcRect.height()){
            height = srcRect.height();
            width = srcRect.height() * (float(w) / float(h));
        }
        return QRect(0, 0, width, height);
    }
};

/////////////////////////////////////////////////////////////////////////////////////////

VideoWidget::VideoWidget(QObject *parent) : MediaNode(VideoSink, parent)
{
    m_videoRenderWidget = new VideoRenderWidget();
}

VideoWidget::~VideoWidget()
{
    delete m_videoRenderWidget;
}

QWidget *VideoWidget::widget()
{
    IMPLEMENTED;
    return m_videoRenderWidget;
}

Phonon::VideoWidget::AspectRatio VideoWidget::aspectRatio() const
{
    IMPLEMENTED;
    return  m_videoRenderWidget->m_aspect;
}

void VideoWidget::setAspectRatio(Phonon::VideoWidget::AspectRatio aspect)
{
    IMPLEMENTED;
    m_videoRenderWidget->setAspectRatio(aspect);
}

Phonon::VideoWidget::ScaleMode VideoWidget::scaleMode() const
{
    IMPLEMENTED;
    return m_videoRenderWidget->m_scaleMode;
}

void VideoWidget::setScaleMode(Phonon::VideoWidget::ScaleMode scaleMode)
{
    IMPLEMENTED;
    m_videoRenderWidget->setScaleMode(scaleMode);
}

qreal VideoWidget::brightness() const
{
    IMPLEMENTED;
    return m_videoRenderWidget->m_brightness;
}

void VideoWidget::setBrightness(qreal value)
{
    IMPLEMENTED;
    m_videoRenderWidget->m_brightness = value;
    if (m_owningMediaObject && m_owningMediaObject->state() == Phonon::PausedState)
        m_videoRenderWidget->updateVideoFrame();
}

qreal VideoWidget::contrast() const
{
    IMPLEMENTED;
    return m_videoRenderWidget->m_contrast;
}

void VideoWidget::setContrast(qreal value)
{
    IMPLEMENTED;
    m_videoRenderWidget->m_contrast = value;
    if (m_owningMediaObject && m_owningMediaObject->state() == Phonon::PausedState)
        m_videoRenderWidget->updateVideoFrame();
}

qreal VideoWidget::hue() const
{
    IMPLEMENTED;
    return m_videoRenderWidget->m_hue;
}

void VideoWidget::setHue(qreal value)
{
    IMPLEMENTED;
    m_videoRenderWidget->m_hue = value;
    if (m_owningMediaObject && m_owningMediaObject->state() == Phonon::PausedState)
        m_videoRenderWidget->updateVideoFrame();
}

qreal VideoWidget::saturation() const
{
    IMPLEMENTED;
    return m_videoRenderWidget->m_saturation;
}

void VideoWidget::setSaturation(qreal value)
{
    IMPLEMENTED;
    m_videoRenderWidget->m_saturation = value;
    if (m_owningMediaObject && m_owningMediaObject->state() == Phonon::PausedState)
        m_videoRenderWidget->updateVideoFrame();
}

void VideoWidget::mediaNodeEvent(const MediaNodeEvent *event)
{
    switch (event->type()){
    case MediaNodeEvent::VideoFrameSizeChanged:
        m_videoRenderWidget->setMovieRect(*static_cast<QRect *>(event->data()));
        break;
	case MediaNodeEvent::VideoOutputCountChanged:
	     m_videoRenderWidget->updateVideoOutputCount(*static_cast<int *>(event->data()));
	     break;
	case MediaNodeEvent::MediaPlaying:
	     m_videoRenderWidget->setMovieIsPaused(!(*static_cast<bool *>(event->data())));
	     break;
	default:
        break;
    }
}

void VideoWidget::updateVideo(VideoFrame &frame){
	PhononAutoReleasePool pool;
    m_videoRenderWidget->setVideoFrame(frame);
    MediaNode::updateVideo(frame);
}

}} // namespace Phonon::QT7

QT_END_NAMESPACE

#include "moc_videowidget.cpp"