javauis/mmapi_akn/baseline/src/cmmammfplayerbase.cpp
branchRCL_3
changeset 19 04becd199f91
child 46 4376525cdefb
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     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 <AudioPreference.h>
       
    22 #include <jdebug.h>
       
    23 #include "cmmammfplayerbase.h"
       
    24 #include "cmmammfresolver.h"
       
    25 
       
    26 
       
    27 CMMAMMFPlayerBase::~CMMAMMFPlayerBase()
       
    28 {
       
    29     // First delete the control and then close the controller
       
    30     // Added after AudioOutputControl
       
    31     DeleteControls();
       
    32     if (iControllerInfos)
       
    33     {
       
    34         iControllerInfos->ResetAndDestroy();
       
    35     }
       
    36     delete iControllerInfos;
       
    37 
       
    38     if (iEventMonitor)
       
    39     {
       
    40         iEventMonitor->Cancel();
       
    41     }
       
    42 
       
    43     iController.Close();
       
    44 
       
    45     delete iEventMonitor;
       
    46 
       
    47     delete iFileName;
       
    48 }
       
    49 
       
    50 
       
    51 CMMAMMFPlayerBase::CMMAMMFPlayerBase(
       
    52     CMMAMMFResolver* aResolver) :
       
    53         iMediaTime(KTimeUnknown), iStartedEventTime(0)
       
    54 {
       
    55     // implementation array ownership is transferred
       
    56     iControllerInfos = aResolver->ImplementationsOwnership();
       
    57 
       
    58     // content type ownership is transferred
       
    59     iContentType = aResolver->ContentTypeOwnership();
       
    60 
       
    61     // file name ownership is transferred
       
    62     iFileName = aResolver->FileNameOwnership();
       
    63 }
       
    64 
       
    65 
       
    66 void CMMAMMFPlayerBase::ConstructL()
       
    67 {
       
    68     CMMAPlayer::ConstructL();
       
    69 
       
    70     // Create an event monitor
       
    71     iEventMonitor =
       
    72         CMMFControllerEventMonitor::NewL(*this, iController);
       
    73 }
       
    74 
       
    75 
       
    76 EXPORT_C RMMFController& CMMAMMFPlayerBase::Controller()
       
    77 {
       
    78     return iController;
       
    79 }
       
    80 
       
    81 EXPORT_C TInt CMMAMMFPlayerBase::DoOpen(TUid aSourceUid,
       
    82                                         const TDesC8& aSourceData,
       
    83                                         TUid aSinkUid,
       
    84                                         const TDesC8& aSinkData,
       
    85                                         TMMFPrioritySettings aPrioritySettings)
       
    86 {
       
    87     // Make sure any existing controller is closed.
       
    88     iEventMonitor->Cancel();
       
    89     iController.Close();
       
    90 
       
    91     // Try opening and configuring each controller in turn
       
    92     TInt error = KErrNotSupported;
       
    93     TInt index = 0;
       
    94 
       
    95     // Try controllers until found a good controller or out of list
       
    96     while ((error != KErrNone) &&
       
    97             (index < iControllerInfos->Count()))
       
    98     {
       
    99         // Open the controller
       
   100         error = iController.Open((*iControllerInfos)[ index ]->Uid(),
       
   101                                  aPrioritySettings, ETrue);
       
   102 
       
   103         // If the controller was opened without error, start receiving events from it.
       
   104         if (!error)
       
   105         {
       
   106             iEventMonitor->Start();
       
   107 
       
   108             // Add the data source to the controller.
       
   109             error = iController.AddDataSource(aSourceUid, aSourceData);
       
   110         }
       
   111 
       
   112         // Add the data sink
       
   113         if (!error)
       
   114         {
       
   115             error = iController.AddDataSink(aSinkUid, aSinkData);
       
   116         }
       
   117 
       
   118         // If an error occurred in any of the above, close the controller.
       
   119         if (error)
       
   120         {
       
   121             iEventMonitor->Cancel();
       
   122             iController.Close();
       
   123         }
       
   124 
       
   125         index++;
       
   126     }
       
   127 
       
   128     return error;
       
   129 }
       
   130 
       
   131 TBool CMMAMMFPlayerBase::IsFilePlayer()
       
   132 {
       
   133     if (iFileName != NULL)
       
   134     {
       
   135         return ETrue;
       
   136     }
       
   137     return EFalse;
       
   138 }
       
   139 
       
   140 void CMMAMMFPlayerBase::StartL()
       
   141 {
       
   142     iMediaTime = KTimeUnknown;
       
   143     User::LeaveIfError(iController.Play());
       
   144     // inform java side
       
   145     PostLongEvent(CMMAPlayerEvent::EStarted, iStartedEventTime);
       
   146     ChangeState(EStarted);
       
   147     PostActionCompleted(KErrNone);   // java start return
       
   148 }
       
   149 
       
   150 void CMMAMMFPlayerBase::StopL(TBool aPostEvent)
       
   151 {
       
   152     if (iState == EStarted)
       
   153     {
       
   154         TInt64 time;
       
   155         GetMediaTime(&time);
       
   156         iStartedEventTime = time;
       
   157 
       
   158         TInt err = KErrNone;
       
   159         // AAC controller does not support multiple
       
   160         // calls to pause but leave with KErrNotReady.
       
   161         // That error is dismissed as player should
       
   162         // be paused already in that case.
       
   163         if (time == 0)
       
   164         {
       
   165             DEBUG("CMMAMMFPlayerBase::StopL: Position is zero, stopping");
       
   166             // Normally pause would be called, but if
       
   167             // current time is zero, Stop is called instead.
       
   168             // This is done because video playback breaks
       
   169             // if pause is called between events
       
   170             // KMMFEventCategoryVideoLoadingStarted and
       
   171             // KMMFEventCategoryVideoLoadingCompleted
       
   172             // (no wurther events are delivered altough
       
   173             // playback continues fine).
       
   174             // However calling Stop is tolerated in that
       
   175             // situation.
       
   176             err = iController.Stop();
       
   177             if (err == KErrNone)
       
   178             {
       
   179                 err = iController.Prime();
       
   180             }
       
   181         }
       
   182         else
       
   183         {
       
   184             DEBUG("CMMAMMFPlayerBase::StopL: Position not zero, pausing");
       
   185             err = iController.Pause();
       
   186         }
       
   187 
       
   188         if ((err != KErrNone) && (err != KErrNotReady))
       
   189         {
       
   190             DEBUG_INT("CMMAMMFPlayerBase::StopL: pause/stop failed %d, leaving", err);
       
   191             User::Leave(err);
       
   192         }
       
   193 
       
   194         if (aPostEvent)
       
   195         {
       
   196             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   197         }
       
   198         // go back to prefetched state
       
   199         ChangeState(EPrefetched);
       
   200     }
       
   201 }
       
   202 
       
   203 
       
   204 void CMMAMMFPlayerBase::DeallocateL()
       
   205 {
       
   206     if (iState == EPrefetched)
       
   207     {
       
   208         // release all resources
       
   209         if (iEventMonitor)
       
   210         {
       
   211             iEventMonitor->Cancel();
       
   212         }
       
   213 
       
   214         // Change state first to enable AMMS to delete Effect API classes
       
   215         ChangeState(ERealized);
       
   216         iController.Stop();
       
   217         ResetSourceStreams();
       
   218     }
       
   219 }
       
   220 
       
   221 
       
   222 EXPORT_C void CMMAMMFPlayerBase::GetDuration(TInt64* aDuration)
       
   223 {
       
   224     DEBUG("CMMAMMFPlayerBase::GetDuration ");
       
   225     if (iDuration == KTimeUnknown)
       
   226     {
       
   227         DEBUG("CMMAMMFPlayerBase::GetDuration Time unknown ");
       
   228         TTimeIntervalMicroSeconds duration;
       
   229         TInt err = iController.GetDuration(duration);
       
   230         if (!err)
       
   231         {
       
   232             iDuration = duration.Int64();
       
   233         }
       
   234     }
       
   235     *aDuration = iDuration;
       
   236     DEBUG("CMMAMMFPlayerBase::GetDuration - ");
       
   237 }
       
   238 
       
   239 void CMMAMMFPlayerBase::SetMediaTimeL(TInt64* aTime)
       
   240 {
       
   241     DEBUG("CMMAMMFPlayerBase::SetMediaTimeL");
       
   242 
       
   243     // Negative values are not checked here
       
   244     // because it's done already in Java side.
       
   245 
       
   246     // Get clip duration
       
   247     TTimeIntervalMicroSeconds duration;
       
   248     User::LeaveIfError(iController.GetDuration(duration));
       
   249     DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.GetDuration=%d", duration.Int64());
       
   250 
       
   251     TTimeIntervalMicroSeconds position;
       
   252 
       
   253     // If the desired media time is beyond the duration,
       
   254     // the time is set to the end of the media.
       
   255     if (*aTime > duration.Int64())
       
   256     {
       
   257         position = duration;
       
   258     }
       
   259     else
       
   260     {
       
   261         position = *aTime;
       
   262     }
       
   263 
       
   264     TBool paused = EFalse;
       
   265     TInt err = KErrNone;
       
   266 
       
   267     if (iState == EStarted)
       
   268     {
       
   269         paused = ETrue;
       
   270         User::LeaveIfError(err = iController.Pause());
       
   271         DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL after iController.Pause = %d", err);
       
   272     }
       
   273 
       
   274     if (err == KErrNone)
       
   275     {
       
   276         // The controller must be in the PRIMED or PLAYING state
       
   277         User::LeaveIfError(err = iController.SetPosition(position));
       
   278         DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.SetPosition() = %d", err);
       
   279     }
       
   280 
       
   281     // Reset cached media time, because actual set position may be
       
   282     // something else than aTime.
       
   283     iMediaTime = KTimeUnknown;
       
   284 
       
   285     // Inform about the position change to the StateListeners
       
   286     ChangeState(iState);
       
   287 
       
   288     // Get the actual media time
       
   289     GetMediaTime(aTime);
       
   290 
       
   291     iStartedEventTime = iMediaTime;
       
   292 
       
   293     if (err == KErrNone)
       
   294     {
       
   295         if (paused == (TBool)ETrue)
       
   296         {
       
   297             User::LeaveIfError(err = iController.Play());
       
   298             DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.Play() = %d", err);
       
   299         }
       
   300     }
       
   301 
       
   302     if (err != KErrNone)
       
   303     {
       
   304         User::Leave(err);
       
   305     }
       
   306 }
       
   307 
       
   308 void CMMAMMFPlayerBase::GetMediaTime(TInt64* aMediaTime)
       
   309 {
       
   310     DEBUG("CMMAMMFPlayerBase::GetMediaTime +");
       
   311     TTimeIntervalMicroSeconds position(0);
       
   312 
       
   313     if (iMediaTime == KTimeUnknown || iState == EStarted)
       
   314     {
       
   315         // The controller must be in the PRIMED or PLAYING state
       
   316         TInt error(iController.GetPosition(position));
       
   317 
       
   318         if (error == KErrNone)
       
   319         {
       
   320             TInt64 newTime = position.Int64();
       
   321             DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime iController.GetPosition : %d", newTime);
       
   322 
       
   323             // Sanity check for media time going backwards or beyond the
       
   324             // duration.
       
   325             // Some native controls may return zero media time for
       
   326             // a few moments just before playback will complete.
       
   327             if (newTime < iMediaTime ||
       
   328                     (iDuration > 0 && newTime > iDuration))
       
   329             {
       
   330                 DEBUG("CMMAMMFPlayerBase::GetMediaTime.GetDuration ");
       
   331                 GetDuration(&iMediaTime);
       
   332             }
       
   333             else
       
   334             {
       
   335                 DEBUG("CMMAMMFPlayerBase::GetMediaTime.else ");
       
   336                 // set return value
       
   337                 iMediaTime = newTime;
       
   338             }
       
   339         }
       
   340         else
       
   341         {
       
   342             DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime: error=%d, returning TIME_UNKNOWN", error);
       
   343             // cannot get media time
       
   344             iMediaTime = KTimeUnknown;
       
   345         }
       
   346     }
       
   347     *aMediaTime = iMediaTime;
       
   348     DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime - %d", *aMediaTime);
       
   349 }
       
   350 
       
   351 void CMMAMMFPlayerBase::CloseL()
       
   352 {
       
   353     CMMAPlayer::CloseL();
       
   354     if (iEventMonitor)
       
   355     {
       
   356         iEventMonitor->Cancel();
       
   357     }
       
   358     // First delete the control and then close the controller
       
   359     // Added after AudioOutputControl
       
   360     iController.Stop();
       
   361     delete iEventMonitor;
       
   362     iEventMonitor = NULL;
       
   363 }
       
   364 
       
   365 void CMMAMMFPlayerBase::HandleEvent(const TMMFEvent& /*aEvent*/)
       
   366 {
       
   367     // empty implementation
       
   368 }
       
   369 
       
   370 //  END OF FILE