bluetoothengine/btsac/btrcc/src/btrccplayerstarter.cpp
branchRCL_3
changeset 61 269724087bed
parent 56 9386f31cc85b
child 64 1934667b0e2b
equal deleted inserted replaced
56:9386f31cc85b 61:269724087bed
     1 /*
       
     2 * Copyright (c) 2008 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:  This class launchs music player, then send play command
       
    15 *                to the player at 2 seconds timeout.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <apacmdln.h>
       
    22 #include <apgcli.h>
       
    23 #include <e32property.h>
       
    24 #include <remconcoreapitarget.h>
       
    25 #include "btrccplayerstarter.h"
       
    26 #include "btaudioremconpskeys.h"
       
    27 #include "debug.h"
       
    28 
       
    29 // MODULE DATA STRUCTURES
       
    30 
       
    31 // mpx playeris name is mpx.exe in 5.0.
       
    32 // it is musicplayer.exe in 3.2.
       
    33 _LIT(KMpxPlayerExeName, "mpx.exe");
       
    34 
       
    35 _LIT(KMpxPlayerSearchPatternBySID, "*102072c3*");
       
    36 _LIT(KVideoPlayerSearchPatternBySID, "*200159B2*");
       
    37 
       
    38 static const TInt KPlayCmdToPlayerDelay = 6000000; // value will be tuned later 
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CPlayerStarter::NewL
       
    44 // Two-phased constructor.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CPlayerStarter* CPlayerStarter::NewL()
       
    48 	{
       
    49 	CPlayerStarter* self = new (ELeave) CPlayerStarter();
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL();
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 void CPlayerStarter::SetCoreTarget(CRemConCoreApiTarget& aTarget)
       
    57     {
       
    58     iCoreTarget = &aTarget;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CPlayerStarter::CPlayerStarter
       
    63 // C++ constructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CPlayerStarter::CPlayerStarter()
       
    67   :CActive(EPriorityNormal)
       
    68     {
       
    69     CActiveScheduler::Add(this);
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CPlayerStarter::ConstructL
       
    74 // Symbian 2nd phase constructor.
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CPlayerStarter::ConstructL()
       
    78     {
       
    79     TRACE_FUNC
       
    80     iTimer.CreateLocal();
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // Destructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CPlayerStarter::~CPlayerStarter()
       
    88 	{
       
    89 	TRACE_FUNC
       
    90 	// Starting player is not finished yet, send play command. Just wish the player 
       
    91 	// can receive it.
       
    92 	if (IsActive() && iState != ERespondPlayNok)
       
    93 	    {
       
    94         RProperty::Set(KBTAudioRemCon, KBTAudioPlayerControl, 
       
    95                 EBTAudioResumePlayer);	    
       
    96 	    }
       
    97 	Cancel();
       
    98 	iTimer.Close();
       
    99 	}
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CPlayerStarter::RunL
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CPlayerStarter::RunL()
       
   106     {
       
   107     TRACE_INFO((_L("CPlayerStarter::RunL() state %d, status %d"), iState, iStatus.Int()));
       
   108     if (iStatus != KErrNone)
       
   109         {
       
   110         iState = EIdle;
       
   111         return;
       
   112         }
       
   113     switch (iState)
       
   114         {
       
   115         case ERespondPlayOk:
       
   116             {
       
   117             // Response OK has been sent to the remote device, start timer waiting
       
   118             // for player's initialization.
       
   119             iTimer.After(iStatus, KPlayCmdToPlayerDelay);
       
   120             SetActive();
       
   121             iState = EPlayMusicTiming;
       
   122             TRACE_INFO(_L("schedule PLAY command ..."));
       
   123             break;
       
   124             }
       
   125         case EPlayMusicTiming:
       
   126             {
       
   127             // Player should have fully up and running. Send PLAY command to it
       
   128             // through our internal Play API.
       
   129             RProperty::Set(KBTAudioRemCon, KBTAudioPlayerControl, 
       
   130                     EBTAudioResumePlayer);
       
   131             TRACE_INFO(_L("PLAY command sent to btmonobearer"));
       
   132             iState = EIdle;
       
   133             break;
       
   134             }
       
   135         default:
       
   136             {
       
   137             iState = EIdle;
       
   138             break;
       
   139             }
       
   140         }
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CPlayerStarter::RunError
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 TInt CPlayerStarter::RunError(TInt aError)
       
   148     {
       
   149     TRACE_FUNC
       
   150     (void) aError;
       
   151     return KErrNone;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CPlayerStarter::DoCancel
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CPlayerStarter::DoCancel()
       
   159     {
       
   160     TRACE_FUNC
       
   161     switch (iState)
       
   162         {
       
   163         case ERespondPlayOk:
       
   164         case ERespondPlayNok:
       
   165             {
       
   166             // This cancel won't cancel the response sending to the remote 
       
   167             // device. (RemCon FW's cancel mechanism is only completing client's request.
       
   168             // the real operation in its server side won't be stopped.)
       
   169             iCoreTarget->Cancel();
       
   170             break;
       
   171             }
       
   172         case EPlayMusicTiming:
       
   173             {
       
   174             iTimer.Cancel();
       
   175             break;
       
   176             }
       
   177         }
       
   178     }
       
   179 // ---------------------------------------------------------------------------
       
   180 // A 'play' command has been received.
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CPlayerStarter::MrccatoPlay(
       
   184     TRemConCoreApiPlaybackSpeed /* aSpeed */,
       
   185     TRemConCoreApiButtonAction aButtonAct )
       
   186     {
       
   187     TRACE_FUNC
       
   188     if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
   189         ( aButtonAct == ERemConCoreApiButtonPress ) )
       
   190         {
       
   191         StartPlayIfNeeded();
       
   192         }
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // A command has been received.
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CPlayerStarter::MrccatoCommand(
       
   200     TRemConCoreApiOperationId aOperationId,
       
   201     TRemConCoreApiButtonAction aButtonAct )
       
   202     {
       
   203     TRACE_INFO((_L("CPlayerStarter::MrccatoCommand opId %d, button %d"), 
       
   204         aOperationId, aButtonAct));
       
   205     switch (aOperationId)
       
   206         {
       
   207         case ERemConCoreApiPlay:
       
   208             {
       
   209             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
   210                 ( aButtonAct == ERemConCoreApiButtonPress ) )
       
   211                 {
       
   212                 StartPlayIfNeeded();
       
   213                 }
       
   214             break;
       
   215             }
       
   216         case ERemConCoreApiStop:
       
   217         case ERemConCoreApiPause:
       
   218             {
       
   219             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
   220                 ( aButtonAct == ERemConCoreApiButtonPress ) )
       
   221                 {
       
   222                 // if starting player is ongoing, possibly PLAY command has been scheduled
       
   223                 // need to abort it.
       
   224                 Cancel();
       
   225                 }
       
   226             break;
       
   227             }
       
   228         default:
       
   229             {
       
   230             break;
       
   231             }
       
   232         }
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CPlayerStarter::StartPlayIfNeeded
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 void CPlayerStarter::StartPlayIfNeeded() 
       
   240     {
       
   241     TRACE_FUNC
       
   242     // unfinished business ongoing, eat the event.
       
   243     if (IsActive())
       
   244         {
       
   245         return;
       
   246         }
       
   247     if (!IsMusicPlayerRunning() && !IsVideoPlayerRunning())
       
   248          {
       
   249          TRAPD(err, LaunchMusicPlayerL());
       
   250          // Send the response of play command to remote device
       
   251          iCoreTarget->PlayResponse(iStatus, err);
       
   252          SetActive();
       
   253          iState = (err) ? ERespondPlayNok : ERespondPlayOk;
       
   254          }
       
   255     }
       
   256 
       
   257 TBool CPlayerStarter::IsMusicPlayerRunning()
       
   258     {
       
   259     TRACE_FUNC
       
   260     // Music player is running if we can find a thread whose name contains 
       
   261     // S60 Music Player's SID.
       
   262     TFindThread findt(KMpxPlayerSearchPatternBySID);
       
   263     TFullName result;
       
   264     TBool running(EFalse);
       
   265     if (!findt.Next(result))
       
   266         {
       
   267         TRACE_INFO((_L("Thread '%S'is found"), &result));
       
   268         running = ETrue;
       
   269         }
       
   270     return running;
       
   271     }
       
   272 
       
   273 TBool CPlayerStarter::IsVideoPlayerRunning()
       
   274     {
       
   275     TRACE_FUNC
       
   276     // Video player is running if we can find a thread whose name contains 
       
   277     // Video Player's SID.
       
   278     TFindThread findt(KVideoPlayerSearchPatternBySID);
       
   279     TFullName result;
       
   280     TBool running(EFalse);
       
   281     if (!findt.Next(result))
       
   282         {
       
   283         TRACE_INFO((_L("Thread '%S'is found"), &result));
       
   284         running = ETrue;
       
   285         }
       
   286     return running;
       
   287     }
       
   288 
       
   289 void CPlayerStarter::LaunchMusicPlayerL()
       
   290     {
       
   291     RApaLsSession ls;
       
   292     User::LeaveIfError(ls.Connect());
       
   293     CleanupClosePushL(ls);
       
   294     CApaCommandLine* cmd = CApaCommandLine::NewL();
       
   295     CleanupStack::PushL(cmd);
       
   296     cmd->SetExecutableNameL( KMpxPlayerExeName );
       
   297     cmd->SetCommandL( EApaCommandOpen );
       
   298     User::LeaveIfError(ls.StartApp( *cmd ));
       
   299     CleanupStack::PopAndDestroy(2); // cmd, ls
       
   300     }
       
   301 
       
   302 
       
   303 //  End of File