javauis/mmapi_akn/baseline/src/player.cpp
branchRCL_3
changeset 26 2455ef1f5bbc
equal deleted inserted replaced
25:ae942d28ec0e 26: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 has JNI wrappers for CMMAPlayer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <jutils.h>
       
    20 #include "com_nokia_microedition_media_PlayerImpl.h"
       
    21 
       
    22 #include "cmmaeventsource.h"
       
    23 #include "cmmaplayer.h"
       
    24 #include <jdebug.h>
       
    25 
       
    26 // If eventsource is already disposed, then do nothing
       
    27 #define CHECK_HANDLE(x, j) { if ( !( x ) || ( ( x )->Players().Count() == 0 ) ) { return ( j ); } }
       
    28 /*
       
    29  *  Wrappers for calling non static methods
       
    30  */
       
    31 
       
    32 TInt VoidFunc(TInt aPlayer, CMMAEventSource* aEventSource,
       
    33               TInt(CMMAPlayer::*aFunc)())
       
    34 {
       
    35     CMMAPlayer* p = aEventSource->FindPlayer(aPlayer);
       
    36     if (p)
       
    37     {
       
    38         return (p->*aFunc)();
       
    39     }
       
    40     else
       
    41     {
       
    42         return KErrBadHandle;
       
    43     }
       
    44 }
       
    45 
       
    46 void BoolFuncL(TInt aPlayer, CMMAEventSource* aEventSource,
       
    47                void(CMMAPlayer::*aFunc)(TBool),
       
    48                TBool aValue)
       
    49 {
       
    50     CMMAPlayer* p = aEventSource->FindPlayer(aPlayer);
       
    51     if (p)
       
    52     {
       
    53         (p->*aFunc)(aValue);
       
    54     }
       
    55     // ignore call if player is not found
       
    56 }
       
    57 
       
    58 void VVoidFuncL(TInt aPlayer, CMMAEventSource* aEventSource, void(CMMAPlayer::*aFunc)())
       
    59 {
       
    60     CMMAPlayer* p = aEventSource->FindPlayer(aPlayer);
       
    61     if (p)
       
    62     {
       
    63         (p->*aFunc)();
       
    64     }
       
    65     // ignore call if player is not found
       
    66 }
       
    67 
       
    68 void IntFunc(TInt aPlayer,
       
    69              CMMAEventSource* aEventSource,
       
    70              void(CMMAPlayer::*aFunc)(TInt),
       
    71              TInt aData)
       
    72 {
       
    73     CMMAPlayer* p = aEventSource->FindPlayer(aPlayer);
       
    74     if (p)
       
    75     {
       
    76         (p->*aFunc)(aData);
       
    77     }
       
    78     // ignore call if player is not found
       
    79 }
       
    80 
       
    81 void TInt64Func(TInt aPlayer, CMMAEventSource* aEventSource,
       
    82                 void(CMMAPlayer::*aFunc)(TInt64* aData),
       
    83                 TInt64* aData)
       
    84 {
       
    85     CMMAPlayer* p = aEventSource->FindPlayer(aPlayer);
       
    86     if (p)
       
    87     {
       
    88         (p->*aFunc)(aData);
       
    89     }
       
    90     else
       
    91     {
       
    92         *aData = KErrBadHandle;
       
    93     }
       
    94 }
       
    95 
       
    96 
       
    97 
       
    98 /*
       
    99  * Class:     com_nokia_microedition_media_PlayerImpl
       
   100  * Method:    _createPlayer
       
   101  * Signature: (III)I
       
   102  */
       
   103 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1initPlayer
       
   104 (JNIEnv* aJni,
       
   105  jobject aObject,
       
   106  jobject aListenerObject,
       
   107  jint aEventSourceHandle,
       
   108  jint aPlayerHandle)
       
   109 {
       
   110     CMMAEventSource* eventSource =
       
   111         JavaUnhand< CMMAEventSource >(aEventSourceHandle);
       
   112 
       
   113     CHECK_HANDLE(eventSource, KErrNone);
       
   114 
       
   115     CMMAPlayer* player = JavaUnhand< CMMAPlayer >(aPlayerHandle);
       
   116 
       
   117     // init player
       
   118     jobject playerObject = aJni->NewWeakGlobalRef(aObject);
       
   119     TInt err = eventSource->ExecuteTrap(&CMMAPlayer::StaticInitPlayerL,
       
   120                                         player,
       
   121                                         playerObject,
       
   122                                         aJni);
       
   123     if (err != KErrNone)
       
   124     {
       
   125         aJni->DeleteWeakGlobalRef((jweak)playerObject);
       
   126         return err;
       
   127     }
       
   128 
       
   129     // set player listener
       
   130     jobject playerListener = aJni->NewWeakGlobalRef(aListenerObject);
       
   131     err = eventSource->ExecuteTrap(&CMMAPlayer::StaticSetPlayerListenerObjectL,
       
   132                                    player,
       
   133                                    playerListener,
       
   134                                    aJni,
       
   135                                    (MMMAEventPoster*)eventSource);
       
   136     if (err != KErrNone)
       
   137     {
       
   138         aJni->DeleteWeakGlobalRef((jweak)playerListener);
       
   139     }
       
   140     return err;
       
   141 }
       
   142 
       
   143 /*
       
   144  * Class:     com_nokia_microedition_media_PlayerImpl
       
   145  * Method:    _start
       
   146  * Signature: (II)I
       
   147  */
       
   148 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1start
       
   149 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   150 {
       
   151     CMMAEventSource* eventSource =
       
   152         JavaUnhand< CMMAEventSource >(aEventSource);
       
   153 
       
   154     CHECK_HANDLE(eventSource, KErrNone);
       
   155 
       
   156 
       
   157 
       
   158     TInt err = eventSource->ExecuteTrap(&BoolFuncL,
       
   159                                         aPlayer,
       
   160                                         eventSource,
       
   161                                         &CMMAPlayer::StartL,
       
   162                                         (TBool)ETrue);
       
   163     // complete java side request in case of leave.
       
   164     if (err != KErrNone)
       
   165     {
       
   166         CMMAPlayer* p = eventSource->FindPlayer(aPlayer);
       
   167         p->PostActionCompleted(err);   // java start return
       
   168     }
       
   169 
       
   170     return err;
       
   171 }
       
   172 
       
   173 /*
       
   174  * Class:     com_nokia_microedition_media_PlayerImpl
       
   175  * Method:    _stop
       
   176  * Signature: (II)I
       
   177  */
       
   178 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1stop
       
   179 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   180 {
       
   181     CMMAEventSource* eventSource =
       
   182         JavaUnhand< CMMAEventSource >(aEventSource);
       
   183 
       
   184     CHECK_HANDLE(eventSource, KErrNone);
       
   185 
       
   186     TInt err = eventSource->ExecuteTrap(&BoolFuncL,
       
   187                                         aPlayer,
       
   188                                         eventSource,
       
   189                                         &CMMAPlayer::StopL,
       
   190                                         (TBool)ETrue);
       
   191     return err;
       
   192 }
       
   193 
       
   194 /*
       
   195  * Class:     com_nokia_microedition_media_PlayerImpl
       
   196  * Method:    _close
       
   197  * Signature: (II)I
       
   198  */
       
   199 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1close
       
   200 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   201 {
       
   202     // if eventsource is already disposed, then do nothing
       
   203 
       
   204     CMMAEventSource* eventSource =
       
   205         JavaUnhand< CMMAEventSource >(aEventSource);
       
   206 
       
   207     CHECK_HANDLE(eventSource, KErrNone);
       
   208 
       
   209     TInt err = eventSource->ExecuteTrap(&VVoidFuncL,
       
   210                                         aPlayer,
       
   211                                         eventSource,
       
   212                                         &CMMAPlayer::CloseL);
       
   213     return err;
       
   214 }
       
   215 
       
   216 /*
       
   217  * Class:     com_nokia_microedition_media_PlayerImpl
       
   218  * Method:    _prefetch
       
   219  */
       
   220 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1prefetch
       
   221 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   222 {
       
   223     CMMAEventSource* eventSource =
       
   224         JavaUnhand< CMMAEventSource >(aEventSource);
       
   225 
       
   226     CHECK_HANDLE(eventSource, KErrNone);
       
   227 
       
   228     TInt err = eventSource->ExecuteTrap(&VVoidFuncL, aPlayer,
       
   229                                         eventSource,
       
   230                                         &CMMAPlayer::PrefetchL);
       
   231     return err;
       
   232 }
       
   233 
       
   234 
       
   235 /*
       
   236  * Class:     com_nokia_microedition_media_PlayerImpl
       
   237  * Method:    _realize
       
   238  * Signature: (II)I
       
   239  */
       
   240 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1realize
       
   241 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   242 {
       
   243     CMMAEventSource* eventSource =
       
   244         JavaUnhand< CMMAEventSource >(aEventSource);
       
   245 
       
   246     CHECK_HANDLE(eventSource, KErrNone);
       
   247 
       
   248     TInt err = eventSource->ExecuteTrap(&VVoidFuncL,
       
   249                                         aPlayer, eventSource,
       
   250                                         &CMMAPlayer::RealizeL);
       
   251     return err;
       
   252 }
       
   253 
       
   254 
       
   255 /*
       
   256  * Class:     com_nokia_microedition_media_PlayerImpl
       
   257  * Method:    _setLoopCount
       
   258  * Signature: (III)I
       
   259  */
       
   260 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1setLoopCount
       
   261 (JNIEnv *, jclass, jint aEventSource, jint aPlayer, jint aLoopCount)
       
   262 {
       
   263     CMMAEventSource* eventSource =
       
   264         JavaUnhand< CMMAEventSource >(aEventSource);
       
   265 
       
   266     CHECK_HANDLE(eventSource, KErrNone);
       
   267 
       
   268     eventSource->ExecuteV(&IntFunc,
       
   269                           aPlayer, eventSource,
       
   270                           &CMMAPlayer::SetLoopCount,
       
   271                           aLoopCount);
       
   272     return KErrNone;
       
   273 }
       
   274 
       
   275 /*
       
   276  * Class:     com_nokia_microedition_media_PlayerImpl
       
   277  * Method:    _duration
       
   278  * Signature: (II)J
       
   279  */
       
   280 JNIEXPORT jlong JNICALL Java_com_nokia_microedition_media_PlayerImpl__1duration
       
   281 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   282 {
       
   283     CMMAEventSource* eventSource =
       
   284         JavaUnhand< CMMAEventSource >(aEventSource);
       
   285 
       
   286     CHECK_HANDLE(eventSource, KErrNone);
       
   287 
       
   288     TInt64 duration(0);
       
   289 
       
   290     TInt err = eventSource->ExecuteTrap(&TInt64Func,
       
   291                                         aPlayer, eventSource,
       
   292                                         &CMMAPlayer::GetDuration,
       
   293                                         &duration);
       
   294     if (err != KErrNone)
       
   295     {
       
   296         return err;
       
   297     }
       
   298     return *reinterpret_cast< jlong* >(&duration);
       
   299 }
       
   300 
       
   301 /*
       
   302  * Class:     com_nokia_microedition_media_PlayerImpl
       
   303  * Method:    _setMediaTime
       
   304  * Signature: (IIJ)J
       
   305  */
       
   306 JNIEXPORT jlong JNICALL Java_com_nokia_microedition_media_PlayerImpl__1setMediaTime
       
   307 (JNIEnv *, jclass, jint aEventSource, jint aPlayer, jlong aNow)
       
   308 {
       
   309     CMMAEventSource* eventSource =
       
   310         JavaUnhand< CMMAEventSource >(aEventSource);
       
   311 
       
   312     CHECK_HANDLE(eventSource, KErrNone);
       
   313 
       
   314 
       
   315     TInt64 time = *reinterpret_cast< TInt64* >(&aNow);
       
   316 
       
   317     TInt err = eventSource->ExecuteTrap(&TInt64Func,
       
   318                                         aPlayer, eventSource,
       
   319                                         &CMMAPlayer::SetMediaTimeL,
       
   320                                         &time);
       
   321 
       
   322     if (err != KErrNone)
       
   323     {
       
   324         DEBUG_INT("MMA::Java_com_nokia_microedition_media_PlayerImpl__1setMediaTime error %d ",
       
   325                   err);
       
   326         return err;
       
   327     }
       
   328     return *reinterpret_cast< jlong* >(&time);
       
   329 }
       
   330 
       
   331 /*
       
   332  * Class:     com_nokia_microedition_media_PlayerImpl
       
   333  * Method:    _deallocate
       
   334  * Signature: (II)I
       
   335  */
       
   336 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1deallocate
       
   337 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   338 {
       
   339     CMMAEventSource* eventSource =
       
   340         JavaUnhand< CMMAEventSource >(aEventSource);
       
   341 
       
   342     CHECK_HANDLE(eventSource, KErrNone);
       
   343 
       
   344     TInt err = eventSource->ExecuteTrap(&VVoidFuncL,
       
   345                                         aPlayer, eventSource,
       
   346                                         &CMMAPlayer::DeallocateL);
       
   347     return err;
       
   348 }
       
   349 
       
   350 /*
       
   351  * Class:     com_nokia_microedition_media_PlayerImpl
       
   352  * Method:    _getMediaTime
       
   353  * Signature: (II)J
       
   354  */
       
   355 JNIEXPORT jlong JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getMediaTime
       
   356 (JNIEnv*, jclass, jint aEventSource, jint aPlayer)
       
   357 {
       
   358     CMMAEventSource* eventSource =
       
   359         JavaUnhand< CMMAEventSource >(aEventSource);
       
   360 
       
   361     CHECK_HANDLE(eventSource, KErrNone);
       
   362 
       
   363     TInt64 mediaTime(0);
       
   364 
       
   365     TInt err = eventSource->ExecuteTrap(&TInt64Func, aPlayer,
       
   366                                         eventSource,
       
   367                                         &CMMAPlayer::GetMediaTime,
       
   368                                         &mediaTime);
       
   369     if (err != KErrNone)
       
   370     {
       
   371         return err;
       
   372     }
       
   373     return *reinterpret_cast< jlong* >(&mediaTime);
       
   374 }
       
   375 
       
   376 /*
       
   377  * Class:     com_nokia_microedition_media_PlayerImpl
       
   378  * Method:    _getState
       
   379  * Signature: (II)I
       
   380  */
       
   381 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getState
       
   382 (JNIEnv *, jclass, jint aEventSource, jint aPlayer)
       
   383 {
       
   384     CMMAEventSource* eventSource =
       
   385         JavaUnhand< CMMAEventSource >(aEventSource);
       
   386 
       
   387     CHECK_HANDLE(eventSource, KErrNone);
       
   388 
       
   389     CMMAPlayer* player = eventSource->FindPlayer(aPlayer);
       
   390 
       
   391     TInt state = CMMAPlayer::EClosed;
       
   392     if (player != NULL)
       
   393     {
       
   394         state = player->State();
       
   395     }
       
   396 
       
   397     return state;
       
   398 }
       
   399 
       
   400 /*
       
   401  *
       
   402  */
       
   403 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1addSourceStream
       
   404 (JNIEnv* aJni, jclass, jint aEventSource, jint aPlayer, jobject aReader)
       
   405 {
       
   406     CMMAEventSource* eventSource =
       
   407         JavaUnhand< CMMAEventSource >(aEventSource);
       
   408 
       
   409     CHECK_HANDLE(eventSource, KErrNone);
       
   410 
       
   411     CMMAPlayer* player = eventSource->FindPlayer(aPlayer);
       
   412     CMMASourceStream* sourceStream;
       
   413 
       
   414     jobject readerRef = aJni->NewWeakGlobalRef(aReader);
       
   415 
       
   416     TInt err = KErrNotFound;
       
   417     if (player != NULL)
       
   418     {
       
   419         err = eventSource->ExecuteTrap(&CMMAPlayer::StaticAddSourceStreamL,
       
   420                                        aJni,
       
   421                                        player,
       
   422                                        eventSource,
       
   423                                        readerRef,
       
   424                                        &sourceStream);
       
   425     }
       
   426     if (err != KErrNone)
       
   427     {
       
   428         aJni->DeleteWeakGlobalRef((jweak)readerRef);
       
   429         return err;
       
   430     }
       
   431     return JavaMakeHandle(sourceStream);
       
   432 }
       
   433 
       
   434 
       
   435 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getControlsCount
       
   436 (JNIEnv*, jclass, jint aEventSourceHandle, jint aPlayer)
       
   437 {
       
   438     CMMAEventSource* eventSource =
       
   439         JavaUnhand< CMMAEventSource >(aEventSourceHandle);
       
   440 
       
   441     CHECK_HANDLE(eventSource, KErrNone);
       
   442 
       
   443 
       
   444     TInt count = eventSource->Execute(&VoidFunc,
       
   445                                       aPlayer, eventSource,
       
   446                                       &CMMAPlayer::ControlCount);
       
   447     return count;
       
   448 }
       
   449 
       
   450 
       
   451 JNIEXPORT jstring JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getControlClassName
       
   452 (JNIEnv* aJniEnv, jclass, jint aEventSourceHandle, jint aControlHandle)
       
   453 {
       
   454     CMMAEventSource* eventSource =
       
   455         JavaUnhand< CMMAEventSource >(aEventSourceHandle);
       
   456 
       
   457     CMMAControl* control = JavaUnhand< CMMAControl >(aControlHandle);
       
   458 
       
   459     const TDesC* name = eventSource->Execute(CMMAControl::ClassNameJni,
       
   460                         control);
       
   461 
       
   462     return CreateJavaString(*aJniEnv, *name);
       
   463 }
       
   464 
       
   465 
       
   466 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getControlHandle
       
   467 (JNIEnv*, jclass, jint aEventSourceHandle, jint aPlayer, jint aIndex)
       
   468 {
       
   469     CMMAEventSource* eventSource =
       
   470         JavaUnhand< CMMAEventSource >(aEventSourceHandle);
       
   471 
       
   472     CHECK_HANDLE(eventSource, KErrNone);
       
   473 
       
   474     CMMAPlayer* player = eventSource->FindPlayer(aPlayer);
       
   475     CMMAControl* control = NULL;
       
   476     if (player)
       
   477     {
       
   478         control = eventSource->Execute(CMMAPlayer::StaticControl,
       
   479                                        player,
       
   480                                        aIndex);
       
   481     }
       
   482     else
       
   483     {
       
   484         return KErrBadHandle;
       
   485     }
       
   486 
       
   487     return JavaMakeHandle(control);
       
   488 }
       
   489 
       
   490 LOCAL_C void DisposePlayer(CMMAEventSource* aEventSource,
       
   491                            TInt aPlayer)
       
   492 {
       
   493     CMMAPlayer* player = aEventSource->FindPlayer(aPlayer);
       
   494     if (player)
       
   495     {
       
   496         aEventSource->DisposePlayer(player);
       
   497     }
       
   498     // else already disposed
       
   499 }
       
   500 
       
   501 JNIEXPORT void JNICALL Java_com_nokia_microedition_media_PlayerImpl__1dispose
       
   502 (JNIEnv*, jclass, jint aEventSourceHandle, jint aPlayer)
       
   503 {
       
   504     CMMAEventSource* eventSource =
       
   505         JavaUnhand< CMMAEventSource >(aEventSourceHandle);
       
   506 
       
   507     // if eventsource is already disposed, then do nothing
       
   508     if (!eventSource || eventSource->IsDisposed())
       
   509     {
       
   510         return;
       
   511     }
       
   512 
       
   513     eventSource->ExecuteV(&DisposePlayer,
       
   514                           eventSource,
       
   515                           aPlayer);
       
   516 }
       
   517 
       
   518 JNIEXPORT jstring JNICALL Java_com_nokia_microedition_media_PlayerImpl__1getContentType
       
   519 (JNIEnv* aJni, jclass,
       
   520  jint aPlayerHandle)
       
   521 {
       
   522     CMMAPlayer* player = JavaUnhand< CMMAPlayer >(aPlayerHandle);
       
   523     jstring contentType = NULL;
       
   524 
       
   525     // if content type is null, we just return NULL to Java
       
   526     if (player->ContentType())
       
   527     {
       
   528         // need to create Java String object
       
   529         contentType = CreateJavaString(*aJni, *player->ContentType());
       
   530     }
       
   531     return contentType;
       
   532 }
       
   533 
       
   534 //  END OF FILE