javauis/mmapi_qt/baseline/src/cmmaplayer.cpp
changeset 23 98ccebc37403
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     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 base class for all players.
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDE FILES
       
    19 #include <logger.h>
       
    20 
       
    21 #include <JniEnvWrapper.h>
       
    22 
       
    23 #include "cmmaplayer.h"
       
    24 #include "mmafunctionserver.h"
       
    25 #include "cmmadurationupdater.h"
       
    26 #include "cmmavolumecontrol.h"
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT(KPanicOutOfMem, "out of memory");
       
    30 
       
    31 #ifdef _DEBUG // Needed only in ASSER_DEBUG statements.
       
    32 _LIT(KPanicMethod, "method not found");
       
    33 #endif
       
    34 
       
    35 CMMAPlayer::~CMMAPlayer()
       
    36 {
       
    37     LOG( EJavaMMAPI, EInfo, "CMMAPlayer::~CMMAPlayer()");
       
    38 
       
    39     iSourceStreams.ResetAndDestroy();
       
    40     if (iControls.Count() > 0)
       
    41         iControls.ResetAndDestroy();
       
    42     iStateListeners.Reset();
       
    43     delete iActionCompletedEvent;
       
    44     delete iOOMErrorEvent;
       
    45     delete iDurationUpdater;
       
    46     delete iContentType;
       
    47 }
       
    48 
       
    49 
       
    50 CMMAPlayer::CMMAPlayer():
       
    51         iRepeatNumberOfTimes(0),
       
    52         iRepeatForever(EFalse),
       
    53         iRepeatCount(0),
       
    54         iDuration(KTimeUnknown),
       
    55         iState(EUnrealized)
       
    56 {
       
    57 }
       
    58 
       
    59 
       
    60 void CMMAPlayer::ConstructL()
       
    61 {
       
    62     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ConstructL + ");
       
    63     iDurationUpdater = CMMADurationUpdater::NewL(*this);
       
    64     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ConstructL - ");
       
    65 }
       
    66 
       
    67 
       
    68 void CMMAPlayer::StaticAddSourceStreamL(JNIEnv* aJniEnv,
       
    69                                         CMMAPlayer* aPlayer,
       
    70                                         MMAFunctionServer* aEventSource,
       
    71                                         jobject aReader,
       
    72                                         CMMASourceStream** aSourceStream)
       
    73 {
       
    74     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticAddSourceStreamL +");
       
    75 
       
    76     // JNI interface pointer can't be passed to different thread, so
       
    77     // it is needed to get valid JNI interface pointer for Event Server thread
       
    78      aJniEnv = aEventSource-> getValidJniEnv();
       
    79 
       
    80     *aSourceStream = aPlayer->AddSourceStreamL(aJniEnv,
       
    81                      aEventSource,
       
    82                      aReader);
       
    83     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticAddSourceStreamL -");
       
    84 }
       
    85 
       
    86 
       
    87 
       
    88 void CMMAPlayer::StaticSetPlayerListenerObjectL(CMMAPlayer* aPlayer,
       
    89 		MMAFunctionServer* aEventsource,
       
    90         jobject aListenerObject,
       
    91         JNIEnv* aJni,
       
    92         MMMAEventPoster* aPoster)
       
    93 {
       
    94 
       
    95     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticSetPlayerListenerObjectL +");
       
    96 
       
    97     // JNI interface pointer can't be passed to different thread, so
       
    98     // it is needed to get valid JNI interface pointer for Event Server thread
       
    99      aJni = aEventsource-> getValidJniEnv();
       
   100 
       
   101     aPlayer->SetPlayerListenerObjectL(aListenerObject, aJni, aPoster);
       
   102 
       
   103     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticSetPlayerListenerObjectL -");
       
   104 }
       
   105 
       
   106 
       
   107 void CMMAPlayer::StaticInitPlayerL(CMMAPlayer* aPlayer,
       
   108 								   MMAFunctionServer* aEventsource,
       
   109                                    jobject aPlayerObject,
       
   110                                    JNIEnv* aJni)
       
   111 {
       
   112     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticInitPlayerL +");
       
   113 
       
   114     // JNI interface pointer can't be passed to different thread, so
       
   115     // it is needed to get valid JNI interface pointer for Event Server thread
       
   116  	aJni = aEventsource-> getValidJniEnv();
       
   117 
       
   118     jmethodID actionCompletedMethod = aJni->GetMethodID(
       
   119                                           aJni->GetObjectClass(aPlayerObject),
       
   120                                           "actionCompleted",
       
   121                                           "(I)V");
       
   122                                           
       
   123     jmethodID actionCompletedFileMethod = aJni->GetMethodID(
       
   124                                           aJni->GetObjectClass(aPlayerObject),
       
   125                                           "actionCompletedFile",
       
   126                                           "()V");    
       
   127                                           
       
   128     jmethodID actionCompletedStartMethod = aJni->GetMethodID(
       
   129                                           aJni->GetObjectClass(aPlayerObject),
       
   130                                           "actionCompletedStart",
       
   131                                           "()V");                                                                                 
       
   132 
       
   133     // Sanity Check, something is really wrong if methods cannot be found
       
   134     __ASSERT_DEBUG(actionCompletedMethod,
       
   135                    User::Panic(KPanicMethod, KErrGeneral));
       
   136 
       
   137     aPlayer->iActionCompletedEvent = new(ELeave) CMMAEvent(
       
   138         aPlayerObject,
       
   139         actionCompletedMethod,
       
   140         CMMAEvent::EReusableEvent);
       
   141             
       
   142     aPlayer->iActionCompletedFileEvent = new(ELeave) CMMAEvent(
       
   143         aPlayerObject,
       
   144         actionCompletedFileMethod,
       
   145         CMMAEvent::EReusableEvent);     
       
   146             
       
   147     aPlayer->iActionCompletedStartEvent = new(ELeave) CMMAEvent(
       
   148         aPlayerObject,
       
   149         actionCompletedStartMethod,
       
   150         CMMAEvent::EReusableEvent);
       
   151 
       
   152     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticInitPlayerL -");
       
   153 }
       
   154 
       
   155 CMMAControl* CMMAPlayer::StaticControl(CMMAPlayer* aPlayer, TInt aIndex)
       
   156 {
       
   157     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::StaticControl +");
       
   158     return aPlayer->Control(aIndex);
       
   159 
       
   160 }
       
   161 
       
   162 
       
   163 void CMMAPlayer::RealizeL()
       
   164 {
       
   165     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::RealizeL +");
       
   166     ChangeState(ERealized);
       
   167 
       
   168 }
       
   169 
       
   170 
       
   171 void CMMAPlayer::CloseL()
       
   172 {
       
   173     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::CloseL ");
       
   174     ChangeState(EClosed);
       
   175     PostObjectEvent(CMMAPlayerEvent::EClosed, NULL);   
       
   176 }
       
   177 
       
   178 
       
   179 void CMMAPlayer::GetDuration(TInt64* aDuration)
       
   180 {
       
   181     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::GetDuration ");
       
   182     *aDuration = iDuration;
       
   183 }
       
   184 
       
   185 
       
   186 void CMMAPlayer::SetMediaTimeL(TInt64* aTime)
       
   187 {
       
   188     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::SetMediaTimeL ");
       
   189     *aTime = KErrNotSupported;
       
   190 }
       
   191 
       
   192 void CMMAPlayer::GetMediaTime(TInt64* aMediaTime)
       
   193 {
       
   194     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::GetMediaTime ");
       
   195     *aMediaTime = KErrNotSupported;
       
   196 }
       
   197 
       
   198 
       
   199 EXPORT_C void CMMAPlayer::SetLoopCount(TInt aCount)
       
   200 {
       
   201     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::SetLoopCount ");
       
   202     iRepeatForever = (aCount == KJavaRepeatForever);
       
   203     iRepeatNumberOfTimes = aCount;
       
   204     iRepeatCount = 0;
       
   205 }
       
   206 
       
   207 HBufC* CMMAPlayer::ContentType() const
       
   208 {
       
   209     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ContentType ");
       
   210     return iContentType;
       
   211 }
       
   212 
       
   213 void CMMAPlayer::SetPlayerListenerObjectL(jobject aListenerObject,
       
   214         JNIEnv* aJni,
       
   215         MMMAEventPoster* aEventPoster)
       
   216 {
       
   217     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::SetPlayerListenerObjectL +");
       
   218     iListenerObject = aListenerObject;
       
   219     iEventPoster = aEventPoster;
       
   220 
       
   221     jclass listenerClass = aJni->GetObjectClass(aListenerObject);
       
   222 
       
   223     iPostEvent = aJni->GetMethodID(
       
   224                      listenerClass,
       
   225                      "postEvent",
       
   226                      "(Ljava/lang/String;Ljava/lang/Object;)V");
       
   227 
       
   228     iPostObjectEvent = aJni->GetMethodID(listenerClass,
       
   229                                          "postObjectEvent",
       
   230                                          "(ILjava/lang/Object;)V");
       
   231 
       
   232     iPostLongEvent = aJni->GetMethodID(listenerClass,
       
   233                                        "postLongEvent",
       
   234                                        "(IJ)V");
       
   235 
       
   236     iPostStringEvent = aJni->GetMethodID(listenerClass,
       
   237                                          "postStringEvent",
       
   238                                          "(ILjava/lang/String;)V");
       
   239 
       
   240     iPostControlEvent = aJni->GetMethodID(listenerClass,
       
   241                                           "postControlEvent",
       
   242                                           "(ILjava/lang/String;)V");
       
   243 
       
   244 
       
   245     // Sanity Check, something is really wrong if methods cannot be found
       
   246     __ASSERT_DEBUG(iPostEvent &&
       
   247                    iPostObjectEvent &&
       
   248                    iPostLongEvent &&
       
   249                    iPostStringEvent &&
       
   250                    iPostControlEvent,
       
   251                    User::Panic(KPanicMethod, KErrGeneral));
       
   252 
       
   253     iOOMErrorEvent = new(ELeave) CMMAPlayerEvent(iListenerObject,
       
   254             iPostStringEvent,
       
   255             CMMAEvent::EReusableEvent);
       
   256     iOOMErrorEvent->SetStringEventL(CMMAPlayerEvent::EError,
       
   257                                     KPanicOutOfMem);
       
   258 
       
   259     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::SetPlayerListenerObjectL -");
       
   260 }
       
   261 
       
   262 
       
   263 EXPORT_C TInt CMMAPlayer::ControlCount()
       
   264 {
       
   265     return iControls.Count();
       
   266 }
       
   267 
       
   268 
       
   269 EXPORT_C CMMAControl* CMMAPlayer::Control(TInt aIndex)
       
   270 {
       
   271     return iControls[ aIndex ];
       
   272 }
       
   273 
       
   274 void CMMAPlayer::RefreshControls()
       
   275 {
       
   276 
       
   277     for (TInt index = 0; index < iControls.Count(); index++)
       
   278     {
       
   279         CMMAControl* control = iControls[ index ];
       
   280         control->RefreshControl();
       
   281     }
       
   282 
       
   283 }
       
   284 
       
   285 EXPORT_C  void CMMAPlayer::AddStateListenerL(MMMAPlayerStateListener* aListener)
       
   286 {
       
   287 	LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::AddStateListenerL +");
       
   288 	TInt err = iStateListeners.Append(aListener);
       
   289 	LOG1( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::AddStateListenerL - err = %d ",err);
       
   290 		User::LeaveIfError(err);
       
   291     //User::LeaveIfError(iStateListeners.Append(aListener));
       
   292 
       
   293 }
       
   294 
       
   295 EXPORT_C void CMMAPlayer::RemoveStateListener(MMMAPlayerStateListener* aListener)
       
   296 {
       
   297 
       
   298     TInt index = iStateListeners.Find(aListener);
       
   299 
       
   300     if (index != KErrNotFound)
       
   301     {
       
   302         iStateListeners.Remove(index);
       
   303     }
       
   304 
       
   305 }
       
   306 
       
   307 const TDesC& CMMAPlayer::Type()
       
   308 {
       
   309     // by default player has not the type
       
   310     return KNullDesC;
       
   311 }
       
   312 
       
   313 void CMMAPlayer::SetContentType(HBufC* aContentType)
       
   314 {
       
   315 
       
   316     delete iContentType;
       
   317     iContentType = aContentType;
       
   318 
       
   319 }
       
   320 
       
   321 void CMMAPlayer::ResetSourceStreams()
       
   322 {
       
   323     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ResetSourceStreams +");
       
   324     int sourceStreamsCount = iSourceStreams.Count();
       
   325     for (int i = 0; i < sourceStreamsCount; i++)
       
   326     {
       
   327         iSourceStreams[ i ]->ResetData();
       
   328     }
       
   329     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ResetSourceStreams -");
       
   330 }
       
   331 
       
   332 EXPORT_C void CMMAPlayer::AddControlL(CMMAControl* aControl)
       
   333 {
       
   334 
       
   335     User::LeaveIfError(iControls.Append(aControl));
       
   336 
       
   337 }
       
   338 
       
   339 void CMMAPlayer::PostLongEvent(CMMAPlayerEvent::TEventType aEventType,
       
   340                                const TInt64& aLongEventData)
       
   341 {
       
   342     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostLongEvent ");
       
   343     if (!iListenerObject || !iEventPoster)
       
   344     {
       
   345         LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostLongEvent No listener");
       
   346         // return since player is not ready for posting any events and is not initialized
       
   347         return;
       
   348     }
       
   349 
       
   350     CMMAPlayerEvent* playerEvent = new CMMAPlayerEvent(iListenerObject,
       
   351             iPostLongEvent);
       
   352     if (!playerEvent)
       
   353     {
       
   354         // creation of player event failed, informing Java
       
   355         iEventPoster->PostEvent(iOOMErrorEvent);
       
   356         return;
       
   357     }
       
   358     playerEvent->SetLongEvent(aEventType, aLongEventData);
       
   359 
       
   360     // event poster takes ownership of event
       
   361     iEventPoster->PostEvent(playerEvent);
       
   362 }
       
   363 
       
   364 
       
   365 EXPORT_C void CMMAPlayer::PostStringEvent(CMMAPlayerEvent::TEventType aEventType,
       
   366         const TDesC& aStringEventData)
       
   367 {
       
   368     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostStringEvent ");
       
   369     if (!iListenerObject || !iEventPoster)
       
   370     {
       
   371         LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostStringEvent No listener");
       
   372         // return since player is not ready for posting any events and is not initialized
       
   373         return;
       
   374     }
       
   375 
       
   376     CMMAPlayerEvent* playerEvent = new CMMAPlayerEvent(iListenerObject,
       
   377             iPostStringEvent);
       
   378     if (!playerEvent)
       
   379     {
       
   380         // creation of player event failed, informing Java
       
   381         iEventPoster->PostEvent(iOOMErrorEvent);
       
   382         return;
       
   383     }
       
   384 
       
   385     TRAPD(err, playerEvent->SetStringEventL(aEventType, aStringEventData));
       
   386     if (err != KErrNone)
       
   387     {
       
   388         // string setting failed, informing Java
       
   389         iEventPoster->PostEvent(iOOMErrorEvent);
       
   390         return;
       
   391     }
       
   392 
       
   393     // event poster takes ownership of event
       
   394     iEventPoster->PostEvent(playerEvent);
       
   395 }
       
   396 
       
   397 
       
   398 EXPORT_C void CMMAPlayer::PostObjectEvent(CMMAPlayerEvent::TEventType aEventType,
       
   399         const jobject aEventData)
       
   400 {
       
   401     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostObjectEvent ");
       
   402 
       
   403     if (!iListenerObject || !iEventPoster)
       
   404     {
       
   405         LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostObjectEvent No listener");
       
   406         // return since player is not ready for posting any events and is not initialized
       
   407         return;
       
   408     }
       
   409 
       
   410     CMMAPlayerEvent* playerEvent = new CMMAPlayerEvent(iListenerObject,
       
   411             iPostObjectEvent);
       
   412 
       
   413     if (!playerEvent)
       
   414     {
       
   415         // creation of player event failed, informing Java
       
   416         iEventPoster->PostEvent(iOOMErrorEvent);
       
   417         return;
       
   418     }
       
   419 
       
   420     TRAPD(err, playerEvent->SetObjectEventL(aEventType, aEventData));
       
   421     if (err != KErrNone)
       
   422     {
       
   423         // creation of player event failed, informing Java
       
   424         iEventPoster->PostEvent(iOOMErrorEvent);
       
   425         return;
       
   426     }
       
   427 
       
   428     // event poster takes ownership of event
       
   429     iEventPoster->PostEvent(playerEvent);
       
   430 }
       
   431 
       
   432 EXPORT_C CMMASourceStream* CMMAPlayer::AddSourceStreamL(JNIEnv* aJNIEnv,
       
   433         MMAFunctionServer* aEventSource,
       
   434         jobject aReader)
       
   435 {
       
   436     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::AddSourceStreamL ");
       
   437     CMMASourceStream* sourceStream = CMMASourceStream::NewL(aJNIEnv,
       
   438                                      aEventSource,
       
   439                                      aReader,
       
   440                                      this);
       
   441 
       
   442     CleanupStack::PushL(sourceStream);
       
   443     User::LeaveIfError(iSourceStreams.Append(sourceStream));
       
   444     CleanupStack::Pop(); // sourceStream
       
   445     return sourceStream;
       
   446 }
       
   447 
       
   448 
       
   449 void CMMAPlayer::PostActionCompleted(TInt aError)
       
   450 {
       
   451 	LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostActionCompleted ");
       
   452     iActionCompletedEvent->SetEventData(aError);
       
   453     iEventPoster->PostEvent(iActionCompletedEvent,
       
   454                             CMMAEvent::ENotifyPriority);
       
   455 }
       
   456 
       
   457 void CMMAPlayer::PostActionCompletedFile()
       
   458 {
       
   459 	LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostActionCompleted ");
       
   460    // iActionCompletedFileEvent->SetEventData(aError);
       
   461     iEventPoster->PostEvent(iActionCompletedFileEvent,
       
   462                             CMMAEvent::ENotifyPriority);
       
   463 }
       
   464 
       
   465 
       
   466 void CMMAPlayer::PostActionCompletedStart()
       
   467 {
       
   468 	   LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::PostActionCompleted ");
       
   469     iEventPoster->PostEvent(iActionCompletedStartEvent,
       
   470                             CMMAEvent::ENotifyPriority);
       
   471 }
       
   472 
       
   473 void CMMAPlayer::ChangeState(TPlayerState aState)
       
   474 {
       
   475     iState = aState;
       
   476     LOG1( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ChangeState - iStateListeners count is %d", iStateListeners.Count());
       
   477     for (TInt i(0); i<iStateListeners.Count(); i++)
       
   478     {
       
   479         iStateListeners[ i ]->StateChanged(aState);
       
   480     }
       
   481     LOG1( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ChangeState - State changed to %d", iState);
       
   482 }
       
   483 
       
   484 void CMMAPlayer::ReadCompletedL(TInt /*aStatus*/, const TDesC8& /*aData*/)
       
   485 {
       
   486     // empty implementation
       
   487     LOG( EJavaMMAPI, EInfo, "MMA::CMMAPlayer::ReadCompletedL ");
       
   488 }
       
   489 void CMMAPlayer:: DeleteControls()
       
   490 {
       
   491     if (iControls.Count() > 0)
       
   492     {
       
   493         iControls.ResetAndDestroy();
       
   494     }
       
   495 }
       
   496 //  END OF FILE