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