qtmobility/src/multimedia/qmlgraphicsvideo.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     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 #include "qmlgraphicsvideo_p.h"
       
    43 
       
    44 #include <qmediaplayercontrol.h>
       
    45 #include <qmediaservice.h>
       
    46 #include <qpaintervideosurface_p.h>
       
    47 #include <qvideooutputcontrol.h>
       
    48 #include <qvideorenderercontrol.h>
       
    49 
       
    50 QML_DEFINE_TYPE(Qt,4,6,Video,QTM_PREPEND_NAMESPACE(QmlGraphicsVideo));
       
    51 
       
    52 QTM_BEGIN_NAMESPACE
       
    53 
       
    54 void QmlGraphicsVideo::_q_nativeSizeChanged(const QSizeF &size)
       
    55 {
       
    56     setImplicitWidth(size.width());
       
    57     setImplicitHeight(size.height());
       
    58 }
       
    59 
       
    60 void QmlGraphicsVideo::_q_error(QMediaPlayer::Error errorCode, const QString &errorString)
       
    61 {
       
    62     m_error = errorCode;
       
    63     m_errorString = errorString;
       
    64 
       
    65     emit error(Error(errorCode), errorString);
       
    66     emit errorChanged();
       
    67 }
       
    68 
       
    69 
       
    70 /*!
       
    71     \qmlclass Video QmlGraphicsVideo
       
    72     \brief The Video element allows you to add videos to a scene.
       
    73     \inherits Item
       
    74 */
       
    75 
       
    76 /*!
       
    77     \internal
       
    78     \class QmlGraphicsVideo
       
    79     \brief The QmlGraphicsVideo class provides a video item that you can add to a QmlView.
       
    80 */
       
    81 
       
    82 QmlGraphicsVideo::QmlGraphicsVideo(QmlGraphicsItem *parent)
       
    83     : QmlGraphicsItem(parent)
       
    84     , m_graphicsItem(0)
       
    85     , m_fillMode(QmlGraphicsVideo::PreserveAspectFit)
       
    86 
       
    87 {
       
    88     m_graphicsItem = new QGraphicsVideoItem(this);
       
    89     connect(m_graphicsItem, SIGNAL(nativeSizeChanged(QSizeF)),
       
    90             this, SLOT(_q_nativeSizeChanged(QSizeF)));
       
    91 
       
    92     setObject(this);
       
    93 
       
    94     if (m_mediaService) {
       
    95         connect(m_playerControl, SIGNAL(audioAvailableChanged(bool)),
       
    96                 this, SIGNAL(hasAudioChanged()));
       
    97         connect(m_playerControl, SIGNAL(videoAvailableChanged(bool)),
       
    98                 this, SIGNAL(hasVideoChanged()));
       
    99 
       
   100         m_graphicsItem->setMediaObject(m_mediaObject);
       
   101     }
       
   102 }
       
   103 
       
   104 QmlGraphicsVideo::~QmlGraphicsVideo()
       
   105 {
       
   106     shutdown();
       
   107 
       
   108     delete m_graphicsItem;
       
   109 }
       
   110 
       
   111 /*!
       
   112     \qmlproperty url Video::source
       
   113 
       
   114     This property holds the source URL of the video.
       
   115 */
       
   116 
       
   117 /*!
       
   118     \qmlproperty bool Video::playing
       
   119 
       
   120     This property holds whether the video is playing.
       
   121 */
       
   122 
       
   123 /*!
       
   124     \qmlproperty bool Video::paused
       
   125 
       
   126     This property holds whether the video is paused.
       
   127 */
       
   128 
       
   129 /*!
       
   130     \qmlsignal Video::onStarted()
       
   131 
       
   132     This handler is called when playback is started.
       
   133 */
       
   134 
       
   135 /*!
       
   136     \qmlsignal Video::onResumed()
       
   137 
       
   138     This handler is called when playback is resumed from the paused state.
       
   139 */
       
   140 
       
   141 /*!
       
   142     \qmlsignal Video::onPaused()
       
   143 
       
   144     This handler is called when playback is paused.
       
   145 */
       
   146 
       
   147 /*!
       
   148     \qmlsignal Video::onStopped()
       
   149 
       
   150     This handler is called when playback is stopped.
       
   151 */
       
   152 
       
   153 /*!
       
   154     \qmlproperty enum Video::status
       
   155 
       
   156     This property holds the status of video loading. It can be one of:
       
   157 
       
   158     \list
       
   159     \o NoMedia - no video has been set.
       
   160     \o Loading - the video is currently being loaded.
       
   161     \o Loaded - the video has been loaded.
       
   162     \o Buffering - the video is buffering data.
       
   163     \o Stalled - playback has been interrupted while the video is buffering data.
       
   164     \o Buffered - the video has buffered data.
       
   165     \o EndOfMedia - the video has played to the end.
       
   166     \o InvalidMedia - the video cannot be played.
       
   167     \o UnknownStatus - the status of the video is unknown.
       
   168     \endlist
       
   169 */
       
   170 
       
   171 QmlGraphicsVideo::Status QmlGraphicsVideo::status() const
       
   172 {
       
   173     return Status(m_status);
       
   174 }
       
   175 
       
   176 /*!
       
   177     \qmlsignal Video::onLoaded()
       
   178 
       
   179     This handler is called when the video source has been loaded.
       
   180 */
       
   181 
       
   182 /*!
       
   183     \qmlsignal Video::onBuffering()
       
   184 
       
   185     This handler is called when the video stream starts buffering.
       
   186 */
       
   187 
       
   188 /*!
       
   189     \qmlsignal Video::onStalled()
       
   190 
       
   191     This handler is called when playback has stalled while the video stream buffers.
       
   192 */
       
   193 
       
   194 /*!
       
   195     \qmlsignal Video::onBuffered()
       
   196 
       
   197     This handler is called when the video stream has finished buffering.
       
   198 */
       
   199 
       
   200 /*!
       
   201     \qmlsignal Video::onEndOfMedia
       
   202 
       
   203     This handler is called when playback stops because end of the video has been reached.
       
   204 */
       
   205 
       
   206 /*!
       
   207     \qmlproperty int Video::duration
       
   208 
       
   209     This property holds the duration of the video in milliseconds.
       
   210 
       
   211     If the video doesn't have a fixed duration (a live stream for example) this will be 0.
       
   212 */
       
   213 
       
   214 /*!
       
   215     \qmlproperty int Video::position
       
   216 
       
   217     This property holds the current playback position in milliseconds.
       
   218 */
       
   219 
       
   220 /*!
       
   221     \qmlproperty qreal Video::volume
       
   222 
       
   223     This property holds the volume of the audio output, from 0.0 (silent) to 1.0 (maximum volume).
       
   224 */
       
   225 
       
   226 /*!
       
   227     \qmlproperty bool Video::muted
       
   228 
       
   229     This property holds whether the audio output is muted.
       
   230 */
       
   231 
       
   232 /*!
       
   233     \qmlproperty bool Audio::hasAudio
       
   234 
       
   235     This property holds whether the source contains audio.
       
   236 */
       
   237 
       
   238 bool QmlGraphicsVideo::hasAudio() const
       
   239 {
       
   240     return m_playerControl->isAudioAvailable();
       
   241 }
       
   242 
       
   243 /*!
       
   244     \qmlproperty bool Video::hasVideo
       
   245 
       
   246     This property holds whether the source contains video.
       
   247 */
       
   248 
       
   249 bool QmlGraphicsVideo::hasVideo() const
       
   250 {
       
   251     return m_playerControl->isVideoAvailable();
       
   252 }
       
   253 
       
   254 /*!
       
   255     \qmlproperty qreal Video::bufferProgress
       
   256 
       
   257     This property holds how much of the data buffer is currently filled, from 0.0 (empty) to 1.0
       
   258     (full).
       
   259 */
       
   260 
       
   261 /*!
       
   262     \qmlproperty bool Video::seekable
       
   263 
       
   264     This property holds whether position of the video can be changed.
       
   265 */
       
   266 
       
   267 /*!
       
   268     \qmlproperty qreal playbackRate
       
   269 
       
   270     This property holds the rate at which video is played at as a multiple of the normal rate.
       
   271 */
       
   272 
       
   273 /*!
       
   274     \qmlproperty enum Video::error
       
   275 
       
   276     This property holds the error state of the video.  It can be one of:
       
   277 
       
   278     \list
       
   279     \o NoError - there is no current error.
       
   280     \o ResourceError - the video cannot be played due to a problem allocating resources.
       
   281     \o FormatError - the video format is not supported.
       
   282     \o NetworkError - the video cannot be played due to network issues.
       
   283     \o AccessDenied - the video cannot be played due to insufficient permissions.
       
   284     \o ServiceMissing -  the video cannot be played because the media service could not be
       
   285     instantiated.
       
   286     \endlist
       
   287 */
       
   288 
       
   289 
       
   290 QmlGraphicsVideo::Error QmlGraphicsVideo::error() const
       
   291 {
       
   292     return Error(m_error);
       
   293 }
       
   294 
       
   295 /*!
       
   296     \qmlproperty string Video::errorString
       
   297 
       
   298     This property holds a string describing the current error condition in more detail.
       
   299 */
       
   300 
       
   301 /*!
       
   302     \qmlproperty Video::onError(error, errorString)
       
   303 
       
   304     This property is called when an \l {Error}{error} has occurred.  The errorString parameter
       
   305     may contain more detailed information about the error.
       
   306 */
       
   307 
       
   308 /*!
       
   309     \qmlproperty enum Video::FillMode
       
   310 
       
   311     Set this property to define how the video is scaled to fit the target area.
       
   312 
       
   313     \list
       
   314     \o Stretch - the video is scaled to fit.
       
   315     \o PreserveAspectFit - the video is scaled uniformly to fit without cropping
       
   316     \o PreserveAspectCrop - the video is scaled uniformly to fill, cropping if necessary
       
   317     \endlist
       
   318 */
       
   319 
       
   320 QmlGraphicsVideo::FillMode QmlGraphicsVideo::fillMode() const
       
   321 {
       
   322     return FillMode(m_graphicsItem->aspectRatioMode());
       
   323 }
       
   324 
       
   325 void QmlGraphicsVideo::setFillMode(FillMode mode)
       
   326 {
       
   327     m_graphicsItem->setAspectRatioMode(Qt::AspectRatioMode(mode));
       
   328 }
       
   329 
       
   330 /*!
       
   331     \qmlmethod Video::play()
       
   332 
       
   333     Starts playback of the video.
       
   334 */
       
   335 
       
   336 void QmlGraphicsVideo::play()
       
   337 {
       
   338     m_playerControl->play();
       
   339 }
       
   340 
       
   341 /*!
       
   342     \qmlmethod Video::pause()
       
   343 
       
   344     Pauses playback of the video.
       
   345 */
       
   346 
       
   347 void QmlGraphicsVideo::pause()
       
   348 {
       
   349     m_playerControl->pause();
       
   350 }
       
   351 
       
   352 /*!
       
   353     \qmlmethod Video::stop()
       
   354 
       
   355     Stops playback of the video.
       
   356 */
       
   357 
       
   358 void QmlGraphicsVideo::stop()
       
   359 {
       
   360     m_playerControl->stop();
       
   361 }
       
   362 
       
   363 void QmlGraphicsVideo::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
       
   364 {
       
   365 }
       
   366 
       
   367 void QmlGraphicsVideo::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
       
   368 {
       
   369     m_graphicsItem->setSize(newGeometry.size());
       
   370 
       
   371     QmlGraphicsItem::geometryChanged(newGeometry, oldGeometry);
       
   372 }
       
   373 
       
   374 #include "moc_qmlgraphicsvideo_p.cpp"
       
   375 
       
   376 QTM_END_NAMESPACE