camerauis/cameraapp/generic/src/audioplayer/camaudioplayercontroller.cpp
branchRCL_3
changeset 24 bac7acad7cb3
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Implementation of Controller for Sound / Tone Wrappers
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // ===========================================================================  
       
    21 // includes
       
    22 #include <e32base.h>
       
    23 #include <aknappui.h>       // CAknAppUi
       
    24 #include <aknsoundsystem.h> // CAknKeySoundSystem
       
    25 
       
    26 #include <cameraapp.rsg>
       
    27 #include <vgacamsettings.rsg>
       
    28 
       
    29 #include "camlogging.h"
       
    30 
       
    31 #include "CamSettingsInternal.hrh"       // TCamCameraMode, TCamSettingItemIds
       
    32 #include "CamAppController.h"
       
    33 #include "mcamplayerobserver.h"
       
    34 #include "mcamplayerwrapper.h"
       
    35 #include "camaudioplayerwrapper.h"
       
    36 #include "camaudioplayercontroller.h"
       
    37 #include "OstTraceDefinitions.h"
       
    38 #ifdef OST_TRACE_COMPILER_IN_USE
       
    39 #include "camaudioplayercontrollerTraces.h"
       
    40 #endif
       
    41 
       
    42 
       
    43 
       
    44 // ===========================================================================
       
    45 // Constants
       
    46 namespace NCamAudioPlayerController
       
    47   {
       
    48   static const TInt KImageCaptureSoundId = KMinTInt;
       
    49 
       
    50 
       
    51   // Cached sounds which are loaded once entering image mode.
       
    52   // No need to include capture sound, it is selected based
       
    53   // on the setting.
       
    54   static const TInt KImageCachedSounds[] =
       
    55     {
       
    56     // Our own id for image capture sound,
       
    57     // which can change.
       
    58     // Also avoid compilation error, if AF beep not supported.
       
    59     KImageCaptureSoundId,
       
    60     ECamAutoFocusComplete
       
    61     };
       
    62   
       
    63   // Cached sounds which are loaded once entering video mode.
       
    64   static const TInt KVideoCachedSounds[] =
       
    65     {
       
    66     ECamVideoStartSoundId,
       
    67     ECamVideoStopSoundId,
       
    68     ECamVideoPauseSoundId,
       
    69     ECamVideoResumeSoundId
       
    70     };
       
    71   }
       
    72 
       
    73 using namespace NCamAudioPlayerController;
       
    74 
       
    75 // ===========================================================================
       
    76 // public constructors and destructor
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // NewL <<static>>
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CCamAudioPlayerController* 
       
    83 CCamAudioPlayerController::NewL( CCamAppController&  aController,
       
    84                                  MCamPlayerObserver& aObserver )
       
    85   {
       
    86   CCamAudioPlayerController* self = 
       
    87     new (ELeave) CCamAudioPlayerController( aController, aObserver );
       
    88     
       
    89   CleanupStack::PushL( self );
       
    90   self->ConstructL();
       
    91   CleanupStack::Pop( self );
       
    92 
       
    93   return self;  
       
    94   }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // destructor <<virtual>>
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 CCamAudioPlayerController::~CCamAudioPlayerController()
       
   101   {
       
   102   iController.RemoveControllerObserver( this );
       
   103   iController.RemoveSettingsObserver( this );
       
   104 
       
   105   Reset();
       
   106   iPlayers.Close();
       
   107 
       
   108   iKeySoundSystem = NULL; // not own
       
   109   }
       
   110 
       
   111 
       
   112 
       
   113 
       
   114 // ===========================================================================
       
   115 // From MCamControllerObserver
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // HandleControllerEventL <<virtual>>
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void 
       
   122 CCamAudioPlayerController
       
   123 ::HandleControllerEventL( TCamControllerEvent aEvent,
       
   124                           TInt                aError )
       
   125   {
       
   126   PRINT( _L("Camera => CCamAudioPlayerController::HandleControllerEventL") );
       
   127   switch( aEvent )
       
   128     {
       
   129     case ECamEventEngineStateChanged:
       
   130       {
       
   131       PRINT( _L("Camera <> CCamAudioPlayerController: case ECamEventEngineStateChanged") );
       
   132       if( KErrNone == aError )
       
   133         {
       
   134         TCamCameraMode mode = iController.CurrentMode();
       
   135         PRINT2( _L("Camera <> CCamAudioPlayerController: KErrNone, old mode[%s] new mode[%s]"), KCamModeNames[iCameraMode], KCamModeNames[mode] );
       
   136         if( mode != iCameraMode )
       
   137           {
       
   138           // Changing to video or image mode.
       
   139           // Cache the players needed.
       
   140           // If this fails we just retry once a sound is really needed.
       
   141           if     ( ECamControllerImage    == mode 
       
   142                 || ECamControllerVideo    == mode ) { Setup( mode ); }
       
   143           // Changing to shutdown mode
       
   144           // Release all players.
       
   145           else if( ECamControllerShutdown == mode || ECamControllerIdle == mode ) 
       
   146         	  {
       
   147         	  Setup( mode );
       
   148         	  }
       
   149           // Other mode entered, no action
       
   150           else                                      { /* no action */ }
       
   151           }
       
   152         }
       
   153       else
       
   154         {
       
   155         PRINT1( _L("Camera <> CCamAudioPlayerController: error in (%d), call Reset.."), aError );
       
   156         Reset();
       
   157         }
       
   158       break;
       
   159       }
       
   160 
       
   161     default:
       
   162       {
       
   163       break;
       
   164       }
       
   165     }
       
   166   PRINT( _L("Camera <= CCamAudioPlayerController::HandleControllerEventL") );
       
   167   }
       
   168 
       
   169 
       
   170 
       
   171 // ===========================================================================
       
   172 // From MCamSettingsModelObserver
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // IntSettingChangedL <<virtual>>
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void 
       
   179 CCamAudioPlayerController
       
   180 ::IntSettingChangedL( TCamSettingItemIds aSettingItem, 
       
   181                       TInt               /*aSettingValue*/ )
       
   182   {
       
   183   PRINT( _L("Camera => CCamAudioPlayerController::IntSettingChangedL") );
       
   184   switch( aSettingItem )
       
   185     {
       
   186     case ECamSettingItemPhotoCaptureTone:
       
   187       {
       
   188       PRINT( _L("Camera <> CCamAudioPlayerController: case ECamSettingItemPhotoCaptureTone") );
       
   189 
       
   190       // The setting value is *not* of TCamSoundId type.
       
   191       // See CCamAppController::CaptureToneId().
       
   192       const TCamSoundId newSoundId = iController.CaptureToneId();
       
   193       SetupCaptureSoundPlayerL( newSoundId );
       
   194 
       
   195       break;
       
   196       }
       
   197     default:
       
   198       {
       
   199       break;
       
   200       }
       
   201     }
       
   202 
       
   203   PRINT( _L("Camera <= CCamAudioPlayerController::IntSettingChangedL") );
       
   204   }
       
   205 
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // TextSettingChangedL <<virtual>>
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void 
       
   212 CCamAudioPlayerController
       
   213 ::TextSettingChangedL(       TCamSettingItemIds /*aSettingItem*/, 
       
   214                        const TDesC&             /*aSettingValue*/ )
       
   215   {
       
   216   // these events are ignored
       
   217   }
       
   218 
       
   219 
       
   220 
       
   221 // ===========================================================================
       
   222 // new public
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // PlaySound
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void 
       
   229 CCamAudioPlayerController::PlaySound( TInt  aSoundId,
       
   230                                       TBool aCallback )
       
   231   {
       
   232   TRAPD( status,
       
   233     {
       
   234     // Get cached player, if available.
       
   235     // Otherwise create new one.
       
   236     CCamPlayerWrapperBase* player = GetPlayerL( aSoundId );
       
   237     player->Play( aCallback );
       
   238     });
       
   239 
       
   240   if( KErrNone != status )
       
   241     {
       
   242     if( KErrNoMemory == status )
       
   243       {
       
   244       // If OOM, release all players.
       
   245       // Create new ones again when needed.
       
   246       Reset();
       
   247       }
       
   248     if( aCallback )
       
   249       {
       
   250       // If could not instantiate a player, notify observer right away.
       
   251       iObserver.PlayComplete( status, aSoundId );
       
   252       }
       
   253     }
       
   254   }
       
   255 
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // CancelAllPlaying
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void 
       
   262 CCamAudioPlayerController::CancelAllPlaying()
       
   263   {
       
   264   PRINT( _L("Camera => CCamAudioPlayerController::CancelAllPlaying") );
       
   265 
       
   266   for( TInt i = 0; i < iPlayers.Count(); i++ )
       
   267     {
       
   268     iPlayers[i]->CancelPlay();
       
   269     }
       
   270 
       
   271   PRINT( _L("Camera <= CCamAudioPlayerController::CancelAllPlaying") );
       
   272   }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // CancelPlaying
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void 
       
   279 CCamAudioPlayerController::CancelPlaying( TInt aSoundId )
       
   280   {
       
   281   TInt index = FindPlayerIndex( aSoundId );
       
   282   if( KErrNotFound != index )
       
   283     {
       
   284     CCamPlayerWrapperBase* player = iPlayers[index];
       
   285     player->CancelPlay();
       
   286     }
       
   287   }
       
   288 
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // InitKeySoundSystem
       
   292 // ---------------------------------------------------------------------------
       
   293 // 
       
   294 void
       
   295 CCamAudioPlayerController::InitKeySoundSystem()
       
   296   {
       
   297   PRINT( _L("Camera => CCamAudioPlayerController::InitKeySoundSystem") );
       
   298   iKeySoundSystem = static_cast<CAknAppUi*>( CEikonEnv::Static()->AppUi() )->KeySounds();
       
   299   /* No sounds played via iKeySoundSystem. No need to add sound list.
       
   300   if( !iKeySoundSystem )
       
   301     {
       
   302     iKeySoundSystem = static_cast<CAknAppUi*>( CEikonEnv::Static()->AppUi() )->KeySounds();
       
   303     if ( iKeySoundSystem )
       
   304       {
       
   305       TRAPD( status, iKeySoundSystem->AddAppSoundInfoListL( R_CAM_SOUND_LIST ) );
       
   306       PRINT1( _L("Camera <> CCamAudioPlayerController: status:%d"), status );
       
   307       if( KErrNone          != status
       
   308        && KErrAlreadyExists != status
       
   309         )
       
   310         {
       
   311         // Set unusable
       
   312         iKeySoundSystem = NULL;
       
   313         }  
       
   314       }
       
   315     }
       
   316   */
       
   317   PRINT( _L("Camera <= CCamAudioPlayerController::InitKeySoundSystem") );
       
   318   }
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // EnableAllKeySounds
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void 
       
   325 CCamAudioPlayerController::EnableAllKeySounds()
       
   326   {
       
   327   if ( iKeySoundSystem && iKeySoundsDisabled )
       
   328     {
       
   329     iKeySoundSystem->ReleaseContext();
       
   330     iKeySoundSystem->PopContext();
       
   331     iKeySoundsDisabled = EFalse;
       
   332     }
       
   333   }
       
   334 
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // DisableAllKeySoundsL
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void 
       
   341 CCamAudioPlayerController::DisableAllKeySoundsL()
       
   342   {
       
   343   if ( iKeySoundSystem && !iKeySoundsDisabled )    
       
   344     {
       
   345     iKeySoundSystem->PushContextL( R_AVKON_SILENT_SKEY_LIST );
       
   346     iKeySoundSystem->BringToForeground();
       
   347     iKeySoundSystem->LockContext();
       
   348     iKeySoundsDisabled = ETrue;
       
   349     }
       
   350   }
       
   351 
       
   352 
       
   353 
       
   354 
       
   355 // ===========================================================================
       
   356 // new private
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // FindPlayerIndex
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362 TInt 
       
   363 CCamAudioPlayerController::FindPlayerIndex( TInt aSoundId ) const
       
   364   {
       
   365   TInt found = KErrNotFound;
       
   366 
       
   367   // Check that id is valid.
       
   368   if( aSoundId > 0 )
       
   369     {
       
   370     TInt current = iPlayers.Count(); 
       
   371     while( --current >= 0 )
       
   372       {
       
   373       // Reuse player, if the sound is the same despite
       
   374       // sound id being different.
       
   375       // if( aSoundId == iPlayers[current]->Id() )
       
   376       if( iPlayers[current]->IsEqualSound( aSoundId ) )
       
   377         {
       
   378         found   = current;
       
   379         current = -1;
       
   380         }
       
   381       }
       
   382     }
       
   383   return found;
       
   384   }
       
   385     
       
   386 // ---------------------------------------------------------------------------
       
   387 // GetPlayerL
       
   388 // ---------------------------------------------------------------------------
       
   389 //
       
   390 CCamPlayerWrapperBase*
       
   391 CCamAudioPlayerController::GetPlayerL( TInt aSoundId )
       
   392   {
       
   393   PRINT1( _L("Camera => CCamAudioPlayerController::GetPlayerL, sound id:%d"), aSoundId );
       
   394   CCamPlayerWrapperBase* player( NULL );
       
   395   
       
   396   if( KImageCaptureSoundId == aSoundId )
       
   397     {
       
   398     player = GetPlayerL( iController.CaptureToneId() );
       
   399     }
       
   400   else
       
   401     {
       
   402     TInt index = FindPlayerIndex( aSoundId );
       
   403   
       
   404     if( KErrNotFound != index )
       
   405       {
       
   406       player = iPlayers[index];
       
   407       }
       
   408     else
       
   409       {
       
   410       OstTrace0( CAMERAAPP_PERFORMANCE, CCAMAUDIOPLAYERCONTROLLER_GETPLAYERL, "e_CAM_APP_CAPTURE_SOUND_LOAD 1" ); //CCORAPP_CAPTURE_SOUND_LOAD_START
       
   411       
       
   412       player = CCamAudioPlayerWrapper::NewL( iObserver, aSoundId );
       
   413   
       
   414       CleanupStack::PushL( player );
       
   415       iPlayers.AppendL( player );
       
   416       CleanupStack::Pop( player );
       
   417       OstTrace0( CAMERAAPP_PERFORMANCE, DUP1_CCAMAUDIOPLAYERCONTROLLER_GETPLAYERL, "e_CAM_APP_CAPTURE_SOUND_LOAD 0" ); //CCORAPP_CAPTURE_SOUND_LOAD_END
       
   418       }
       
   419     }
       
   420 
       
   421   PRINT( _L("Camera <= CCamAudioPlayerController::GetPlayerL") );
       
   422   return player;
       
   423   }
       
   424 
       
   425 
       
   426 
       
   427 // ---------------------------------------------------------------------------
       
   428 // RemovePlayer
       
   429 // ---------------------------------------------------------------------------
       
   430 //
       
   431 void
       
   432 CCamAudioPlayerController::RemovePlayer( TInt aSoundId )
       
   433   {
       
   434   TInt index = FindPlayerIndex( aSoundId );
       
   435   if( KErrNotFound != index )
       
   436     {
       
   437     CCamPlayerWrapperBase* player = iPlayers[index];
       
   438     iPlayers.Remove( index );
       
   439     delete player;
       
   440     player = NULL;
       
   441     }
       
   442   }
       
   443 
       
   444 // ---------------------------------------------------------------------------
       
   445 // Setup
       
   446 // ---------------------------------------------------------------------------
       
   447 //
       
   448 void 
       
   449 CCamAudioPlayerController::Setup( TCamCameraMode aCameraMode )
       
   450   {
       
   451   PRINT( _L("Camera => CCamAudioPlayerController::Setup") );
       
   452   Reset();
       
   453 
       
   454   TInt status( KErrNone );  
       
   455   switch( aCameraMode )
       
   456     {
       
   457     case ECamControllerImage:
       
   458       {
       
   459       TRAP( status, SetupForImageL() );
       
   460       break;
       
   461       }
       
   462 
       
   463     case ECamControllerVideo:
       
   464       {
       
   465       TRAP( status, SetupForVideoL() );
       
   466       break;
       
   467       }
       
   468 
       
   469     case ECamControllerIdle:
       
   470     case ECamControllerShutdown:
       
   471     default:
       
   472       {
       
   473       // Already reset, no sounds needed now.
       
   474       break;
       
   475       }
       
   476     }
       
   477 
       
   478   // If any problems, try reload sounds once really needed, so reset now.
       
   479   if( KErrNone != status ) 
       
   480     Reset();
       
   481   else
       
   482     iCameraMode = aCameraMode;
       
   483 
       
   484   PRINT( _L("Camera <= CCamAudioPlayerController::Setup") );
       
   485   }
       
   486 
       
   487 // ---------------------------------------------------------------------------
       
   488 // SetupForVideoL
       
   489 // ---------------------------------------------------------------------------
       
   490 //
       
   491 void
       
   492 CCamAudioPlayerController::SetupForVideoL()
       
   493   {
       
   494   PRINT( _L("Camera => CCamAudioPlayerController::SetupForVideoL") );
       
   495   InitKeySoundSystem();
       
   496 
       
   497   TInt count( sizeof( KVideoCachedSounds ) / sizeof( TInt ) );
       
   498   PRINT1( _L("Camera <> CCamAudioPlayerController: count of players to load: %d"), count );
       
   499   for( TInt i = 0; i < count; i++ )
       
   500     {
       
   501     GetPlayerL( KVideoCachedSounds[i] );
       
   502     }
       
   503   PRINT( _L("Camera <= CCamAudioPlayerController::SetupForVideoL") );
       
   504   }
       
   505 
       
   506 // ---------------------------------------------------------------------------
       
   507 // SetupForImageL
       
   508 // ---------------------------------------------------------------------------
       
   509 //
       
   510 void 
       
   511 CCamAudioPlayerController::SetupForImageL()
       
   512   {
       
   513   PRINT( _L("Camera => CCamAudioPlayerController::SetupForImageL") );
       
   514   TInt count( sizeof( KImageCachedSounds ) / sizeof( TInt ) );
       
   515 
       
   516   PRINT1( _L("Camera <> CCamAudioPlayerController: count of players to load: %d"), count );
       
   517   for( TInt i = 0; i < count; i++ )
       
   518     {
       
   519     GetPlayerL( KImageCachedSounds[i] );
       
   520     }
       
   521   PRINT( _L("Camera => CCamAudioPlayerController::SetupForImageL") );
       
   522   }
       
   523 
       
   524 // ---------------------------------------------------------------------------
       
   525 // SetupCaptureSoundPlayerL
       
   526 // ---------------------------------------------------------------------------
       
   527 //
       
   528 void
       
   529 CCamAudioPlayerController::SetupCaptureSoundPlayerL( TInt aNewSoundId )
       
   530   {
       
   531   PRINT2( _L("Camera => CCamAudioPlayerController::SetupCaptureSoundPlayerL, old sound:%d new sound:%d"), iCaptureSoundId, aNewSoundId );
       
   532 
       
   533   if( aNewSoundId != iCaptureSoundId )
       
   534     {
       
   535     // Free old player, if exists.
       
   536     RemovePlayer( iCaptureSoundId );
       
   537   
       
   538     // If loading new fails, retry when sound is needed.
       
   539     iCaptureSoundId = -1;
       
   540     GetPlayerL( aNewSoundId ); 
       
   541     iCaptureSoundId = aNewSoundId;
       
   542     }
       
   543   PRINT ( _L("Camera <= CCamAudioPlayerController::SetupCaptureSoundPlayerL") );
       
   544   }
       
   545 
       
   546 // ---------------------------------------------------------------------------
       
   547 // Reset
       
   548 // ---------------------------------------------------------------------------
       
   549 //
       
   550 void 
       
   551 CCamAudioPlayerController::Reset()
       
   552   {
       
   553   PRINT ( _L("Camera => CCamAudioPlayerController::Reset") );
       
   554   iPlayers.ResetAndDestroy();
       
   555 
       
   556 
       
   557   iCameraMode     = ECamControllerIdle;
       
   558   iCaptureSoundId = -1;
       
   559   PRINT ( _L("Camera <= CCamAudioPlayerController::Reset") );
       
   560   }
       
   561 
       
   562 
       
   563 
       
   564 // ===========================================================================
       
   565 // private constructors
       
   566 
       
   567 // ---------------------------------------------------------------------------
       
   568 // ConstructL
       
   569 // ---------------------------------------------------------------------------
       
   570 //
       
   571 void 
       
   572 CCamAudioPlayerController::ConstructL()
       
   573   {
       
   574   iController.AddControllerObserverL( this );
       
   575   iController.AddSettingsObserverL( this );
       
   576   }
       
   577 
       
   578 // ---------------------------------------------------------------------------
       
   579 // constructor
       
   580 // ---------------------------------------------------------------------------
       
   581 //
       
   582 CCamAudioPlayerController
       
   583 ::CCamAudioPlayerController( CCamAppController&  aController,
       
   584                              MCamPlayerObserver& aObserver )
       
   585   : iController       ( aController            ),
       
   586     iObserver         ( aObserver              ),
       
   587     iCameraMode       ( ECamControllerIdle ),
       
   588     iCaptureSoundId   ( -1                     ),
       
   589     iKeySoundsDisabled( EFalse                 )
       
   590   {
       
   591   }
       
   592 
       
   593 // ===========================================================================  
       
   594 // end of file
       
   595