qtmobility/plugins/multimedia/phonon/qphononplayercontrol.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 "qphononplayercontrol.h"
       
    43 
       
    44 #include <qmediaplaylistnavigator.h>
       
    45 
       
    46 #include <QtCore/qurl.h>
       
    47 #include <QtCore/qdebug.h>
       
    48 
       
    49 QPhononPlayerControl::QPhononPlayerControl(Phonon::MediaObject *session, QObject *parent)
       
    50    : QMediaPlayerControl(parent)
       
    51    , m_session(session)
       
    52    , m_state(QMediaPlayer::StoppedState)
       
    53    , m_mediaStatus(QMediaPlayer::NoMedia)
       
    54    , m_mediaStream(0)
       
    55 {
       
    56     m_audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
       
    57     Phonon::createPath(m_session, m_audioOutput);
       
    58 
       
    59     m_session->setTickInterval(1000);
       
    60 
       
    61     connect(m_session, SIGNAL(tick(qint64)),
       
    62             this, SIGNAL(positionChanged(qint64)));
       
    63     connect(m_session, SIGNAL(totalTimeChanged(qint64)),
       
    64             this, SIGNAL(durationChanged(qint64)));
       
    65 
       
    66     connect(m_audioOutput, SIGNAL(mutedChanged(bool)),
       
    67             this, SIGNAL(mutedChanged(bool)));
       
    68     connect(m_audioOutput, SIGNAL(volumeChanged(qreal)),
       
    69             this, SLOT(updateVolume()));
       
    70 
       
    71     connect(m_session, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
       
    72             this, SLOT(updateState(Phonon::State,Phonon::State)));
       
    73     connect(m_session, SIGNAL(hasVideoChanged(bool)),
       
    74             this, SIGNAL(videoAvailableChanged(bool)));
       
    75     connect(m_session, SIGNAL(seekableChanged(bool)),
       
    76             this, SIGNAL(seekableChanged(bool)));
       
    77     connect(m_session, SIGNAL(finished()),
       
    78             this, SLOT(processEOS()));
       
    79 }
       
    80 
       
    81 QPhononPlayerControl::~QPhononPlayerControl()
       
    82 {
       
    83 }
       
    84 
       
    85 qint64 QPhononPlayerControl::position() const
       
    86 {
       
    87     return m_session->currentTime();
       
    88 }
       
    89 
       
    90 qint64 QPhononPlayerControl::duration() const
       
    91 {
       
    92     return m_session->totalTime();
       
    93 }
       
    94 
       
    95 QMediaPlayer::State QPhononPlayerControl::state() const
       
    96 {
       
    97     return m_state;
       
    98 }
       
    99 
       
   100 QMediaPlayer::MediaStatus QPhononPlayerControl::mediaStatus() const
       
   101 {
       
   102     return m_mediaStatus;
       
   103 }
       
   104 
       
   105 int QPhononPlayerControl::bufferStatus() const
       
   106 {
       
   107     return 100;
       
   108 }
       
   109 
       
   110 int QPhononPlayerControl::volume() const
       
   111 {
       
   112     return int(m_audioOutput->volume()*100);
       
   113 }
       
   114 
       
   115 bool QPhononPlayerControl::isMuted() const
       
   116 {
       
   117     return m_audioOutput->isMuted();
       
   118 }
       
   119 
       
   120 bool QPhononPlayerControl::isSeekable() const
       
   121 {
       
   122     return m_session->isSeekable();
       
   123 }
       
   124 
       
   125 QMediaTimeRange QPhononPlayerControl::availablePlaybackRanges() const
       
   126 {
       
   127     QMediaTimeRange ranges;
       
   128 
       
   129     if(m_session->isSeekable())
       
   130         ranges.addInterval(0, m_session->totalTime());
       
   131 
       
   132     return ranges;
       
   133 }
       
   134 
       
   135 qreal QPhononPlayerControl::playbackRate() const
       
   136 {
       
   137     return 1;
       
   138 }
       
   139 
       
   140 void QPhononPlayerControl::setPlaybackRate(qreal rate)
       
   141 {
       
   142     Q_UNUSED(rate);
       
   143 }
       
   144 
       
   145 void QPhononPlayerControl::setPosition(qint64 pos)
       
   146 {
       
   147     m_session->seek(pos);
       
   148 }
       
   149 
       
   150 void QPhononPlayerControl::play()
       
   151 {
       
   152     m_state = QMediaPlayer::PlayingState;
       
   153 
       
   154     m_session->play();
       
   155 
       
   156     if (m_state == QMediaPlayer::PlayingState)
       
   157         emit stateChanged(m_state);
       
   158 
       
   159 }
       
   160 
       
   161 void QPhononPlayerControl::pause()
       
   162 {
       
   163     m_state = QMediaPlayer::PausedState;
       
   164 
       
   165     m_session->pause();
       
   166 
       
   167     if (m_state == QMediaPlayer::PausedState)
       
   168         emit stateChanged(m_state);
       
   169 }
       
   170 
       
   171 void QPhononPlayerControl::stop()
       
   172 {
       
   173     m_state = QMediaPlayer::StoppedState;
       
   174 
       
   175     m_session->stop();
       
   176 
       
   177     if (m_state == QMediaPlayer::StoppedState)
       
   178         emit stateChanged(m_state);
       
   179 }
       
   180 
       
   181 void QPhononPlayerControl::setVolume(int volume)
       
   182 {
       
   183     m_audioOutput->setVolume(volume/100.0);
       
   184 }
       
   185 
       
   186 void QPhononPlayerControl::setMuted(bool muted)
       
   187 {
       
   188     m_audioOutput->setMuted(muted);
       
   189 }
       
   190 
       
   191 QMediaContent QPhononPlayerControl::media() const
       
   192 {
       
   193     return m_resources;
       
   194 }
       
   195 
       
   196 const QIODevice *QPhononPlayerControl::mediaStream() const
       
   197 {
       
   198     return m_mediaStream;
       
   199 }
       
   200 
       
   201 void QPhononPlayerControl::setMedia(const QMediaContent &content, QIODevice *stream)
       
   202 {
       
   203     m_resources = content;
       
   204     m_mediaStream = stream;
       
   205 
       
   206     QUrl url;
       
   207 
       
   208     if (!content.isNull())
       
   209         url = content.canonicalUrl();
       
   210 
       
   211     m_session->stop();
       
   212     if (m_mediaStream)
       
   213         m_session->setCurrentSource(Phonon::MediaSource(m_mediaStream));
       
   214     else
       
   215         m_session->setCurrentSource(Phonon::MediaSource(url));
       
   216 }
       
   217 
       
   218 bool QPhononPlayerControl::isAudioAvailable() const
       
   219 {
       
   220     return true;
       
   221 }
       
   222 
       
   223 bool QPhononPlayerControl::isVideoAvailable() const
       
   224 {
       
   225     return m_session->hasVideo();
       
   226 }
       
   227 
       
   228 void QPhononPlayerControl::updateState(Phonon::State newState, Phonon::State oldState)
       
   229 {
       
   230     switch (newState) {
       
   231     case Phonon::LoadingState:
       
   232         break;
       
   233     case Phonon::StoppedState:
       
   234         m_mediaStatus = m_session->currentSource().type() != Phonon::MediaSource::Invalid
       
   235                 ? QMediaPlayer::LoadedMedia
       
   236                 : QMediaPlayer::NoMedia;
       
   237         emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   238         emit mediaStatusChanged(m_mediaStatus);
       
   239         break;
       
   240     case Phonon::PlayingState:
       
   241         m_mediaStatus = QMediaPlayer::BufferedMedia;
       
   242         emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   243         emit mediaStatusChanged(m_mediaStatus);
       
   244         break;
       
   245     case Phonon::PausedState:
       
   246         m_mediaStatus = QMediaPlayer::BufferedMedia;
       
   247         emit stateChanged(m_state = QMediaPlayer::PausedState);
       
   248         emit mediaStatusChanged(m_mediaStatus);
       
   249         break;
       
   250     case Phonon::BufferingState:
       
   251         m_mediaStatus = QMediaPlayer::BufferingMedia;
       
   252         if (oldState == Phonon::StoppedState)
       
   253             emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   254         emit mediaStatusChanged(m_mediaStatus);
       
   255         break;
       
   256     case Phonon::ErrorState:
       
   257         if (m_session->errorType() == Phonon::FatalError && m_state != QMediaPlayer::StoppedState)
       
   258             emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   259         break;
       
   260     }
       
   261 }
       
   262 
       
   263 void QPhononPlayerControl::processEOS()
       
   264 {
       
   265     m_mediaStatus = QMediaPlayer::EndOfMedia;
       
   266     emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   267     emit mediaStatusChanged(m_mediaStatus);
       
   268 }
       
   269 
       
   270 void QPhononPlayerControl::updateVolume()
       
   271 {
       
   272     emit volumeChanged(volume());
       
   273 }