qtmobility/plugins/multimedia/qt7/qt7movieviewrenderer.mm
changeset 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #import <QTKit/QTKit.h>
       
    43 
       
    44 #include "qt7backend.h"
       
    45 
       
    46 #include "qt7playercontrol.h"
       
    47 #include "qt7movieviewrenderer.h"
       
    48 #include "qt7playersession.h"
       
    49 #include <QtCore/qdebug.h>
       
    50 #include <QtCore/qcoreevent.h>
       
    51 #include <QtCore/qcoreapplication.h>
       
    52 
       
    53 #include <QtMultimedia/qabstractvideobuffer.h>
       
    54 #include <QtMultimedia/qabstractvideosurface.h>
       
    55 #include <QtMultimedia/qvideosurfaceformat.h>
       
    56 
       
    57 
       
    58 QTM_USE_NAMESPACE
       
    59 
       
    60 class NSBitmapVideoBuffer : public QAbstractVideoBuffer
       
    61 {
       
    62 public:
       
    63     NSBitmapVideoBuffer(NSBitmapImageRep *buffer)
       
    64         : QAbstractVideoBuffer(NoHandle)
       
    65         , m_buffer(buffer)
       
    66         , m_mode(NotMapped)
       
    67     {
       
    68         [m_buffer retain];
       
    69     }
       
    70 
       
    71     virtual ~NSBitmapVideoBuffer()
       
    72     {
       
    73         [m_buffer release];
       
    74     }
       
    75 
       
    76     MapMode mapMode() const { return m_mode; }
       
    77 
       
    78     uchar *map(MapMode mode, int *numBytes, int *bytesPerLine)
       
    79     {
       
    80         if (mode != NotMapped && m_mode == NotMapped) {
       
    81             if (numBytes)
       
    82                 *numBytes = [m_buffer bytesPerPlane];
       
    83 
       
    84             if (bytesPerLine)
       
    85                 *bytesPerLine = [m_buffer bytesPerRow];
       
    86 
       
    87             m_mode = mode;
       
    88 
       
    89             return [m_buffer bitmapData];
       
    90         } else {
       
    91             return 0;
       
    92         }
       
    93     }
       
    94 
       
    95     void unmap() { m_mode = NotMapped; }
       
    96 
       
    97 private:
       
    98     NSBitmapImageRep *m_buffer;
       
    99     MapMode m_mode;
       
   100 };
       
   101 
       
   102 
       
   103 #define VIDEO_TRANSPARENT(m) -(void)m:(NSEvent *)e{[[self superview] m:e];}
       
   104 
       
   105 @interface HiddenQTMovieView : QTMovieView
       
   106 {
       
   107 @private
       
   108     QWidget *m_window;
       
   109     QT7MovieViewRenderer *m_renderer;
       
   110 }
       
   111 
       
   112 - (HiddenQTMovieView *) initWithRenderer:(QT7MovieViewRenderer *)renderer;
       
   113 - (void) setRenderer:(QT7MovieViewRenderer *)renderer;
       
   114 - (void) setDrawRect:(const QRect &)rect;
       
   115 @end
       
   116 
       
   117 @implementation HiddenQTMovieView
       
   118 
       
   119 - (HiddenQTMovieView *) initWithRenderer:(QT7MovieViewRenderer *)renderer
       
   120 {
       
   121     self = [super initWithFrame:NSZeroRect];
       
   122     if (self) {
       
   123         [self setControllerVisible:NO];
       
   124         [self setDelegate:self];
       
   125 
       
   126         self->m_renderer = renderer;
       
   127 
       
   128         self->m_window = new QWidget;
       
   129         self->m_window->setWindowOpacity(0.0);
       
   130         self->m_window->show();
       
   131         self->m_window->hide();
       
   132 
       
   133         [(NSView *)(self->m_window->winId()) addSubview:self];
       
   134         [self setDrawRect:QRect(0,0,1,1)];
       
   135     }
       
   136     return self;
       
   137 }
       
   138 
       
   139 - (void) dealloc
       
   140 {
       
   141     [super dealloc];
       
   142 }
       
   143 
       
   144 - (void) setRenderer:(QT7MovieViewRenderer *)renderer
       
   145 {
       
   146     m_renderer = renderer;
       
   147 }
       
   148 
       
   149 - (void) setDrawRect:(const QRect &)rect
       
   150 {
       
   151     NSRect nsrect;
       
   152     nsrect.origin.x = rect.x();
       
   153     nsrect.origin.y = rect.y();
       
   154     nsrect.size.width = rect.width();
       
   155     nsrect.size.height = rect.height();
       
   156     [self setFrame:nsrect];
       
   157 }
       
   158 
       
   159 - (CIImage *) view:(QTMovieView *)view willDisplayImage:(CIImage *)img
       
   160 {
       
   161     // This method is called from QTMovieView just
       
   162     // before the image will be drawn.
       
   163     Q_UNUSED(view);
       
   164     if (m_renderer) {
       
   165         NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCIImage:img];
       
   166         CGRect bounds = [img extent];
       
   167         int w = bounds.size.width;
       
   168         int h = bounds.size.height;
       
   169 
       
   170         // Swap red and blue (same as QImage::rgbSwapped, but without copy)
       
   171         uchar *data = [bitmap bitmapData];
       
   172         //qDebug() << data << w << h;
       
   173         int bytesPerLine = [bitmap bytesPerRow];
       
   174         for (int i=0; i<h; ++i) {
       
   175             quint32 *p = (quint32*)data;
       
   176             data += bytesPerLine;
       
   177             quint32 *end = p + w;
       
   178             while (p < end) {
       
   179                 *p = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | (*p & 0xff00ff00);
       
   180                 p++;
       
   181             }
       
   182         }
       
   183 
       
   184         QVideoFrame frame( new NSBitmapVideoBuffer(bitmap), QSize(w,h), QVideoFrame::Format_RGB32 );
       
   185 
       
   186         //static int i=0;
       
   187         //i++;
       
   188         //QImage img([bitmap bitmapData], w, h, QImage::Format_RGB32);
       
   189         //img.save(QString("img%1.jpg").arg(i));
       
   190 
       
   191         [bitmap release];
       
   192 
       
   193         if (m_renderer)
       
   194             m_renderer->renderFrame(frame);
       
   195     }
       
   196 
       
   197     return img;
       
   198 }
       
   199 
       
   200 // Override this method so that the movie doesn't stop if
       
   201 // the window becomes invisible
       
   202 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
       
   203 {
       
   204     Q_UNUSED(newWindow);
       
   205 }
       
   206 
       
   207 
       
   208 VIDEO_TRANSPARENT(mouseDown);
       
   209 VIDEO_TRANSPARENT(mouseDragged);
       
   210 VIDEO_TRANSPARENT(mouseUp);
       
   211 VIDEO_TRANSPARENT(mouseMoved);
       
   212 VIDEO_TRANSPARENT(mouseEntered);
       
   213 VIDEO_TRANSPARENT(mouseExited);
       
   214 VIDEO_TRANSPARENT(rightMouseDown);
       
   215 VIDEO_TRANSPARENT(rightMouseDragged);
       
   216 VIDEO_TRANSPARENT(rightMouseUp);
       
   217 VIDEO_TRANSPARENT(otherMouseDown);
       
   218 VIDEO_TRANSPARENT(otherMouseDragged);
       
   219 VIDEO_TRANSPARENT(otherMouseUp);
       
   220 VIDEO_TRANSPARENT(keyDown);
       
   221 VIDEO_TRANSPARENT(keyUp);
       
   222 VIDEO_TRANSPARENT(scrollWheel)
       
   223 
       
   224 @end
       
   225 
       
   226 
       
   227 QT7MovieViewRenderer::QT7MovieViewRenderer(QObject *parent)
       
   228    :QT7VideoRendererControl(parent),
       
   229     m_movie(0),
       
   230     m_movieView(0),
       
   231     m_surface(0)
       
   232 {    
       
   233 }
       
   234 
       
   235 QT7MovieViewRenderer::~QT7MovieViewRenderer()
       
   236 {
       
   237     [(HiddenQTMovieView*)m_movieView setRenderer:0];
       
   238 
       
   239     QMutexLocker locker(&m_mutex);
       
   240     m_currentFrame = QVideoFrame();
       
   241     [(HiddenQTMovieView*)m_movieView release];
       
   242 }
       
   243 
       
   244 void QT7MovieViewRenderer::setupVideoOutput()
       
   245 {
       
   246     AutoReleasePool pool;
       
   247 
       
   248     qDebug() << "QT7MovieViewRenderer::setupVideoOutput" << m_movie << m_surface;
       
   249 
       
   250     HiddenQTMovieView *movieView = (HiddenQTMovieView*)m_movieView;
       
   251 
       
   252     if (movieView && !m_movie) {
       
   253         [movieView setMovie:nil];
       
   254     }
       
   255 
       
   256     if (m_movie) {
       
   257         NSSize size = [[(QTMovie*)m_movie attributeForKey:@"QTMovieNaturalSizeAttribute"] sizeValue];
       
   258 
       
   259         m_nativeSize = QSize(size.width, size.height);
       
   260 
       
   261         if (!movieView) {
       
   262             movieView = [[HiddenQTMovieView alloc] initWithRenderer:this];
       
   263             m_movieView = movieView;
       
   264             [movieView setControllerVisible:NO];
       
   265         }
       
   266 
       
   267         [movieView setMovie:(QTMovie*)m_movie];
       
   268         //[movieView setDrawRect:QRect(QPoint(0,0), m_nativeSize)];
       
   269     }
       
   270 
       
   271     if (m_surface && !m_nativeSize.isEmpty()) {
       
   272         QVideoSurfaceFormat format(m_nativeSize, QVideoFrame::Format_RGB32);
       
   273 
       
   274         if (m_surface->isActive() && m_surface->surfaceFormat() != format) {
       
   275             qDebug() << "Surface format was changed, stop the surface.";
       
   276             m_surface->stop();
       
   277         }
       
   278 
       
   279         if (!m_surface->isActive()) {
       
   280             qDebug() << "Starting the surface with format" << format;
       
   281             if (!m_surface->start(format))
       
   282                 qDebug() << "failed to start video surface" << m_surface->error();
       
   283         }
       
   284     }
       
   285 }
       
   286 
       
   287 void QT7MovieViewRenderer::setEnabled(bool)
       
   288 {
       
   289 }
       
   290 
       
   291 void QT7MovieViewRenderer::setMovie(void *movie)
       
   292 {
       
   293     if (movie == m_movie)
       
   294         return;
       
   295 
       
   296     QMutexLocker locker(&m_mutex);
       
   297     m_movie = movie;
       
   298     setupVideoOutput();
       
   299 }
       
   300 
       
   301 QAbstractVideoSurface *QT7MovieViewRenderer::surface() const
       
   302 {
       
   303     return m_surface;
       
   304 }
       
   305 
       
   306 void QT7MovieViewRenderer::setSurface(QAbstractVideoSurface *surface)
       
   307 {
       
   308     if (surface == m_surface)
       
   309         return;
       
   310 
       
   311     QMutexLocker locker(&m_mutex);
       
   312 
       
   313     if (m_surface && m_surface->isActive())
       
   314         m_surface->stop();
       
   315 
       
   316     m_surface = surface;
       
   317     setupVideoOutput();
       
   318 }
       
   319 
       
   320 void QT7MovieViewRenderer::renderFrame(const QVideoFrame &frame)
       
   321 {
       
   322     {
       
   323         QMutexLocker locker(&m_mutex);
       
   324         m_currentFrame = frame;
       
   325     }
       
   326 
       
   327     qApp->postEvent(this, new QEvent(QEvent::User), Qt::HighEventPriority);
       
   328 }
       
   329 
       
   330 bool QT7MovieViewRenderer::event(QEvent *event)
       
   331 {
       
   332     if (event->type() == QEvent::User) {
       
   333         QMutexLocker locker(&m_mutex);
       
   334         if (m_surface->isActive())
       
   335             m_surface->present(m_currentFrame);
       
   336     }
       
   337 
       
   338     return QT7VideoRendererControl::event(event);
       
   339 }