musichomescreen/mcpmusicplayer/src/aiplayerpluginengine.cpp
changeset 0 ff3acec5bc43
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Music Player stautus observer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mpxplaybackutility.h>
       
    20 #include <mpxplaybackmessage.h>
       
    21 #include <mpxmedia.h>
       
    22 #include <mpxmediageneraldefs.h>
       
    23 #include <mpxmediamusicdefs.h>
       
    24 #include <mpxmessagegeneraldefs.h>
       
    25 #include <mpxplaybackmessagedefs.h>
       
    26 #include <mpxalbumartutil.h>
       
    27 #include <mpxlog.h>
       
    28 
       
    29 #include "aiplayerpluginengine.h"
       
    30 
       
    31 const TInt KMPXOneSecInMilliSecs( 1000 );
       
    32 const TUid  KMusicPlayerAppUid = { 0x102072C3 };
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CAiPlayerPluginEngine::ConstructL
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 void CAiPlayerPluginEngine::ConstructL()
       
    41     {
       
    42     MPX_DEBUG1("CAiPlayerPluginEngine::ConstructL");
       
    43     // Get the playback utility instance from engine.
       
    44     //iPlaybackUtility = MMPXPlaybackUtility::NewL( KPbModeActivePlayer, this );
       
    45     iPlaybackUtility = MMPXPlaybackUtility::NewL( KMusicPlayerAppUid, this );
       
    46     iAlbumArtUtil = CMPXAlbumArtUtil::NewL();
       
    47     if (iPlaybackUtility->StateL() == EPbStatePlaying)
       
    48         {
       
    49     	  RequestMediaL();
       
    50         }
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // CAiPlayerPluginEngine::NewL
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CAiPlayerPluginEngine* CAiPlayerPluginEngine::NewL( 
       
    58         MAiPlayerPluginEngineObserver& aObserver )
       
    59     {
       
    60     MPX_DEBUG1("CAiPlayerPluginEngine::NewL");
       
    61     CAiPlayerPluginEngine* self = new ( ELeave ) 
       
    62             CAiPlayerPluginEngine( aObserver );
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CAiPlayerPluginEngine::CAiPlayerPluginEngine
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CAiPlayerPluginEngine::CAiPlayerPluginEngine( 
       
    74         MAiPlayerPluginEngineObserver& aObserver )
       
    75     : iObserver( &aObserver ),
       
    76       iPlaybackUtility( NULL ),
       
    77       iTitle( NULL ),
       
    78       iArtist( NULL ),
       
    79       iUri( NULL ),
       
    80       iMedia( NULL ),
       
    81       iPosition( KErrNotFound ),
       
    82       iDuration( KErrNotFound ),
       
    83       iExtractingAlbumArt( EFalse ),
       
    84       iSkipping( EFalse )
       
    85     {
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CAiPlayerPluginEngine::~CAiPlayerPluginEngine
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 CAiPlayerPluginEngine::~CAiPlayerPluginEngine()
       
    93     {
       
    94     MPX_DEBUG1("CAiPlayerPluginEngine::~CAiPlayerPluginEngine");
       
    95     if ( iPlaybackUtility )
       
    96         {
       
    97         iPlaybackUtility->Close();
       
    98         }
       
    99     delete iMedia;
       
   100     delete iAlbumArtUtil;
       
   101     delete iTitle;
       
   102     delete iArtist;
       
   103     delete iUri;
       
   104         
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // From MMPXPlaybackObserver
       
   109 // Handle playback message.
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CAiPlayerPluginEngine::HandlePlaybackMessage( CMPXMessage* aMessage, 
       
   113         TInt aError )
       
   114     {
       
   115     if ( aError == KErrNone && aMessage )
       
   116         {
       
   117         TRAP_IGNORE( DoHandlePlaybackMessageL( *aMessage ) );
       
   118         }
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // From MMPXPlaybackCallback
       
   123 // Handle playback property.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CAiPlayerPluginEngine::HandlePropertyL( TMPXPlaybackProperty aProperty, 
       
   127         TInt aValue, TInt aError )
       
   128     {
       
   129     DoHandlePropertyL( aProperty, aValue, aError );
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // From MMPXPlaybackCallback
       
   134 // Method is called continously until aComplete=ETrue, signifying that
       
   135 // it is done and there will be no more callbacks
       
   136 // Only new items are passed each time
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 void CAiPlayerPluginEngine::HandleSubPlayerNamesL(
       
   140     TUid /* aPlayer */,
       
   141     const MDesCArray* /* aSubPlayers */,
       
   142     TBool /* aComplete */,
       
   143     TInt /* aError */ )
       
   144     {
       
   145     // do nothing
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // From MMPXPlaybackCallback
       
   150 // Handle media
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void CAiPlayerPluginEngine::HandleMediaL( const CMPXMedia& aMedia, 
       
   154         TInt aError )
       
   155     {
       
   156     MPX_DEBUG1("CAiPlayerPluginEngine::HandleMediaL");
       
   157     if ( KErrNone == aError )
       
   158         {
       
   159         delete iUri;
       
   160         iUri = NULL;
       
   161         if (aMedia.IsSupported(KMPXMediaGeneralUri))
       
   162             {
       
   163             TParsePtrC filePath(aMedia.ValueText(KMPXMediaGeneralUri) );
       
   164             iUri = filePath.FullName().AllocL();
       
   165             }
       
   166         
       
   167 		delete iTitle;
       
   168 		iTitle = NULL;
       
   169         if ( aMedia.IsSupported( KMPXMediaGeneralTitle ) )
       
   170             {
       
   171             iTitle = ( aMedia.ValueText( KMPXMediaGeneralTitle ) ).AllocL();
       
   172             }
       
   173         else if ( aMedia.IsSupported( KMPXMediaGeneralUri ) )
       
   174             {
       
   175             TParsePtrC filePath( aMedia.ValueText( KMPXMediaGeneralUri ) );
       
   176             iTitle = (filePath.Name()).AllocL();
       
   177             }
       
   178 		delete iArtist;
       
   179 		iArtist = NULL;
       
   180 		iArtist = ( aMedia.ValueText( KMPXMediaMusicArtist ) ).AllocL();
       
   181 		
       
   182 		iObserver->TrackInfoChanged( *iTitle, *iArtist );
       
   183         
       
   184 		if (!iSkipping)
       
   185             {
       
   186             if (iExtractingAlbumArt)
       
   187                 {
       
   188                 iAlbumArtUtil->CancelRequest();
       
   189                 iExtractingAlbumArt=EFalse;
       
   190                 }
       
   191             
       
   192             if ( aMedia.IsSupported( KMPXMediaMusicAlbumArtFileName ) )
       
   193                 {
       
   194                 delete iMedia;
       
   195                 iMedia=NULL;
       
   196                 iMedia = CMPXMedia::NewL( aMedia );
       
   197                 TRAPD(err,iAlbumArtUtil->ExtractAlbumArtL(
       
   198                         *iMedia,
       
   199                         *this,
       
   200                         TSize(70,70)));
       
   201                 
       
   202                 if (err != KErrNone)
       
   203                     {
       
   204                     iObserver->AlbumArtChanged(NULL);
       
   205                     }
       
   206                 }
       
   207             else
       
   208                 {
       
   209                 iObserver->AlbumArtChanged(NULL);
       
   210                 }
       
   211 
       
   212             }
       
   213 		else
       
   214 		    {
       
   215 		    iObserver->AlbumArtChanged(NULL);
       
   216 		    }
       
   217         }
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // From MMPXPlaybackCallback
       
   222 // Handle completion of a asynchronous command
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CAiPlayerPluginEngine::HandlePlaybackCommandComplete( 
       
   226         CMPXCommand* /*aCommandResult*/, TInt /*aError*/ )
       
   227     {
       
   228     // do nothing
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // From MMPXAlbumArtUtilObserver
       
   233 // Notify that extraction of album art started.
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 void CAiPlayerPluginEngine::ExtractAlbumArtStarted()
       
   237     {
       
   238     iExtractingAlbumArt = ETrue;
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // From MMPXAlbumArtUtilObserver
       
   243 // Notify that the extraction of album art has completed.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CAiPlayerPluginEngine::ExtractAlbumArtCompleted( 
       
   247         CFbsBitmap* aBitmap, 
       
   248         TInt aErr )
       
   249     {
       
   250     if (aErr == KErrNone)
       
   251         {
       
   252         iObserver->AlbumArtChanged(aBitmap);
       
   253         }
       
   254     else
       
   255         {
       
   256         iObserver->AlbumArtChanged(NULL);
       
   257         }
       
   258     iExtractingAlbumArt = EFalse;
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // Get the current state of the active player
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TMPlayerState CAiPlayerPluginEngine::PlayerState()
       
   266     {
       
   267     MPX_DEBUG1( "CAiPlayerPluginEngine::PlayerState" );
       
   268     TMPXPlaybackState state( EPbStateNotInitialised );
       
   269     TRAP_IGNORE( state = iPlaybackUtility->StateL() );
       
   270     return MapState( state );
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // CAiPlayerPluginEngine::TitleL
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 const TDesC& CAiPlayerPluginEngine::TitleL()
       
   278     {
       
   279 
       
   280     if ( iTitle )
       
   281         {
       
   282         return *iTitle;
       
   283         }
       
   284      else
       
   285         {
       
   286         RequestMediaL();
       
   287         return KNullDesC;
       
   288         }
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // CAiPlayerPluginEngine::Artist
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 const TDesC& CAiPlayerPluginEngine::Artist()
       
   296     {
       
   297     if ( iArtist )
       
   298         {
       
   299         return *iArtist;
       
   300         }
       
   301      else
       
   302         {
       
   303         return KNullDesC;
       
   304         }
       
   305     }
       
   306 // ---------------------------------------------------------------------------
       
   307 // CAiPlayerPluginEngine::Artist
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 const TDesC& CAiPlayerPluginEngine::Uri()
       
   311     {
       
   312     if ( iUri )
       
   313         {
       
   314         return *iUri;
       
   315         }
       
   316      else
       
   317         {
       
   318         return KNullDesC;
       
   319         }
       
   320     }
       
   321 
       
   322 // ----------------------------------------------------
       
   323 // CAiPlayerPluginEngine::Position
       
   324 // ----------------------------------------------------
       
   325 //
       
   326 TInt CAiPlayerPluginEngine::Position()
       
   327     {
       
   328     return iPosition;
       
   329     }
       
   330 
       
   331 // ----------------------------------------------------
       
   332 // CAiPlayerPluginEngine::Duration
       
   333 // ----------------------------------------------------
       
   334 //
       
   335 TInt CAiPlayerPluginEngine::Duration()
       
   336     {
       
   337     return iDuration;
       
   338     }
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // Handle playback message.
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 void CAiPlayerPluginEngine::DoHandlePlaybackMessageL( 
       
   345         const CMPXMessage& aMessage )
       
   346     {
       
   347     MPX_DEBUG1("CAiPlayerPluginEngine::DoHandlePlaybackMessageL");
       
   348 
       
   349     TMPXMessageId id( 
       
   350             aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   351     if ( KMPXMessageGeneral == id )
       
   352         {
       
   353         TInt event( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) );
       
   354         MPX_DEBUG2( "CAiPlayerPluginEngine::DoHandlePlaybackMessageL(%d)", 
       
   355                 event );
       
   356         switch ( event )
       
   357             {
       
   358             case TMPXPlaybackMessage::EPropertyChanged:
       
   359                 {
       
   360                 TInt error( KErrNone );
       
   361                 DoHandlePropertyL(
       
   362                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ),
       
   363                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralData ),
       
   364                     error );
       
   365                 break;
       
   366                 }
       
   367             case TMPXPlaybackMessage::EStateChanged:
       
   368                 {
       
   369 				TMPXPlaybackState state( 
       
   370 				        aMessage.ValueTObjectL<TMPXPlaybackState>( 
       
   371 				                KMPXMessageGeneralType ));
       
   372                 MPX_DEBUG2("CAiPlayerPluginEngine::HandlePlaybackMessageL - E"
       
   373                         "StateChanged(%d)", state);
       
   374 
       
   375                 DoHandleStateChangedL( state );
       
   376                 break;
       
   377                 }
       
   378             case TMPXPlaybackMessage::EMediaChanged:
       
   379             case TMPXPlaybackMessage::EPlaylistUpdated:
       
   380                 {
       
   381                 iPlaybackUtility->PropertyL( *this, EPbPropertyPosition );
       
   382                 iPlaybackUtility->PropertyL( *this, EPbPropertyDuration );
       
   383                 RequestMediaL();
       
   384                 break;
       
   385                 }
       
   386             case TMPXPlaybackMessage::ECommandReceived:
       
   387                 {
       
   388                 MPX_DEBUG2("CAiPlayerPluginEngine::HandlePlaybackMessageL - E"
       
   389                         "CommandReceived(%d)",
       
   390                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ) );
       
   391                 break;
       
   392                 }
       
   393             case TMPXPlaybackMessage::EActivePlayerChanged:
       
   394                 {
       
   395                 MPX_DEBUG3("CAiPlayerPluginEngine::HandlePlaybackMessageL - E"
       
   396                         "ActivePlayerChanged(%d, %d)",
       
   397                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ), 
       
   398                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralData ) );
       
   399                 iPlaybackUtility->PropertyL( *this, EPbPropertyPosition );
       
   400                 iPlaybackUtility->PropertyL( *this, EPbPropertyDuration );
       
   401                 DoHandleStateChangedL( iPlaybackUtility->StateL() );
       
   402                 // refresh media property
       
   403                 RequestMediaL();
       
   404                 break;
       
   405                 }
       
   406             case TMPXPlaybackMessage::ESkipping:
       
   407                 {
       
   408                 MPX_DEBUG1( "CAiPlayerPluginEngine::DoHandlePlaybackMessageL - ESkipping");
       
   409                 iAlbumArtUtil->CancelRequest();
       
   410                 iObserver->Opening();
       
   411                 iSkipping =ETrue;
       
   412                 break;
       
   413                 }
       
   414             case TMPXPlaybackMessage::ESkipEnd:
       
   415                 {
       
   416                 MPX_DEBUG1( "CAiPlayerPluginEngine::DoHandlePlaybackMessageL - ESkipEnd()");
       
   417                 iSkipping = EFalse;
       
   418                 break;
       
   419                 }
       
   420             default:
       
   421                 {
       
   422                 break;
       
   423                 }
       
   424             }
       
   425         }
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // Handle playback property.
       
   430 // ---------------------------------------------------------------------------
       
   431 //
       
   432 void CAiPlayerPluginEngine::DoHandlePropertyL( TInt aProperty, TInt aValue, 
       
   433         TInt aError )
       
   434     {
       
   435     MPX_DEBUG4("CAiPlayerPluginEngine::DoHandlePropertyL - Property(%d); Valu"
       
   436             "e(%d); Error(%d)", aProperty, aValue, aError );
       
   437     if ( KErrNone == aError )
       
   438         {
       
   439         switch ( aProperty	)
       
   440             {
       
   441             case EPbPropertyPosition:
       
   442                 {
       
   443 				iPosition = aValue / KMPXOneSecInMilliSecs;
       
   444                 iObserver->PlaybackPositionChanged( iPosition );
       
   445                 break;
       
   446                 }
       
   447             case EPbPropertyDuration:
       
   448                 {
       
   449 				iDuration = aValue / KMPXOneSecInMilliSecs;
       
   450                 break;
       
   451                 }
       
   452             default:
       
   453                 {
       
   454                 break;
       
   455                 }
       
   456             }
       
   457         }
       
   458     }
       
   459 
       
   460 // ---------------------------------------------------------------------------
       
   461 // Handle playback state changed.
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 void CAiPlayerPluginEngine::DoHandleStateChangedL( TMPXPlaybackState aState )
       
   465     {
       
   466     TMPlayerState state = MapState( aState );
       
   467     MPX_DEBUG3("CAiPlayerPluginEngine::DoHandleStateChangedL - State mapped "
       
   468             "from (%d) to (%d)", aState, state );
       
   469     if (state != EMPlayerStateStarting)
       
   470         {
       
   471         iObserver->PlayerStateChanged(state);
       
   472         }
       
   473     else 
       
   474         {
       
   475         iObserver->Opening();
       
   476         }
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // Displays error notes.
       
   481 // ---------------------------------------------------------------------------
       
   482 //
       
   483 void CAiPlayerPluginEngine::HandleErrorL( TInt aError )
       
   484     {
       
   485 	MPX_DEBUG2("CAiPlayerPluginEngine::HandleErrorL(%d)", aError );
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // Map states from TMPXPlaybackState to TMPlayerState
       
   490 // ---------------------------------------------------------------------------
       
   491 //
       
   492 TMPlayerState CAiPlayerPluginEngine::MapState( TMPXPlaybackState aState )
       
   493     {
       
   494 	TMPlayerState state = EMPlayerStateOther;
       
   495     switch ( aState )
       
   496         {
       
   497         case EPbStatePlaying:
       
   498             state = EMPlayerStatePlaying;
       
   499             break;
       
   500         case EPbStatePaused:
       
   501             state = EMPlayerStatePaused;
       
   502             break;
       
   503         case EPbStateSeekingForward:
       
   504         case EPbStateSeekingBackward:
       
   505             state = EMPlayerStateSeeking;
       
   506             break;
       
   507         case EPbStateNotInitialised:
       
   508         case EPbStateInitialising:
       
   509             state = EMPlayerStateStarting;
       
   510             break;
       
   511         case EPbStateStopped:
       
   512             state = EMPlayerStateStopped;
       
   513             break;
       
   514         default:
       
   515             break;
       
   516         }
       
   517     return state;
       
   518     }
       
   519 
       
   520 // ---------------------------------------------------------------------------
       
   521 // Requests Media.
       
   522 // ---------------------------------------------------------------------------
       
   523 //
       
   524 void CAiPlayerPluginEngine::RequestMediaL()
       
   525     {
       
   526     MPX_DEBUG1( "CAiPlayerPluginEngine::RequestMediaL" );
       
   527     MMPXSource* s = iPlaybackUtility->Source();
       
   528     if ( s )
       
   529         {
       
   530         RArray<TMPXAttribute> attrs;
       
   531         CleanupClosePushL(attrs);
       
   532         attrs.Append( KMPXMediaGeneralUri );
       
   533         attrs.Append( KMPXMediaGeneralTitle );
       
   534         attrs.Append( KMPXMediaMusicArtist );
       
   535         attrs.Append( KMPXMediaMusicAlbumArtFileName );
       
   536         s->MediaL( attrs.Array(), *this );
       
   537         CleanupStack::PopAndDestroy( &attrs );
       
   538         }
       
   539     else 
       
   540         {
       
   541         iObserver->PlaylisIsEmpty();
       
   542         }
       
   543     }
       
   544 //  End of File