messagingappbase/msgeditor/mediacontrolsrc/MsgAudioControl.cpp
branchRCL_3
changeset 27 7fdbb852d323
equal deleted inserted replaced
26:ebe688cedc25 27:7fdbb852d323
       
     1 /*
       
     2 * Copyright (c) 2005 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:  
       
    15 *       MsgEditor Audio media control - a Message Editor Base control.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "MsgAudioControl.h"
       
    23 
       
    24 #include <AudioPreference.h>
       
    25 #include <DRMCommon.h>
       
    26 #include <mmf/common/mmcaf.h>                      // TMMSource
       
    27 
       
    28 #include <MsgEditorCommon.h>
       
    29 
       
    30 #include "MsgFrameControl.h"            // for FrameControl
       
    31 
       
    32 #include "MsgMediaControlLogging.h"
       
    33 
       
    34 // ==========================================================
       
    35 
       
    36 // EXTERNAL DATA STRUCTURES
       
    37 
       
    38 // EXTERNAL FUNCTION PROTOTYPES
       
    39 
       
    40 // CONSTANTS
       
    41 
       
    42 // MACROS
       
    43 
       
    44 // LOCAL CONSTANTS AND MACROS
       
    45 
       
    46 // MODULE DATA STRUCTURES
       
    47 
       
    48 // LOCAL FUNCTION PROTOTYPES
       
    49 
       
    50 // ================= MEMBER FUNCTIONS =======================
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // CMsgAudioControl::CMsgAudioControl
       
    54 //
       
    55 // Constructor.
       
    56 // ---------------------------------------------------------
       
    57 //
       
    58 CMsgAudioControl::CMsgAudioControl( MMsgBaseControlObserver& aBaseControlObserver,
       
    59                                     TMdaPriority aPriority,
       
    60                                     TMdaPriorityPreference aPreference ) :
       
    61     CMsgMediaControl( aBaseControlObserver, EMsgComponentIdAudio, EMsgAudioControl ),
       
    62     iPriority( aPriority ),
       
    63     iPreference( aPreference )
       
    64     {
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // CMsgAudioControl::ConstructL
       
    69 //
       
    70 //
       
    71 // ---------------------------------------------------------
       
    72 //
       
    73 void CMsgAudioControl::ConstructL( CMsgEditorView& aParent, MMsgAsyncControlObserver* aObserver )
       
    74     {
       
    75     BaseConstructL( aParent, aObserver );
       
    76     
       
    77     SetRect( MsgEditorCommons::MsgDataPane() );
       
    78     }
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 // CMsgAudioControl::NewL
       
    83 //
       
    84 // Two-phased constructor.
       
    85 // ---------------------------------------------------------
       
    86 //
       
    87 EXPORT_C CMsgAudioControl* CMsgAudioControl::NewL( CMsgEditorView& aParent,
       
    88                                                    MMsgAsyncControlObserver* aObserver,
       
    89                                                    TMdaPriority aPriority,
       
    90                                                    TMdaPriorityPreference aPreference )
       
    91     {
       
    92     CMsgAudioControl* self = new( ELeave ) CMsgAudioControl( aParent, aPriority, aPreference );
       
    93     
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL( aParent, aObserver );
       
    96     CleanupStack::Pop( self );
       
    97     
       
    98     return self;
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------
       
   102 // CMsgAudioControl::~CMsgAudioControl
       
   103 //
       
   104 // Destructor
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 CMsgAudioControl::~CMsgAudioControl()
       
   108     {
       
   109     delete iAudioPlayer;
       
   110     
       
   111     iAudioFile.Close();
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // CMsgAudioControl::SetAndGetSizeL
       
   116 //
       
   117 // Calculates and sets the size of the control and returns new size as
       
   118 // reference aSize.
       
   119 // ---------------------------------------------------------
       
   120 //
       
   121 void CMsgAudioControl::SetAndGetSizeL( TSize& aSize )
       
   122     {
       
   123     // No need to change width
       
   124     // Frame size is always larger than icon size
       
   125     aSize.iHeight = iFrame->FrameSize().iHeight;
       
   126     aSize.iHeight = iBaseLine *
       
   127         ( ( aSize.iHeight + iBaseLine - 1 ) / iBaseLine );
       
   128 
       
   129     SetSize( aSize );
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------
       
   133 // CMsgAudioControl::Reset
       
   134 //
       
   135 // Empties the control.
       
   136 // ---------------------------------------------------------
       
   137 //
       
   138 void CMsgAudioControl::Reset()
       
   139     {
       
   140     Close();
       
   141     
       
   142     CMsgMediaControl::Reset();
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // CMsgAudioControl::SizeChanged
       
   147 //
       
   148 // Called when size is changed.
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 void CMsgAudioControl::SizeChanged()
       
   152     {
       
   153     CMsgMediaControl::SizeChanged();
       
   154     
       
   155     SetPosition( Rect().iTl );
       
   156     }
       
   157 
       
   158 
       
   159 // ---------------------------------------------------------
       
   160 // CMsgAudioControl::LoadL
       
   161 // ---------------------------------------------------------
       
   162 //
       
   163 void CMsgAudioControl::LoadL( RFile& aFileHandle )
       
   164     {
       
   165     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::LoadL()" );
       
   166     
       
   167     iAudioFile.Close();
       
   168     User::LeaveIfError( iAudioFile.Duplicate( aFileHandle ) );
       
   169         
       
   170     if ( !iAudioPlayer )
       
   171         {
       
   172         iAudioPlayer = CMdaAudioPlayerUtility::NewL( *this,
       
   173                                                      iPriority,
       
   174                                                      iPreference );
       
   175         }
       
   176     else
       
   177         {
       
   178         iAudioPlayer->Close();
       
   179         }
       
   180 
       
   181     TMMFileHandleSource mmSource( iAudioFile, iUniqueContentId );
       
   182     iAudioPlayer->OpenFileL( mmSource );
       
   183     
       
   184     SetState( EMsgAsyncControlStateOpening );
       
   185     
       
   186     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::LoadL()" );
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------
       
   190 // CMsgAudioControl::Cancel
       
   191 // ---------------------------------------------------------
       
   192 //
       
   193 void CMsgAudioControl::Cancel()
       
   194     {
       
   195     if ( iState == EMsgAsyncControlStateOpening )
       
   196         {
       
   197         iAudioPlayer->Close();
       
   198         SetState( EMsgAsyncControlStateIdle );
       
   199         }
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------
       
   203 // CMsgAudioControl::Play
       
   204 // ---------------------------------------------------------
       
   205 //
       
   206 void CMsgAudioControl::PlayL()
       
   207     {
       
   208     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::PlayL()" );
       
   209     
       
   210     if ( iAudioPlayer )
       
   211         {           
       
   212         iAudioPlayer->Play();
       
   213         SetState( EMsgAsyncControlStatePlaying );
       
   214         }
       
   215     
       
   216     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::PlayL()" );
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------
       
   220 // CMsgAudioControl::Close
       
   221 // ---------------------------------------------------------
       
   222 //
       
   223 void CMsgAudioControl::Close()
       
   224     {
       
   225     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::Close()" );
       
   226     
       
   227     if ( iAudioPlayer )
       
   228         {
       
   229         iAudioPlayer->Close();
       
   230         delete iAudioPlayer;
       
   231         iAudioPlayer = NULL;
       
   232         }
       
   233     
       
   234     iAudioFile.Close();
       
   235     
       
   236     SetState( EMsgAsyncControlStateIdle );
       
   237     
       
   238     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::Close()" );
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------
       
   242 // CMsgAudioControl::Stop
       
   243 // ---------------------------------------------------------
       
   244 //
       
   245 void CMsgAudioControl::Stop()
       
   246     {
       
   247     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::Stop()" );
       
   248     
       
   249     if ( iAudioPlayer )
       
   250         {
       
   251         iAudioPlayer->Stop();
       
   252         SetState( EMsgAsyncControlStateStopped );
       
   253         }
       
   254     
       
   255     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::Stop()" );
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------
       
   259 // CMsgAudioControl::PauseL
       
   260 // ---------------------------------------------------------
       
   261 //
       
   262 void CMsgAudioControl::PauseL()
       
   263     {
       
   264     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::PauseL()" );
       
   265     
       
   266     if ( iAudioPlayer )
       
   267         {
       
   268         User::LeaveIfError( iAudioPlayer->Pause() );
       
   269         SetState( EMsgAsyncControlStatePaused );
       
   270         }
       
   271     
       
   272     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::PauseL()" );
       
   273     }
       
   274 
       
   275 // =========================================================
       
   276 // Callbacks from CMdaAudioPlayerUtility
       
   277 // =========================================================
       
   278 
       
   279 // ---------------------------------------------------------
       
   280 // CMsgAudioControl::MapcInitComplete
       
   281 // ---------------------------------------------------------
       
   282 //
       
   283 void CMsgAudioControl::MapcInitComplete( TInt aError, 
       
   284                                          const TTimeIntervalMicroSeconds& /*aDuration*/ )
       
   285     {
       
   286     // Close the handle now
       
   287     iAudioFile.Close();
       
   288     
       
   289     // Needed so that audio controller won't consume DRM rights automatically.
       
   290     MMMFDRMCustomCommand* customCommand = iAudioPlayer->GetDRMCustomCommand();
       
   291     if ( customCommand )
       
   292         {
       
   293         customCommand->DisableAutomaticIntent( ETrue );            
       
   294         }
       
   295 
       
   296     HandleCallback( aError, EFalse, EMsgAsyncControlStateReady );    
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------
       
   300 // CMsgAudioControl::MapcPlayComplete
       
   301 // ---------------------------------------------------------
       
   302 //
       
   303 void CMsgAudioControl::MapcPlayComplete( TInt aError )
       
   304     {
       
   305     HandleCallback( aError, ETrue , EMsgAsyncControlStateStopped );
       
   306     }
       
   307 
       
   308 
       
   309 // ---------------------------------------------------------
       
   310 // CMsgAudioControl::HandleCallback
       
   311 // ---------------------------------------------------------
       
   312 //
       
   313 void CMsgAudioControl::HandleCallback( TInt aError, 
       
   314                                        TBool aDefaultStop,
       
   315                                        TMsgAsyncControlState aNewState )
       
   316     {
       
   317     MSGMEDIACONTROLLOGGER_ENTERFN( "MsgAudioControl::HandleCallback()" );
       
   318     MSGMEDIACONTROLLOGGER_WRITEF_ERROR_STATE( _L("CMsgAudioControl::HandleCallbackL: **aError: %d, CurrentState: %S"),aError, iState);
       
   319     MSGMEDIACONTROLLOGGER_WRITEF_ERROR_STATE( _L("CMsgAudioControl::HandleCallbackL: **aError: %d, aNewState: %S"),aError, aNewState);
       
   320     
       
   321     iError = aError;
       
   322 
       
   323     // To simulate drm error, use e.g. -30100
       
   324     switch ( aError )
       
   325         {
       
   326         case KErrNone:
       
   327             {
       
   328             SetState( aNewState );
       
   329             break;
       
   330             }
       
   331         case DRMCommon::EGeneralError:
       
   332         case DRMCommon::EUnknownMIME:
       
   333         case DRMCommon::EVersionNotSupported:
       
   334         case DRMCommon::ESessionError:
       
   335         // const TInt KErrCANoRights      =-17452:
       
   336         case DRMCommon::ENoRights:
       
   337         case DRMCommon::ERightsDBCorrupted:
       
   338         case DRMCommon::EUnsupported:
       
   339         case DRMCommon::EInvalidRights:
       
   340         // const TInt KErrCANoPermission  =-17451: 
       
   341         case DRMCommon::ERightsExpired:
       
   342             {
       
   343             SetState( EMsgAsyncControlStateNoRights );
       
   344             break;
       
   345             }
       
   346 		case KErrInUse: // To handle if head phone is removed from the jack while playing audio.
       
   347             {
       
   348             Stop();
       
   349             break;
       
   350             }
       
   351         case KErrNoMemory:
       
   352         case KErrNotSupported:
       
   353         case KErrCorrupt:
       
   354             {
       
   355             // These errors are always "fatal".
       
   356             aDefaultStop = EFalse;
       
   357             // Fall-through...
       
   358             }
       
   359         default:
       
   360             {
       
   361             if ( aDefaultStop )
       
   362                 {
       
   363                 Stop();
       
   364                 }
       
   365                 
       
   366             SetState( EMsgAsyncControlStateCorrupt );
       
   367             }
       
   368         }
       
   369     
       
   370     MSGMEDIACONTROLLOGGER_LEAVEFN( "MsgAudioControl::HandleCallback()" );
       
   371     }
       
   372 
       
   373 //  End of File