javauis/mmapi_akn/baseline/src/cmmammfplayerbase.cpp
branchRCL_3
changeset 66 2455ef1f5bbc
equal deleted inserted replaced
65:ae942d28ec0e 66: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 <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(TBool aPostEvent)
       
   141 {
       
   142     iMediaTime = KTimeUnknown;
       
   143     User::LeaveIfError(iController.Play());
       
   144     if (aPostEvent)
       
   145     {
       
   146         // inform java side
       
   147         PostLongEvent(CMMAPlayerEvent::EStarted, iStartedEventTime);
       
   148     }
       
   149     
       
   150     ChangeState(EStarted);
       
   151     PostActionCompleted(KErrNone);   // java start return
       
   152 }
       
   153 
       
   154 void CMMAMMFPlayerBase::StopL(TBool aPostEvent)
       
   155 {
       
   156     if (iState == EStarted)
       
   157     {
       
   158         TInt64 time;
       
   159         GetMediaTime(&time);
       
   160         iStartedEventTime = time;
       
   161 
       
   162         TInt err = KErrNone;
       
   163         DEBUG("CMMAMMFPlayerBase::StopL: Position not zero, pausing");
       
   164         err = iController.Pause();
       
   165         
       
   166         if ((err != KErrNone) && (err != KErrNotReady))
       
   167         {
       
   168             DEBUG_INT("CMMAMMFPlayerBase::StopL: pause/stop failed %d, leaving", err);
       
   169             User::Leave(err);
       
   170         }
       
   171 
       
   172         if (aPostEvent)
       
   173         {
       
   174             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   175         }
       
   176         // go back to prefetched state
       
   177         ChangeState(EPrefetched);
       
   178     }
       
   179 }
       
   180 
       
   181 
       
   182 void CMMAMMFPlayerBase::DeallocateL()
       
   183 {
       
   184     if (iState == EPrefetched)
       
   185     {
       
   186         // release all resources
       
   187         if (iEventMonitor)
       
   188         {
       
   189             iEventMonitor->Cancel();
       
   190         }
       
   191 
       
   192         // Change state first to enable AMMS to delete Effect API classes
       
   193         ChangeState(ERealized);
       
   194         iController.Stop();
       
   195         ResetSourceStreams();
       
   196     }
       
   197 }
       
   198 
       
   199 
       
   200 EXPORT_C void CMMAMMFPlayerBase::GetDuration(TInt64* aDuration)
       
   201 {
       
   202     DEBUG("CMMAMMFPlayerBase::GetDuration ");
       
   203     if (iDuration == KTimeUnknown)
       
   204     {
       
   205         DEBUG("CMMAMMFPlayerBase::GetDuration Time unknown ");
       
   206         TTimeIntervalMicroSeconds duration;
       
   207         TInt err = iController.GetDuration(duration);
       
   208         if (!err)
       
   209         {
       
   210             iDuration = duration.Int64();
       
   211         }
       
   212     }
       
   213     *aDuration = iDuration;
       
   214     DEBUG("CMMAMMFPlayerBase::GetDuration - ");
       
   215 }
       
   216 
       
   217 void CMMAMMFPlayerBase::SetMediaTimeL(TInt64* aTime)
       
   218 {
       
   219     DEBUG("CMMAMMFPlayerBase::SetMediaTimeL");
       
   220 
       
   221     // Negative values are not checked here
       
   222     // because it's done already in Java side.
       
   223 
       
   224     // Get clip duration
       
   225     TTimeIntervalMicroSeconds duration;
       
   226     User::LeaveIfError(iController.GetDuration(duration));
       
   227     DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.GetDuration=%d", duration.Int64());
       
   228 
       
   229     TTimeIntervalMicroSeconds position;
       
   230 
       
   231     // If the desired media time is beyond the duration,
       
   232     // the time is set to the end of the media.
       
   233     if (*aTime > duration.Int64())
       
   234     {
       
   235         position = duration;
       
   236     }
       
   237     else
       
   238     {
       
   239         position = *aTime;
       
   240     }
       
   241 
       
   242     TBool paused = EFalse;
       
   243     TInt err = KErrNone;
       
   244 
       
   245     if (iState == EStarted)
       
   246     {
       
   247         paused = ETrue;
       
   248         User::LeaveIfError(err = iController.Pause());
       
   249         DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL after iController.Pause = %d", err);
       
   250     }
       
   251 
       
   252     if (err == KErrNone)
       
   253     {
       
   254         // The controller must be in the PRIMED or PLAYING state
       
   255         User::LeaveIfError(err = iController.SetPosition(position));
       
   256         DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.SetPosition() = %d", err);
       
   257     }
       
   258 
       
   259     // Reset cached media time, because actual set position may be
       
   260     // something else than aTime.
       
   261     iMediaTime = KTimeUnknown;
       
   262 
       
   263     // Inform about the position change to the StateListeners
       
   264     ChangeState(iState);
       
   265 
       
   266     // Get the actual media time
       
   267     GetMediaTime(aTime);
       
   268 
       
   269     iStartedEventTime = iMediaTime;
       
   270 
       
   271     if (err == KErrNone)
       
   272     {
       
   273         if (paused == (TBool)ETrue)
       
   274         {
       
   275             User::LeaveIfError(err = iController.Play());
       
   276             DEBUG_INT("CMMAMMFPlayerBase::SetMediaTimeL iController.Play() = %d", err);
       
   277         }
       
   278     }
       
   279 
       
   280     if (err != KErrNone)
       
   281     {
       
   282         User::Leave(err);
       
   283     }
       
   284 }
       
   285 
       
   286 void CMMAMMFPlayerBase::GetMediaTime(TInt64* aMediaTime)
       
   287 {
       
   288     DEBUG("CMMAMMFPlayerBase::GetMediaTime +");
       
   289     TTimeIntervalMicroSeconds position(0);
       
   290 
       
   291     if (iMediaTime == KTimeUnknown || iState == EStarted)
       
   292     {
       
   293         // The controller must be in the PRIMED or PLAYING state
       
   294         TInt error(iController.GetPosition(position));
       
   295 
       
   296         if (error == KErrNone)
       
   297         {
       
   298             TInt64 newTime = position.Int64();
       
   299             DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime iController.GetPosition : %d", newTime);
       
   300 
       
   301             // Sanity check for media time going backwards or beyond the
       
   302             // duration.
       
   303             // Some native controls may return zero media time for
       
   304             // a few moments just before playback will complete.
       
   305             if (newTime < iMediaTime ||
       
   306                     (iDuration > 0 && newTime > iDuration))
       
   307             {
       
   308                 DEBUG("CMMAMMFPlayerBase::GetMediaTime.GetDuration ");
       
   309                 GetDuration(&iMediaTime);
       
   310             }
       
   311             else
       
   312             {
       
   313                 DEBUG("CMMAMMFPlayerBase::GetMediaTime.else ");
       
   314                 // set return value
       
   315                 iMediaTime = newTime;
       
   316             }
       
   317         }
       
   318         else
       
   319         {
       
   320             DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime: error=%d, returning TIME_UNKNOWN", error);
       
   321             // cannot get media time
       
   322             iMediaTime = KTimeUnknown;
       
   323         }
       
   324     }
       
   325     *aMediaTime = iMediaTime;
       
   326     DEBUG_INT("CMMAMMFPlayerBase::GetMediaTime - %d", *aMediaTime);
       
   327 }
       
   328 
       
   329 void CMMAMMFPlayerBase::CloseL()
       
   330 {
       
   331     CMMAPlayer::CloseL();
       
   332     if (iEventMonitor)
       
   333     {
       
   334         iEventMonitor->Cancel();
       
   335     }
       
   336     // First delete the control and then close the controller
       
   337     // Added after AudioOutputControl
       
   338     iController.Stop();
       
   339     delete iEventMonitor;
       
   340     iEventMonitor = NULL;
       
   341 }
       
   342 
       
   343 void CMMAMMFPlayerBase::HandleEvent(const TMMFEvent& /*aEvent*/)
       
   344 {
       
   345     // empty implementation
       
   346 }
       
   347 
       
   348 //  END OF FILE