qtmobility/examples/videowidget/videowidgetsurface.cpp
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 examples of the Qt Toolkit.
       
     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 #include "videowidgetsurface.h"
       
    43 
       
    44 #include <QtGui>
       
    45 #include <qabstractvideosurface.h>
       
    46 #include <qvideosurfaceformat.h>
       
    47 
       
    48 VideoWidgetSurface::VideoWidgetSurface(QWidget *widget, QObject *parent)
       
    49     : QAbstractVideoSurface(parent)
       
    50     , widget(widget)
       
    51     , imageFormat(QImage::Format_Invalid)
       
    52 {
       
    53 }
       
    54 
       
    55 //! [0]
       
    56 QList<QVideoFrame::PixelFormat> VideoWidgetSurface::supportedPixelFormats(
       
    57         QAbstractVideoBuffer::HandleType handleType) const
       
    58 {
       
    59     if (handleType == QAbstractVideoBuffer::NoHandle) {
       
    60         return QList<QVideoFrame::PixelFormat>()
       
    61                 << QVideoFrame::Format_RGB32
       
    62                 << QVideoFrame::Format_ARGB32
       
    63                 << QVideoFrame::Format_ARGB32_Premultiplied
       
    64                 << QVideoFrame::Format_RGB565
       
    65                 << QVideoFrame::Format_RGB555;
       
    66     } else {
       
    67         return QList<QVideoFrame::PixelFormat>();
       
    68     }
       
    69 }
       
    70 //! [0]
       
    71 
       
    72 //! [1]
       
    73 bool VideoWidgetSurface::isFormatSupported(
       
    74         const QVideoSurfaceFormat &format, QVideoSurfaceFormat *similar) const
       
    75 {
       
    76     Q_UNUSED(similar);
       
    77 
       
    78     const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
       
    79     const QSize size = format.frameSize();
       
    80 
       
    81     return imageFormat != QImage::Format_Invalid
       
    82             && !size.isEmpty()
       
    83             && format.handleType() == QAbstractVideoBuffer::NoHandle;
       
    84 }
       
    85 //! [1]
       
    86 
       
    87 //! [2]
       
    88 bool VideoWidgetSurface::start(const QVideoSurfaceFormat &format)
       
    89 {
       
    90     const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
       
    91     const QSize size = format.frameSize();
       
    92 
       
    93     if (imageFormat != QImage::Format_Invalid && !size.isEmpty()) {
       
    94         this->imageFormat = imageFormat;
       
    95         imageSize = size;
       
    96         sourceRect = format.viewport();
       
    97 
       
    98         QAbstractVideoSurface::start(format);
       
    99 
       
   100         widget->updateGeometry();
       
   101         updateVideoRect();
       
   102 
       
   103         return true;
       
   104     } else {
       
   105         return false;
       
   106     }
       
   107 }
       
   108 //! [2]
       
   109 
       
   110 //! [3]
       
   111 void VideoWidgetSurface::stop()
       
   112 {
       
   113     currentFrame = QVideoFrame();
       
   114     targetRect = QRect();
       
   115 
       
   116     QAbstractVideoSurface::stop();
       
   117 
       
   118     widget->update();
       
   119 }
       
   120 //! [3]
       
   121 
       
   122 //! [4]
       
   123 bool VideoWidgetSurface::present(const QVideoFrame &frame)
       
   124 {
       
   125     if (surfaceFormat().pixelFormat() != frame.pixelFormat()
       
   126             || surfaceFormat().frameSize() != frame.size()) {
       
   127         setError(IncorrectFormatError);
       
   128         stop();
       
   129 
       
   130         return false;
       
   131     } else {
       
   132         currentFrame = frame;
       
   133 
       
   134         widget->repaint(targetRect);
       
   135 
       
   136         return true;
       
   137     }
       
   138 }
       
   139 //! [4]
       
   140 
       
   141 //! [5]
       
   142 void VideoWidgetSurface::updateVideoRect()
       
   143 {
       
   144     QSize size = surfaceFormat().sizeHint();
       
   145     size.scale(widget->size().boundedTo(size), Qt::KeepAspectRatio);
       
   146 
       
   147     targetRect = QRect(QPoint(0, 0), size);
       
   148     targetRect.moveCenter(widget->rect().center());
       
   149 }
       
   150 //! [5]
       
   151 
       
   152 //! [6]
       
   153 void VideoWidgetSurface::paint(QPainter *painter)
       
   154 {
       
   155     if (currentFrame.map(QAbstractVideoBuffer::ReadOnly)) {
       
   156         const QTransform oldTransform = painter->transform();
       
   157 
       
   158         if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
       
   159            painter->scale(1, -1);
       
   160            painter->translate(0, -widget->height());
       
   161         }
       
   162 
       
   163         QImage image(
       
   164                 currentFrame.bits(),
       
   165                 currentFrame.width(),
       
   166                 currentFrame.height(),
       
   167                 currentFrame.bytesPerLine(),
       
   168                 imageFormat);
       
   169 
       
   170         painter->drawImage(targetRect, image, sourceRect);
       
   171 
       
   172         painter->setTransform(oldTransform);
       
   173 
       
   174         currentFrame.unmap();
       
   175     }
       
   176 }
       
   177 //! [6]