qtinternetradio/irqmediaplayer/src/irqmmfadapter.cpp
changeset 0 09774dfdd46b
child 5 0930554dc389
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include <mmf/common/mmferrors.h>
       
    18 #include <NokiaAudioPreference.h>
       
    19 #include <coedef.h>
       
    20 #include <QStringList>
       
    21 #include <hxmetadatakeys.h>
       
    22 #include "irqmetadata.h"
       
    23 #include "irqenums.h"
       
    24 #include "irqmmfadapter.h"
       
    25 
       
    26 //Constants
       
    27 const TUid KUidController        = { 0x101F8514 }; // Helix Video controller UID
       
    28 const TInt KConnectingTime       = 30*1000000;     // 30 seconds
       
    29 const TInt KVolumeMinPercentage  = 0;              // Minimum volume percentage
       
    30 const TInt KVolumeMaxPercentage  = 100;            // Maximum volume percentage
       
    31 const TInt KLoadingCompletePercentage = 100;       // Loading Complete percentage
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 //  IRQMMFAdapter::IRQMMFAdapter
       
    35 //  Constructor
       
    36 //  Initialize viriants
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 IRQMMFAdapter::IRQMMFAdapter() :
       
    40     iVideoPlayer(NULL)
       
    41     ,iQMetaData(NULL)
       
    42     ,iPrepareTimer(NULL)
       
    43 {
       
    44     iPlayState = EStopped;
       
    45 }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 //  IRQMMFAdapter::~IRQMMFAdapter
       
    49 //  Destructor
       
    50 //  Clean up resources
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 IRQMMFAdapter::~IRQMMFAdapter()
       
    54 {
       
    55     destroyPlayer();
       
    56 
       
    57     delete iQMetaData;
       
    58     iQMetaData = NULL;
       
    59 
       
    60     if (iPrepareTimer)
       
    61     {
       
    62         if (iPrepareTimer->IsActive())
       
    63         {
       
    64             iPrepareTimer->Cancel();
       
    65         }
       
    66         delete iPrepareTimer;
       
    67         iPrepareTimer = NULL;
       
    68     }
       
    69 }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 //  IRQMMFAdapter::playStation
       
    73 //  IRQPlayerAdapterInterface method
       
    74 //  Play url via specific access point id
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void IRQMMFAdapter::playStation(const QString &aUrl, int aApId)
       
    78 {
       
    79     TRAPD(error, playL(aUrl, aApId));
       
    80     if (NULL == iQMetaData)
       
    81     {
       
    82         emit errorOccured(EIRQErrorOutOfMemory);
       
    83     }
       
    84 
       
    85     if (KErrNone != error)
       
    86     {
       
    87         emit errorOccured(EIRQPlayerErrorGeneral);
       
    88     }
       
    89 }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 //  IRQMMFAdapter::playL
       
    93 //  Play url via specific access point id
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void IRQMMFAdapter::playL(const QString &aUrl, int aApId)
       
    97 {
       
    98     // Save stream Url
       
    99     if (NULL == iQMetaData)
       
   100     {
       
   101         iQMetaData = new (ELeave) IRQMetaData();
       
   102     }
       
   103     else
       
   104     {
       
   105         // Clear MetaData
       
   106         iQMetaData->clear();
       
   107     }
       
   108     iQMetaData->setStreamUrl(aUrl);
       
   109 
       
   110     // Transfer url from QString to TDesC
       
   111     TPtrC stationUrl(reinterpret_cast<const TUint16*>(aUrl.utf16()));
       
   112 
       
   113     // Create player if it doesn't exist
       
   114     if (NULL == iVideoPlayer)
       
   115     {
       
   116         createPlayerL();
       
   117     }
       
   118 
       
   119     // If the status is not stopped, clean up last playback resources
       
   120     stop();
       
   121 
       
   122     // Open url
       
   123     iVideoPlayer->OpenUrlL(stationUrl, aApId, KNullDesC8, KUidController);
       
   124     iPlayState = EOpenning;
       
   125 }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 //  IRQMMFAdapter::stop
       
   129 //  IRQPlayerAdapterInterface method
       
   130 //  Stop playback, call Close() to clean up allocated resources
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void IRQMMFAdapter::stop()
       
   134 {
       
   135     if (iVideoPlayer && EStopped != iPlayState)
       
   136     {
       
   137         if (iPrepareTimer)
       
   138         {
       
   139             if (iPrepareTimer->IsActive())
       
   140             {
       
   141                 iPrepareTimer->Cancel();
       
   142             }
       
   143         }
       
   144 
       
   145         iVideoPlayer->Stop();
       
   146         iVideoPlayer->Close();
       
   147         iPlayState = EStopped;
       
   148     }
       
   149 }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 //  IRQMMFAdapter::setVolume
       
   153 //  IRQPlayerAdapterInterface method
       
   154 //  Set volume to player
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void IRQMMFAdapter::setVolume(int aVolume)
       
   158 {
       
   159     if (iVideoPlayer && iPlayState > EOpenning)
       
   160     {
       
   161         // aVolume is a percentage
       
   162         if (aVolume < KVolumeMinPercentage)
       
   163         {
       
   164             aVolume = KVolumeMinPercentage;
       
   165         }
       
   166         else if (aVolume > KVolumeMaxPercentage)
       
   167         {
       
   168             aVolume = KVolumeMaxPercentage;
       
   169         }
       
   170         int volume = aVolume*iVideoPlayer->MaxVolume()/KVolumeMaxPercentage;
       
   171 
       
   172         TRAPD(error, iVideoPlayer->SetVolumeL(volume));
       
   173         if (KErrNone != error)
       
   174         {
       
   175             emit errorOccured(EIRQPlayerErrorGeneral);
       
   176         }
       
   177     }
       
   178 }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 //  IRQMMFAdapter::getVolume
       
   182 //  IRQPlayerAdapterInterface method
       
   183 //  Get current volume value from player
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 int IRQMMFAdapter::getVolume()
       
   187 {
       
   188     int volume = KVolumeMinPercentage;
       
   189 
       
   190     if (iVideoPlayer && iPlayState > EOpenning)
       
   191     {
       
   192         // Return a percentage
       
   193         volume = iVideoPlayer->Volume()*KVolumeMaxPercentage/iVideoPlayer->MaxVolume();
       
   194     }
       
   195     return volume;
       
   196 }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 //  IRQMMFAdapter::getPlayerInstance
       
   200 //  IRQPlayerAdapterInterface method
       
   201 //  Get audio player instance
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 void* IRQMMFAdapter::getPlayerInstance()
       
   205 {
       
   206     return (void*)iVideoPlayer;
       
   207 }
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 //  IRQMMFAdapter::MvpuoOpenComplete
       
   211 //  Callback function, MVideoPlayerUtilityObserver method
       
   212 //  Called after calling CVideoPlayerUtility::OpenUrlL()
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void IRQMMFAdapter::MvpuoOpenComplete(TInt aError)
       
   216 {
       
   217     if (KErrNone == aError)
       
   218     {
       
   219         if (NULL == iPrepareTimer)
       
   220         {
       
   221             TRAPD(error, iPrepareTimer = CPeriodic::NewL(CPeriodic::EPriorityStandard));
       
   222             if (KErrNone != error)
       
   223             {
       
   224                 emit errorOccured(EIRQErrorOutOfMemory);
       
   225                 return;
       
   226              }
       
   227         }
       
   228 
       
   229         // Prepare to playback
       
   230         iVideoPlayer->Prepare();
       
   231         iPlayState = EConnecting;
       
   232 
       
   233         // Start a timer to check preparation status
       
   234         if (iPrepareTimer->IsActive())
       
   235         {
       
   236             // Cancel the previous request if pending
       
   237             iPrepareTimer->Cancel();
       
   238         }
       
   239         TTimeIntervalMicroSeconds32 interval(KConnectingTime);
       
   240         iPrepareTimer->Start(interval,interval,
       
   241                              TCallBack(IRQMMFAdapter::isPrepareCompleted,this));
       
   242     }
       
   243     else
       
   244     {
       
   245         emit errorOccured(EIRQPlayerErrorConnectingFailed);
       
   246     }
       
   247 }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 //  IRQMMFAdapter::MvpuoPrepareComplete
       
   251 //  Callback function, MVideoPlayerUtilityObserver method
       
   252 //  Called after calling CVideoPlayerUtility::Prepare. Since some audio types
       
   253 //  are not explicitly(hxmmffourccmap.cpp), they are not retrieved here.
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 void IRQMMFAdapter::MvpuoPrepareComplete(TInt aError)
       
   257 {
       
   258     // Cancel the previous request if pending
       
   259     if (iPrepareTimer->IsActive())
       
   260     {
       
   261         iPrepareTimer->Cancel();
       
   262     }
       
   263 
       
   264     if (KErrNone == aError)
       
   265     {
       
   266         // Get volume from preset
       
   267         int volumeval = KVolumeMaxPercentage/2;
       
   268         emit volumeExpected(volumeval);
       
   269         setVolume(volumeval);
       
   270 
       
   271         // Save bit rate
       
   272         int bitrate = 0;
       
   273         TRAPD(error, bitrate = iVideoPlayer->AudioBitRateL());
       
   274         if (KErrNone == error)
       
   275         {
       
   276             iQMetaData->setBitrate(bitrate/1000);
       
   277         }
       
   278 
       
   279         // Send signal ConnectionEstablished
       
   280         emit connectionEstablished(iQMetaData->getBitrate());
       
   281 
       
   282         // Set specific event to get meta data from player
       
   283         setMetadataEventConfig();
       
   284 
       
   285         // Start playback
       
   286         iVideoPlayer->Play();
       
   287         iPlayState = EBuffering;
       
   288     }
       
   289     else if (KErrServerBusy == aError)
       
   290     {
       
   291         emit errorOccured(EIRQPlayerErrorServerFull);
       
   292     }
       
   293     else
       
   294     {
       
   295         emit errorOccured(EIRQPlayerErrorConnectingFailed);
       
   296     }
       
   297 }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 //  IRQMMFAdapter::MvpuoPlayComplete
       
   301 //  Callback function, MVideoPlayerUtilityObserver method
       
   302 //  Notification that video playback has completed. This is not called if play
       
   303 //  back is explicitly stopped by calling Stop. Moreover, radio station stream
       
   304 //  has no end. So it should be NEVER called.
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 void IRQMMFAdapter::MvpuoPlayComplete(TInt aError)
       
   308 {
       
   309     if (KErrNone != aError)
       
   310     {
       
   311         emit errorOccured(EIRQPlayerErrorGeneral);
       
   312     }
       
   313 }
       
   314 
       
   315 // ---------------------------------------------------------------------------
       
   316 //  IRQMMFAdapter::MvpuoEvent
       
   317 //  Callback function, MVideoPlayerUtilityObserver method
       
   318 //  Handle events from player.
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 void IRQMMFAdapter::MvpuoEvent(TMMFEvent const & aEvent)
       
   322 {
       
   323     if (KMMFEventCategoryVideoPlayerGeneralError == aEvent.iEventType)
       
   324     {
       
   325         switch (aEvent.iErrorCode)
       
   326         {
       
   327             case KErrHardwareNotAvailable:
       
   328             case KErrMMAudioDevice:
       
   329                 // Higher priority application has taken over the
       
   330                 // audio device. --> Do stop.
       
   331                 emit errorOccured(EIRQPlayerErrorAudioDeviceLost);
       
   332                 break;
       
   333             case KErrDisconnected:
       
   334                 emit errorOccured(EIRQPlayerErrorConnectionLost);
       
   335                 break;
       
   336             case KErrTimedOut:    
       
   337                 emit errorOccured(EIRQPlayerErrorTimeOut);
       
   338                 break;
       
   339             case KErrServerBusy:    
       
   340                 emit errorOccured(EIRQPlayerErrorServerFull);
       
   341                 break;                                            
       
   342             default:
       
   343                 emit errorOccured(EIRQPlayerErrorGeneral);			
       
   344                 break;
       
   345         }
       
   346     }
       
   347     else if (KMMFRefreshMetaData == aEvent.iEventType)
       
   348     {
       
   349         // Get refreshed meta data
       
   350         TRAPD(error, getRefreshedMetaDataL(aEvent.iErrorCode));
       
   351         if (KErrNone != error)
       
   352         {
       
   353             emit errorOccured(EIRQPlayerErrorGeneral);
       
   354         }
       
   355     }
       
   356 }
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 //  IRQMMFAdapter::MvpuoFrameReady
       
   360 //  Callback function, MVideoPlayerUtilityObserver method
       
   361 //  For video stream only, never called
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void IRQMMFAdapter::MvpuoFrameReady(CFbsBitmap& aFrame,TInt aError)
       
   365 {
       
   366     Q_UNUSED(aFrame);
       
   367     Q_UNUSED(aError);
       
   368 }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 //  IRQMMFAdapter::MvloLoadingStarted
       
   372 //  Callback function, MVideoLoadingObserver method
       
   373 //  Start buffering after CVideoPlayerUtility::Play() is called
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 void IRQMMFAdapter::MvloLoadingStarted()
       
   377 {
       
   378     // Get buffering progress and send it to application
       
   379     int percentageComplete = 0;
       
   380 
       
   381     TRAPD(error, iVideoPlayer->GetVideoLoadingProgressL(percentageComplete));
       
   382 
       
   383     if (KErrNone == error)
       
   384     {
       
   385         // Send signal to UpdateProgress
       
   386         emit percentageBuffered(percentageComplete);
       
   387     }
       
   388     else
       
   389     {
       
   390         emit errorOccured(EIRQPlayerErrorGeneral);
       
   391     }
       
   392 }
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 //  IRQMMFAdapter::MvloLoadingComplete
       
   396 //  Callback function, MVideoLoadingObserver method
       
   397 //  Send 100% buffering status out
       
   398 // ---------------------------------------------------------------------------
       
   399 //
       
   400 void IRQMMFAdapter::MvloLoadingComplete()
       
   401 {
       
   402     iPlayState = EPlaying;
       
   403 
       
   404     // Send signal to update progress, 100%
       
   405     emit percentageBuffered(KLoadingCompletePercentage);
       
   406 }
       
   407 
       
   408 // ---------------------------------------------------------------------------
       
   409 //  IRQMMFAdapter::getRefreshedMetaData
       
   410 //  Get refreshed meta data according to the index
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void IRQMMFAdapter::getRefreshedMetaDataL(TInt index)
       
   414 {
       
   415     if (iQMetaData)
       
   416     {
       
   417         CMMFMetaDataEntry* pMetadataEntry = iVideoPlayer->MetaDataEntryL(index);
       
   418 
       
   419         QString entryName = QString::fromUtf16(pMetadataEntry->Name().Ptr(),
       
   420                                                pMetadataEntry->Name().Length());
       
   421 
       
   422         // If the meta data is the same as last, we don't need to report it.
       
   423         if (iLastArtistSongName == entryName)
       
   424         {
       
   425             return;
       
   426         }
       
   427         else
       
   428         {
       
   429             iLastArtistSongName = entryName;
       
   430         }
       
   431 
       
   432         // Artist, song name
       
   433         if (entryName == HXAuthor)
       
   434         {
       
   435             QString songArtist = QString::fromUtf16(pMetadataEntry->Value().Ptr(),
       
   436                                                     pMetadataEntry->Value().Length());
       
   437             iQMetaData->setArtistSongName(songArtist);
       
   438 
       
   439             // Send signal HandleMetaDataReceived
       
   440             emit metaDataReceived(*iQMetaData);
       
   441         }
       
   442     }
       
   443 }
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 //  IRQMMFAdapter::createPlayer
       
   447 //  Create player instance
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 void IRQMMFAdapter::createPlayerL()
       
   451 {
       
   452     // Create player instance
       
   453     iVideoPlayer = CVideoPlayerUtility2::NewL(*this,KAudioPriorityAudioPlaybackStreaming ,
       
   454                                              (TMdaPriorityPreference)KAudioPrefRealOneStreaming);
       
   455     // Register loading notification
       
   456     iVideoPlayer->RegisterForVideoLoadingNotification(*this);
       
   457 }
       
   458 
       
   459 // ---------------------------------------------------------------------------
       
   460 //  IRQMMFAdapter::destroyPlayer
       
   461 //  Destroy player
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 void IRQMMFAdapter::destroyPlayer()
       
   465 {
       
   466     delete iVideoPlayer;
       
   467     iVideoPlayer = NULL;
       
   468 }
       
   469 
       
   470 // ---------------------------------------------------------------------------
       
   471 //  IRQMMFAdapter::SetMetadataEventConfig
       
   472 //  Enable meta data event
       
   473 // ---------------------------------------------------------------------------
       
   474 //
       
   475 void IRQMMFAdapter::setMetadataEventConfig()
       
   476 {
       
   477     TMMFMessageDestinationPckg  destinationPckg(KUidInterfaceMMFControllerMetadataEventMsg);
       
   478     TPckgBuf<TBool>             metadataEventPckg(EMMFEnableMetadataEvent);
       
   479 
       
   480     //  Enable meta data event.
       
   481     iVideoPlayer->CustomCommandSync(destinationPckg,
       
   482                                     EMMFSetMetadataEventConfig,
       
   483                                     metadataEventPckg,
       
   484                                     KNullDesC8);
       
   485 }
       
   486 
       
   487 // ---------------------------------------------------------------------------
       
   488 //  IRQMMFAdapter::isPrepareCompleted
       
   489 //  Static function for periodic call
       
   490 // ---------------------------------------------------------------------------
       
   491 //
       
   492 TInt IRQMMFAdapter::isPrepareCompleted(TAny* aPtr)
       
   493 {
       
   494     IRQMMFAdapter* self = static_cast<IRQMMFAdapter*>(aPtr);
       
   495     if (self)
       
   496     {
       
   497         self->checkPrepare();
       
   498     }
       
   499     return KErrNone;
       
   500 }
       
   501 
       
   502 // ---------------------------------------------------------------------------
       
   503 //  IRQMMFAdapter::checkPrepare
       
   504 //  Check if the preparation is complete
       
   505 // ---------------------------------------------------------------------------
       
   506 //
       
   507 void IRQMMFAdapter::checkPrepare()
       
   508 {
       
   509     if (iPrepareTimer->IsActive())
       
   510     {
       
   511         // Cancel the previous request if pending
       
   512         iPrepareTimer->Cancel();
       
   513     }
       
   514 
       
   515     if (EConnecting == iPlayState)
       
   516     {
       
   517         emit errorOccured(EIRQPlayerErrorConnectingFailed);
       
   518         stop();
       
   519     }
       
   520 }