upnpframework/upnpmusicadapter/src/upnpmusicadapter.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     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:  handles music playing within UPnP framework
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // MPX framework api
       
    21 #include <mpxcommonframeworkdefs.h>
       
    22 #include <mpxcollectionplaylist.h>
       
    23 #include <mpxplaybackframeworkdefs.h>
       
    24 #include <mpxmediaarray.h>
       
    25 #include <mpxmediageneraldefs.h>
       
    26 #include <mpxmediacontainerdefs.h>
       
    27 #include <mpxplaybackutility.h>
       
    28 #include <mpxcollectionutility.h>
       
    29 #include <mpxcollectionplaylist.h>
       
    30 #include <mpxplaybackmessage.h>
       
    31 #include <mpxviewutility.h>
       
    32 #include <mpxcollectionpath.h>
       
    33 #include <mpxcollectionmessage.h>
       
    34 #include <mpxmessagegeneraldefs.h>
       
    35 
       
    36 // upnpframework / avcontroller api
       
    37 #include "upnpavcontroller.h"
       
    38 #include "upnpavbrowsingsession.h"
       
    39 #include "upnpavdevice.h"
       
    40 
       
    41 // musicadapter internal
       
    42 #include "upnpmpxhelper.h"
       
    43 #include "upnpplaylistfiller.h"
       
    44 #include "upnpmusicadapter.h"
       
    45 
       
    46 // debug
       
    47 _LIT16( KComponentLogfile, "musicadapter.txt" );
       
    48 #include "upnplog.h"
       
    49 
       
    50 // constants
       
    51 _LIT16( KTempPlaylistTitle, "TempPlaylist" );
       
    52 const TUid KRemotePlayerUid = { 0x200075D8 }; // Remote player UID
       
    53 const TUid KLocalPlayerUid = { 0x200075D9 };  // Local player UID
       
    54 const TUid KHomeConnectUid = { 0x10208A0A };  // Home Connect app UID
       
    55 const TUid KPlaybackDialogUid = { 0x101FFCBD }; // UPnPPlayback dialog UID
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 
       
    59 // --------------------------------------------------------------------------
       
    60 // CUPnPMusicAdapter::NewL
       
    61 // 1st phase constructor.
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 EXPORT_C CUPnPMusicAdapter* CUPnPMusicAdapter::NewL( 
       
    65 MUPnPAVController& aAVController )
       
    66     {
       
    67     __LOG( "CUPnPMusicAdapter::NewL" );
       
    68     
       
    69     CUPnPMusicAdapter* self = new(ELeave) CUPnPMusicAdapter( aAVController );
       
    70     __LOG1( "CUPnPMusicAdapter: self = %x", self );
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     CleanupStack::Pop( self );
       
    74     return self;
       
    75     }
       
    76 
       
    77 // --------------------------------------------------------------------------
       
    78 // CUPnPMusicAdapter::CUPnPMusicAdapter
       
    79 // Default constructor.
       
    80 // --------------------------------------------------------------------------
       
    81 //
       
    82 CUPnPMusicAdapter::CUPnPMusicAdapter( MUPnPAVController& aAVController )
       
    83     : iAVController( aAVController )
       
    84     {
       
    85     iTrackArray = 0;
       
    86     iPlaylist = 0;
       
    87     iState = EStateIdle;
       
    88     }
       
    89 
       
    90 // --------------------------------------------------------------------------
       
    91 // CUPnPMusicAdapter::ConstructL
       
    92 // 1st phase constructor.
       
    93 // --------------------------------------------------------------------------
       
    94 // 
       
    95 void CUPnPMusicAdapter::ConstructL()
       
    96     {
       
    97     __LOG( "CUPnPMusicAdapter::ConstructL" );
       
    98     
       
    99     // Create playbackutility for playback comands
       
   100     iPlaybackUtility = MMPXPlaybackUtility::NewL( KHomeConnectUid, this );
       
   101     __LOG1( "iPlaybackUtility = %x", iPlaybackUtility );
       
   102 
       
   103     // Create collectionutility 
       
   104     iCollectionUtility = MMPXCollectionUtility::NewL( this, 
       
   105     KHomeConnectUid );
       
   106     __LOG1( "iCollectionUtility = %x", iCollectionUtility );
       
   107             
       
   108     // Get an instance of view utility
       
   109     iViewUtility = MMPXViewUtility::UtilityL();
       
   110     __LOG1( "iViewUtility = %x", iViewUtility );
       
   111     
       
   112     // internal MPX helper for some advanced operations
       
   113     iMpxHelper = CUPnPMpxHelper::NewL( KHomeConnectUid );
       
   114 
       
   115     __LOG( "CUPnPMusicAdapter::ConstructL - end" );
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------
       
   119 // CUPnPMusicAdapter::~CUPnPMusicAdapter
       
   120 // Destructor.
       
   121 // --------------------------------------------------------------------------
       
   122 //
       
   123 EXPORT_C CUPnPMusicAdapter::~CUPnPMusicAdapter()
       
   124     {
       
   125     __LOG( "CUPnPMusicAdapter::~CUPnPMusicAdapter()" );
       
   126 
       
   127     if ( iCurrentFillerState == EFillerStateFilling )
       
   128         {
       
   129         iCurrentFiller->CancelFill();
       
   130         }
       
   131 
       
   132     delete iMedia;
       
   133     delete iCurrentFiller;    
       
   134     delete iTrackArray;    
       
   135     delete iPlaylist;
       
   136     delete iWait;
       
   137     delete iMpxHelper;
       
   138 
       
   139     if( iPlaybackUtility )
       
   140         {
       
   141         iPlaybackUtility->Close();
       
   142         iPlaybackUtility = 0;
       
   143         }
       
   144         
       
   145         
       
   146     if( iCollectionUtility )
       
   147         {
       
   148         iCollectionUtility->Close();
       
   149         iCollectionUtility = 0;
       
   150         }
       
   151     
       
   152     if( iViewUtility )
       
   153         {
       
   154         iViewUtility->Close();
       
   155         iViewUtility = 0;
       
   156         }
       
   157     __LOG( "CUPnPMusicAdapter::~CUPnPMusicAdapter - End" );
       
   158     }
       
   159 
       
   160 // --------------------------------------------------------------------------
       
   161 // CUPnPMusicAdapter::ExecuteMusicLiteInRemoteL
       
   162 // --------------------------------------------------------------------------
       
   163 //
       
   164 EXPORT_C TInt CUPnPMusicAdapter::ExecuteMusicLiteInRemoteL(
       
   165     CEikAppUi* aAppUi,
       
   166     MUPnPPlaylistFiller* aFiller,
       
   167     const CUpnpAVDevice& aRenderer )
       
   168     {
       
   169     __LOG( "CUPnPMusicAdapter::ExecuteMusicLiteInRemoteL" );
       
   170 
       
   171     InitState();
       
   172     iAppUi = aAppUi;     
       
   173     iRemotePlayer = ETrue;
       
   174 
       
   175     TakeOwnershipOfTheFiller( aFiller );
       
   176     iTargetDevice = &aRenderer;
       
   177 
       
   178     // Create and fill playlist
       
   179     CreatePlayListL();
       
   180 
       
   181     // show the UI
       
   182     UiShowLoopL();
       
   183  
       
   184      __LOG1( "CUPnPMusicAdapter::ExecuteMusicInRemoteL returns %d",
       
   185         iReturnCode );
       
   186     return iReturnCode;
       
   187     }
       
   188 
       
   189 // --------------------------------------------------------------------------
       
   190 // CUPnPMusicAdapter::ExecuteMusicLiteInLocalL
       
   191 // --------------------------------------------------------------------------
       
   192 //
       
   193 EXPORT_C TInt CUPnPMusicAdapter::ExecuteMusicLiteInLocalL(
       
   194     CEikAppUi* aAppUi,
       
   195     MUPnPPlaylistFiller* aFiller )
       
   196     {
       
   197     __LOG( "CUPnPMusicAdapter::ExecuteMusicLiteInLocalL" );
       
   198 
       
   199     InitState();
       
   200     iAppUi = aAppUi;
       
   201     iRemotePlayer = EFalse;
       
   202     
       
   203     TakeOwnershipOfTheFiller( aFiller );
       
   204     iTargetDevice = 0;
       
   205     
       
   206     // Create and fill playlist
       
   207     CreatePlayListL();
       
   208 
       
   209     // show the UI
       
   210     UiShowLoopL();
       
   211 
       
   212     __LOG1( "CUPnPMusicAdapter::ExecuteMusicInLocalL returns %d",
       
   213         iReturnCode );
       
   214     return iReturnCode;
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // CUPnPMusicAdapter::ExecuteMusicInRemoteL
       
   219 // --------------------------------------------------------------------------
       
   220 //
       
   221 EXPORT_C TInt CUPnPMusicAdapter::ExecuteMusicInRemoteL(
       
   222     CEikAppUi* aAppUi,
       
   223     CUPnPPlayListFiller* aFiller,
       
   224     const CUpnpAVDevice& aRenderer )
       
   225     {
       
   226     __LOG( "CUPnPMusicAdapter::ExecuteMusicInRemoteL" );
       
   227 
       
   228     InitState();
       
   229     iAppUi = aAppUi;     
       
   230     iRemotePlayer = ETrue;
       
   231 
       
   232     TakeOwnershipOfTheFiller( aFiller );
       
   233     iTargetDevice = &aRenderer;
       
   234 
       
   235     // Create and fill playlist
       
   236     CreatePlayListL();
       
   237 
       
   238     // show the UI
       
   239     UiShowLoopL();
       
   240  
       
   241      __LOG1( "CUPnPMusicAdapter::ExecuteMusicInRemoteL returns %d",
       
   242         iReturnCode );
       
   243     return iReturnCode;
       
   244     }
       
   245 
       
   246 // --------------------------------------------------------------------------
       
   247 // CUPnPMusicAdapter::ExecuteMusicInLocalL
       
   248 // --------------------------------------------------------------------------
       
   249 //
       
   250 EXPORT_C TInt CUPnPMusicAdapter::ExecuteMusicInLocalL(
       
   251     CEikAppUi* aAppUi,
       
   252     CUPnPPlayListFiller* aFiller )
       
   253     {
       
   254     __LOG( "CUPnPMusicAdapter::ExecuteMusicInLocalL" );
       
   255 
       
   256     InitState();
       
   257     iAppUi = aAppUi;
       
   258     iRemotePlayer = EFalse;
       
   259     
       
   260     TakeOwnershipOfTheFiller( aFiller );
       
   261     iTargetDevice = 0;
       
   262     
       
   263     // Create and fill playlist
       
   264     CreatePlayListL();
       
   265 
       
   266     // show the UI
       
   267     UiShowLoopL();
       
   268 
       
   269     __LOG1( "CUPnPMusicAdapter::ExecuteMusicInLocalL returns %d",
       
   270         iReturnCode );
       
   271     return iReturnCode;
       
   272     }
       
   273 
       
   274 // --------------------------------------------------------------------------
       
   275 // CUPnPMusicAdapter::Dismiss
       
   276 // Dismiss the music player UI
       
   277 // --------------------------------------------------------------------------
       
   278 //
       
   279 EXPORT_C void CUPnPMusicAdapter::Dismiss( TInt aReturnCode )
       
   280     {
       
   281     Exit( aReturnCode );
       
   282     }
       
   283 
       
   284 // --------------------------------------------------------------------------
       
   285 // CUPnPMusicAdapter::UiShowLoop
       
   286 // Wait to show the UI
       
   287 // --------------------------------------------------------------------------
       
   288 //
       
   289 void CUPnPMusicAdapter::UiShowLoopL()
       
   290     {
       
   291     // ----------------------------------------
       
   292     // PART 1 - wait part
       
   293     // ----------------------------------------
       
   294     if ( iState == EStatePreparing )
       
   295         {
       
   296         // Not yet ready to go. Wait until signalled to proceed
       
   297         iWait = new (ELeave) CActiveSchedulerWait();
       
   298         __LOG( "UiShowLoopL: wait..." );
       
   299         iState = EStatePreparingWait;
       
   300         iWait->Start();
       
   301         delete iWait;
       
   302         iWait = 0;
       
   303         }
       
   304     else 
       
   305         {
       
   306         __LOG( "UiShowLoopL: no need to wait" );
       
   307         }
       
   308 
       
   309     // ----------------------------------------
       
   310     // PART 2 - action part
       
   311     // ----------------------------------------
       
   312     if ( iState == EStateInitialising )
       
   313         {
       
   314         __LOG( "Activate playback view" );  
       
   315         iViewUtility->ActivateViewL( KPlaybackDialogUid );
       
   316         iState = EStateClosing;
       
   317         }
       
   318     else if ( iState == EStateClosing )
       
   319         {
       
   320         // Exit signalled before UI was shown
       
   321         }
       
   322     else
       
   323         {
       
   324         // illegal state
       
   325         __PANICD( __FILE__, __LINE__ );
       
   326         }
       
   327     iState = EStateIdle;
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CUPnPMusicAdapter::DisplayUI
       
   332 // Commands the UI to show
       
   333 // --------------------------------------------------------------------------
       
   334 //
       
   335 void CUPnPMusicAdapter::DisplayUI()
       
   336     {
       
   337     __LOG1( "CUPnPMusicAdapter::DisplayUI() in state %d",
       
   338         iState );
       
   339     if ( iState == EStatePreparing )
       
   340         {
       
   341         // UiShowLoop can show UI directly - no wait
       
   342         iState = EStateInitialising;
       
   343         }
       
   344     else if ( iState == EStatePreparingWait )
       
   345         {
       
   346         // release wait loop in UiShowLoop
       
   347         __ASSERTD( iWait != 0, __FILE__, __LINE__ );
       
   348         iState = EStateInitialising;
       
   349         iWait->AsyncStop();
       
   350         }
       
   351     else
       
   352         {
       
   353         // should never be here
       
   354         __PANICD( __FILE__, __LINE__ );
       
   355         }
       
   356     }
       
   357 
       
   358 // --------------------------------------------------------------------------
       
   359 // CUPnPMusicAdapter::Exit
       
   360 // Performs cleanup and releases the execute thread
       
   361 // --------------------------------------------------------------------------
       
   362 //
       
   363 void CUPnPMusicAdapter::Exit( TInt aReturnCode )
       
   364     {
       
   365     __LOG2( "CUPnPMusicAdapter::Exit(%d) in state %d",
       
   366         aReturnCode, iState );
       
   367     if ( iState == EStatePreparing )
       
   368         {
       
   369         // change state - UiShowLoop will exit immediately
       
   370         iReturnCode = aReturnCode;
       
   371         iState = EStateClosing;
       
   372         }
       
   373     if ( iState == EStatePreparingWait )
       
   374         {
       
   375         // release wait loop in UiShowLoop
       
   376         __ASSERTD( iWait != 0, __FILE__, __LINE__ );
       
   377         iReturnCode = aReturnCode;
       
   378         iState = EStateClosing;
       
   379         iWait->AsyncStop();
       
   380         }
       
   381     else if ( iState == EStateInitialising ||
       
   382         iState == EStateRunning )
       
   383         {
       
   384         __ASSERTD( iViewUtility != 0, __FILE__, __LINE__ );
       
   385         iReturnCode = aReturnCode;
       
   386         iState = EStateClosing;
       
   387         // Send a close command to plugin with this error code.
       
   388         // Plugin will notify that it is not available,
       
   389         // and then the player UI will exit
       
   390         TRAP_IGNORE( iPlaybackUtility->CommandL( 
       
   391             EPbCmdClose, KMusicPluginMessageExit ) );
       
   392         }
       
   393     else if ( iState == EStateClosing )
       
   394         {
       
   395         // do nothing - already in exit process
       
   396         }
       
   397     else
       
   398         {
       
   399         // should never be here
       
   400         __PANICD( __FILE__, __LINE__ );
       
   401         }
       
   402     }
       
   403 
       
   404 
       
   405 // --------------------------------------------------------------------------
       
   406 // CUPnPMusicAdapter::CreatePlayList
       
   407 // --------------------------------------------------------------------------
       
   408 //    
       
   409 void CUPnPMusicAdapter::CreatePlayListL()
       
   410     {
       
   411     __LOG( "CUPnPMusicAdapter::CreatePlayList" );
       
   412     __ASSERTD( iState == EStateIdle, __FILE__, __LINE__ );
       
   413     iState = EStatePreparing;
       
   414 
       
   415     // Get collection Uid
       
   416     iPlaylistCollectionId = KInMemoryCollection;
       
   417     iPlaylistCollectionId = iCollectionUtility->Collection()
       
   418         .CollectionIDL( iPlaylistCollectionId );
       
   419 
       
   420     // Create and fill the playlist
       
   421     iTrackArray = CMPXMediaArray::NewL();
       
   422     __LOG( "Filling object list" );
       
   423     iCurrentFiller->FillL( *this, *iTrackArray );
       
   424     if ( iCurrentFillerState == EFillerStateIdle )
       
   425         {
       
   426         iCurrentFillerState = EFillerStateFilling;
       
   427         }
       
   428 
       
   429     iMedia = CMPXMedia::NewL();
       
   430     // set collection id
       
   431     iMedia->SetTObjectValueL<TUid>(
       
   432         KMPXMediaGeneralCollectionId, iPlaylistCollectionId );
       
   433     // Set type
       
   434     iMedia->SetTObjectValueL<TMPXGeneralType>( 
       
   435         KMPXMediaGeneralType, EMPXItem );
       
   436     // Set category
       
   437     iMedia->SetTObjectValueL<TMPXGeneralCategory>( 
       
   438         KMPXMediaGeneralCategory, EMPXPlaylist );
       
   439     // Set title
       
   440     iMedia->SetTextValueL( 
       
   441         KMPXMediaGeneralTitle, KTempPlaylistTitle );
       
   442     // set contents
       
   443     iMedia->SetCObjectValueL( 
       
   444         KMPXMediaArrayContents, iTrackArray );  
       
   445     // set size
       
   446     __LOG1( "Set Array count=%d", iCurrentFiller->PlaylistSize() );
       
   447     iMedia->SetTObjectValueL( 
       
   448         KMPXMediaArrayCount,  iCurrentFiller->PlaylistSize() );
       
   449 
       
   450     __LOG( "Create collection playlist" );
       
   451     // Create collection playlist
       
   452     iMpxHelper->AddAndOpenL( KHomeConnectUid, *iMedia );
       
   453 
       
   454     __LOG( "CUPnPMusicAdapter::CreatePlayList - end" );
       
   455     }
       
   456 
       
   457 // --------------------------------------------------------------------------
       
   458 // CUPnPMusicAdapter::HandleCollectionMessage
       
   459 // From MMPXCollectionObserver.
       
   460 // --------------------------------------------------------------------------
       
   461 //
       
   462 void CUPnPMusicAdapter::HandleCollectionMessage(CMPXMessage* aMsg, 
       
   463     TInt aErr )
       
   464     {
       
   465     __LOG1( "CUPnPMusicAdapter::HandleCollectionMessage: error %d", aErr );
       
   466     if( aMsg && aErr == KErrNone )
       
   467         {        
       
   468         TInt event = KErrNotSupported;
       
   469         TInt type = KErrNotSupported;
       
   470         TInt data = KErrNotSupported;
       
   471         
       
   472         if( aMsg->IsSupported( KMPXMessageGeneralEvent ) )
       
   473             {
       
   474             event = *aMsg->Value<TInt>( KMPXMessageGeneralEvent );
       
   475             }
       
   476         if( aMsg->IsSupported( KMPXMessageGeneralType ) )
       
   477             {
       
   478             type = *aMsg->Value<TInt>( KMPXMessageGeneralType );
       
   479             }
       
   480         if( aMsg->IsSupported( KMPXMessageGeneralData ) )
       
   481             {
       
   482             data = *aMsg->Value<TInt>( KMPXMessageGeneralData );
       
   483             }
       
   484 
       
   485         TRAPD( error, DoHandleCollectionMessageL( event, type, data ) );
       
   486         if ( error != KErrNone )
       
   487             {
       
   488             __LOG1( "DoHandleCollectionMessageL: leave with %d", error );
       
   489             Exit( error );
       
   490             }
       
   491         }
       
   492     else
       
   493         {
       
   494         Exit( aErr );
       
   495         }
       
   496     }
       
   497 
       
   498 // --------------------------------------------------------------------------
       
   499 // CUPnPMusicAdapter::DoHandleCollectionMessageL
       
   500 // --------------------------------------------------------------------------
       
   501 //
       
   502 void CUPnPMusicAdapter::DoHandleCollectionMessageL(
       
   503     TInt aEvent, TInt /*aType*/, TInt /*aData*/ )
       
   504     {
       
   505     __LOG( "CUPnPMusicAdapter::DoHandleCollectionMessageL" );
       
   506 
       
   507     switch ( aEvent )
       
   508         {
       
   509         case TMPXCollectionMessage::EPathChanged:
       
   510             {
       
   511             __LOG( "HandleCollectionMessageL: EPathChanged" );
       
   512 
       
   513             if ( iState == EStatePreparing || iState == EStatePreparingWait )
       
   514                 {
       
   515                 __LOG( "Pick up playlist from collection" );
       
   516                 CMPXCollectionPath* path = 
       
   517                 iCollectionUtility->Collection().PathL();
       
   518                 CleanupStack::PushL( path );
       
   519                 // select track with given index
       
   520                 __LOG1( "Set selected index = %d",
       
   521                     iCurrentFiller->SelectedIndex() );
       
   522                 path->Set( iCurrentFiller->SelectedIndex() );
       
   523 
       
   524                 __LOG( "Ccreating new playlist" );
       
   525                 delete iPlaylist;
       
   526                 iPlaylist = NULL;
       
   527                 iPlaylist = CMPXCollectionPlaylist::NewL( *path );
       
   528                 __LOG1( "iPlaylist = %x", iPlaylist );
       
   529                 iPlaylist->SetEmbeddedPlaylist( ETrue );
       
   530                 CleanupStack::PopAndDestroy( path );
       
   531 
       
   532                 // Disable repeat and shuffle modes
       
   533                 iPlaylist->SetRepeatEnabled( EFalse );
       
   534                 iPlaylist->SetShuffleEnabledL( EFalse );
       
   535 
       
   536                 // Get subplayers if remote playback
       
   537                 if( iRemotePlayer )
       
   538                     {
       
   539                     __LOG( "calling SubPlayerNamesL" );
       
   540                     iPlaybackUtility->PlayerManager().SubPlayerNamesL( *this,
       
   541                         KRemotePlayerUid );
       
   542                     }
       
   543                 else // Local playback. Get subplayer is no needed.
       
   544                     {
       
   545                     __LOG( "calling SelectPlayerL(local)" );
       
   546                     iPlaybackUtility->PlayerManager().SelectPlayerL(
       
   547                         KLocalPlayerUid );
       
   548                     __LOG("calling InitL");
       
   549                     iPlaybackUtility->InitL( *iPlaylist, ETrue );          
       
   550                     }
       
   551                 }
       
   552             }
       
   553             break;
       
   554         default:
       
   555             {
       
   556             } 
       
   557             break;
       
   558         }
       
   559     }
       
   560 
       
   561 // --------------------------------------------------------------------------
       
   562 // CUPnPMusicAdapter::HandleSubPlayerNamesL
       
   563 // From MMPXPlaybackCallback 
       
   564 // --------------------------------------------------------------------------
       
   565 //  
       
   566 void CUPnPMusicAdapter::HandleSubPlayerNamesL( TUid aPlayer, 
       
   567     const MDesCArray* aSubPlayers, TBool aComplete, TInt aError )
       
   568     {
       
   569     TRAPD( error, DoHandleSubPlayerNamesL(
       
   570         aPlayer, aSubPlayers, aComplete, aError ) );
       
   571     if ( error != KErrNone )
       
   572         {
       
   573         __LOG1( "HandleSubPlayerNamesL: leave with %d", error );
       
   574         Exit( error );
       
   575         }
       
   576     }
       
   577 
       
   578 // --------------------------------------------------------------------------
       
   579 // CUPnPMusicAdapter::DoHandleSubPlayerNamesL
       
   580 // --------------------------------------------------------------------------
       
   581 //  
       
   582 void CUPnPMusicAdapter::DoHandleSubPlayerNamesL( TUid /*aPlayer*/, 
       
   583     const MDesCArray* aSubPlayers, TBool /*aComplete*/, TInt aError )
       
   584     {
       
   585     __LOG( "CUPnPMusicAdapter::HandleSubPlayerNamesL" );
       
   586 
       
   587     if( aError == KErrNone)
       
   588         {
       
   589         // Find out Subplayer index
       
   590         TInt count = aSubPlayers->MdcaCount();
       
   591         TInt subplayerIndex = KErrNotFound; 
       
   592             
       
   593         TBuf<256> id16;
       
   594         id16.Copy( iTargetDevice->FriendlyName() );
       
   595                 
       
   596         for( TInt i = 0; i < count; i++ )
       
   597             {
       
   598             // If subplayer match.
       
   599             if( !aSubPlayers->MdcaPoint(i).Compare( id16 ) )
       
   600                 {
       
   601                 subplayerIndex = i;
       
   602                 break;
       
   603                 }
       
   604             }   
       
   605             
       
   606         if( subplayerIndex != KErrNotFound )
       
   607             {
       
   608             __LOG1( "calling SelectSubPlayerL(%d)", subplayerIndex );
       
   609             iPlaybackUtility->PlayerManager().SelectSubPlayerL(
       
   610             KRemotePlayerUid, subplayerIndex );
       
   611                     
       
   612             iTargetDevice = 0; 
       
   613                                                         
       
   614             __LOG( "calling InitL" );
       
   615             iPlaybackUtility->InitL( *iPlaylist, ETrue );             
       
   616             }
       
   617        else
       
   618             {
       
   619             __LOG( "HandleSubPlayerNamesL: subplayer not found! " );
       
   620             Exit( KErrNotFound );
       
   621             } 
       
   622         }
       
   623     else // Error with SubPlayerNamesL
       
   624         {
       
   625         __LOG1( "HandleSubPlayerNamesL: Error=%d", aError );
       
   626         Exit( aError );
       
   627         }
       
   628     }
       
   629 
       
   630 // --------------------------------------------------------------------------
       
   631 // CUPnPMusicAdapter::HandlePlaybackMessage
       
   632 // --------------------------------------------------------------------------
       
   633 //
       
   634 void CUPnPMusicAdapter::HandlePlaybackMessage(CMPXMessage* aMsg,
       
   635     TInt aErr )
       
   636     {
       
   637     __LOG1( "HandlePlaybackMessage: error %d", aErr );
       
   638     if( aMsg && aErr == KErrNone )
       
   639         {
       
   640         if (aMsg->IsSupported(KMPXMessageGeneralId))
       
   641             {
       
   642             TInt id = static_cast<TInt>(
       
   643                     *aMsg->Value<TMPXMessageId>(KMPXMessageGeneralId));
       
   644             TInt error(KErrNone);
       
   645             if ( id==KMPXMessageGeneral )
       
   646                 {                    
       
   647                 TInt event = KErrNotSupported;
       
   648                 TInt type = KErrNotSupported;
       
   649                 TInt data = KErrNotSupported;
       
   650                 
       
   651                 if( aMsg->IsSupported( KMPXMessageGeneralEvent ) )
       
   652                     {
       
   653                     event = *aMsg->Value<TInt>( KMPXMessageGeneralEvent );
       
   654                     }
       
   655                 if( aMsg->IsSupported( KMPXMessageGeneralType ) )
       
   656                     {
       
   657                     type = *aMsg->Value<TInt>( KMPXMessageGeneralType );
       
   658                     }
       
   659                 if( aMsg->IsSupported( KMPXMessageGeneralData ) )
       
   660                     {
       
   661                     data = *aMsg->Value<TInt>( KMPXMessageGeneralData );
       
   662                     }    
       
   663                     
       
   664                 TRAP( error, DoHandlePlaybackMessageL( event, type, data ) );
       
   665                 }
       
   666             if ( error != KErrNone )
       
   667                 {
       
   668                 __LOG1( "DoHandlePlaybackMessage: leave with %d", error );
       
   669                 Exit( error );
       
   670                 }
       
   671             } // else dummy message
       
   672         }
       
   673     }
       
   674 
       
   675 // --------------------------------------------------------------------------
       
   676 // CUPnPMusicAdapter::DoHandlePlaybackMessageL
       
   677 // --------------------------------------------------------------------------
       
   678 //
       
   679 void CUPnPMusicAdapter::DoHandlePlaybackMessageL( 
       
   680     TInt aEvent, TInt /*aType*/, TInt /*aData*/ )
       
   681     {
       
   682     __LOG( "CUPnPMusicAdapter::DoHandlePlaybackMessageL" );
       
   683 
       
   684     switch ( aEvent )
       
   685         {
       
   686         case TMPXPlaybackMessage::EPlayerChanged:
       
   687             {
       
   688             if ( iState == EStatePreparing ||
       
   689                 iState == EStatePreparingWait )
       
   690                 {
       
   691                 DisplayUI();
       
   692                 }
       
   693             }   
       
   694             break;                  
       
   695         case TMPXPlaybackMessage::EInitializeComplete:
       
   696             {
       
   697             if ( iState == EStateInitialising )
       
   698                 {
       
   699                 __LOG( "Calling CommandL( EPbCmdPlay )" );
       
   700                 iState = EStateRunning;
       
   701                 iPlaybackUtility->CommandL( EPbCmdPlay );
       
   702                 }
       
   703             }
       
   704             break;
       
   705         default:
       
   706             {
       
   707             break;
       
   708             }
       
   709         }        
       
   710     }
       
   711 
       
   712 
       
   713 // --------------------------------------------------------------------------
       
   714 // Unused callbacks here
       
   715 // --------------------------------------------------------------------------
       
   716 
       
   717     
       
   718 // --------------------------------------------------------------------------
       
   719 // CUPnPMusicAdapter::HandlePropertyL
       
   720 // From MMPXPlaybackCallback 
       
   721 // --------------------------------------------------------------------------
       
   722 //
       
   723 void CUPnPMusicAdapter::HandlePropertyL( 
       
   724     TMPXPlaybackProperty /*aProperty*/, TInt /*aValue*/, TInt /*aError*/ )
       
   725     {
       
   726     __LOG( "CUPnPMusicAdapter::HandlePropertyL - Not used." );
       
   727     // Not used.
       
   728     }
       
   729 
       
   730 
       
   731 // --------------------------------------------------------------------------
       
   732 // CUPnPMusicAdapter::HandleMediaL
       
   733 // From MMPXPlaybackCallback 
       
   734 // --------------------------------------------------------------------------
       
   735 // 
       
   736 void CUPnPMusicAdapter::HandleMediaL( 
       
   737     const CMPXMedia& /*aProperties*/, TInt /*aError*/ )
       
   738     {
       
   739     __LOG( "CUPnPMusicAdapter::HandleMediaL - Not used" );
       
   740     // Not used.
       
   741     }
       
   742 
       
   743 
       
   744 
       
   745 // --------------------------------------------------------------------------
       
   746 // CUPnPMusicAdapter::HandleCollectionMediaL
       
   747 // From MMPXCollectionObserver.
       
   748 // --------------------------------------------------------------------------
       
   749 //
       
   750 void CUPnPMusicAdapter::HandleCollectionMediaL(
       
   751     const CMPXMedia& /*aMedia*/, 
       
   752     TInt /*aError*/ )
       
   753     {
       
   754     __LOG( "CUPnPMusicAdapter::HandleCollectionMediaL - Not used!" );
       
   755     // Not used
       
   756     }
       
   757 
       
   758 
       
   759 // --------------------------------------------------------------------------
       
   760 // CUPnPMusicAdapter::HandleOpenL
       
   761 // From MMPXCollectionObserver.
       
   762 // Handles the collection entries being opened.
       
   763 // --------------------------------------------------------------------------
       
   764 //
       
   765 void CUPnPMusicAdapter::HandleOpenL(
       
   766     const CMPXMedia& /* aEntries */,
       
   767     TInt /* aIndex */,
       
   768     TBool /* aComplete */,
       
   769     TInt /* aError */ )
       
   770     {
       
   771     __LOG( "CUPnPMusicAdapter::HandleOpenL - not used!" );
       
   772     // Not used
       
   773     }
       
   774 
       
   775 // --------------------------------------------------------------------------
       
   776 // CUPnPMusicAdapter::HandleOpenL
       
   777 // From MMPXCollectionObserver.
       
   778 // Handles the item being opened.
       
   779 // --------------------------------------------------------------------------
       
   780 //
       
   781 void CUPnPMusicAdapter::HandleOpenL(
       
   782     const CMPXCollectionPlaylist& /*aPlaylist*/,
       
   783     TInt /*aError*/ )
       
   784     {
       
   785     __LOG( "CUPnPMusicAdapter::HandleOpenL - not used!" );
       
   786     // Not used
       
   787     }
       
   788  
       
   789  // --------------------------------------------------------------------------
       
   790 // CUPnPMusicAdapter::HandleEmbeddedOpenL
       
   791 // From MMPXCHelperEmbeddedOpenObserver
       
   792 // Handles errors from that occured in embedded mode
       
   793 // --------------------------------------------------------------------------
       
   794 //  
       
   795 void CUPnPMusicAdapter::HandleCommandComplete(
       
   796     CMPXCommand* aCommandResult, TInt /*aError*/ )
       
   797     {
       
   798      __LOG( "CUPnPMusicAdapter::HandleCommandComplete - not used!" );
       
   799      delete aCommandResult;
       
   800     // Not used
       
   801     }
       
   802  
       
   803 // --------------------------------------------------------------------------
       
   804 // CUPnPMusicAdapter::InitState
       
   805 // initialises the state before execution
       
   806 // --------------------------------------------------------------------------
       
   807 // 
       
   808 void CUPnPMusicAdapter::InitState()
       
   809     {
       
   810     iReturnCode = KErrNone;
       
   811     if ( iState != EStateIdle )
       
   812         {
       
   813         // dispose the wait object (just in case)
       
   814         delete iWait;
       
   815         iWait = 0;
       
   816         // dispose temporary class members
       
   817         delete iTrackArray;
       
   818         iTrackArray = 0;
       
   819         delete iMedia;
       
   820         iMedia = 0;
       
   821         delete iPlaylist;
       
   822         iPlaylist = 0;
       
   823         // reset state
       
   824         iState = EStateIdle;
       
   825         }
       
   826     if ( !iViewUtility )
       
   827         {
       
   828         TInt error = KErrNone;
       
   829         TRAP( error, iViewUtility = MMPXViewUtility::UtilityL() );
       
   830 	    if( error != KErrNone )
       
   831 	        {
       
   832 	        __LOG( "InitState: Creating viewutility leaves" );
       
   833 	        Exit( error );
       
   834 	        }
       
   835 	    __LOG1( "iViewUtility = %x", iViewUtility );
       
   836         }
       
   837     }
       
   838 
       
   839 // --------------------------------------------------------------------------
       
   840 // CUPnPMusicAdapter::TakeOwnershipOfTheFiller
       
   841 // this one should be pretty self-explanatory.
       
   842 // --------------------------------------------------------------------------
       
   843 // 
       
   844 void CUPnPMusicAdapter::TakeOwnershipOfTheFiller(
       
   845     MUPnPPlaylistFiller* aFiller )
       
   846     {
       
   847     if ( iCurrentFiller != 0 )
       
   848         {
       
   849         delete iCurrentFiller;
       
   850         iCurrentFiller = 0;
       
   851         }
       
   852     iCurrentFiller = aFiller;
       
   853     iCurrentFillerState = EFillerStateIdle;
       
   854     }
       
   855 
       
   856 // --------------------------------------------------------------------------
       
   857 // CUPnPMusicAdapter::Complete
       
   858 // Default constructor.
       
   859 // --------------------------------------------------------------------------
       
   860 //
       
   861 void CUPnPMusicAdapter::Complete( MUPnPPlaylistFiller* aFiller )
       
   862     {
       
   863     __LOG( "CUPnPMusicAdapter::Complete" );
       
   864     if ( aFiller == iCurrentFiller )
       
   865         {
       
   866         iCurrentFillerState = EFillerStateComplete;
       
   867         }
       
   868     }
       
   869 
       
   870 // --------------------------------------------------------------------------
       
   871 // CUPnPMusicAdapter::AVController
       
   872 // Returns the shared AVController resource
       
   873 // --------------------------------------------------------------------------
       
   874 //    
       
   875 MUPnPAVController& CUPnPMusicAdapter::AVController()
       
   876     {
       
   877     return iAVController;
       
   878     }
       
   879 
       
   880 // --------------------------------------------------------------------------
       
   881 // CUPnPMusicAdapter::CollectionUiHelper
       
   882 // --------------------------------------------------------------------------
       
   883 //
       
   884 TUid CUPnPMusicAdapter::PlaylistCollectionId()
       
   885     {
       
   886     return iPlaylistCollectionId;
       
   887     }
       
   888 
       
   889     
       
   890 // --------------------------------------------------------------------------
       
   891 // CUPnPMusicAdapter::CollectionUtility
       
   892 // --------------------------------------------------------------------------
       
   893 //
       
   894 MMPXCollectionUtility& CUPnPMusicAdapter::CollectionUtility()
       
   895     {
       
   896     return *iCollectionUtility;
       
   897     }
       
   898 
       
   899