src/3rdparty/phonon/mmf/mediaobject.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /*  This file is part of the KDE project.
       
     2 
       
     3 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 
       
     5 This library is free software: you can redistribute it and/or modify
       
     6 it under the terms of the GNU Lesser General Public License as published by
       
     7 the Free Software Foundation, either version 2.1 or 3 of the License.
       
     8 
       
     9 This library is distributed in the hope that it will be useful,
       
    10 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 GNU Lesser General Public License for more details.
       
    13 
       
    14 You should have received a copy of the GNU Lesser General Public License
       
    15 along with this library.  If not, see <http://www.gnu.org/licenses/>.
       
    16 
       
    17 */
       
    18 
       
    19 #include "audiooutput.h"
       
    20 #include "audioplayer.h"
       
    21 #include "defs.h"
       
    22 #include "dummyplayer.h"
       
    23 #include "utils.h"
       
    24 #include "utils.h"
       
    25 #include "mmf_videoplayer.h"
       
    26 #include "videowidget.h"
       
    27 
       
    28 #include "mediaobject.h"
       
    29 
       
    30 #include <QDir>
       
    31 #include <QUrl>
       
    32 
       
    33 QT_BEGIN_NAMESPACE
       
    34 
       
    35 using namespace Phonon;
       
    36 using namespace Phonon::MMF;
       
    37 
       
    38 /*! \class MMF::MediaObject
       
    39   \internal
       
    40 */
       
    41 
       
    42 //-----------------------------------------------------------------------------
       
    43 // Constructor / destructor
       
    44 //-----------------------------------------------------------------------------
       
    45 
       
    46 MMF::MediaObject::MediaObject(QObject *parent) : MMF::MediaNode::MediaNode(parent)
       
    47                                                , m_recognizerOpened(false)
       
    48 {
       
    49     m_player.reset(new DummyPlayer());
       
    50 
       
    51     TRACE_CONTEXT(MediaObject::MediaObject, EAudioApi);
       
    52     TRACE_ENTRY_0();
       
    53 
       
    54     Q_UNUSED(parent);
       
    55 
       
    56     TRACE_EXIT_0();
       
    57 }
       
    58 
       
    59 MMF::MediaObject::~MediaObject()
       
    60 {
       
    61     TRACE_CONTEXT(MediaObject::~MediaObject, EAudioApi);
       
    62     TRACE_ENTRY_0();
       
    63 
       
    64     m_file.Close();
       
    65     m_fileServer.Close();
       
    66     m_recognizer.Close();
       
    67 
       
    68     TRACE_EXIT_0();
       
    69 }
       
    70 
       
    71 
       
    72 //-----------------------------------------------------------------------------
       
    73 // Recognizer
       
    74 //-----------------------------------------------------------------------------
       
    75 
       
    76 bool MMF::MediaObject::openRecognizer()
       
    77 {
       
    78     TRACE_CONTEXT(MediaObject::openRecognizer, EAudioInternal);
       
    79 
       
    80     if (!m_recognizerOpened) {
       
    81         TInt err = m_recognizer.Connect();
       
    82         if (KErrNone != err) {
       
    83             TRACE("RApaLsSession::Connect error %d", err);
       
    84             return false;
       
    85         }
       
    86 
       
    87         err = m_fileServer.Connect();
       
    88         if (KErrNone != err) {
       
    89             TRACE("RFs::Connect error %d", err);
       
    90             return false;
       
    91         }
       
    92 
       
    93         // This must be called in order to be able to share file handles with
       
    94         // the recognizer server (see fileMediaType function).
       
    95         err = m_fileServer.ShareProtected();
       
    96         if (KErrNone != err) {
       
    97             TRACE("RFs::ShareProtected error %d", err);
       
    98             return false;
       
    99         }
       
   100 
       
   101         m_recognizerOpened = true;
       
   102     }
       
   103 
       
   104     return true;
       
   105 }
       
   106 
       
   107 MMF::MediaType MMF::MediaObject::fileMediaType
       
   108 (const QString& fileName)
       
   109 {
       
   110     TRACE_CONTEXT(MediaObject::fileMediaType, EAudioInternal);
       
   111 
       
   112     MediaType result = MediaTypeUnknown;
       
   113 
       
   114     if (openRecognizer()) {
       
   115 
       
   116         const QHBufC fileNameSymbian(QDir::toNativeSeparators(fileName));
       
   117 
       
   118         m_file.Close();
       
   119         TInt err = m_file.Open(m_fileServer, *fileNameSymbian, EFileRead | EFileShareReadersOnly);
       
   120 
       
   121         if (KErrNone == err) {
       
   122             TDataRecognitionResult recognizerResult;
       
   123             err = m_recognizer.RecognizeData(m_file, recognizerResult);
       
   124             if (KErrNone == err) {
       
   125                 const TPtrC mimeType = recognizerResult.iDataType.Des();
       
   126                 result = Utils::mimeTypeToMediaType(mimeType);
       
   127             } else {
       
   128                 TRACE("RApaLsSession::RecognizeData filename %S error %d", fileNameSymbian.data(), err);
       
   129             }
       
   130         } else {
       
   131             TRACE("RFile::Open filename %S error %d", fileNameSymbian.data(), err);
       
   132         }
       
   133     }
       
   134 
       
   135     return result;
       
   136 }
       
   137 
       
   138 
       
   139 //-----------------------------------------------------------------------------
       
   140 // MediaObjectInterface
       
   141 //-----------------------------------------------------------------------------
       
   142 
       
   143 void MMF::MediaObject::play()
       
   144 {
       
   145     m_player->play();
       
   146 }
       
   147 
       
   148 void MMF::MediaObject::pause()
       
   149 {
       
   150     m_player->pause();
       
   151 }
       
   152 
       
   153 void MMF::MediaObject::stop()
       
   154 {
       
   155     m_player->stop();
       
   156 }
       
   157 
       
   158 void MMF::MediaObject::seek(qint64 ms)
       
   159 {
       
   160     m_player->seek(ms);
       
   161 
       
   162     if (state() == PausedState or state() == PlayingState) {
       
   163         emit tick(currentTime());
       
   164     }
       
   165 }
       
   166 
       
   167 qint32 MMF::MediaObject::tickInterval() const
       
   168 {
       
   169     return m_player->tickInterval();
       
   170 }
       
   171 
       
   172 void MMF::MediaObject::setTickInterval(qint32 interval)
       
   173 {
       
   174     m_player->setTickInterval(interval);
       
   175 }
       
   176 
       
   177 bool MMF::MediaObject::hasVideo() const
       
   178 {
       
   179     return m_player->hasVideo();
       
   180 }
       
   181 
       
   182 bool MMF::MediaObject::isSeekable() const
       
   183 {
       
   184     return m_player->isSeekable();
       
   185 }
       
   186 
       
   187 Phonon::State MMF::MediaObject::state() const
       
   188 {
       
   189     return m_player->state();
       
   190 }
       
   191 
       
   192 qint64 MMF::MediaObject::currentTime() const
       
   193 {
       
   194     return m_player->currentTime();
       
   195 }
       
   196 
       
   197 QString MMF::MediaObject::errorString() const
       
   198 {
       
   199     return m_player->errorString();
       
   200 }
       
   201 
       
   202 Phonon::ErrorType MMF::MediaObject::errorType() const
       
   203 {
       
   204     return m_player->errorType();
       
   205 }
       
   206 
       
   207 qint64 MMF::MediaObject::totalTime() const
       
   208 {
       
   209     return m_player->totalTime();
       
   210 }
       
   211 
       
   212 MediaSource MMF::MediaObject::source() const
       
   213 {
       
   214     return m_player->source();
       
   215 }
       
   216 
       
   217 void MMF::MediaObject::setSource(const MediaSource &source)
       
   218 {
       
   219     createPlayer(source);
       
   220 
       
   221     // This is a hack to work around KErrInUse from MMF client utility
       
   222     // OpenFileL calls
       
   223     m_player->setFileSource(source, m_file);
       
   224 
       
   225     emit currentSourceChanged(source);
       
   226 }
       
   227 
       
   228 void MMF::MediaObject::createPlayer(const MediaSource &source)
       
   229 {
       
   230     TRACE_CONTEXT(MediaObject::createPlayer, EAudioApi);
       
   231     TRACE_ENTRY("state %d source.type %d", state(), source.type());
       
   232     TRACE_ENTRY("source.type %d", source.type());
       
   233 
       
   234     MediaType mediaType = MediaTypeUnknown;
       
   235 
       
   236     AbstractPlayer* oldPlayer = m_player.data();
       
   237 
       
   238     const bool oldPlayerHasVideo = oldPlayer->hasVideo();
       
   239     const bool oldPlayerSeekable = oldPlayer->isSeekable();
       
   240 
       
   241     Phonon::ErrorType error = NoError;
       
   242 
       
   243     // Determine media type
       
   244     switch (source.type()) {
       
   245     case MediaSource::LocalFile:
       
   246         mediaType = fileMediaType(source.fileName());
       
   247         break;
       
   248 
       
   249     case MediaSource::Url:
       
   250         {
       
   251             const QUrl url(source.url());
       
   252             if (url.scheme() == QLatin1String("file")) {
       
   253                 mediaType = fileMediaType(url.toLocalFile());
       
   254             }
       
   255             else {
       
   256                 TRACE_0("Network streaming not supported yet");
       
   257                 error = NormalError;
       
   258             }
       
   259         }
       
   260         break;
       
   261 
       
   262     case MediaSource::Invalid:
       
   263     case MediaSource::Disc:
       
   264     case MediaSource::Stream:
       
   265         TRACE_0("Unsupported media type");
       
   266         error = NormalError;
       
   267         break;
       
   268 
       
   269     case MediaSource::Empty:
       
   270         TRACE_0("Empty media source");
       
   271         break;
       
   272     }
       
   273 
       
   274     AbstractPlayer* newPlayer = 0;
       
   275 
       
   276     // Construct newPlayer using oldPlayer (if not 0) in order to copy
       
   277     // parameters (volume, prefinishMark, transitionTime) which may have
       
   278     // been set on oldPlayer.
       
   279 
       
   280     switch (mediaType) {
       
   281     case MediaTypeUnknown:
       
   282         TRACE_0("Media type could not be determined");
       
   283         if (oldPlayer) {
       
   284             newPlayer = new DummyPlayer(*oldPlayer);
       
   285         } else {
       
   286             newPlayer = new DummyPlayer();
       
   287         }
       
   288 
       
   289         newPlayer->setError(NormalError);
       
   290         break;
       
   291 
       
   292     case MediaTypeAudio:
       
   293         if (oldPlayer) {
       
   294             newPlayer = new AudioPlayer(*oldPlayer);
       
   295         } else {
       
   296             newPlayer = new AudioPlayer();
       
   297         }
       
   298         break;
       
   299 
       
   300     case MediaTypeVideo:
       
   301         if (oldPlayer) {
       
   302             newPlayer = new VideoPlayer(*oldPlayer);
       
   303         } else {
       
   304             newPlayer = new VideoPlayer();
       
   305         }
       
   306         break;
       
   307     }
       
   308 
       
   309     m_player.reset(newPlayer);
       
   310 
       
   311     if (oldPlayerHasVideo != hasVideo()) {
       
   312         emit hasVideoChanged(hasVideo());
       
   313     }
       
   314 
       
   315     if (oldPlayerSeekable != isSeekable()) {
       
   316         emit seekableChanged(isSeekable());
       
   317     }
       
   318 
       
   319     connect(m_player.data(), SIGNAL(totalTimeChanged(qint64)), SIGNAL(totalTimeChanged(qint64)));
       
   320     connect(m_player.data(), SIGNAL(stateChanged(Phonon::State, Phonon::State)), SIGNAL(stateChanged(Phonon::State, Phonon::State)));
       
   321     connect(m_player.data(), SIGNAL(finished()), SIGNAL(finished()));
       
   322     connect(m_player.data(), SIGNAL(tick(qint64)), SIGNAL(tick(qint64)));
       
   323 
       
   324     if (error != NoError ) {
       
   325         newPlayer = new DummyPlayer();
       
   326         newPlayer->setError(error);
       
   327     }
       
   328 
       
   329     TRACE_EXIT_0();
       
   330 }
       
   331 
       
   332 void MMF::MediaObject::setNextSource(const MediaSource &source)
       
   333 {
       
   334     m_player->setNextSource(source);
       
   335 }
       
   336 
       
   337 qint32 MMF::MediaObject::prefinishMark() const
       
   338 {
       
   339     return m_player->prefinishMark();
       
   340 }
       
   341 
       
   342 void MMF::MediaObject::setPrefinishMark(qint32 mark)
       
   343 {
       
   344     m_player->setPrefinishMark(mark);
       
   345 }
       
   346 
       
   347 qint32 MMF::MediaObject::transitionTime() const
       
   348 {
       
   349     return m_player->transitionTime();
       
   350 }
       
   351 
       
   352 void MMF::MediaObject::setTransitionTime(qint32 time)
       
   353 {
       
   354     m_player->setTransitionTime(time);
       
   355 }
       
   356 
       
   357 
       
   358 //-----------------------------------------------------------------------------
       
   359 // VolumeObserver
       
   360 //-----------------------------------------------------------------------------
       
   361 
       
   362 void MMF::MediaObject::volumeChanged(qreal volume)
       
   363 {
       
   364     m_player->volumeChanged(volume);
       
   365 }
       
   366 
       
   367 //-----------------------------------------------------------------------------
       
   368 // Video output
       
   369 //-----------------------------------------------------------------------------
       
   370 
       
   371 void MMF::MediaObject::setVideoOutput(VideoOutput* videoOutput)
       
   372 {
       
   373     m_player->setVideoOutput(videoOutput);
       
   374 }
       
   375 
       
   376 
       
   377 AbstractPlayer *MMF::MediaObject::abstractPlayer() const
       
   378 {
       
   379     return m_player.data();
       
   380 }
       
   381 
       
   382 bool MMF::MediaObject::activateOnMediaObject(MediaObject *)
       
   383 {
       
   384     // Guess what, we do nothing.
       
   385     return true;
       
   386 }
       
   387 
       
   388 QT_END_NAMESPACE
       
   389