javauis/mmapi_akn/baseline/src/cmmaaudioplayer.cpp
branchRCL_3
changeset 26 2455ef1f5bbc
equal deleted inserted replaced
25:ae942d28ec0e 26:2455ef1f5bbc
       
     1 /*
       
     2 * Copyright (c) 2002-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:  This class is used for playing sounds
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <mmf/server/mmfdes.h>
       
    21 #include <mmf/server/mmffile.h>
       
    22 #include <AudioPreference.h>
       
    23 #include <jdebug.h>
       
    24 #include "cmmaaudioplayer.h"
       
    25 
       
    26 const TInt KPlayerPriority = KAudioPriorityRecording;
       
    27 const TInt KErrorMessageSize = 32;
       
    28 
       
    29 const TUid KSourceUid = { KMmfUidDescriptorSource };
       
    30 const TUid KFileSourceUid = { KMmfUidFileSource };
       
    31 _LIT(KErrDefaultError, "Symbian OS Error: %d");
       
    32 
       
    33 const TInt KMinIntervalBeforePrime = 0;
       
    34 
       
    35 CPlaybackCompletedCallback* CPlaybackCompletedCallback::NewL(MPlaybackCompletedCallback& aObs)
       
    36 {
       
    37     CPlaybackCompletedCallback* self = new(ELeave)CPlaybackCompletedCallback(aObs);
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop();
       
    41     return self;
       
    42 }
       
    43 
       
    44 CPlaybackCompletedCallback::~CPlaybackCompletedCallback()
       
    45 {
       
    46     Cancel();
       
    47 }
       
    48 
       
    49 void CPlaybackCompletedCallback::RunL()
       
    50 {
       
    51     iObs.HandlePlaybackCompleteL();
       
    52 }
       
    53 
       
    54 TInt CPlaybackCompletedCallback::RunError(TInt aError)
       
    55 {
       
    56     iObs.ErrorPlaybackComplete(aError);
       
    57     return KErrNone;
       
    58 }
       
    59 
       
    60 CPlaybackCompletedCallback::CPlaybackCompletedCallback(MPlaybackCompletedCallback& aObs)
       
    61         : CTimer(EPriorityStandard), iObs(aObs)
       
    62 {
       
    63     CActiveScheduler::Add(this);
       
    64 }
       
    65 
       
    66 void CPlaybackCompletedCallback::Callback()
       
    67 {
       
    68     if (!IsActive())
       
    69         After(KMinIntervalBeforePrime);
       
    70 }
       
    71 
       
    72 CMMAAudioPlayer* CMMAAudioPlayer::NewLC(
       
    73     CMMAMMFResolver* aResolver)
       
    74 {
       
    75     CMMAAudioPlayer* self = new(ELeave) CMMAAudioPlayer(aResolver);
       
    76     CleanupStack::PushL(self);
       
    77     self->ConstructL();
       
    78     return self;
       
    79 }
       
    80 
       
    81 
       
    82 CMMAAudioPlayer::~CMMAAudioPlayer()
       
    83 {
       
    84 
       
    85     delete iPlaybackCompleted;
       
    86 }
       
    87 
       
    88 
       
    89 CMMAAudioPlayer::CMMAAudioPlayer(
       
    90     CMMAMMFResolver* aResolver):
       
    91         CMMAMMFPlayerBase(aResolver)
       
    92 {
       
    93 }
       
    94 
       
    95 
       
    96 void CMMAAudioPlayer::ConstructL()
       
    97 {
       
    98     CMMAMMFPlayerBase::ConstructL();
       
    99 
       
   100     iPlaybackCompleted = CPlaybackCompletedCallback::NewL(*this);
       
   101 }
       
   102 
       
   103 
       
   104 EXPORT_C void CMMAAudioPlayer::PrefetchDataL(const TDesC8& aData)
       
   105 {
       
   106     DEBUG_INT("MMA::CMMAAudioPlayer::PrefetchDataL aData size %d",
       
   107               aData.Size());
       
   108 
       
   109     // player priority settings
       
   110     TMMFPrioritySettings prioritySettings;
       
   111     prioritySettings.iPriority = KPlayerPriority;
       
   112     prioritySettings.iPref = EMdaPriorityPreferenceTimeAndQuality;
       
   113     prioritySettings.iState = EMMFStatePlaying;
       
   114 
       
   115     TMMFDescriptorConfig srcConfig;
       
   116     srcConfig().iDes = (TAny*)&aData; // wants pointer to TPtr8
       
   117     srcConfig().iDesThreadId = RThread().Id();
       
   118 
       
   119     User::LeaveIfError(DoOpen(KSourceUid,
       
   120                               srcConfig,
       
   121                               KUidMmfAudioOutput,
       
   122                               KNullDesC8,
       
   123                               prioritySettings));
       
   124 
       
   125     User::LeaveIfError(iController.Prime());
       
   126 }
       
   127 
       
   128 EXPORT_C void CMMAAudioPlayer::PrefetchFileL()
       
   129 {
       
   130     DEBUG("MMA::CMMAAudioPlayer::Prefetching from file");
       
   131 
       
   132     // player priority settings
       
   133     TMMFPrioritySettings prioritySettings;
       
   134     prioritySettings.iPriority = KPlayerPriority;
       
   135     prioritySettings.iPref = EMdaPriorityPreferenceTimeAndQuality;
       
   136     prioritySettings.iState = EMMFStatePlaying;
       
   137 
       
   138     TMMFFileConfig srcConfig;
       
   139     srcConfig().iPath = iFileName->Des();
       
   140 
       
   141     User::LeaveIfError(DoOpen(KFileSourceUid,
       
   142                               srcConfig,
       
   143                               KUidMmfAudioOutput,
       
   144                               KNullDesC8,
       
   145                               prioritySettings));
       
   146 
       
   147     User::LeaveIfError(iController.Prime());
       
   148 }
       
   149 
       
   150 EXPORT_C void CMMAAudioPlayer::PlayCompleteL(TInt aError)
       
   151 {
       
   152     TInt64 time;
       
   153     GetDuration(&time);
       
   154     iMediaTime = time;
       
   155     iStartedEventTime = 0;
       
   156 
       
   157     ChangeState(EPrefetched);   // ready to play again
       
   158 
       
   159     // Send 'Stopped' only when stop() is called.
       
   160     PostLongEvent(CMMAPlayerEvent::EEndOfMedia, time);
       
   161 
       
   162     if (aError == KErrNone)
       
   163     {
       
   164         iRepeatCount++;
       
   165 
       
   166         // priming again for allowing e.g. mediatime setting
       
   167         User::LeaveIfError(iController.Prime());
       
   168 
       
   169         if (iRepeatForever || iRepeatCount < iRepeatNumberOfTimes)
       
   170         {
       
   171             StartL(ETrue);
       
   172         }
       
   173         else
       
   174         {
       
   175             iRepeatCount = 0;
       
   176 
       
   177             TTimeIntervalMicroSeconds position(0);
       
   178             User::LeaveIfError(iController.SetPosition(position));
       
   179         }
       
   180     }
       
   181     else
       
   182     {
       
   183         // error has occured, setting correct number of
       
   184         // repeats for next start
       
   185         SetLoopCount(iRepeatNumberOfTimes);
       
   186     }
       
   187 }
       
   188 
       
   189 void CMMAAudioPlayer::RealizeL()
       
   190 {
       
   191     DEBUG("CMMAAudioPlayer::RealizeL");
       
   192     // DataSource must have at least 1 stream or
       
   193     // we must have file to play
       
   194     if ((iSourceStreams.Count() == 0) && !iFileName)
       
   195     {
       
   196         User::Leave(KErrNotEnoughStreams);
       
   197     }
       
   198     CMMAPlayer::RealizeL();
       
   199 }
       
   200 
       
   201 
       
   202 void CMMAAudioPlayer::PrefetchL()
       
   203 {
       
   204     DEBUG("CMMAAudioPlayer::PrefetchL");
       
   205     __ASSERT_DEBUG((iSourceStreams.Count() > 0) || iFileName, User::Invariant());
       
   206 
       
   207     if (iFileName)
       
   208     {
       
   209         // prefetching the file
       
   210         PrefetchFileL();
       
   211         // we can go to prefetched state immediately
       
   212         ChangeState(EPrefetched);
       
   213         PostActionCompleted(KErrNone);
       
   214     }
       
   215     else
       
   216     {
       
   217         // Using TDes -- load the whole sound
       
   218         iSourceStreams[ 0 ]->ReadAllL();
       
   219     }
       
   220     // CMMASourceStream will notify with ReadCompleted
       
   221 }
       
   222 
       
   223 const TDesC& CMMAAudioPlayer::Type()
       
   224 {
       
   225     return KMMAAudioPlayer;
       
   226 }
       
   227 
       
   228 //
       
   229 // CMMASourceStreamReader finished read operation
       
   230 // This is called when ReadL is completed in Prefetch()
       
   231 //
       
   232 void CMMAAudioPlayer::ReadCompletedL(TInt aStatus, const TDesC8& aData)
       
   233 {
       
   234     DEBUG_INT("CMMAAudioPlayer::ReadCompletedL: status = %d", aStatus);
       
   235     if (aStatus < KErrNone)
       
   236     {
       
   237         PostActionCompleted(aStatus);
       
   238     }
       
   239     else
       
   240     {
       
   241         TRAPD(err, PrefetchDataL(aData));
       
   242         if (err == KErrNone)
       
   243         {
       
   244             ChangeState(EPrefetched);
       
   245         }
       
   246         PostActionCompleted(err);
       
   247     }
       
   248 }
       
   249 
       
   250 
       
   251 void CMMAAudioPlayer::HandleEvent(const TMMFEvent& aEvent)
       
   252 {
       
   253     DEBUG("MID::CMMAAudioPlayer::HandleEvent");
       
   254     TInt err = aEvent.iErrorCode;
       
   255     if (iState == EStarted)
       
   256     {
       
   257         // normal situation; will loop in PlayCompleteL if looping is set
       
   258         if ((err == KErrEof || err == KErrUnderflow || err == KErrNone)
       
   259                 && aEvent.iEventType == KMMFEventCategoryPlaybackComplete)
       
   260         {
       
   261             iPlaybackCompleted->Callback();
       
   262         }
       
   263     }
       
   264 
       
   265     if (err ==  KErrDied && aEvent.iEventType == KMMFEventCategoryPlaybackComplete && err == KErrInUse)
       
   266     {
       
   267 
       
   268         // basically pausing the playback
       
   269         //1. when the phone call is received/answered , the player will be pushed to pause state and phone call is given high priority.
       
   270         //2. when the call ends the player will still be in pause state , In this case the user should resume the player.
       
   271         err = iController.Pause();
       
   272 
       
   273         if (iState == EStarted)
       
   274         {
       
   275             TInt64 time;
       
   276             GetMediaTime(&time);
       
   277             iStartedEventTime = time;
       
   278             DEBUG("MID::CMMAAudioPlayer::Going to ChangeState( EPrefetched );");
       
   279 
       
   280             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   281             ChangeState(EPrefetched);
       
   282         }
       
   283     }
       
   284 
       
   285     if (err != KErrNone && err != KErrDied && err != KErrInUse)
       
   286     {
       
   287         ErrorPlaybackComplete(err);
       
   288     }
       
   289 }
       
   290 
       
   291 EXPORT_C void CMMAAudioPlayer::HandlePlaybackCompleteL()
       
   292 {
       
   293     PlayCompleteL(KErrNone);
       
   294 }
       
   295 
       
   296 EXPORT_C void CMMAAudioPlayer::ErrorPlaybackComplete(TInt aError)
       
   297 {
       
   298     DEBUG_INT("MID::CMMAAudioPlayer::ErrorPlaybackComplete: aError = %d", aError);
       
   299     TBuf<KErrorMessageSize> errorMessage;
       
   300     errorMessage.Format(KErrDefaultError, aError);
       
   301     PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   302 
       
   303     // Preparing controller for next try
       
   304     TInt err = iController.Prime();
       
   305     if (err != KErrNone)
       
   306     {
       
   307         // Prime failed
       
   308         errorMessage.Format(KErrDefaultError, err);
       
   309         PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   310         // we cannot recover, going back to unrealized state
       
   311         ChangeState(EUnrealized);
       
   312         return;
       
   313     }
       
   314 
       
   315     // If player was in started state, then error will change state to
       
   316     // EPrefetched. In other cases the old state is retained.
       
   317     if (iState == EStarted)
       
   318     {
       
   319         ChangeState(EPrefetched);
       
   320     }
       
   321 }
       
   322 
       
   323 //  END OF FILE