camerauis/cameraapp/generic/src/audioplayer/camaudioplayerwrapper.cpp
changeset 19 d9aefe59d544
parent 3 8b2d6d0384b0
child 21 fa6d9f75d6a6
child 28 3075d9b614e6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
     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 Wrapper for Audio Player Utility
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // ===========================================================================  
       
    20 // includes
       
    21 #include <mdaaudiosampleplayer.h>
       
    22 #include <AudioPreference.h>
       
    23 
       
    24 #include "Cam.hrh"
       
    25 #include "CamPanic.h"
       
    26 #include "camlogging.h"
       
    27 #include "CamUtility.h" // for resource id's of sounds
       
    28 #include "mcamplayerobserver.h"
       
    29 #include "camaudioplayerwrapper.h"
       
    30 #include "CamPerformance.h"
       
    31 #include "OstTraceDefinitions.h"
       
    32 #ifdef OST_TRACE_COMPILER_IN_USE
       
    33 #include "camaudioplayerwrapperTraces.h"
       
    34 #endif
       
    35 
       
    36 
       
    37 
       
    38 // ===========================================================================  
       
    39 // Constants
       
    40 
       
    41 // Migrated directly from old sound player.
       
    42 #ifndef __WINSCW__
       
    43   static const TUint KAudioPriority = KAudioPriorityCameraTone;
       
    44 #else
       
    45   static const TUint KAudioPriority = KAudioPriorityVideoRecording;
       
    46 #endif // __WINSCW__                                                                                              
       
    47 
       
    48 
       
    49 // ===========================================================================  
       
    50 // public constructors and destructor
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // NewL <<static>>
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CCamAudioPlayerWrapper* 
       
    57 CCamAudioPlayerWrapper::NewL( MCamPlayerObserver& aObserver, 
       
    58                               TInt                aSoundId )
       
    59   {
       
    60   PRINT( _L("Camera => CCamAudioPlayerWrapper::NewL()") );
       
    61   CCamAudioPlayerWrapper* self = 
       
    62     new (ELeave) CCamAudioPlayerWrapper( aObserver, aSoundId );
       
    63 
       
    64   CleanupStack::PushL( self );
       
    65   self->ConstructL();
       
    66   CleanupStack::Pop( self );
       
    67 
       
    68   PRINT( _L("Camera <= CCamAudioPlayerWrapper::NewL()") );
       
    69   return self;
       
    70   }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // destructor <<virtual>>
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CCamAudioPlayerWrapper::~CCamAudioPlayerWrapper()
       
    77   {
       
    78   if( iPlayer )
       
    79     {
       
    80     iPlayer->Stop();
       
    81     iPlayer->Close();
       
    82     delete iPlayer;
       
    83     iPlayer = NULL;
       
    84     }
       
    85   }
       
    86 
       
    87 
       
    88 
       
    89 // ===========================================================================  
       
    90 // from CCamPlayerWrapperBase
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Id <<virtual>><<const>>
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 TInt 
       
    97 CCamAudioPlayerWrapper::Id() const
       
    98   {
       
    99   return iSoundId;
       
   100   }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // IsEqualSound <<virtual>><<const>>
       
   104 //
       
   105 // Compare if two sounds are equal. 
       
   106 // If the ids or filenames match, the sounds are considered to be equal.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 TBool
       
   110 CCamAudioPlayerWrapper::IsEqualSound( TInt aSoundId ) const
       
   111   {
       
   112   TInt eq( EFalse );
       
   113   TRAP_IGNORE( 
       
   114     {
       
   115     eq = aSoundId == iSoundId
       
   116       || MapSoundId2FilenameL( aSoundId ) == MapSoundId2FilenameL( iSoundId );
       
   117     });
       
   118   return eq;
       
   119   }
       
   120 
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Play <<virtual>>
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void 
       
   127 CCamAudioPlayerWrapper::Play( TBool aCallback )
       
   128   {
       
   129   PRINT2( _L("Camera => CCamAudioPlayerWrapper::Play, callback:%d iState[%s]"), 
       
   130           aCallback, 
       
   131           KCamAudioPlayerWrapperStateNames[iState] );
       
   132 
       
   133   switch( iState )
       
   134     {
       
   135     case CCamAudioPlayerWrapper::EReady:
       
   136       iPlayRequested = EFalse;
       
   137       iState         = CCamAudioPlayerWrapper::EPlaying;
       
   138       iCallback      = aCallback;
       
   139       iPlayer->Play();
       
   140       break;
       
   141 
       
   142     case CCamAudioPlayerWrapper::EInitializing:
       
   143       iPlayRequested = ETrue;
       
   144       iCallback      = aCallback;
       
   145       break;
       
   146 
       
   147     case CCamAudioPlayerWrapper::ECorrupt:
       
   148       // Notify observer rigth away as init has failed.
       
   149       iCallback      = aCallback;
       
   150       NotifyPlayComplete( KErrCorrupt );
       
   151       break;
       
   152 
       
   153     case CCamAudioPlayerWrapper::EPlaying:
       
   154       PRINT( _L("Camera <> WARNING: Still playing old sound, need to stop early") );
       
   155       iPlayer->Stop();
       
   156       // Notify observer that playing was interrupted (if callback was requested for previous play).
       
   157       // Sets state to idle so that we are ready to play again.
       
   158       NotifyPlayComplete( KErrAbort );
       
   159       // Now we can repeat the call as the state is right.
       
   160       Play( aCallback ); 
       
   161       break;
       
   162     case CCamAudioPlayerWrapper::EIdle:
       
   163     default:
       
   164       //Set state and ignore otherwise...
       
   165       PRINT( _L("Camera <> CCamAudioPlayerWrapper::Play invalid state") );
       
   166       iState = CCamAudioPlayerWrapper::ECorrupt;
       
   167       break;
       
   168     }
       
   169 
       
   170   PRINT( _L("Camera <= CCamAudioPlayerWrapper::Play") );
       
   171   }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CancelPlay <<virtual>>
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void 
       
   179 CCamAudioPlayerWrapper::CancelPlay()
       
   180   {
       
   181   PRINT1( _L("Camera => CCamAudioPlayerWrapper::CancelPlay, id:%d"), iSoundId );
       
   182   switch( iState )
       
   183     {
       
   184     case CCamAudioPlayerWrapper::EPlaying:
       
   185       PRINT( _L("Camera <> CCamAudioPlayerWrapper::CancelPlay .. iState == EPlaying, call Stop()..") );
       
   186       iPlayer->Stop();
       
   187       NotifyPlayComplete( KErrAbort );
       
   188       break;
       
   189     default:
       
   190       break;  
       
   191     }
       
   192 
       
   193   // Make sure pending Play() is cancelled.
       
   194   iPlayRequested = EFalse;      
       
   195   PRINT( _L("Camera <= CCamAudioPlayerWrapper::CancelPlay") );
       
   196   }
       
   197 
       
   198 
       
   199 // ===========================================================================  
       
   200 // from MMdaAudioPlayerCallback
       
   201   
       
   202 // ---------------------------------------------------------------------------
       
   203 // MapcInitComplete <<virtual>>
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void 
       
   207 CCamAudioPlayerWrapper
       
   208 ::MapcInitComplete(       TInt                       aStatus, 
       
   209                     const TTimeIntervalMicroSeconds& /*aDuration*/ )
       
   210   {
       
   211   PRINT2( _L("Camera => CCamAudioPlayerWrapper::MapcInitComplete, id:%d status:%d"), iSoundId, aStatus );
       
   212   if( KErrNone != aStatus )
       
   213     {
       
   214     // If we encounter an error in init phase,
       
   215     // we'll notify observer once Play gets called.
       
   216     iState = CCamAudioPlayerWrapper::ECorrupt;
       
   217     }
       
   218   else
       
   219     {
       
   220     iState = CCamAudioPlayerWrapper::EReady;
       
   221     }
       
   222 
       
   223   // If play has been called before init has finished
       
   224   // try to play now. If we had an error in init,
       
   225   // Play() will handle this based on iState.
       
   226   if( iPlayRequested )
       
   227     {
       
   228     Play( iCallback );
       
   229     }
       
   230   PRINT( _L("Camera <= CCamAudioPlayerWrapper::MapcInitComplete") );
       
   231   }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // MapcPlayComplete <<virtual>>
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void 
       
   238 CCamAudioPlayerWrapper::MapcPlayComplete( TInt aStatus )
       
   239   {
       
   240   NotifyPlayComplete( aStatus );
       
   241   }
       
   242 
       
   243 
       
   244 
       
   245 
       
   246 // ===========================================================================  
       
   247 // other public
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // MapSoundId2FilenameL <<static>>
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 TPtrC
       
   254 CCamAudioPlayerWrapper::MapSoundId2FilenameL( TInt aSoundId )
       
   255   {
       
   256   PRINT1( _L("Camera => CCamAudioPlayerWrapper::MapSoundId2FilenameL, id:%d"), aSoundId );
       
   257     
       
   258   TPtrC filename( KNullDesC );
       
   259 
       
   260   switch( aSoundId )
       
   261     {
       
   262     case ECamVideoStartSoundId:    filename.Set( KCamVideoStartTone );    break;
       
   263     case ECamVideoStopSoundId:     filename.Set( KCamVideoStopTone  );    break;
       
   264     case ECamVideoPauseSoundId:    filename.Set( KCamVideoPauseTone );    break;
       
   265     case ECamVideoResumeSoundId:   filename.Set( KCamVideoStartTone );    break;
       
   266     case ECamAutoFocusFailed:      User::Leave( KErrNotSupported );       break;
       
   267     case ECamAutoFocusComplete:    filename.Set( KCamAutoFocusComplete ); break;
       
   268     case ECamStillCaptureSoundId1: filename.Set( KCamCaptureTone1 );      break;
       
   269     case ECamStillCaptureSoundId2: filename.Set( KCamCaptureTone2 );      break;
       
   270     case ECamStillCaptureSoundId3: filename.Set( KCamCaptureTone3 );      break;
       
   271     case ECamStillCaptureSoundId4: filename.Set( KCamCaptureTone4 );      break;
       
   272     case ECamBurstCaptureSoundId1: filename.Set( KCamBurstCaptureTone1 );      break;
       
   273     case ECamBurstCaptureSoundId2: filename.Set( KCamBurstCaptureTone2 );      break;
       
   274     case ECamBurstCaptureSoundId3: filename.Set( KCamBurstCaptureTone3 );      break;
       
   275     case ECamBurstCaptureSoundId4: filename.Set( KCamBurstCaptureTone4 );      break;
       
   276     
       
   277     case ECamSelfTimerSoundId:     User::Leave( KErrNotSupported );       break;
       
   278     case ECamInCallToneId:         User::Leave( KErrNotSupported );       break;
       
   279 
       
   280     default:
       
   281       __ASSERT_DEBUG( EFalse, CamPanic( ECamPanicNotSupported ) );
       
   282       break;
       
   283     }
       
   284 
       
   285   PRINT1( _L("Camera <= CCamAudioPlayerWrapper::MapSoundId2FilenameL, return [%S]"), &filename );
       
   286   return filename;
       
   287   }
       
   288 
       
   289 
       
   290 
       
   291 // ===========================================================================  
       
   292 // other private
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // InitL
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 void 
       
   299 CCamAudioPlayerWrapper::InitL()
       
   300   {
       
   301   PRINT( _L("Camera => CCamAudioPlayerWrapper::InitL") );
       
   302   if( CCamAudioPlayerWrapper::EIdle == iState )
       
   303     {
       
   304     iState = CCamAudioPlayerWrapper::EInitializing;
       
   305     // Our process uses multiple players. Ensure they
       
   306     // (or their internal controllers) share the same
       
   307     // heap so we don't run out of memory chunks.
       
   308     iPlayer->UseSharedHeap();
       
   309     iPlayer->OpenFileL( MapSoundId2FilenameL( iSoundId ) );
       
   310     }
       
   311   else
       
   312     {
       
   313     CamPanic( ECamPanicInvalidState );
       
   314     PRINT( _L("Camera <> CCamAudioPlayerWrapper::InitL invalid state") );
       
   315     iState = CCamAudioPlayerWrapper::ECorrupt;
       
   316     User::Leave(KErrCorrupt);
       
   317     }
       
   318   PRINT( _L("Camera <= CCamAudioPlayerWrapper::InitL") );
       
   319   }
       
   320 
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // NotifyPlayComplete
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 void
       
   327 CCamAudioPlayerWrapper::NotifyPlayComplete( TInt aStatus )
       
   328   {
       
   329   PRINT1( _L("Camera => CCamAudioPlayerWrapper::NotifyPlayComplete, status:%d"), aStatus );
       
   330   switch( iState ) 
       
   331     {
       
   332     // Problem initializing.
       
   333     // Init is not tried again as it could slow down the capturing.
       
   334     case CCamAudioPlayerWrapper::ECorrupt:
       
   335       break;
       
   336 
       
   337     // Normal case where the playing has completed.
       
   338     // Set state to EReady to indicate we are ready 
       
   339     // for new play request. 
       
   340     case CCamAudioPlayerWrapper::EPlaying:
       
   341       iState = CCamAudioPlayerWrapper::EReady;
       
   342       break;
       
   343 
       
   344     // Other states not allowed here.
       
   345     default:
       
   346       // Make this player ready that we can play new sound.
       
   347       PRINT( _L("Camera <> CCamAudioPlayerWrapper::NotifyPlayComplete invalid state") );
       
   348       iState = CCamAudioPlayerWrapper::EReady;
       
   349       break;
       
   350     }
       
   351 
       
   352   // Clear flag just in case.
       
   353   iPlayRequested = EFalse;
       
   354 
       
   355   OstTrace0( CAMERAAPP_PERFORMANCE, CCAMAUDIOPLAYERWRAPPER_NOTIFYPLAYCOMPLETE, "e_CAM_APP_CAPTURE_SOUND_PLAY 0" ); //CCORAPP_CAPTURE_SOUND_PLAY_END
       
   356 
       
   357   // Notify observer with our sound id and received status.
       
   358   if( iCallback )
       
   359     iObserver.PlayComplete( aStatus, iSoundId );
       
   360 
       
   361   PRINT( _L("Camera <= CCamAudioPlayerWrapper::NotifyPlayComplete") );
       
   362   }
       
   363 
       
   364 // ===========================================================================  
       
   365 // private constructors
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // ConstructL
       
   369 // ---------------------------------------------------------------------------
       
   370 //
       
   371 void 
       
   372 CCamAudioPlayerWrapper::ConstructL()
       
   373   {
       
   374   PRINT( _L("Camera => CCamAudioPlayerWrapper::ConstructL()") );
       
   375   iPlayer = CMdaAudioPlayerUtility::NewL( 
       
   376               *this, 
       
   377                KAudioPriority ,
       
   378                TMdaPriorityPreference( KAudioPrefCamera ) );
       
   379 
       
   380   InitL();
       
   381   PRINT( _L("Camera <= CCamAudioPlayerWrapper::ConstructL()") );
       
   382   }
       
   383 
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // C++ constructor
       
   387 // ---------------------------------------------------------------------------
       
   388 //
       
   389 CCamAudioPlayerWrapper
       
   390 ::CCamAudioPlayerWrapper( MCamPlayerObserver& aObserver, 
       
   391                           TInt                aSoundId )
       
   392   : iObserver     ( aObserver                     ),
       
   393     iSoundId      ( aSoundId                      ),
       
   394     iPlayer       ( NULL                          ),
       
   395     iState        ( CCamAudioPlayerWrapper::EIdle ),
       
   396     iPlayRequested( EFalse                        ),
       
   397     iCallback     ( EFalse                        )
       
   398   {
       
   399   }
       
   400 
       
   401 // ===========================================================================  
       
   402 // end of file
       
   403