javauis/mmapi_qt/audiostreaming/src.mmf/cmmaaudiostreamplayer.cpp
changeset 23 98ccebc37403
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     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 <mmf/server/mmfdes.h>
       
    21 #include <audiopreference.h>
       
    22 #include <logger.h>
       
    23 
       
    24 #include "CMMAAudioStreamPlayer.h"
       
    25 #include "CMMADataSourceStream.h"
       
    26 #include "MMAFunctionServer.h"
       
    27 #include "CMMAStreamHandler.h"
       
    28 
       
    29 const TInt KPlayerPriority = KAudioPriorityRecording;
       
    30 _LIT(KMMAStreamErrorMessage, "Internal error: %d");
       
    31 
       
    32 // Uid must be same as in S60StreamingSourceUIDs.hrh which is not exported from
       
    33 // EnhancedAudioPlayerUtility/AudioStreaming/AudioStreamingSource.
       
    34 const TUid KMMAMmfS60StreamingSourceUid = { 0x10207AF3 };
       
    35 
       
    36 
       
    37 CMMAAudioStreamPlayer* CMMAAudioStreamPlayer::NewLC(
       
    38     CMMAMMFResolver* aResolver)
       
    39 {
       
    40     CMMAAudioStreamPlayer* self = new(ELeave) CMMAAudioStreamPlayer(aResolver);
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL();
       
    43     return self;
       
    44 }
       
    45 
       
    46 CMMAAudioStreamPlayer::~CMMAAudioStreamPlayer()
       
    47 {
       
    48     delete iStreamHandler;
       
    49 }
       
    50 
       
    51 CMMAAudioStreamPlayer::CMMAAudioStreamPlayer(
       
    52     CMMAMMFResolver* aResolver):
       
    53         CMMAAudioPlayer(aResolver)
       
    54 {
       
    55 }
       
    56 
       
    57 void CMMAAudioStreamPlayer::ConstructL()
       
    58 {
       
    59     CMMAAudioPlayer::ConstructL();
       
    60     iStreamHandler = CMMAStreamHandler::NewL(*this, iController);
       
    61 }
       
    62 
       
    63 TInt CMMAAudioStreamPlayer::DoOpen(TUid aSourceUid,
       
    64                                    const TDesC8& aSourceData,
       
    65                                    TUid aSinkUid,
       
    66                                    const TDesC8& aSinkData,
       
    67                                    TMMFPrioritySettings aPrioritySettings)
       
    68 {
       
    69     // Make sure any existing controller is closed.
       
    70     iEventMonitor->Cancel();
       
    71     iController.Close();
       
    72 
       
    73     // Try opening and configuring each controller in turn
       
    74     TInt error = KErrNotSupported;
       
    75     TInt index = 0;
       
    76 
       
    77     // Try controllers until found a good controller or out of list
       
    78     while ((error != KErrNone) &&
       
    79             (index < iControllerInfos->Count()))
       
    80     {
       
    81         // Open the controller
       
    82         error = iController.Open((*iControllerInfos)[ index ]->Uid(),
       
    83                                  aPrioritySettings);
       
    84 
       
    85         // If the controller was opened without error, start receiving events from it.
       
    86         if (!error)
       
    87         {
       
    88             iEventMonitor->Start();
       
    89 
       
    90             // Add the data source to the controller.
       
    91             error = iController.AddDataSource(aSourceUid,
       
    92                                               aSourceData,
       
    93                                               iStreamHandler->MessageDestination());
       
    94         }
       
    95 
       
    96         // Add the data sink
       
    97         if (!error)
       
    98         {
       
    99             error = iController.AddDataSink(aSinkUid, aSinkData);
       
   100         }
       
   101 
       
   102         // If an error occurred in any of the above, close the controller.
       
   103         if (error)
       
   104         {
       
   105             iEventMonitor->Cancel();
       
   106             iController.Close();
       
   107         }
       
   108 
       
   109         index++;
       
   110     }
       
   111 
       
   112     return error;
       
   113 }
       
   114 
       
   115 CMMASourceStream* CMMAAudioStreamPlayer::AddSourceStreamL(JNIEnv* aJNIEnv,
       
   116         MMAFunctionServer* aEventSource,
       
   117         jobject aReader)
       
   118 {
       
   119     CMMADataSourceStream* sourceStream = CMMADataSourceStream::NewLC(aJNIEnv,
       
   120                                          aEventSource,
       
   121                                          aReader,
       
   122                                          this);
       
   123     User::LeaveIfError(iSourceStreams.Append(sourceStream));
       
   124     CleanupStack::Pop(sourceStream);
       
   125     iStreamHandler->SetSourceStream(sourceStream);
       
   126     return sourceStream;
       
   127 }
       
   128 
       
   129 void CMMAAudioStreamPlayer::PrefetchL()
       
   130 {
       
   131     __ASSERT_DEBUG(iSourceStreams.Count() > 0, User::Invariant());
       
   132 
       
   133     // player priority settings
       
   134     TMMFPrioritySettings prioritySettings;
       
   135     prioritySettings.iPriority = KPlayerPriority;
       
   136     prioritySettings.iPref = EMdaPriorityPreferenceTimeAndQuality;
       
   137     prioritySettings.iState = EMMFStatePlaying;
       
   138 
       
   139     User::LeaveIfError(DoOpen(KMMAMmfS60StreamingSourceUid,
       
   140                               KNullDesC8,
       
   141                               KUidMmfAudioOutput,
       
   142                               KNullDesC8,
       
   143                               prioritySettings));
       
   144     iStreamHandler->PrepareL();
       
   145 }
       
   146 
       
   147 void CMMAAudioStreamPlayer::StartL()
       
   148 {
       
   149     // If the player is in Prefetched state then it is implied that it has read "KMMAStreamRequestBufferSize"
       
   150     // and it can be played
       
   151     LOG1(EJavaMMAPI,EInfo,"CMMAAudioStreamPlayer::StartL() , state = %d",iState);
       
   152     if (iState == EPrefetched)
       
   153     {
       
   154 
       
   155         TInt64 time;
       
   156         GetMediaTime(&time);
       
   157 
       
   158         // inform java side
       
   159         PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   160 
       
   161         // go to started state
       
   162         ChangeState(EStarted);
       
   163 
       
   164         PostActionCompleted(KErrNone);   // java start return
       
   165     }
       
   166     iStreamHandler->StartL();
       
   167 }
       
   168 
       
   169 void CMMAAudioStreamPlayer::StopL(TBool aPostEvent)
       
   170 {
       
   171     LOG1( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::Stop state %d", iState);
       
   172     if (iState == EStarted)
       
   173     {
       
   174         User::LeaveIfError(Pause());
       
   175 
       
   176         // go back to prefetched state
       
   177         ChangeState(EPrefetched);
       
   178         if (aPostEvent)
       
   179         {
       
   180             TInt64 time;
       
   181             GetMediaTime(&time);
       
   182             PostLongEvent(CMMAPlayerEvent::EStopped, time);
       
   183         }
       
   184 
       
   185     }
       
   186     LOG( EJavaMMAPI, EInfo, "CMMAAudioStreamPlayer::Stop OK");
       
   187 }
       
   188 
       
   189 TInt CMMAAudioStreamPlayer::Pause()
       
   190 {
       
   191     iStreamHandler->Pause();
       
   192     return iController.Pause();
       
   193 }
       
   194 
       
   195 void CMMAAudioStreamPlayer::PlayCompleteL(TInt aError)
       
   196 {
       
   197     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::PlayCompleteL error %d",
       
   198               aError);
       
   199 
       
   200     // Before controller is started it must be primed
       
   201     iControllerPrimed = EFalse;
       
   202 
       
   203     TInt64 time;
       
   204     GetDuration(&time);
       
   205 
       
   206     // Send 'Stopped' only when stop() is called.
       
   207     PostLongEvent(CMMAPlayerEvent::EEndOfMedia, time);
       
   208 
       
   209     ChangeState(EPrefetched);   // ready to play again
       
   210 
       
   211     if (aError == KErrNone)
       
   212     {
       
   213         iRepeatCount++;
       
   214 
       
   215         if (iRepeatForever || iRepeatCount < iRepeatNumberOfTimes)
       
   216         {
       
   217             StartL();
       
   218         }
       
   219         else
       
   220         {
       
   221             iRepeatCount = 0;
       
   222         }
       
   223     }
       
   224     else
       
   225     {
       
   226         // error has occured, setting correct number of
       
   227         // repeats for next start
       
   228         SetLoopCount(iRepeatNumberOfTimes);
       
   229     }
       
   230 }
       
   231 
       
   232 void CMMAAudioStreamPlayer::GetDuration(TInt64* aDuration)
       
   233 {
       
   234     CMMAPlayer::GetDuration(aDuration);
       
   235 }
       
   236 
       
   237 void CMMAAudioStreamPlayer::PrepareComplete(TInt aError)
       
   238 {
       
   239     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::PrepareComplete error %d",
       
   240               aError);
       
   241     if (aError == KErrNone)
       
   242     {
       
   243         ChangeState(EPrefetched);
       
   244     }
       
   245     PostActionCompleted(aError);   // java prefetch return
       
   246 }
       
   247 
       
   248 void CMMAAudioStreamPlayer::StartComplete(TInt aError)
       
   249 {
       
   250     ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete error %d",
       
   251               aError);
       
   252 
       
   253     // do not start if player is deallocated or closed
       
   254     // RateControl start can start controller in started state
       
   255     if ((iState != EStarted) &&
       
   256             (iState != EPrefetched))
       
   257     {
       
   258         return;
       
   259     }
       
   260 
       
   261     TInt err = aError;
       
   262     if (!iControllerPrimed)
       
   263     {
       
   264         // Prime must be called when player is started first time or restarted
       
   265         LOG( EJavaMMAPI, EInfo, "MMA::CMMAAudioStreamPlayer::StartComplete PRIME");
       
   266         err = iController.Prime();
       
   267         iControllerPrimed = ETrue;
       
   268         ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete prime error %d",
       
   269                   err);
       
   270     }
       
   271     else
       
   272     {
       
   273         err = KErrNone;
       
   274     }
       
   275 
       
   276     TInt64 time;
       
   277     if (err == KErrNone)
       
   278     {
       
   279         // must be primed before media time can be get
       
   280         GetMediaTime(&time);
       
   281         err = iController.Play();
       
   282         ELOG1( EJavaMMAPI, "MMA::CMMAAudioStreamPlayer::StartComplete play error %d",
       
   283                   err);
       
   284     }
       
   285 
       
   286     // RateControl can start controller in started state, then Java event is
       
   287     // not sent
       
   288     if (err == KErrNone && iState != EStarted)
       
   289     { // move to started state and post started event
       
   290         PostLongEvent(CMMAPlayerEvent::EStarted, time);
       
   291         ChangeState(EStarted);
       
   292     }
       
   293     else
       
   294     { // post error event
       
   295         HandleError(aError);
       
   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 //  END OF FILE