qtmobility/plugins/multimedia/qt7/qt7movierenderer.mm
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
--- a/qtmobility/plugins/multimedia/qt7/qt7movierenderer.mm	Fri Jun 11 14:26:25 2010 +0300
+++ b/qtmobility/plugins/multimedia/qt7/qt7movierenderer.mm	Wed Jun 23 19:08:38 2010 +0300
@@ -46,23 +46,26 @@
 #include "qt7playercontrol.h"
 #include "qt7movierenderer.h"
 #include "qt7playersession.h"
+#include "qt7ciimagevideobuffer.h"
 #include "qcvdisplaylink.h"
 #include <QtCore/qdebug.h>
 #include <QtCore/qcoreapplication.h>
 
 #include <QGLWidget>
 
-#include <QtMultimedia/qabstractvideobuffer.h>
-#include <QtMultimedia/qabstractvideosurface.h>
-#include <QtMultimedia/qvideosurfaceformat.h>
+#include <qabstractvideobuffer.h>
+#include <qabstractvideosurface.h>
+#include <qvideosurfaceformat.h>
 
 QT_USE_NAMESPACE
 
+//#define USE_MAIN_MONITOR_COLOR_SPACE 1
+
 class CVGLTextureVideoBuffer : public QAbstractVideoBuffer
 {
 public:
     CVGLTextureVideoBuffer(CVOpenGLTextureRef buffer)
-        : QAbstractVideoBuffer(NoHandle)
+        : QAbstractVideoBuffer(GLTextureHandle)
         , m_buffer(buffer)
         , m_mode(NotMapped)
     {
@@ -80,11 +83,6 @@
         return QVariant(int(id));
     }
 
-    HandleType handleType() const
-    {
-        return GLTextureHandle;
-    }
-
     MapMode mapMode() const { return m_mode; }
 
     uchar *map(MapMode mode, int *numBytes, int *bytesPerLine)
@@ -234,8 +232,23 @@
                                                                              &kCFTypeDictionaryValueCallBacks);
     CFDictionarySetValue(visualContextOptions, kQTVisualContextPixelBufferAttributesKey, pixelBufferOptions);
 
-    CFDictionarySetValue(visualContextOptions, kQTVisualContextWorkingColorSpaceKey, CGColorSpaceCreateDeviceRGB());
-    CFDictionarySetValue(visualContextOptions, kQTVisualContextOutputColorSpaceKey, CGColorSpaceCreateDeviceRGB());
+    CGColorSpaceRef colorSpace = NULL;
+
+#if USE_MAIN_MONITOR_COLOR_SPACE
+    CMProfileRef sysprof = NULL;
+
+    // Get the Systems Profile for the main display
+    if (CMGetSystemProfile(&sysprof) == noErr) {
+        // Create a colorspace with the systems profile
+        colorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysprof);
+        CMCloseProfile(sysprof);
+    }
+#endif
+
+    if (!colorSpace)
+        colorSpace = CGColorSpaceCreateDeviceRGB();
+
+    CFDictionarySetValue(visualContextOptions, kQTVisualContextOutputColorSpaceKey, colorSpace);
 
     OSStatus err = QTPixelBufferContextCreate(kCFAllocatorDefault,
                                                visualContextOptions,
@@ -271,7 +284,7 @@
         return;
     }
 
-    NSSize size = [[(QTMovie*)m_movie attributeForKey:@"QTMovieCurrentSizeAttribute"] sizeValue];
+    NSSize size = [[(QTMovie*)m_movie attributeForKey:@"QTMovieNaturalSizeAttribute"] sizeValue];
     m_nativeSize = QSize(size.width, size.height);
 
 #ifdef QUICKTIME_C_API_AVAILABLE
@@ -288,9 +301,9 @@
             if (m_surface->isActive())
                 m_surface->stop();
 
-            qDebug() << "Starting the surface with format" << format;
             if (!m_surface->start(format)) {
-                qDebug() << "failed to start video surface" << m_surface->error();
+                qWarning() << "failed to start video surface" << m_surface->error();
+                qWarning() << "Surface format:" << format;
                 glSupported = false;
             } else {
                 m_usingGLContext = true;
@@ -309,8 +322,10 @@
 
             if (!m_surface->isActive()) {
                 qDebug() << "Starting the surface with format" << format;
-                if (!m_surface->start(format))
-                    qDebug() << "failed to start video surface" << m_surface->error();
+                if (!m_surface->start(format)) {
+                    qWarning() << "failed to start video surface" << m_surface->error();
+                    qWarning() << "Surface format:" << format;
+                }
             }
         }
     }
@@ -322,57 +337,66 @@
             (m_usingGLContext && (m_currentGLContext != QGLContext::currentContext())) ||
             (!m_usingGLContext && (m_pixelBufferContextGeometry != m_nativeSize))) {
             QTVisualContextRelease(m_visualContext);
+            m_pixelBufferContextGeometry = QSize();
             m_visualContext = 0;
         }
     }
 
-    if (!m_visualContext) {
-        if (m_usingGLContext) {
-            qDebug() << "Building OpenGL visual context";
-            m_currentGLContext = QGLContext::currentContext();
-            if (!createGLVisualContext()) {
-                qWarning() << "QT7MovieRenderer: failed to create visual context";
-                return;
-            }
-        } else {
-            qDebug() << "Building Pixel Buffer visual context";
-            if (!createPixelBufferVisualContext()) {
-                qWarning() << "QT7MovieRenderer: failed to create visual context";
-                return;
+    if (!m_nativeSize.isEmpty()) {
+        if (!m_visualContext) {
+            if (m_usingGLContext) {
+                qDebug() << "Building OpenGL visual context" << m_nativeSize;
+                m_currentGLContext = QGLContext::currentContext();
+                if (!createGLVisualContext()) {
+                    qWarning() << "QT7MovieRenderer: failed to create visual context";
+                    return;
+                }
+            } else {
+                qDebug() << "Building Pixel Buffer visual context" << m_nativeSize;
+                if (!createPixelBufferVisualContext()) {
+                    qWarning() << "QT7MovieRenderer: failed to create visual context";
+                    return;
+                }
             }
         }
-    }
 
-    // targets a Movie to render into a visual context
-    SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], m_visualContext);
+        // targets a Movie to render into a visual context
+        SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], m_visualContext);
 
-
+        m_displayLink->start();
+    }
 #endif
 
-    m_displayLink->start();
-}
-
-void QT7MovieRenderer::setEnabled(bool)
-{
 }
 
 void QT7MovieRenderer::setMovie(void *movie)
 {
-    qDebug() << "QT7MovieRenderer::setMovie" << movie;
-
-    if (m_movie == movie)
-        return;
-
-    QMutexLocker locker(&m_mutex);
+    //qDebug() << "QT7MovieRenderer::setMovie" << movie;
 
 #ifdef QUICKTIME_C_API_AVAILABLE
-    //ensure the old movie doesn't hold the visual context, otherwise it can't be reused
-    if (m_movie && m_visualContext)
-        SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], 0);
+    QMutexLocker locker(&m_mutex);
+
+    if (m_movie != movie) {
+        if (m_movie) {
+            //ensure the old movie doesn't hold the visual context, otherwise it can't be reused
+            SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], nil);
+            [(QTMovie*)m_movie release];
+        }
+
+        m_movie = movie;
+        [(QTMovie*)m_movie retain];
+
+        setupVideoOutput();
+    }
 #endif
+}
 
-    m_movie = movie;
-    setupVideoOutput();
+void QT7MovieRenderer::updateNaturalSize(const QSize &newSize)
+{
+    if (m_nativeSize != newSize) {
+        m_nativeSize = newSize;
+        setupVideoOutput();
+    }
 }
 
 QAbstractVideoSurface *QT7MovieRenderer::surface() const
@@ -382,7 +406,7 @@
 
 void QT7MovieRenderer::setSurface(QAbstractVideoSurface *surface)
 {
-    qDebug() << "Set video surface" << surface;
+    //qDebug() << "Set video surface" << surface;
 
     if (surface == m_surface)
         return;
@@ -416,19 +440,18 @@
         OSStatus status = QTVisualContextCopyImageForTime(m_visualContext, NULL, &ts, &imageBuffer);
 
         if (status == noErr && imageBuffer) {
-            //qDebug() << "render video frame";
             QAbstractVideoBuffer *buffer = 0;
 
             if (m_usingGLContext) {
-                buffer = new CVGLTextureVideoBuffer((CVOpenGLTextureRef)imageBuffer);
+                buffer = new QT7CIImageVideoBuffer([CIImage imageWithCVImageBuffer:imageBuffer]);
                 CVOpenGLTextureRelease((CVOpenGLTextureRef)imageBuffer);
-                //qDebug() << "render GL video frame" << buffer->handle();
             } else {
                 buffer = new CVPixelBufferVideoBuffer((CVPixelBufferRef)imageBuffer);
+                //buffer = new QT7CIImageVideoBuffer( [CIImage imageWithCVImageBuffer:imageBuffer] );
                 CVPixelBufferRelease((CVPixelBufferRef)imageBuffer);
             }
 
-            QVideoFrame frame(buffer, m_nativeSize, QVideoFrame::Format_RGB32);            
+            QVideoFrame frame(buffer, m_nativeSize, QVideoFrame::Format_RGB32);
             m_surface->present(frame);
             QTVisualContextTask(m_visualContext);
         }