mpxmusicplayer/activeidle/aiplayerplugin/src/aiplayerpluginengine.cpp
branchRCL_3
changeset 9 13afc0e517bd
parent 5 2a40e88564c8
child 11 943ff5625028
equal deleted inserted replaced
5:2a40e88564c8 9:13afc0e517bd
     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 <mpxlog.h>
       
    27 
       
    28 #include "aiplayerpluginengine.h"
       
    29 
       
    30 const TInt KMPXOneSecInMilliSecs( 1000 );
       
    31 const TInt KVolumeLevelMin = 0;
       
    32 const TInt KVolumeLevelMax = 10;
       
    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     
       
    46     TMPXPlaybackState state( EPbStateNotInitialised ); 
       
    47     
       
    48     // Not fatal error if fetching the playback state fails
       
    49     TRAP_IGNORE( state = iPlaybackUtility->StateL() );
       
    50     
       
    51     if( state != EPbStateNotInitialised &&
       
    52         state != EPbStateInitialising )
       
    53     	{
       
    54     	// Playback is already ongoing. We aren't going to receive EMediaChanged
       
    55     	// for the current song so we need manually update the media info   
       
    56     	RequestMediaL();
       
    57     	}
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CAiPlayerPluginEngine::NewL
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CAiPlayerPluginEngine* CAiPlayerPluginEngine::NewL( MAiPlayerPluginEngineObserver& aObserver )
       
    65     {
       
    66     MPX_DEBUG1("CAiPlayerPluginEngine::NewL");
       
    67     CAiPlayerPluginEngine* self = new ( ELeave ) CAiPlayerPluginEngine( aObserver );
       
    68     CleanupStack::PushL( self );
       
    69     self->ConstructL();
       
    70     CleanupStack::Pop( self );
       
    71     return self;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CAiPlayerPluginEngine::CAiPlayerPluginEngine
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CAiPlayerPluginEngine::CAiPlayerPluginEngine( MAiPlayerPluginEngineObserver& aObserver )
       
    79     : iObserver( &aObserver ),
       
    80       iPlaybackUtility(NULL),
       
    81       iTitle(NULL),
       
    82       iArtist(NULL),
       
    83       iMaxVolume(KVolumeLevelMax),
       
    84       iVolume(KErrNotFound),
       
    85       iPosition(KErrNotFound),
       
    86       iDuration(KErrNotFound)
       
    87     {
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CAiPlayerPluginEngine::~CAiPlayerPluginEngine
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CAiPlayerPluginEngine::~CAiPlayerPluginEngine()
       
    95     {
       
    96     MPX_DEBUG1("CAiPlayerPluginEngine::~CAiPlayerPluginEngine");
       
    97     if ( iPlaybackUtility )
       
    98         {
       
    99         iPlaybackUtility->Close();
       
   100         }
       
   101     delete iTitle;
       
   102     delete iArtist;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // From MMPXPlaybackObserver
       
   107 // Handle playback message.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CAiPlayerPluginEngine::HandlePlaybackMessage( CMPXMessage* aMessage, TInt aError )
       
   111     {
       
   112     if ( aError == KErrNone && aMessage )
       
   113         {
       
   114         TRAP_IGNORE( DoHandlePlaybackMessageL( *aMessage ) );
       
   115         }
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // From MMPXPlaybackCallback
       
   120 // Handle playback property.
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 void CAiPlayerPluginEngine::HandlePropertyL( TMPXPlaybackProperty aProperty, TInt aValue, TInt aError )
       
   124     {
       
   125     DoHandlePropertyL( aProperty, aValue, aError );
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // From MMPXPlaybackCallback
       
   130 // Method is called continously until aComplete=ETrue, signifying that
       
   131 // it is done and there will be no more callbacks
       
   132 // Only new items are passed each time
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void CAiPlayerPluginEngine::HandleSubPlayerNamesL(
       
   136     TUid /* aPlayer */,
       
   137     const MDesCArray* /* aSubPlayers */,
       
   138     TBool /* aComplete */,
       
   139     TInt /* aError */ )
       
   140     {
       
   141     // do nothing
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // From MMPXPlaybackCallback
       
   146 // Handle media
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CAiPlayerPluginEngine::HandleMediaL( const CMPXMedia& aMedia, TInt aError )
       
   150     {
       
   151     MPX_DEBUG1("CAiPlayerPluginEngine::HandleMediaL");
       
   152     if ( KErrNone == aError )
       
   153         {
       
   154 		delete iTitle;
       
   155 		iTitle = NULL;
       
   156         if ( aMedia.IsSupported( KMPXMediaGeneralTitle ) )
       
   157             {
       
   158             iTitle = (aMedia.ValueText( KMPXMediaGeneralTitle )).AllocL();
       
   159             }
       
   160         else if ( aMedia.IsSupported( KMPXMediaGeneralUri ) )
       
   161             {
       
   162             TParsePtrC filePath( aMedia.ValueText( KMPXMediaGeneralUri ) );
       
   163             iTitle = (filePath.Name()).AllocL();
       
   164             }
       
   165 		delete iArtist;
       
   166 		iArtist = NULL;
       
   167 		iArtist = (aMedia.ValueText( KMPXMediaMusicArtist )).AllocL();
       
   168 
       
   169         iObserver->TrackInfoChanged( *iTitle, *iArtist );
       
   170         }
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // From MMPXPlaybackCallback
       
   175 // Handle completion of a asynchronous command
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CAiPlayerPluginEngine::HandlePlaybackCommandComplete( CMPXCommand* /*aCommandResult*/, TInt /*aError*/ )
       
   179     {
       
   180     // do nothing
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // Get the current state of the active player
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 TMPlayerState CAiPlayerPluginEngine::PlayerState()
       
   188     {
       
   189     MPX_DEBUG1("CAiPlayerPluginEngine::PlayerState");
       
   190     TMPXPlaybackState state( EPbStateNotInitialised );
       
   191     TRAP_IGNORE( state = iPlaybackUtility->StateL() );
       
   192     return MapState( state );
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // CAiPlayerPluginEngine::Title
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 const TDesC& CAiPlayerPluginEngine::Title()
       
   200     {
       
   201     if ( iTitle )
       
   202         {
       
   203         return *iTitle;
       
   204         }
       
   205      else
       
   206         {
       
   207         return KNullDesC;
       
   208         }
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CAiPlayerPluginEngine::Artist
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 const TDesC& CAiPlayerPluginEngine::Artist()
       
   216     {
       
   217     if ( iArtist )
       
   218         {
       
   219         return *iArtist;
       
   220         }
       
   221      else
       
   222         {
       
   223         return KNullDesC;
       
   224         }
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------
       
   228 // CAiPlayerPluginEngine::Position
       
   229 // ----------------------------------------------------
       
   230 //
       
   231 TInt CAiPlayerPluginEngine::Position()
       
   232     {
       
   233     return iPosition;
       
   234     }
       
   235 
       
   236 // ----------------------------------------------------
       
   237 // CAiPlayerPluginEngine::Duration
       
   238 // ----------------------------------------------------
       
   239 //
       
   240 TInt CAiPlayerPluginEngine::Duration()
       
   241     {
       
   242     return iDuration;
       
   243     }
       
   244 
       
   245 // ----------------------------------------------------
       
   246 // CAiPlayerPluginEngine::Volume
       
   247 // ----------------------------------------------------
       
   248 //
       
   249 TInt CAiPlayerPluginEngine::Volume()
       
   250     {
       
   251     return iVolume;
       
   252     }
       
   253 
       
   254 // ----------------------------------------------------
       
   255 // CAiPlayerPluginEngine::SetVolumeL
       
   256 // ----------------------------------------------------
       
   257 //
       
   258 void CAiPlayerPluginEngine::SetVolumeL( TInt aValue )
       
   259     {
       
   260 	MPX_DEBUG2("CAiPlayerPluginEngine::SetVolumeL(%d)", aValue );
       
   261     if ( aValue == iVolume )
       
   262         {
       
   263         return;
       
   264         }
       
   265     if ( aValue < KVolumeLevelMin || aValue > KVolumeLevelMax )
       
   266         {
       
   267         User::Leave(KErrArgument);
       
   268         }
       
   269     else
       
   270         {
       
   271        	// iVolume will be updated upon callback DoHandlePropertyL
       
   272         iPlaybackUtility->SetL( EPbPropertyVolume, MapToMpxVolume(aValue) ) ;
       
   273         }
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // Handle playback message.
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 void CAiPlayerPluginEngine::DoHandlePlaybackMessageL( const CMPXMessage& aMessage )
       
   281     {
       
   282     MPX_DEBUG1("CAiPlayerPluginEngine::DoHandlePlaybackMessageL");
       
   283 
       
   284     TMPXMessageId id( aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   285     if ( KMPXMessageGeneral == id )
       
   286         {
       
   287         TInt event( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) );
       
   288         MPX_DEBUG2("CAiPlayerPluginEngine::DoHandlePlaybackMessageL(%d)", event );
       
   289         switch ( event )
       
   290             {
       
   291             case TMPXPlaybackMessage::EPropertyChanged:
       
   292                 {
       
   293                 TInt error( KErrNone );
       
   294                 DoHandlePropertyL(
       
   295                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ),
       
   296                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralData ),
       
   297                     error );
       
   298                 break;
       
   299                 }
       
   300             case TMPXPlaybackMessage::EStateChanged:
       
   301                 {
       
   302 				TMPXPlaybackState state( aMessage.ValueTObjectL<TMPXPlaybackState>( KMPXMessageGeneralType ));
       
   303                 MPX_DEBUG2("CAiPlayerPluginEngine::HandlePlaybackMessageL - EStateChanged(%d)", state);
       
   304 
       
   305                 DoHandleStateChangedL( state );
       
   306                 break;
       
   307                 }
       
   308             case TMPXPlaybackMessage::EMediaChanged:
       
   309             case TMPXPlaybackMessage::EPlaylistUpdated:
       
   310                 {
       
   311                 iPlaybackUtility->PropertyL( *this, EPbPropertyPosition );
       
   312                 iPlaybackUtility->PropertyL( *this, EPbPropertyDuration );
       
   313                 RequestMediaL();
       
   314                 break;
       
   315                 }
       
   316             case TMPXPlaybackMessage::ECommandReceived:
       
   317                 {
       
   318                 MPX_DEBUG2("CAiPlayerPluginEngine::HandlePlaybackMessageL - ECommandReceived(%d)",
       
   319                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ) );
       
   320                 break;
       
   321                 }
       
   322             case TMPXPlaybackMessage::EActivePlayerChanged:
       
   323                 {
       
   324                 MPX_DEBUG3("CAiPlayerPluginEngine::HandlePlaybackMessageL - EActivePlayerChanged(%d, %d)",
       
   325                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ), 
       
   326                     aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralData ) );
       
   327                 iPlaybackUtility->PropertyL( *this, EPbPropertyPosition );
       
   328                 iPlaybackUtility->PropertyL( *this, EPbPropertyDuration );
       
   329                 iPlaybackUtility->PropertyL( *this, EPbPropertyMaxVolume );
       
   330                 iPlaybackUtility->PropertyL( *this, EPbPropertyVolume );
       
   331                 DoHandleStateChangedL( iPlaybackUtility->StateL() );
       
   332                 // refresh media property
       
   333                 RequestMediaL();
       
   334                 break;
       
   335                 }
       
   336             case TMPXPlaybackMessage::EDownloadStateChanged:
       
   337                 {
       
   338                 iPlaybackUtility->PropertyL( *this, EPbPropertyPosition );
       
   339                 iPlaybackUtility->PropertyL( *this, EPbPropertyDuration );
       
   340                 RequestMediaL();
       
   341                 break;
       
   342                 }
       
   343             default:
       
   344                 {
       
   345                 break;
       
   346                 }
       
   347             }
       
   348         }
       
   349     }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // Handle playback property.
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 void CAiPlayerPluginEngine::DoHandlePropertyL( TInt aProperty, TInt aValue, TInt aError )
       
   356     {
       
   357     MPX_DEBUG4("CAiPlayerPluginEngine::DoHandlePropertyL - Property(%d); Value(%d); Error(%d)", aProperty, aValue, aError );
       
   358     if ( KErrNone == aError )
       
   359         {
       
   360         switch ( aProperty	)
       
   361             {
       
   362             case EPbPropertyPosition:
       
   363                 {
       
   364 				iPosition = aValue / KMPXOneSecInMilliSecs;
       
   365                 iObserver->PlaybackPositionChanged( iPosition );
       
   366                 break;
       
   367                 }
       
   368             case EPbPropertyDuration:
       
   369                 {
       
   370 				iDuration = aValue / KMPXOneSecInMilliSecs;
       
   371                 break;
       
   372                 }
       
   373             case EPbPropertyMaxVolume:
       
   374                 {
       
   375                 iMaxVolume = aValue;
       
   376                 break;
       
   377                 }
       
   378             case EPbPropertyVolume:
       
   379                 {
       
   380                 TInt volume = MapToAiVolume(aValue);
       
   381 				if ( iVolume != volume )
       
   382 					{
       
   383 					iVolume = volume;
       
   384 					iObserver->VolumeChanged( iVolume );
       
   385 					}
       
   386 
       
   387                 break;
       
   388                 }
       
   389             default:
       
   390                 {
       
   391                 break;
       
   392                 }
       
   393             }
       
   394         }
       
   395     }
       
   396 
       
   397 // ---------------------------------------------------------------------------
       
   398 // Handle playback state changed.
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 void CAiPlayerPluginEngine::DoHandleStateChangedL( TMPXPlaybackState aState )
       
   402     {
       
   403     TMPlayerState state = MapState( aState );
       
   404     MPX_DEBUG3("CAiPlayerPluginEngine::DoHandleStateChangedL - State mapped from (%d) to (%d)", aState, state );
       
   405     iObserver->PlayerStateChanged(state);
       
   406     }
       
   407 
       
   408 // ---------------------------------------------------------------------------
       
   409 // Displays error notes.
       
   410 // ---------------------------------------------------------------------------
       
   411 //
       
   412 void CAiPlayerPluginEngine::HandleErrorL( TInt aError )
       
   413     {
       
   414 	MPX_DEBUG2("CAiPlayerPluginEngine::HandleErrorL(%d)", aError );
       
   415     }
       
   416 
       
   417 // ---------------------------------------------------------------------------
       
   418 // Map states from TMPXPlaybackState to TMPlayerState
       
   419 // ---------------------------------------------------------------------------
       
   420 //
       
   421 TMPlayerState CAiPlayerPluginEngine::MapState( TMPXPlaybackState aState )
       
   422     {
       
   423 	TMPlayerState state = EMPlayerStateOther;
       
   424     switch ( aState )
       
   425         {
       
   426         case EPbStatePlaying:
       
   427             state = EMPlayerStatePlaying;
       
   428             break;
       
   429         case EPbStatePaused:
       
   430             state = EMPlayerStatePaused;
       
   431             break;
       
   432         case EPbStateSeekingForward:
       
   433         case EPbStateSeekingBackward:
       
   434             state = EMPlayerStateSeeking;
       
   435             break;
       
   436         default:
       
   437             break;
       
   438         }
       
   439     return state;
       
   440     }
       
   441 
       
   442 // ---------------------------------------------------------------------------
       
   443 // Maps volume from MPX player to AI volume.
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 TInt CAiPlayerPluginEngine::MapToAiVolume( TInt aVolume )
       
   447     {
       
   448 	TInt volume;
       
   449 	if ( iMaxVolume == KVolumeLevelMax )
       
   450 		{
       
   451 		// No need to translate
       
   452 		volume = aVolume;
       
   453 		}
       
   454 	else if ( aVolume == KVolumeLevelMin )
       
   455 		{
       
   456 		volume = KVolumeLevelMin;
       
   457 		}
       
   458 	else if ( aVolume == iMaxVolume )
       
   459 		{
       
   460 		volume = KVolumeLevelMax;
       
   461 		}
       
   462 	else
       
   463 		{
       
   464 		volume = (TInt) ((aVolume * KVolumeLevelMax) / iMaxVolume);
       
   465 		}
       
   466 	return volume;
       
   467     }
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // Maps volume from AI to MPX player volume.
       
   471 // ---------------------------------------------------------------------------
       
   472 //
       
   473 TInt CAiPlayerPluginEngine::MapToMpxVolume( TInt aVolume )
       
   474     {
       
   475 	TInt volume;
       
   476 	if ( iMaxVolume == KVolumeLevelMax )
       
   477 		{
       
   478 		// No need to translate
       
   479 		volume = aVolume;
       
   480 		}
       
   481 	else if ( aVolume == KVolumeLevelMin )
       
   482 		{
       
   483 		volume = KVolumeLevelMin;
       
   484 		}
       
   485 	else if ( aVolume == KVolumeLevelMax )
       
   486 		{
       
   487 		volume = iMaxVolume;
       
   488 		}
       
   489 	else
       
   490 		{
       
   491 		volume = (TInt) ((aVolume * iMaxVolume) / KVolumeLevelMax);
       
   492 		}
       
   493 	return volume;
       
   494     }
       
   495 
       
   496 // ---------------------------------------------------------------------------
       
   497 // Requests Media.
       
   498 // ---------------------------------------------------------------------------
       
   499 //
       
   500 void CAiPlayerPluginEngine::RequestMediaL()
       
   501     {
       
   502     MMPXSource* s = iPlaybackUtility->Source();
       
   503     if ( s )
       
   504         {
       
   505         RArray<TMPXAttribute> attrs;
       
   506         CleanupClosePushL(attrs);
       
   507         attrs.Append( KMPXMediaGeneralUri );
       
   508         attrs.Append( KMPXMediaGeneralTitle );
       
   509         attrs.Append( KMPXMediaMusicArtist );
       
   510         s->MediaL( attrs.Array(), *this );
       
   511         CleanupStack::PopAndDestroy( &attrs );
       
   512         }
       
   513     }
       
   514 //  End of File