mpx/playbackframework/playbackengine/src/mpxplaybackinitializer.cpp
changeset 0 a2952bb97e68
child 15 d240f0a77280
child 21 a05c44bc3c61
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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:  Initialize a file before playback
       
    15 *
       
    16 */
       
    17 
       
    18 #include <mpxcollectionpath.h>
       
    19 #include <mpxmediageneraldefs.h>
       
    20 #include <mpxmediadrmdefs.h>
       
    21 #include <mpxmediamusicdefs.h>
       
    22 #include <mpxcommandgeneraldefs.h>
       
    23 #include <mpxmedia.h>
       
    24 #include <mpxcollectionplaylist.h>
       
    25 #include <mpxplaybackplugin.h>
       
    26 #include <mpxlog.h>
       
    27 #include <mpxmediageneralextdefs.h>
       
    28 #include <mpxplaybackpluginversion2.h>
       
    29 
       
    30 #include "mpxplaybackinitializer.h"
       
    31 #include "mpxplaybackpluginhandler.h"
       
    32 #include "mpxplaybackengine.h"
       
    33 #include "mpxplaybackmediahelper.h"
       
    34 
       
    35 const TInt KMPXInitTimer = 3000000; // 3 seconds
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CMPXPlaybackInitializer* NewL
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 CMPXPlaybackInitializer* CMPXPlaybackInitializer::NewL(
       
    42     CMPXPlaybackEngine& aEngine,
       
    43     CMPXPlaybackMediaHelper& aMediaHelper )
       
    44     {
       
    45     CMPXPlaybackInitializer* self = new (ELeave)
       
    46         CMPXPlaybackInitializer( aEngine, aMediaHelper );
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // Constructor
       
    55 // ----------------------------------------------------------------------------
       
    56 //
       
    57 CMPXPlaybackInitializer::CMPXPlaybackInitializer(
       
    58     CMPXPlaybackEngine& aEngine,
       
    59     CMPXPlaybackMediaHelper& aMediaHelper )
       
    60     : iState( EPbStateNotInitialised ),
       
    61       iEngine( aEngine ),
       
    62       iMediaHelper( aMediaHelper )
       
    63     {
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // CMPXPlaybackInitializer::ConstructL
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 void CMPXPlaybackInitializer::ConstructL()
       
    71     {
       
    72     iMedia = CMPXMedia::NewL();
       
    73     iTimer = CPeriodic::NewL( CActive::EPriorityIdle );
       
    74     }
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // Destructor
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 CMPXPlaybackInitializer::~CMPXPlaybackInitializer()
       
    81     {
       
    82     delete iMedia;
       
    83     delete iMediaMessage;
       
    84     Close();
       
    85     CancelPreInitTimer();
       
    86     delete iTimer;
       
    87     }
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // CMPXPlaybackInitializer::Init
       
    91 // ----------------------------------------------------------------------------
       
    92 //
       
    93 void CMPXPlaybackInitializer::Init( CMPXCollectionPlaylist& aPlaylist )
       
    94     {
       
    95     MPX_FUNC_EX("CMPXPlaybackInitializer::Init aPlaylist");
       
    96 
       
    97     // Check if we already initialized the song in this playlist,
       
    98     // if so then do nothing
       
    99     if ( !IsInitializing( aPlaylist ))
       
   100         {
       
   101         Reset(); // clean up all resources (start again)
       
   102         delete iHandler;
       
   103         iHandler = NULL;
       
   104 
       
   105         // Save the path index and count.  This is just in case the
       
   106         // engine calls InitL() again before we make a call to
       
   107         // Request media, because these are used to check if we're
       
   108         // initializing the same song in the IsInitializing() method.
       
   109         iPathIndex = aPlaylist.Path().Index();
       
   110         iInitPlaylistCount = iEngine.iPlaylist->Count();
       
   111         }
       
   112     }
       
   113 
       
   114 // ----------------------------------------------------------------------------
       
   115 // CMPXPlaybackInitializer::Next
       
   116 // Advance the playlist and start initializing (return ETrue) or if there's
       
   117 // no next song, does nothing (return EFalse)
       
   118 // ----------------------------------------------------------------------------
       
   119 //
       
   120 TBool CMPXPlaybackInitializer::Next()
       
   121     {
       
   122     MPX_FUNC("CMPXPlaybackInitializer::Next");
       
   123 
       
   124     ASSERT( iEngine.iPlaylist );
       
   125 
       
   126     TInt nextPlIndex( KErrNotFound );
       
   127     TBool playNext( iEngine.iPlaylist->NextIndex( EFalse, nextPlIndex ));
       
   128     Reset(); // Reset state
       
   129 
       
   130     // Only pre-init if there is a next song
       
   131     if ( playNext )
       
   132         {
       
   133         MPX_DEBUG1("CMPXPlaybackInitializer::Next(): Starting timer");
       
   134         iPathIndex = iEngine.iPlaylist->PathIndex( nextPlIndex );
       
   135         iInitPlaylistCount = iEngine.iPlaylist->Count();
       
   136         CancelPreInitTimer();
       
   137         // Start timer
       
   138         iTimer->Start(
       
   139             KMPXInitTimer,
       
   140             KMPXInitTimer,
       
   141             TCallBack(PreInitTimerCallback, this ));
       
   142         }
       
   143     return playNext;
       
   144     }
       
   145 
       
   146 // ----------------------------------------------------------------------------
       
   147 // CMPXPlaybackInitializer::SetActiveL. Swaps the item being handled in the
       
   148 // initializer to the engine, which takes over in whatever state it currently
       
   149 // is in
       
   150 // ----------------------------------------------------------------------------
       
   151 //
       
   152 void CMPXPlaybackInitializer::SetActiveL()
       
   153     {
       
   154     ASSERT(iEngine.iPlaylist);
       
   155     MPX_DEBUG3("-->CMPXPlaybackInitializer::SetActiveL state %d, error %d",
       
   156                 iState, iError);
       
   157     CancelPreInitTimer();
       
   158     switch(iState)
       
   159         {
       
   160         case EPbStateNotInitialised:
       
   161         //
       
   162         // From NewL()/InitL()/NextL()/PreviousL()
       
   163         // up until the requested media URI is returned.
       
   164         // Need to swap the playlist only (no plugin has
       
   165         // yet been resolved
       
   166         //
       
   167             {
       
   168             if (iEngine.iPlaylist)
       
   169                 {
       
   170                 //
       
   171                 // Get the URI from the collection - the callback triggers
       
   172                 // the initialization
       
   173                 //
       
   174                 RequestMediaL();
       
   175                 UpdateMediaForwarding();
       
   176                 }
       
   177             break;
       
   178             }
       
   179 
       
   180         case EPbStateInitialising:
       
   181         //
       
   182         // From NewL()/InitL()/NextL()/PreviousL() up
       
   183         // until the URI has been returned, plug-in is
       
   184         // being intialized OR an error has been returned
       
   185         // from playlist
       
   186         //
       
   187             {
       
   188             UpdateMediaForwarding();
       
   189             SwapUriL();
       
   190             SwapFlag();
       
   191             if (iError==KErrNone)
       
   192                 //
       
   193                 // Plug-in must be initialising
       
   194                 //
       
   195                 {
       
   196                 SwapPluginL();
       
   197                 }
       
   198             else
       
   199                 {
       
   200                 //
       
   201                 // Hasn't got as far as initialising a plug-in
       
   202                 //
       
   203                 iEngine.HandleCollectionMediaL(*iMedia,iError);
       
   204                 }
       
   205             break;
       
   206             }
       
   207 
       
   208         case EPbStateStopped:
       
   209         //
       
   210         // From NewL()/InitL()/NextL()/PreviousL() up
       
   211         // until the plug-in has been initialised, either
       
   212         // successfully or with an error
       
   213         //
       
   214             {
       
   215             UpdateMediaForwarding();
       
   216             SwapPluginL();
       
   217             SwapUriL();
       
   218             SwapFlag();
       
   219             if (iError==KErrNone)
       
   220                 {
       
   221                 iEngine.HandlePluginEvent(EPInitialised,iDuration,KErrNone);
       
   222                 }
       
   223             else
       
   224                 {
       
   225                 iEngine.HandlePluginEvent(EPInitialised,0,iError);
       
   226                 }
       
   227             break;
       
   228             }
       
   229         default:
       
   230             ASSERT(0) ;
       
   231         }
       
   232     MPX_DEBUG1("<--CMPXPlaybackInitializer::SetActiveL");
       
   233     }
       
   234 
       
   235 // ----------------------------------------------------------------------------
       
   236 // CMPXPlaybackInitializer::Close. Resets and destroys all resources owned by
       
   237 // initializer
       
   238 // ----------------------------------------------------------------------------
       
   239 //
       
   240 void CMPXPlaybackInitializer::Close()
       
   241     {
       
   242     MPX_FUNC("CMPXPlaybackInitializer::Close");
       
   243     CancelPreInitTimer();
       
   244     Reset(); // must come after handler deletion
       
   245     delete iHandler;
       
   246     iHandler=NULL;
       
   247     }
       
   248 
       
   249 // ----------------------------------------------------------------------------
       
   250 // Gets the current item id in use by the initializer
       
   251 // ----------------------------------------------------------------------------
       
   252 //
       
   253 TMPXItemId CMPXPlaybackInitializer::CurrentInitItemId()
       
   254     {
       
   255     TMPXItemId id( KMPXInvalidItemId );
       
   256     if( iState != EPbStateNotInitialised && iEngine.iPlaylist )
       
   257         {
       
   258         id = iEngine.iPlaylist->Path().Id();
       
   259         }
       
   260     return id;
       
   261     }
       
   262 
       
   263 // ----------------------------------------------------------------------------
       
   264 // Implementation uid of the plugin for pre-initialzer
       
   265 // ----------------------------------------------------------------------------
       
   266 //
       
   267 TUid CMPXPlaybackInitializer::PluginUid()
       
   268     {
       
   269     TUid uid(KNullUid);
       
   270     if (iHandler)
       
   271         {
       
   272         if (iHandler->Plugin())
       
   273             {
       
   274             uid = iHandler->Plugin()->Uid();
       
   275             }
       
   276         }
       
   277     return uid;
       
   278     }
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // CMPXPlaybackInitializer::Reset. Resets such that next media can be initialised
       
   282 // ----------------------------------------------------------------------------
       
   283 //
       
   284 void CMPXPlaybackInitializer::Reset()
       
   285     {
       
   286     MPX_FUNC("CMPXPlaybackInitializer::Reset");
       
   287     //
       
   288     // Cancel any activity with current plug-in
       
   289     //
       
   290     if (iHandler)
       
   291         {
       
   292         if (iHandler->Plugin())
       
   293             {
       
   294             TRAP_IGNORE(iHandler->Plugin()->CommandL(EPbCmdStop));
       
   295             TRAP_IGNORE(iHandler->Plugin()->CommandL(EPbCmdClose));
       
   296             }
       
   297         }
       
   298     //
       
   299     // Reset data associated with previous initialization
       
   300     //
       
   301     iState = EPbStateNotInitialised;
       
   302     iError = KErrNone;
       
   303     iDuration = 0;
       
   304     iPathIndex = KErrNotFound;
       
   305     iInitPlaylistCount = 0;
       
   306     }
       
   307 
       
   308 // ----------------------------------------------------------------------------
       
   309 // CMPXPlaybackInitializer::RequestMediaL
       
   310 // Get the Media from the collection - the callback triggers
       
   311 // the initialization
       
   312 // ----------------------------------------------------------------------------
       
   313 //
       
   314 void CMPXPlaybackInitializer::RequestMediaL( )
       
   315     {
       
   316     ASSERT(iEngine.iPlaylist);
       
   317 
       
   318     MPX_DEBUG3("-->CMPXPlaybackInitializer::RequestMediaL 0x%08x item index %d",
       
   319                this, iEngine.iPlaylist->Index());
       
   320     // Reset media message
       
   321     delete iMediaMessage;
       
   322     iMediaMessage = NULL;
       
   323     iErrorOfMediaMessage = KErrNone;
       
   324 
       
   325     // Setup attribute array
       
   326     RArray<TMPXAttribute> attrs;
       
   327     CleanupClosePushL(attrs);
       
   328     attrs.Append(KMPXMediaGeneralId);
       
   329     attrs.Append(KMPXMediaGeneralUri);
       
   330     attrs.Append(KMPXMediaGeneralMimeType);
       
   331     attrs.Append(KMPXMediaGeneralLastPlaybackPosition);
       
   332     attrs.Append(KMPXMediaGeneralFlags);
       
   333     attrs.Append(KMPXMediaGeneralDuration);
       
   334     attrs.Append(KMPXMediaGeneralTitle);
       
   335     attrs.Append( KMPXMediaMusicArtist );
       
   336 
       
   337     // Create object to hold attributes and specs
       
   338     CMPXCommand* cmd = CMPXCommand::NewL();
       
   339     CleanupStack::PushL( cmd );
       
   340     cmd->SetTObjectValueL<TInt>(KMPXCommandGeneralId, KMPXCommandContentIdMedia);
       
   341     CBufBase* buf = CBufFlat::NewL( KMPXBufGranularity );
       
   342     CleanupStack::PushL( buf );
       
   343 
       
   344     RBufWriteStream writeStream( *buf );
       
   345     CleanupClosePushL( writeStream );
       
   346     // externalize attributes array
       
   347     ::ExternalizeL(attrs.Array(), writeStream);
       
   348     // Close and compress buffer
       
   349     writeStream.CommitL();
       
   350     buf->Compress();
       
   351     CleanupStack::PopAndDestroy( &writeStream );
       
   352     TPtrC ptr( MPXUser::Ptr( buf->Ptr( 0 )));
       
   353     cmd->SetTextValueL( KMPXCommandMediaAttribute, ptr );
       
   354 
       
   355     CMPXAttributeSpecs* attrSpecs( CMPXAttributeSpecs::NewL() );
       
   356     CleanupStack::PushL( attrSpecs );
       
   357     cmd->SetCObjectValueL<CMPXAttributeSpecs>(
       
   358         KMPXCommandMediaAttributeSpecs, attrSpecs );
       
   359     CleanupStack::PopAndDestroy( attrSpecs );
       
   360 
       
   361     // Instead of copying the whole path object, which may be large due
       
   362     // to number of songs in the list, just get the container path and
       
   363     // append the item ID to that.  This saves memory and time.
       
   364     const CMPXCollectionPath& path( iEngine.iPlaylist->Path() );
       
   365     CMPXCollectionPath* initPath( path.ContainerPathL() );
       
   366     CleanupStack::PushL( initPath );
       
   367     initPath->AppendL( path.IdOfIndex( iPathIndex ) );
       
   368 
       
   369     iMediaHelper.MediaL( *initPath,
       
   370                          cmd,   // ownership passed
       
   371                          this );
       
   372     CleanupStack::PopAndDestroy( initPath );
       
   373     CleanupStack::PopAndDestroy( buf );
       
   374     CleanupStack::Pop( cmd );
       
   375     CleanupStack::PopAndDestroy( &attrs );
       
   376     iMediaRequested = ETrue;
       
   377     MPX_DEBUG2("<--CMPXPlaybackInitializer::RequestMediaL 0x%08x", this);
       
   378     }    
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // CMPXPlaybackInitializer::UpdateMediaForwarding. Updates the forwarding variable
       
   382 // to send the media to the engine if a media has been requested.
       
   383 // ----------------------------------------------------------------------------
       
   384 //
       
   385 void CMPXPlaybackInitializer::UpdateMediaForwarding()
       
   386     {
       
   387     MPX_FUNC("CMPXPlaybackInitializer::UpdateMediaForwarding");
       
   388 
       
   389     // Forward the media to the engine if it has been requested.
       
   390 
       
   391     // The difference between the iMediaToEngine and iMediaRequested flags,
       
   392     // is that if the engine calls this method first (through SetActive()),
       
   393     // but the media has not been requested yet, then we want to make sure
       
   394     // in that case to not forward the subsequent media requests
       
   395     iMediaToEngine = iMediaRequested ? ETrue : EFalse;
       
   396     }
       
   397 
       
   398 // ----------------------------------------------------------------------------
       
   399 // CMPXPlaybackInitializer::SwapPluginL. Swaps the plugin being handled in the
       
   400 // initializer to the engine, which takes over in whatever state it currently
       
   401 // is in
       
   402 // ----------------------------------------------------------------------------
       
   403 //
       
   404 void CMPXPlaybackInitializer::SwapPluginL()
       
   405     {
       
   406     MPX_DEBUG1("==>CMPXPlaybackInitializer::SwapPluginL");
       
   407     // unfinished business with old playback plug-in
       
   408     if (iEngine.iPluginHandler->Plugin())
       
   409         {
       
   410         iEngine.iPluginHandler->Plugin()->CancelRequest();
       
   411         }
       
   412     TInt index( iEngine.iPlaylist->Index() );
       
   413     TMPXPlaybackMessage msg( TMPXPlaybackMessage::EStateChanged,
       
   414                              EPbStateInitialising,
       
   415                              index );
       
   416     iEngine.SetStateL( msg );
       
   417     TRAPD(err, iEngine.DoStopL());
       
   418     if (err)
       
   419         { // play is not available. e.g. power off
       
   420         MPX_DEBUG2("CMPXPlaybackInitializer::SwapPluginL stop leave %d", err);
       
   421         iError = err; // save error
       
   422         }
       
   423     else
       
   424         {
       
   425         if ( iMedia )
       
   426             {
       
   427             iEngine.RestorePlaybackPositionAndStateL( *iMedia );
       
   428             }
       
   429         //
       
   430         // Set plug-in handler
       
   431         //
       
   432         if ( iHandler )
       
   433             {
       
   434             CMPXPlaybackPluginHandler*& h=iEngine.iPluginHandler;
       
   435             delete h; // delete engine's handler
       
   436 
       
   437             // plug-in handler now reports to engine
       
   438             iHandler->SetObserver(iEngine); 
       
   439 
       
   440             if (iHandler->Plugin())
       
   441                 {
       
   442                 iHandler->Plugin()->SetObserver(iEngine);
       
   443                 }
       
   444 
       
   445             h=iHandler; // replace engine's handler
       
   446             iHandler=NULL; // reset handler in initializer
       
   447             }
       
   448 
       
   449         if (iMediaMessage)
       
   450             {
       
   451             iEngine.HandlePlaybackMessage(iMediaMessage, iErrorOfMediaMessage);
       
   452             }
       
   453         }
       
   454     MPX_DEBUG1("<==CMPXPlaybackInitializer::SwapPluginL");
       
   455     }
       
   456 
       
   457 // ----------------------------------------------------------------------------
       
   458 // CMPXPlaybackInitializer::SwapUriL. Swaps the item uri in the engine
       
   459 // Only in the EPbStateInitialising and EPbStateStopped states
       
   460 // ----------------------------------------------------------------------------
       
   461 //
       
   462 void CMPXPlaybackInitializer::SwapUriL()
       
   463     {
       
   464     // Update the engine's uri to the item that was in the initializer
       
   465     //
       
   466     delete iEngine.iUri ;
       
   467     iEngine.iUri = NULL;
       
   468     if ( iMedia->IsSupported( KMPXMediaGeneralUri ))
       
   469         {
       
   470         iEngine.iUri = iMedia->ValueText(KMPXMediaGeneralUri).AllocL();
       
   471         }
       
   472     }
       
   473 
       
   474 // ----------------------------------------------------------------------------
       
   475 // CMPXPlaybackInitializer::SwapFlag. Swaps the item db flag in the engine
       
   476 // Only in the EPbStateInitialising and EPbStateStopped states
       
   477 // ----------------------------------------------------------------------------
       
   478 //
       
   479 void CMPXPlaybackInitializer::SwapFlag()
       
   480     {
       
   481     // Update the engine's flag to the item that was in the initializer
       
   482     //
       
   483     if ( iMedia->IsSupported( KMPXMediaGeneralFlags ) )
       
   484         {
       
   485         iEngine.iDbFlag = iMedia->ValueTObjectL<TUint>( KMPXMediaGeneralFlags );
       
   486         }
       
   487     else // not supported, reset to 0
       
   488         {
       
   489         iEngine.iDbFlag = 0;
       
   490         }
       
   491 
       
   492     // Also swap item id
       
   493     if ( iMedia->IsSupported( KMPXMediaGeneralId ))
       
   494         {
       
   495         iEngine.iItemId = iMedia->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
       
   496         }
       
   497     else
       
   498         {
       
   499         iEngine.iItemId = KMPXInvalidItemId;
       
   500         }
       
   501 
       
   502     // Also swap duration value
       
   503     if ( iMedia->IsSupported( KMPXMediaGeneralDuration ))
       
   504         {
       
   505         iEngine.iMediaDuration = iMedia->ValueTObjectL<TInt>( KMPXMediaGeneralDuration );
       
   506         }
       
   507     else
       
   508         {
       
   509         iEngine.iMediaDuration = 0;
       
   510         }
       
   511     iEngine.iAccessPointSet = EFalse;
       
   512     if ( iMedia->IsSupported(KMPXMediaGeneralExtAccessPoint) )
       
   513         {
       
   514         TInt accessPoint = iMedia->ValueTObjectL<TInt>( KMPXMediaGeneralExtAccessPoint );
       
   515         iEngine.iAccessPoint = accessPoint;
       
   516         if ( accessPoint )
       
   517             {
       
   518             iEngine.iAccessPointSet = ETrue;
       
   519             }
       
   520         }
       
   521     else
       
   522         {
       
   523         iEngine.iAccessPoint = 0;
       
   524         }
       
   525     }
       
   526 
       
   527 // ----------------------------------------------------------------------------
       
   528 // CMPXPlaybackInitializer::IsInitializing
       
   529 // ----------------------------------------------------------------------------
       
   530 //
       
   531 TBool CMPXPlaybackInitializer::IsInitializing(
       
   532     const CMPXCollectionPlaylist& aPlaylist)
       
   533     {
       
   534     MPX_FUNC("CMPXPlaybackInitializer::IsInitializing aPlaylist");
       
   535     TBool ret=ETrue;
       
   536 
       
   537     if ( !iEngine.iPlaylist )
       
   538         {
       
   539         ret=EFalse;
       
   540         }
       
   541     else if ( iPathIndex != aPlaylist.Path().Index() ||
       
   542               iInitPlaylistCount != aPlaylist.Count() ||
       
   543               aPlaylist.Levels() == 0 )
       
   544         {
       
   545         ret=EFalse;
       
   546         }
       
   547     else if ( iHandler )
       
   548     //
       
   549     // Check that PI obeys the same selection criteria as that of the
       
   550     // engine, else we may have the wrong plug-in
       
   551     //
       
   552         {
       
   553         if (iHandler->Plugin())
       
   554             {
       
   555             TMPXPlaybackPlayerType t1;
       
   556             TUid u1;
       
   557             TInt i1;
       
   558             TPtrC n1;
       
   559             iHandler->GetSelection(t1,u1,i1,n1);
       
   560 
       
   561             TMPXPlaybackPlayerType t2;
       
   562             TUid u2;
       
   563             TInt i2;
       
   564             TPtrC n2;
       
   565             iEngine.iPluginHandler->GetSelection(t2,u2,i2,n2);
       
   566 
       
   567             ret=(t1==t2 && u1==u2 && i1==i2);
       
   568             }
       
   569         }
       
   570 
       
   571     MPX_DEBUG3("CMPXPlaybackInitializer::IsInitializing plugin=%08x, ret=%d",
       
   572                iHandler ? iHandler->Plugin() : NULL, ret);
       
   573     return ret;
       
   574     }
       
   575 
       
   576 // ----------------------------------------------------------------------------
       
   577 // CMPXPlaybackInitializer::InitL
       
   578 // ----------------------------------------------------------------------------
       
   579 //
       
   580 void CMPXPlaybackInitializer::InitL(const CMPXMedia& aMedia,TInt aError)
       
   581     {
       
   582     MPX_FUNC("CMPXPlaybackInitializer::InitL(const CMPXMedia& aMedia,TInt aError)");
       
   583     iState=EPbStateInitialising;
       
   584     if (aError==KErrNone)
       
   585         {
       
   586         *iMedia = aMedia; // Copy and save media(Uri) from collection
       
   587 
       
   588         //
       
   589         // No error, get the URI
       
   590         //
       
   591         const TDesC& uri=aMedia.ValueText(KMPXMediaGeneralUri);
       
   592         if (uri.Length())
       
   593             {
       
   594             //
       
   595             // Got the URI, so get a plug-in and go
       
   596             //
       
   597             if (!iHandler)
       
   598                 {
       
   599                 iHandler=CMPXPlaybackPluginHandler::NewL(iEngine.iPluginMonitor,
       
   600                                                          *this, *this);
       
   601                                                         // arg=plug-in observer
       
   602                 //
       
   603                 // Copy selection criteria
       
   604                 //
       
   605                 TMPXPlaybackPlayerType t;
       
   606                 TUid u;
       
   607                 TInt i;
       
   608                 TPtrC n;
       
   609                 iEngine.iPluginHandler->GetSelection(t,u,i,n);
       
   610 
       
   611                 if (i!=KErrNotFound)
       
   612                     {
       
   613                     iHandler->SelectSubPlayerL(u,i);
       
   614                     }
       
   615                 else if (u!=KNullUid)
       
   616                     {
       
   617                     iHandler->SelectPlayerL(u);
       
   618                     }
       
   619                 else
       
   620                     {
       
   621                     iHandler->SelectPlayersL(t);
       
   622                     }
       
   623                 }
       
   624             TInt accessPoint = 0;
       
   625             if ( aMedia.IsSupported(KMPXMediaGeneralExtAccessPoint) )
       
   626                 {
       
   627                 accessPoint = aMedia.ValueTObjectL<TInt>( KMPXMediaGeneralExtAccessPoint );
       
   628                 MPX_DEBUG2("CMPXPlaybackInitializer::InitL accessPoint %d", accessPoint );
       
   629                 }
       
   630             HBufC8* mimeType =
       
   631                     MPXUser::Alloc8L(aMedia.ValueText(KMPXMediaGeneralMimeType));
       
   632             CleanupStack::PushL(mimeType);
       
   633             iHandler->SelectPlayerL(uri,*mimeType);
       
   634             MPX_DEBUG2("CMPXPlaybackInitializer::InitL plugin=%08x",iHandler->Plugin());
       
   635             // Plugin handler guarantee to choose a plugin
       
   636             // In case there is file open in the audio controller
       
   637             iHandler->Plugin()->CommandL(EPbCmdClose);
       
   638             InitL(uri, *mimeType, accessPoint);
       
   639             CleanupStack::PopAndDestroy(mimeType);
       
   640             }
       
   641         else  // No URI
       
   642             {
       
   643             iError=KErrNotFound;
       
   644             }
       
   645         }
       
   646     else // aError not equal to KErrNone
       
   647         {
       
   648         iError=aError;
       
   649 
       
   650         // Reset media since if there's an error, the media
       
   651         // object passed in is unreliable
       
   652         delete iMedia;
       
   653         iMedia = NULL;
       
   654         iMedia = CMPXMedia::NewL();
       
   655         }
       
   656     }
       
   657 
       
   658 // ----------------------------------------------------------------------------
       
   659 // Cancels the pre-init timer
       
   660 // ----------------------------------------------------------------------------
       
   661 void CMPXPlaybackInitializer::CancelPreInitTimer()
       
   662     {
       
   663     MPX_FUNC("CMPXPlaybackInitializer::CancelPreInitTimer()");
       
   664     if ( iTimer && iTimer->IsActive() )
       
   665         {
       
   666         MPX_DEBUG1("CMPXPlaybackInitializer::CancelPreInitTimer(): Timer active, cancelling");
       
   667         iTimer->Cancel();
       
   668         }
       
   669     }
       
   670 
       
   671 // ----------------------------------------------------------------------------
       
   672 // Handle pre-init timer callback
       
   673 // ----------------------------------------------------------------------------
       
   674 void CMPXPlaybackInitializer::HandlePreInitTimerCallback()
       
   675     {
       
   676     MPX_FUNC("CMPXPlaybackInitializer::HandlePreInitTimerCallback()");
       
   677 
       
   678     CancelPreInitTimer();
       
   679 
       
   680     TRAP_IGNORE(RequestMediaL());
       
   681     }
       
   682 
       
   683 // ----------------------------------------------------------------------------
       
   684 // Callback for pre-init timer.
       
   685 // ----------------------------------------------------------------------------
       
   686 TInt CMPXPlaybackInitializer::PreInitTimerCallback(TAny* aPtr)
       
   687     {
       
   688     MPX_FUNC("CMPXPlaybackInitializer::PreInitTimerCallback()");
       
   689 
       
   690     CMPXPlaybackInitializer* ptr =
       
   691         static_cast<CMPXPlaybackInitializer*>(aPtr);
       
   692     ptr->HandlePreInitTimerCallback();
       
   693 
       
   694     return KErrNone;
       
   695     }
       
   696 
       
   697 // ----------------------------------------------------------------------------
       
   698 // CMPXPlaybackInitializer::HandlePluginEventL. Must be non leaving since client has
       
   699 // no context as this is a callback
       
   700 // ----------------------------------------------------------------------------
       
   701 
       
   702 void CMPXPlaybackInitializer::HandlePluginEvent(
       
   703     TEvent aEvent,TInt aData,TInt aError)
       
   704     {
       
   705     TRAP_IGNORE( HandlePluginEventL( aEvent, aData, aError ) );
       
   706     }
       
   707 
       
   708 // ----------------------------------------------------------------------------
       
   709 // Leaving version of CMPXPlaybackInitializer::HandlePluginEvent
       
   710 // ----------------------------------------------------------------------------
       
   711 void CMPXPlaybackInitializer::HandlePluginEventL(
       
   712     TEvent aEvent,TInt aData,TInt aError)
       
   713     {
       
   714     MPX_DEBUG3("CMPXPlaybackInitializer::HandlePluginEventL %d, error %d",
       
   715                aEvent, aError);
       
   716     if (aEvent==EPInitialised)
       
   717         {
       
   718         if (aError!=KErrNone)
       
   719             {
       
   720             iError=aError;
       
   721             }
       
   722         else
       
   723             {
       
   724             iDuration=aData;
       
   725             if ( iMedia->IsSupported( KMPXMediaGeneralDuration ))
       
   726                 {
       
   727                 TInt dur( iMedia->ValueTObjectL<TInt>( KMPXMediaGeneralDuration ));
       
   728                 if ( dur != iDuration )
       
   729                     {
       
   730                     // Set duration if it's different than what collection has
       
   731                     if ( iEngine.iPlaylist )
       
   732                         {
       
   733                         if ( iEngine.iPlaylist->Count() && !iEngine.iPlaylist->EmbeddedPlaylist() )
       
   734                             {
       
   735                             const CMPXCollectionPath& path( iEngine.iPlaylist->Path() );
       
   736                             CMPXMedia* media = CMPXMedia::NewL();
       
   737                             CleanupStack::PushL(media);
       
   738                             media->SetTObjectValueL( KMPXMediaGeneralType,
       
   739                                                      EMPXItem );
       
   740                             media->SetTObjectValueL( KMPXMediaGeneralCategory,
       
   741                                                      EMPXSong );
       
   742                             media->SetTObjectValueL( KMPXMediaGeneralCollectionId,
       
   743                                                      path.Id(
       
   744                                                      CMPXCollectionPath::ECollectionUid ));
       
   745                             media->SetTObjectValueL<TMPXItemId>(
       
   746                                 KMPXMediaGeneralId,
       
   747                                 iMedia->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   748                             media->SetTObjectValueL( KMPXMediaGeneralDuration,
       
   749                                                      iDuration );
       
   750                             iEngine.iPlaylist->SetL( *media );
       
   751                             CleanupStack::PopAndDestroy( media );
       
   752                             }
       
   753                         }
       
   754                     }
       
   755                 }
       
   756             }
       
   757         iState=EPbStateStopped;
       
   758         }
       
   759     }
       
   760 
       
   761 // ----------------------------------------------------------------------------
       
   762 // Callback from plug-in, handle plugin message
       
   763 // ----------------------------------------------------------------------------
       
   764 //
       
   765 void CMPXPlaybackInitializer::HandlePlaybackMessage(
       
   766     CMPXMessage* aMsg,
       
   767     TInt aErr)
       
   768     {
       
   769     // Save the media update message
       
   770     TRAP_IGNORE(iMediaMessage = CMPXMessage::NewL(*aMsg));
       
   771     iErrorOfMediaMessage = aErr;
       
   772     }
       
   773 
       
   774 // ----------------------------------------------------------------------------
       
   775 // CMPXPlaybackInitializer::HandleProperty
       
   776 // ----------------------------------------------------------------------------
       
   777 //
       
   778 void CMPXPlaybackInitializer::HandleProperty(
       
   779     TMPXPlaybackProperty /*aProperty*/,
       
   780     TInt /*aValue*/,
       
   781     TInt /*aError*/)
       
   782     {
       
   783     }
       
   784 
       
   785 // ----------------------------------------------------------------------------
       
   786 // CMPXPlaybackInitializer::HandleSubPlayerNames
       
   787 // ----------------------------------------------------------------------------
       
   788 //
       
   789 void CMPXPlaybackInitializer::HandleSubPlayerNames(
       
   790     TUid /*aPlayer*/,
       
   791     const MDesCArray* /*aSubPlayers*/,
       
   792     TBool /*aComplete*/,
       
   793     TInt /*aError*/)
       
   794     {
       
   795     }
       
   796 
       
   797 // ----------------------------------------------------------------------------
       
   798 // CMPXPlaybackInitializer::HandleMedia
       
   799 // ----------------------------------------------------------------------------
       
   800 //
       
   801 void CMPXPlaybackInitializer::HandleMedia(
       
   802     const CMPXMedia& aMedia,
       
   803     TInt aError)
       
   804     {
       
   805     MPX_FUNC("CMPXPlaybackInitializer::HandleMedia()");
       
   806 
       
   807     iMediaRequested = EFalse;
       
   808     if ( iMediaToEngine )
       
   809         {
       
   810         TRAP_IGNORE( iEngine.HandleCollectionMediaL( aMedia,iError) );
       
   811         iMediaToEngine = EFalse;
       
   812         }
       
   813     else
       
   814         {
       
   815         TRAPD( err, InitL( aMedia,aError ));
       
   816         if ( err )
       
   817             { // Set the error so that not switch to un-initialized plugin
       
   818             MPX_DEBUG2("CMPXPlaybackInitializer::HandleMedia TRAP InitL %d",
       
   819                        err);
       
   820             iError = err; // reset error code to new error
       
   821             }
       
   822         }
       
   823     }
       
   824 
       
   825 // ----------------------------------------------------------------------------
       
   826 // CMPXPlaybackInitializer::HandlePluginHandlerEvent
       
   827 // Rely on the playback engine to handle the plugin handler events.
       
   828 // ----------------------------------------------------------------------------
       
   829 //
       
   830 void CMPXPlaybackInitializer::HandlePluginHandlerEvent(
       
   831     TPluginHandlerEvents /* aEvent */,
       
   832     const TUid& /* aPluginUid */,
       
   833     TBool /* aLoaded */,
       
   834     TInt /* aData */)
       
   835     {
       
   836     }
       
   837 
       
   838 // ----------------------------------------------------------------------------
       
   839 // CMPXPlaybackInitializer::InitL
       
   840 // Initialize.
       
   841 // ----------------------------------------------------------------------------
       
   842 //
       
   843 void CMPXPlaybackInitializer::InitL(const TDesC& aUri, const TDesC8& aType, TInt aAccessPoint)
       
   844     {
       
   845     MPX_FUNC("CMPXPlaybackInitializer::InitL");
       
   846     CMPXPlaybackPlugin* p = iHandler->Plugin();
       
   847     if ( !p )
       
   848         {
       
   849         MPX_DEBUG1("CMPXPlaybackInitializer::InitL(): *** Init failed -- No plugin selected");                
       
   850         return;
       
   851         }
       
   852     
       
   853     // Check if version2 interface is supported
       
   854     CDesCArray* interfaces = iHandler->SupportedInterfacesL( p->Uid() );
       
   855     TBool version2InterfaceSupported = EFalse;
       
   856     if (interfaces->MdcaCount())
       
   857         {
       
   858         TInt pos(0);            
       
   859         version2InterfaceSupported = !interfaces->FindIsq(KMPXPlaybackPluginVersion2, pos);
       
   860         }
       
   861     delete interfaces;
       
   862     
       
   863     if (version2InterfaceSupported)
       
   864         {
       
   865         // cast the plugin to use our interface
       
   866         CMPXPlaybackPluginVersion2* plugin = NULL;
       
   867         plugin = static_cast<CMPXPlaybackPluginVersion2*>(p);
       
   868         
       
   869         // if cast was successful, then init streaming with access point
       
   870         if (plugin)
       
   871             {
       
   872             if ( aAccessPoint )
       
   873                 {
       
   874                 plugin->InitStreamingL( aUri, aType, aAccessPoint );
       
   875                 }
       
   876             else
       
   877                 {
       
   878                 plugin->InitialiseL(aUri);
       
   879                 }
       
   880             }
       
   881         else
       
   882             {
       
   883             MPX_DEBUG1("CMPXPlaybackInitializer::InitL(): *** Init failed -- failure to convert to expected interface");                
       
   884             }
       
   885         }
       
   886     else
       
   887         {
       
   888         p->InitialiseL(aUri);
       
   889         }
       
   890     }
       
   891 
       
   892 // end of file