javauis/mmapi_qt/audiostreaming/src.emc/cmmaaudiostreamplayer.cpp
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     1 /*
       
     2 * Copyright (c) 2002 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 streaming audio.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <AudioPreference.h>
       
    21 #include <logger.h>
       
    22 
       
    23 #include "cmmaaudiostreamplayer.h"
       
    24 #include "cmmadatasourcestream.h"
       
    25 #include "mmafunctionserver.h"
       
    26 #include "cmmastreamhandler.h"
       
    27 
       
    28 _LIT(KMMAStreamErrorMessage, "Internal error: %d");
       
    29 
       
    30 
       
    31 CMMAAudioStreamPlayer* CMMAAudioStreamPlayer::NewLC(
       
    32     CMMAEMCResolver* aResolver)
       
    33 {
       
    34     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::NewLC +");
       
    35     CMMAAudioStreamPlayer* self = new(ELeave) CMMAAudioStreamPlayer(aResolver);
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL();
       
    38     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::NewLC -");
       
    39     return self;
       
    40 }
       
    41 
       
    42 CMMAAudioStreamPlayer::~CMMAAudioStreamPlayer()
       
    43 {
       
    44     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::~CMMAAudioStreamPlayer +");
       
    45 
       
    46     if (iMStreamControl->GetState() > MStreamControl::CLOSED)
       
    47     {
       
    48         iMStreamControl->Close();
       
    49     }
       
    50 
       
    51     delete iStreamHandler;
       
    52 
       
    53     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::~CMMAAudioStreamPlayer -");
       
    54 }
       
    55 
       
    56 
       
    57 CMMAAudioStreamPlayer::CMMAAudioStreamPlayer(
       
    58     CMMAEMCResolver* aResolver):
       
    59         CMMAEMCAudioPlayer(aResolver)
       
    60 {
       
    61     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::CMMAAudioStreamPlayer");
       
    62 }
       
    63 
       
    64 void CMMAAudioStreamPlayer::ConstructL()
       
    65 {
       
    66     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::ConstructL +");
       
    67     iControllerPrimed = EFalse;
       
    68     CMMAEMCAudioPlayer::ConstructL();
       
    69     iMetaDataUtility = CMetaDataUtility::NewL();
       
    70     iStreamHandler = CMMAStreamHandler::NewL(*this,
       
    71                      *iMStreamControl,
       
    72                      *iMDataBufferSource,
       
    73                      *iFactory,
       
    74                      *iMetaDataUtility);
       
    75     iActiveSchedulerWait = new(ELeave)CActiveSchedulerWait();
       
    76     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::ConstructL -");
       
    77 }
       
    78 
       
    79 CMMASourceStream* CMMAAudioStreamPlayer::AddSourceStreamL(JNIEnv* aJNIEnv,
       
    80         MMAFunctionServer* aEventSource,
       
    81         jobject aReader)
       
    82 {
       
    83     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::AddSourceStreamL +");
       
    84     CMMADataSourceStream* sourceStream = CMMADataSourceStream::NewLC(aJNIEnv,
       
    85                                          aEventSource,
       
    86                                          aReader,
       
    87                                          this,
       
    88                                          aEventSource);
       
    89     User::LeaveIfError(iSourceStreams.Append(sourceStream));
       
    90     CleanupStack::Pop(sourceStream);
       
    91     iStreamHandler->SetSourceStream(sourceStream);
       
    92     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::AddSourceStreamL -");
       
    93     return sourceStream;
       
    94 }
       
    95 
       
    96 CMetaDataUtility* CMMAAudioStreamPlayer::MetaDataUtilityOwnership()
       
    97 {
       
    98     CMetaDataUtility* temp = iMetaDataUtility;
       
    99     iMetaDataUtility = NULL;
       
   100     return temp;
       
   101 }
       
   102 
       
   103 void CMMAAudioStreamPlayer::DeallocateL()
       
   104 {
       
   105     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::DeallocateL +");
       
   106     iStreamHandler->Stop();
       
   107     iControllerPrimed = EFalse;
       
   108     CMMAEMCPlayerBase::DeallocateL();
       
   109     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::DeallocateL -");
       
   110 }
       
   111 
       
   112 void CMMAAudioStreamPlayer::PrefetchL()
       
   113 {
       
   114     LOG( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::PrefetchL +");
       
   115     __ASSERT_DEBUG(iSourceStreams.Count() > 0, User::Invariant());
       
   116     iStreamHandler->Prepare(*iMimeType);
       
   117     LOG( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::PrefetchL -");
       
   118 }
       
   119 
       
   120 void CMMAAudioStreamPlayer::StartL()
       
   121 {
       
   122     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL +");
       
   123     if (iStreamHandler->LastBufferWritten() &&
       
   124             (iMStreamControl ->GetState() == MStreamControl::PAUSED))
       
   125     {
       
   126         LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL If outer+");
       
   127         TInt64 time;
       
   128         GetMediaTime(&time);
       
   129         TInt err = iMStreamControl->Start();
       
   130         if (err == KErrNone && iState != EStarted)
       
   131         {   LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL If inner+");
       
   132             // move to started state and post started event            
       
   133             ChangeState(EStarted);
       
   134             PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   135         }
       
   136         else
       
   137         {   LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL else inner+");
       
   138             HandleError(err);
       
   139         }
       
   140     }
       
   141     else
       
   142     {   LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL else outer+");
       
   143         iStreamHandler->Start();
       
   144     }
       
   145 
       
   146     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartL -");
       
   147 }
       
   148 
       
   149 void CMMAAudioStreamPlayer::StopL(TBool aPostEvent)
       
   150 {
       
   151     LOG1( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::Stop state %d", iState);
       
   152     if (iState == EStarted)
       
   153     {
       
   154         User::LeaveIfError(Pause());
       
   155         // go back to prefetched state
       
   156         ChangeState(EPrefetched);
       
   157         if (aPostEvent)
       
   158         {
       
   159             TInt64 time;
       
   160             GetMediaTime(&time);
       
   161             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   162         }
       
   163     }
       
   164     LOG( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::Stop OK");
       
   165 }
       
   166 
       
   167 TInt CMMAAudioStreamPlayer::Pause()
       
   168 {
       
   169     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Pause +");
       
   170     iStreamHandler->Pause();
       
   171     LOG1( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer:: iStreamControl State = %d",iMStreamControl->GetState());
       
   172 
       
   173     TInt err = KErrNone;
       
   174     if (iMStreamControl->GetState() != MStreamControl::PAUSED)
       
   175     {
       
   176         err = iMStreamControl->Pause();
       
   177         ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer:: iStreamControl Pause error = %d", err);
       
   178         if ((!iActiveSchedulerWait->IsStarted()) && (err == KErrNone))
       
   179         {
       
   180             iActiveSchedulerWait->Start();
       
   181         }
       
   182     }
       
   183     LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Pause -");
       
   184     return err;
       
   185 }
       
   186 
       
   187 void CMMAAudioStreamPlayer::PlayCompleteL(TInt aError)
       
   188 {
       
   189     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::PlayCompleteL error %d",
       
   190               aError);
       
   191 
       
   192     // Before controller is started it must be primed
       
   193     iControllerPrimed = EFalse;
       
   194 
       
   195     TInt64 time;
       
   196     GetDuration(&time);
       
   197 
       
   198     // Send 'Stopped' only when stop() is called.
       
   199     PostLongEvent(CMMAPlayerEvent::EEndOfMedia, time);
       
   200 
       
   201     ChangeState(EPrefetched);   // ready to play again
       
   202 
       
   203     if (aError == KErrNone)
       
   204     {
       
   205         iRepeatCount++;
       
   206 
       
   207         if (iRepeatForever || iRepeatCount < iRepeatNumberOfTimes)
       
   208         {
       
   209             StartL();
       
   210         }
       
   211         else
       
   212         {
       
   213             iRepeatCount = 0;
       
   214         }
       
   215     }
       
   216     else
       
   217     {
       
   218         // error has occured, setting correct number of
       
   219         // repeats for next start
       
   220         SetLoopCount(iRepeatNumberOfTimes);
       
   221     }
       
   222 }
       
   223 
       
   224 void CMMAAudioStreamPlayer::GetDuration(TInt64* aDuration)
       
   225 {
       
   226     CMMAPlayer::GetDuration(aDuration);
       
   227 }
       
   228 
       
   229 void CMMAAudioStreamPlayer::PrepareComplete(TInt aError)
       
   230 {
       
   231     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::PrepareComplete error %d",
       
   232               aError);
       
   233 
       
   234     if (aError == KErrNone)
       
   235     {
       
   236         ChangeState(EPrefetched);
       
   237     }
       
   238     PostActionCompleted(aError);   // java prefetch return
       
   239 }
       
   240 
       
   241 void CMMAAudioStreamPlayer::StartComplete(TInt aError)
       
   242 {
       
   243     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete error %d",
       
   244               aError);
       
   245 
       
   246     // do not start if player is deallocated or closed
       
   247     // RateControl start can start controller in started state
       
   248     if ((iState != EStarted) &&
       
   249             (iState != EPrefetched))
       
   250     {
       
   251         PostActionCompleted(KErrNone);   // java start return
       
   252         return;
       
   253     }
       
   254 
       
   255     TInt err = aError;
       
   256     if (!iControllerPrimed)
       
   257     {
       
   258         // Prime must be called when player is started first time or restarted
       
   259         err = iMStreamControl->Prime();
       
   260 
       
   261         ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete prime error %d",
       
   262                   err);
       
   263     }
       
   264     else
       
   265     {
       
   266         err = KErrNone;
       
   267     }
       
   268 
       
   269     if (iControllerPrimed && (iState == EPrefetched))
       
   270     {
       
   271 
       
   272         TInt64 time;
       
   273         if (err == KErrNone)
       
   274         {
       
   275             // must be primed before media time can be get
       
   276             GetMediaTime(&time);
       
   277             err = iMStreamControl->Start();
       
   278             ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete play error %d",
       
   279                       err);
       
   280 
       
   281         }
       
   282 
       
   283         // RateControl can start controller in started state, then Java event is
       
   284         // not sent
       
   285         if (err == KErrNone && iState != EStarted)
       
   286         { // move to started state and post started event
       
   287             PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   288             ChangeState(EStarted);
       
   289         }
       
   290         else
       
   291         { // post error event
       
   292             HandleError(aError);
       
   293             PostActionCompleted(aError);   // java start return
       
   294         }
       
   295     }
       
   296 
       
   297 }
       
   298 
       
   299 void CMMAAudioStreamPlayer::HandleError(TInt aError)
       
   300 {
       
   301     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::HandleError error %d",
       
   302               aError);
       
   303 
       
   304     TName errorMessage;
       
   305     errorMessage.Format(KMMAStreamErrorMessage, aError);
       
   306     PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   307 }
       
   308 
       
   309 void CMMAAudioStreamPlayer::Event(MControl* aControl, TUint aEventType, TAny* aEventObject)
       
   310 {
       
   311 
       
   312     switch (aEventType)
       
   313     {
       
   314 
       
   315     case MStreamControlObserver::KStateChangedEvent:
       
   316     {
       
   317         MStateChangedEvent* evt = (MStateChangedEvent*)aEventObject;
       
   318         MStreamControl* control1 = (MStreamControl*)(aControl);
       
   319         switch (control1->GetState())
       
   320         {
       
   321         case MStreamControl::CLOSED:
       
   322         {
       
   323             iPrevStreamControlState = MStreamControl::CLOSED;
       
   324             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :CLOSED");
       
   325         }
       
   326         break;
       
   327 
       
   328         case MStreamControl::INITIALIZED:
       
   329         {
       
   330             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :INITIALIZED");
       
   331             switch (iPrevStreamControlState)
       
   332             {
       
   333             case MStreamControl::CLOSED:
       
   334             {
       
   335                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   336                 if (iActiveSchedulerWait->IsStarted())
       
   337                 {
       
   338                     iActiveSchedulerWait->AsyncStop();
       
   339                 }
       
   340             }
       
   341             break;
       
   342 
       
   343             case MStreamControl::INITIALIZED:
       
   344             {
       
   345                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   346                 LOG( EJavaMMAPI, EInfo, "inner Switch case: MStreamControl::INITIALIZED ");
       
   347                 ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent:ErrorCode = %d ",evt->GetErrorCode());
       
   348                 // error occured during prime operation
       
   349                 // move player back to prefetched state
       
   350                 if (iState == EStarted)
       
   351                 {
       
   352                     ChangeState(EPrefetched);
       
   353                 }
       
   354                 PostActionCompleted(evt->GetErrorCode());   // java start return
       
   355             }
       
   356             break;
       
   357 
       
   358             case MStreamControl::PRIMED:
       
   359             {
       
   360                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   361                 LOG( EJavaMMAPI, EInfo, "inner Switch case: MStreamControl::PRIMED ");
       
   362 
       
   363             }
       
   364             break;
       
   365 
       
   366             case MStreamControl::EXECUTING:
       
   367             {
       
   368                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   369                 LOG( EJavaMMAPI, EInfo, "inner Switch case: MStreamControl::EXECUTING ");
       
   370                 ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent:ErrorCode = %d ",evt->GetErrorCode());
       
   371                 if (KErrEof == evt->GetErrorCode())
       
   372                 {
       
   373                     TRAPD(error, PlayCompleteL(KErrNone));
       
   374                     if (KErrNone != error)
       
   375                     {
       
   376                         ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::Event:PlayCompleteL Error = %d", error);
       
   377                     }
       
   378                 }
       
   379             }
       
   380             break;
       
   381 
       
   382             case MStreamControl::BUFFERING:
       
   383             {
       
   384                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   385                 LOG( EJavaMMAPI, EInfo, "inner Switch case: MStreamControl::BUFFERING ");
       
   386             }
       
   387             break;
       
   388 
       
   389             case MStreamControl::PAUSED:
       
   390             {
       
   391                 iPrevStreamControlState = MStreamControl::INITIALIZED;
       
   392                 LOG( EJavaMMAPI, EInfo, "inner Switch case: MStreamControl::PAUSED ");
       
   393             }
       
   394             break;
       
   395             }
       
   396         }
       
   397         break;
       
   398 
       
   399         case MStreamControl::PRIMED:
       
   400         {
       
   401             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :PRIMED");
       
   402             iPrevStreamControlState = MStreamControl::PRIMED;
       
   403             iControllerPrimed = ETrue;
       
   404             TInt64 time;
       
   405             // must be primed before media time can be get
       
   406             GetMediaTime(&time);
       
   407             TInt err = iMStreamControl->Start();
       
   408             ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::Event play error %d",
       
   409                       err);
       
   410 
       
   411             // RateControl can start controller in started state, then Java event is
       
   412             // not sent
       
   413             if (err == KErrNone && iState != EStarted)
       
   414             { // move to started state and post started event
       
   415                 PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   416                 ChangeState(EStarted);
       
   417             }
       
   418             else
       
   419             {
       
   420                 PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   421             }
       
   422         }
       
   423         break;
       
   424 
       
   425         case MStreamControl::EXECUTING:
       
   426         {
       
   427             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :EXECUTING");
       
   428             iPrevStreamControlState = MStreamControl::EXECUTING;
       
   429             PostActionCompleted(KErrNone);   // java start return
       
   430         }
       
   431         break;
       
   432 
       
   433         case MStreamControl::BUFFERING:
       
   434         {
       
   435             iPrevStreamControlState = MStreamControl::BUFFERING;
       
   436             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :BUFFERING");
       
   437         }
       
   438         break;
       
   439 
       
   440         case MStreamControl::PAUSED:
       
   441         {
       
   442             iPrevStreamControlState = MStreamControl::PAUSED;
       
   443             if (iActiveSchedulerWait->IsStarted())
       
   444             {
       
   445                 iActiveSchedulerWait->AsyncStop();
       
   446             }
       
   447             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :PAUSED");
       
   448         }
       
   449         break;
       
   450 
       
   451         default:
       
   452             LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::Event:KStateChangedEvent :DEFAULT");
       
   453             break;
       
   454         }
       
   455     }
       
   456     break;
       
   457 
       
   458     case MControlObserver::KErrorEvent:
       
   459     {
       
   460         MErrorCode* evt = (MErrorCode*)aEventObject;
       
   461         if (KErrNone != evt->GetErrorCode())
       
   462         {
       
   463             ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::Event:KErrorEvent, err = %d", evt->GetErrorCode());
       
   464         }
       
   465     }
       
   466     break;
       
   467     }
       
   468 }
       
   469 //  END OF FILE