mpengine/src/mpmediakeyhandler_p.cpp
changeset 48 af3740e3753f
parent 42 79c49924ae23
child 54 c5b304f4d89b
equal deleted inserted replaced
42:79c49924ae23 48:af3740e3753f
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Music Player media key handler - private implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mpmediakeyhandler_p.h"
       
    19 #include "mpmediakeyhandler.h"
       
    20 #include "mpmediakeyremconresponse.h"
       
    21 #include "mpcommondefs.h"
       
    22 
       
    23 //symbian header files
       
    24 #include <remconcoreapitarget.h>
       
    25 #include <remconinterfaceselector.h>
       
    26 
       
    27 //mpx header files
       
    28 #include <mpxplaybackutility.h>
       
    29 #include <mpxplaybackcommanddefs.h>
       
    30 #include <mpxplaybackmessagedefs.h>
       
    31 #include <mpxlog.h>
       
    32 
       
    33 const TInt KFirstTimerExpiryInterval( 1 ); // Expire immediately
       
    34 const TInt KTimerExpiryInterval( 1000000/6 );
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // MpMediaKeyHandlerPrivate()
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 MpMediaKeyHandlerPrivate::MpMediaKeyHandlerPrivate( MpMediaKeyHandler *wrapper )
       
    41     : q_ptr( wrapper ),
       
    42       iInterfaceSelector(NULL),
       
    43       iResponseHandler(NULL),
       
    44       iTimer(NULL),
       
    45       iPlaybackUtility(NULL),
       
    46       iEnabled(ETrue)
       
    47 {
       
    48 }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // ~MpMediaKeyHandlerPrivate()
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 MpMediaKeyHandlerPrivate::~MpMediaKeyHandlerPrivate()
       
    55 {
       
    56     if ( iPlaybackUtility ) {
       
    57         //TRAP_IGNORE( iPlaybackUtility->RemoveObserverL( *this ) );
       
    58         iPlaybackUtility->Close();
       
    59     }
       
    60 
       
    61     delete iResponseHandler;
       
    62     delete iInterfaceSelector;
       
    63 
       
    64     if ( iTimer ) {
       
    65         iTimer->Cancel();
       
    66         delete iTimer;
       
    67     }
       
    68 
       
    69 }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // init
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void MpMediaKeyHandlerPrivate::init()
       
    76 {
       
    77     TRAPD(err, DoInitL());
       
    78     if ( err != KErrNone ) {
       
    79         emit q_ptr->errorOccured(err);
       
    80     }
       
    81 
       
    82 }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // setEnabled
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 void MpMediaKeyHandlerPrivate::setEnabled( bool enable )
       
    89 {
       
    90     iEnabled = enable;
       
    91 }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // From MRemConCoreApiTargetObserver
       
    95 // A command has been received.
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void MpMediaKeyHandlerPrivate::MrccatoCommand(
       
    99     TRemConCoreApiOperationId aOperationId,
       
   100     TRemConCoreApiButtonAction aButtonAct )
       
   101 {
       
   102     MPX_DEBUG3( "MpMediaKeyHandlerPrivate::MrccatoCommand(aOperationId=%d, aButtonAct=%d)",
       
   103         aOperationId, aButtonAct );
       
   104 
       
   105     switch ( aOperationId ) {
       
   106     case ERemConCoreApiPausePlayFunction:
       
   107         if ( aButtonAct == ERemConCoreApiButtonClick ) {
       
   108             SendCommand( EPbCmdPlayPause );
       
   109         }
       
   110         break;
       
   111     case ERemConCoreApiPlay:
       
   112         if ( aButtonAct == ERemConCoreApiButtonClick
       
   113              || aButtonAct == ERemConCoreApiButtonPress ) {
       
   114             SendCommand( EPbCmdPlay );
       
   115         }
       
   116         break;
       
   117     case ERemConCoreApiStop:
       
   118         if ( aButtonAct == ERemConCoreApiButtonClick
       
   119              || aButtonAct == ERemConCoreApiButtonPress ) {
       
   120             SendCommand( EPbCmdStop );
       
   121         }
       
   122         break;
       
   123     case ERemConCoreApiPause:
       
   124         if ( aButtonAct == ERemConCoreApiButtonClick
       
   125              || aButtonAct == ERemConCoreApiButtonPress ) {
       
   126             SendCommand( EPbCmdPause );
       
   127         }
       
   128         break;
       
   129     case ERemConCoreApiRewind:
       
   130         switch ( aButtonAct ) {
       
   131         case ERemConCoreApiButtonPress:
       
   132             SendCommand( EPbCmdStartSeekBackward );
       
   133             break;
       
   134         case ERemConCoreApiButtonRelease:
       
   135             SendCommand( EPbCmdStopSeeking );
       
   136             break;
       
   137         default:
       
   138             break;
       
   139         }
       
   140         break;
       
   141     case ERemConCoreApiFastForward:
       
   142         switch ( aButtonAct ) {
       
   143         case ERemConCoreApiButtonPress:
       
   144             SendCommand( EPbCmdStartSeekForward );
       
   145             break;
       
   146         case ERemConCoreApiButtonRelease:
       
   147             SendCommand( EPbCmdStopSeeking );
       
   148             break;
       
   149         default:
       
   150             break;
       
   151         }
       
   152         break;
       
   153     case ERemConCoreApiBackward:
       
   154         if ( aButtonAct == ERemConCoreApiButtonClick ) {
       
   155             SendCommand( EPbCmdPrevious );
       
   156         }
       
   157         break;
       
   158     case ERemConCoreApiForward:
       
   159         if ( aButtonAct == ERemConCoreApiButtonClick ) {
       
   160             SendCommand( EPbCmdNext );
       
   161         }
       
   162         break;
       
   163     case ERemConCoreApiVolumeUp:
       
   164         iTimer->Cancel();
       
   165         iIncreaseVol = ETrue;
       
   166         switch ( aButtonAct ) {
       
   167         case ERemConCoreApiButtonPress:
       
   168             //Start Timer
       
   169             iTimer->Start(
       
   170                 KFirstTimerExpiryInterval,
       
   171                 KTimerExpiryInterval,
       
   172                 TCallBack( TimerCallback, this ) );
       
   173             break;
       
   174         case ERemConCoreApiButtonClick:
       
   175             SendCommand( EPbCmdIncreaseVolume );
       
   176             break;
       
   177         case ERemConCoreApiButtonRelease:
       
   178         default:
       
   179             break;
       
   180         }
       
   181         break;
       
   182     case ERemConCoreApiVolumeDown:
       
   183         iTimer->Cancel();
       
   184         iIncreaseVol = EFalse;
       
   185         switch ( aButtonAct ) {
       
   186         case ERemConCoreApiButtonPress:
       
   187             //Start Timer
       
   188             iTimer->Start(
       
   189                 KFirstTimerExpiryInterval,
       
   190                 KTimerExpiryInterval,
       
   191                 TCallBack( TimerCallback, this ) );
       
   192             break;
       
   193         case ERemConCoreApiButtonClick:
       
   194             SendCommand( EPbCmdDecreaseVolume );
       
   195             break;
       
   196         case ERemConCoreApiButtonRelease:
       
   197         default:
       
   198             break;
       
   199         }
       
   200         break;
       
   201     default:
       
   202         break;
       
   203     }
       
   204     iResponseHandler->CompleteAnyKey( aOperationId );
       
   205 
       
   206 }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // From MRemConCoreApiTargetObserver
       
   210 // A 'play' command has been received.
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void MpMediaKeyHandlerPrivate::MrccatoPlay(
       
   214     TRemConCoreApiPlaybackSpeed aSpeed,
       
   215     TRemConCoreApiButtonAction aButtonAct )
       
   216 {
       
   217     MPX_DEBUG2( "MpMediaKeyHandlerPrivate::MrccatoPlay(aButtonAct=%d)", aButtonAct );
       
   218     Q_UNUSED(aSpeed);
       
   219 
       
   220     if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
   221         ( aButtonAct == ERemConCoreApiButtonPress ) )
       
   222         {
       
   223         SendCommand( EPbCmdPlay );
       
   224         }
       
   225     iResponseHandler->CompleteAnyKey( ERemConCoreApiPlay );
       
   226 
       
   227 }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // From MRemConCoreApiTargetObserver
       
   231 // A 'tune function' command has been received.
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void MpMediaKeyHandlerPrivate::MrccatoTuneFunction(
       
   235     TBool aTwoPart,
       
   236     TUint aMajorChannel,
       
   237     TUint aMinorChannel,
       
   238     TRemConCoreApiButtonAction aButtonAct )
       
   239 {
       
   240     Q_UNUSED(aTwoPart);
       
   241     Q_UNUSED(aMajorChannel);
       
   242     Q_UNUSED(aMinorChannel);
       
   243     Q_UNUSED(aButtonAct);
       
   244 
       
   245     iResponseHandler->CompleteAnyKey( ERemConCoreApiTuneFunction );
       
   246 
       
   247 }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From MRemConCoreApiTargetObserver
       
   251 // A 'select disk function' has been received.
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void MpMediaKeyHandlerPrivate::MrccatoSelectDiskFunction(
       
   255     TUint aDisk,
       
   256     TRemConCoreApiButtonAction aButtonAct )
       
   257 {
       
   258     Q_UNUSED(aDisk);
       
   259     Q_UNUSED(aButtonAct);
       
   260 
       
   261     iResponseHandler->CompleteAnyKey( ERemConCoreApiSelectDiskFunction );
       
   262 
       
   263 }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // From MRemConCoreApiTargetObserver
       
   267 // A 'select AV input function' has been received.
       
   268 // ---------------------------------------------------------------------------
       
   269 //
       
   270 void MpMediaKeyHandlerPrivate::MrccatoSelectAvInputFunction(
       
   271     TUint8 aAvInputSignalNumber,
       
   272     TRemConCoreApiButtonAction aButtonAct )
       
   273 {
       
   274     Q_UNUSED(aAvInputSignalNumber);
       
   275     Q_UNUSED(aButtonAct);
       
   276 
       
   277     iResponseHandler->CompleteAnyKey( ERemConCoreApiSelectAvInputFunction );
       
   278 
       
   279 }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // From MRemConCoreApiTargetObserver
       
   283 // A 'select audio input function' has been received.
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void MpMediaKeyHandlerPrivate::MrccatoSelectAudioInputFunction(
       
   287     TUint8 aAudioInputSignalNumber,
       
   288     TRemConCoreApiButtonAction aButtonAct )
       
   289 {
       
   290     Q_UNUSED(aAudioInputSignalNumber);
       
   291     Q_UNUSED(aButtonAct);
       
   292 
       
   293     iResponseHandler->CompleteAnyKey( ERemConCoreApiSelectAudioInputFunction );
       
   294 
       
   295 }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 // From MMPXPlaybackCallback
       
   299 // Handle playback property.
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 void MpMediaKeyHandlerPrivate::HandlePropertyL(
       
   303     TMPXPlaybackProperty aProperty,
       
   304     TInt aValue,
       
   305     TInt aError )
       
   306 {
       
   307     MPX_FUNC( "MpMediaKeyHandlerPrivate::DoHandlePropertyL" );
       
   308     MPX_DEBUG4( "MpMediaKeyHandlerPrivate::HandlePropertyL - Property(%d); Value(%d); Error(%d)", aProperty, aValue, aError );
       
   309 
       
   310     if ( KErrNone == aError ) {
       
   311         switch ( aProperty  ) {
       
   312         case EPbPropertyVolume:
       
   313             q_ptr->emit volumeChanged(aValue);
       
   314             break;
       
   315         case EPbPropertyPosition:
       
   316         case EPbPropertyMaxVolume:
       
   317         case EPbPropertyMute:
       
   318         default:
       
   319             break;
       
   320         }
       
   321     }
       
   322 
       
   323 }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // From MMPXPlaybackCallback
       
   327 // HandleSubPlayerNamesL
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void MpMediaKeyHandlerPrivate::HandleSubPlayerNamesL(
       
   331     TUid aPlayer,
       
   332     const MDesCArray* aSubPlayers,
       
   333     TBool aComplete,
       
   334     TInt aError )
       
   335 {
       
   336     MPX_FUNC( "MpMediaKeyHandlerPrivate::HandleSubPlayerNamesL" );
       
   337     Q_UNUSED(aPlayer);
       
   338     Q_UNUSED(aSubPlayers);
       
   339     Q_UNUSED(aComplete);
       
   340     Q_UNUSED(aError);
       
   341 
       
   342 }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // From MMPXPlaybackCallback
       
   346 // Handle media properties.
       
   347 // Notes: The client is responsible for delete the object of aMedia.
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void MpMediaKeyHandlerPrivate::HandleMediaL(
       
   351     const CMPXMedia& aMedia,
       
   352     TInt aError )
       
   353 {
       
   354     MPX_FUNC( "MpMediaKeyHandlerPrivate::DoHandleMediaL" );
       
   355     Q_UNUSED(aMedia);
       
   356     Q_UNUSED(aError);
       
   357 
       
   358 }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // DoInitL()
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void MpMediaKeyHandlerPrivate::DoInitL()
       
   365 {
       
   366     // Register to remote control framework
       
   367     iInterfaceSelector = CRemConInterfaceSelector::NewL();
       
   368     CRemConCoreApiTarget *coreTarget = CRemConCoreApiTarget::NewL( *iInterfaceSelector, *this );
       
   369     CleanupStack::PushL( coreTarget );
       
   370     iInterfaceSelector->OpenTargetL();
       
   371     iResponseHandler = MpMediaKeyRemConResponse::NewL( *coreTarget );
       
   372     CleanupStack::Pop(coreTarget);
       
   373 
       
   374     // Timer for implementing repeat
       
   375     iTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   376 
       
   377     // Get the playback utility instance from engine.
       
   378     iPlaybackUtility = MMPXPlaybackUtility::UtilityL( TUid::Uid(MpCommon::KMusicPlayerUid) );
       
   379     //iPlaybackUtility->AddObserverL( *this );
       
   380 
       
   381     iEnabled = ETrue;
       
   382 }
       
   383 
       
   384 // ---------------------------------------------------------------------------
       
   385 // Send command to playback utility.
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 void MpMediaKeyHandlerPrivate::SendCommand( TMPXPlaybackCommand aCommandId )
       
   389 {
       
   390     MPX_FUNC( "MpMediaKeyHandlerPrivate::SendCommand" );
       
   391     TRAP_IGNORE( DoSendCommandL( aCommandId ) );
       
   392 
       
   393 }
       
   394 
       
   395 // ---------------------------------------------------------------------------
       
   396 // Send command to playback utility.
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 void MpMediaKeyHandlerPrivate::DoSendCommandL( TMPXPlaybackCommand aCommandId )
       
   400 {
       
   401     MPX_FUNC( "MpMediaKeyHandlerPrivate::DoFilterAndSendCommandL" );
       
   402 
       
   403     if ( iEnabled )
       
   404     {
       
   405         TMPXPlaybackState playerState( iPlaybackUtility->StateL() );
       
   406 
       
   407         switch ( aCommandId ) {
       
   408         case EPbCmdPlayPause:
       
   409             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused 
       
   410                  || playerState == EPbStateStopped ) {
       
   411                 iPlaybackUtility->CommandL( EPbCmdPlayPause );
       
   412             }
       
   413             break;
       
   414         case EPbCmdPlay:
       
   415             if ( playerState == EPbStateStopped || playerState == EPbStatePaused ) {
       
   416                 iPlaybackUtility->CommandL( EPbCmdPlay );
       
   417             }
       
   418             break;
       
   419         case EPbCmdStop:
       
   420             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused ) {
       
   421                 iPlaybackUtility->CommandL( EPbCmdStop );
       
   422             }
       
   423             break;
       
   424         case EPbCmdPause:
       
   425             if ( playerState == EPbStatePlaying ) {
       
   426                 iPlaybackUtility->CommandL( EPbCmdPause );
       
   427             }
       
   428             break;
       
   429         case EPbCmdStartSeekBackward:
       
   430             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused ) {
       
   431                 iPlaybackUtility->CommandL( EPbCmdStartSeekBackward );
       
   432             }
       
   433             break;
       
   434         case EPbCmdStartSeekForward:
       
   435             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused ) {
       
   436                 iPlaybackUtility->CommandL( EPbCmdStartSeekForward );
       
   437             }
       
   438             break;
       
   439         case EPbCmdStopSeeking:
       
   440             if ( playerState == EPbStateSeekingBackward || playerState == EPbStateSeekingForward ) {
       
   441                 iPlaybackUtility->CommandL( EPbCmdStopSeeking );
       
   442             }
       
   443             break;
       
   444         case EPbCmdPrevious:
       
   445             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused 
       
   446                  || playerState == EPbStateStopped ) {
       
   447                 iPlaybackUtility->CommandL( EPbCmdPrevious );
       
   448             }
       
   449             break;
       
   450         case EPbCmdNext:
       
   451             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused 
       
   452                  || playerState == EPbStateStopped ) {
       
   453                 iPlaybackUtility->CommandL( EPbCmdNext );
       
   454             }
       
   455             break;
       
   456         case EPbCmdIncreaseVolume:
       
   457             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused 
       
   458                  || playerState == EPbStateStopped ) {
       
   459                 iPlaybackUtility->CommandL( EPbCmdIncreaseVolume );
       
   460             }
       
   461             break;
       
   462         case EPbCmdDecreaseVolume:
       
   463             if ( playerState == EPbStatePlaying || playerState == EPbStatePaused 
       
   464                  || playerState == EPbStateStopped ) {
       
   465                 iPlaybackUtility->CommandL( EPbCmdDecreaseVolume );
       
   466             }
       
   467             break;
       
   468         default:
       
   469             break;
       
   470         }
       
   471     }
       
   472 }
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // Callback for timer
       
   476 // ---------------------------------------------------------------------------
       
   477 //
       
   478 TInt MpMediaKeyHandlerPrivate::TimerCallback( TAny* aPtr )
       
   479 {
       
   480 
       
   481     static_cast<MpMediaKeyHandlerPrivate*>( aPtr )->HandleRepeatEvent();
       
   482 
       
   483     return KErrNone;
       
   484 }
       
   485 
       
   486 // ---------------------------------------------------------------------------
       
   487 // Handle repeat event
       
   488 // ---------------------------------------------------------------------------
       
   489 //
       
   490 void MpMediaKeyHandlerPrivate::HandleRepeatEvent()
       
   491 {
       
   492 
       
   493     if ( iIncreaseVol ) {
       
   494         SendCommand( EPbCmdIncreaseVolume );
       
   495     }
       
   496     else {
       
   497         SendCommand( EPbCmdDecreaseVolume );
       
   498     }
       
   499 
       
   500 }
       
   501