javauis/mmapi_qt/baseline/src/cmmaaudioplayer.cpp
branchRCL_3
changeset 24 0fd27995241b
child 26 dc7c549001d5
equal deleted inserted replaced
20:f9bb0fca356a 24:0fd27995241b
       
     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 <logger.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     LOG1( EJavaMMAPI, EInfo, "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     LOG( EJavaMMAPI, EInfo, "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 	LOG( EJavaMMAPI, EInfo, "CMMAAudioPlayer::PlayCompleteL +");
       
   153     TInt64 time;
       
   154     GetDuration(&time);
       
   155     iMediaTime = time;
       
   156     iStartedEventTime = 0;
       
   157 
       
   158     ChangeState(EPrefetched);   // ready to play again
       
   159 
       
   160     // Send 'Stopped' only when stop() is called.
       
   161     PostLongEvent(CMMAPlayerEvent::EEndOfMedia, time);
       
   162 
       
   163     if (aError == KErrNone)
       
   164     {
       
   165         iRepeatCount++;
       
   166 
       
   167         // priming again for allowing e.g. mediatime setting
       
   168         User::LeaveIfError(iController.Prime());
       
   169 
       
   170         if (iRepeatForever || iRepeatCount < iRepeatNumberOfTimes)
       
   171         {
       
   172             StartL();
       
   173         }
       
   174         else
       
   175         {
       
   176             iRepeatCount = 0;
       
   177 
       
   178             TTimeIntervalMicroSeconds position(0);
       
   179             User::LeaveIfError(iController.SetPosition(position));
       
   180         }
       
   181     }
       
   182     else
       
   183     {
       
   184         // error has occured, setting correct number of
       
   185         // repeats for next start
       
   186         SetLoopCount(iRepeatNumberOfTimes);
       
   187     }
       
   188     	LOG( EJavaMMAPI, EInfo, "CMMAAudioPlayer::PlayCompleteL -");
       
   189 }
       
   190 
       
   191 void CMMAAudioPlayer::RealizeL()
       
   192 {
       
   193     LOG( EJavaMMAPI, EInfo, "CMMAAudioPlayer::RealizeL");
       
   194     // DataSource must have at least 1 stream or
       
   195     // we must have file to play
       
   196     if ((iSourceStreams.Count() == 0) && !iFileName)
       
   197     {
       
   198         User::Leave(KErrNotEnoughStreams);
       
   199     }
       
   200     CMMAPlayer::RealizeL();
       
   201 }
       
   202 
       
   203 
       
   204 void CMMAAudioPlayer::PrefetchL()
       
   205 {
       
   206     LOG( EJavaMMAPI, EInfo, "CMMAAudioPlayer::PrefetchL");
       
   207     __ASSERT_DEBUG((iSourceStreams.Count() > 0) || iFileName, User::Invariant());
       
   208 
       
   209     if (iFileName)
       
   210     {
       
   211         // prefetching the file
       
   212         PrefetchFileL();
       
   213         // we can go to prefetched state immediately
       
   214         PostActionCompletedFile();
       
   215         ChangeState(EPrefetched);
       
   216         //PostActionCompleted(KErrNone);
       
   217     }
       
   218     else
       
   219     {
       
   220         // Using TDes -- load the whole sound
       
   221         iSourceStreams[ 0 ]->ReadAllL();
       
   222     }
       
   223     // CMMASourceStream will notify with ReadCompleted
       
   224 }
       
   225 
       
   226 const TDesC& CMMAAudioPlayer::Type()
       
   227 {
       
   228     return KMMAAudioPlayer;
       
   229 }
       
   230 
       
   231 //
       
   232 // CMMASourceStreamReader finished read operation
       
   233 // This is called when ReadL is completed in Prefetch()
       
   234 //
       
   235 void CMMAAudioPlayer::ReadCompletedL(TInt aStatus, const TDesC8& aData)
       
   236 {
       
   237     LOG1( EJavaMMAPI, EInfo, "CMMAAudioPlayer::ReadCompletedL: status = %d", aStatus);
       
   238     if (aStatus < KErrNone)
       
   239     {
       
   240         PostActionCompleted(aStatus);
       
   241     }
       
   242     else
       
   243     {
       
   244         TRAPD(err, PrefetchDataL(aData));
       
   245         if (err == KErrNone)
       
   246         {
       
   247             ChangeState(EPrefetched);
       
   248         }
       
   249         PostActionCompleted(err);
       
   250     }
       
   251 }
       
   252 
       
   253 
       
   254 void CMMAAudioPlayer::HandleEvent(const TMMFEvent& aEvent)
       
   255 {
       
   256     LOG( EJavaMMAPI, EInfo, "MID::CMMAAudioPlayer::HandleEvent");
       
   257     TInt err = aEvent.iErrorCode;
       
   258     if (iState == EStarted)
       
   259     {
       
   260         // normal situation; will loop in PlayCompleteL if looping is set
       
   261         if ((err == KErrEof || err == KErrUnderflow || err == KErrNone)
       
   262                 && aEvent.iEventType == KMMFEventCategoryPlaybackComplete)
       
   263         {
       
   264             iPlaybackCompleted->Callback();
       
   265         }
       
   266     }
       
   267 
       
   268     if (err ==  KErrDied && aEvent.iEventType == KMMFEventCategoryPlaybackComplete)
       
   269     {
       
   270 
       
   271         // basically pausing the playback
       
   272         //1. when the phone call is received/answered , the player will be pushed to pause state and phone call is given high priority.
       
   273         //2. when the call ends the player will still be in pause state , In this case the user should resume the player.
       
   274         err = iController.Pause();
       
   275 
       
   276         if (iState == EStarted)
       
   277         {
       
   278             TInt64 time;
       
   279             GetMediaTime(&time);
       
   280             iStartedEventTime = time;
       
   281             LOG( EJavaMMAPI, EInfo, "MID::CMMAAudioPlayer::Going to ChangeState( EPrefetched );");
       
   282 
       
   283             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   284             ChangeState(EPrefetched);
       
   285         }
       
   286     }
       
   287 
       
   288     if (err != KErrNone && err != KErrDied)
       
   289     {
       
   290         ErrorPlaybackComplete(err);
       
   291     }
       
   292 }
       
   293 
       
   294 EXPORT_C void CMMAAudioPlayer::HandlePlaybackCompleteL()
       
   295 {
       
   296     PlayCompleteL(KErrNone);
       
   297 }
       
   298 
       
   299 EXPORT_C void CMMAAudioPlayer::ErrorPlaybackComplete(TInt aError)
       
   300 {
       
   301     ELOG1( EJavaMMAPI, "MID::CMMAAudioPlayer::ErrorPlaybackComplete: aError = %d", aError);
       
   302     TBuf<KErrorMessageSize> errorMessage;
       
   303     errorMessage.Format(KErrDefaultError, aError);
       
   304     PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   305 
       
   306     // Preparing controller for next try
       
   307     TInt err = iController.Prime();
       
   308     if (err != KErrNone)
       
   309     {
       
   310         // Prime failed
       
   311         errorMessage.Format(KErrDefaultError, err);
       
   312         PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   313         // we cannot recover, going back to unrealized state
       
   314         ChangeState(EUnrealized);
       
   315         return;
       
   316     }
       
   317 
       
   318     // If player was in started state, then error will change state to
       
   319     // EPrefetched. In other cases the old state is retained.
       
   320     if (iState == EStarted)
       
   321     {
       
   322         ChangeState(EPrefetched);
       
   323     }
       
   324 }
       
   325 
       
   326 //  END OF FILE