javauis/mmapi_qt/baseline/src/nativeplayerfactory.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 has NativePlayerFactory JNI functions
       
    15 *
       
    16 */
       
    17 
       
    18 #include <logger.h>
       
    19 
       
    20 #include "com_nokia_microedition_media_NativePlayerFactory.h"
       
    21 #include "cmmamanager.h"
       
    22 #include "cmmaplayer.h"
       
    23 
       
    24 #include "s60commonutils.h"
       
    25 #include "jstringutils.h"
       
    26 using namespace java::util;
       
    27 
       
    28 /**
       
    29  * Adds player to event source. If player could not be created
       
    30  * handle KErrNone indicates in Java side that there wasn't suitable factory.
       
    31  */
       
    32 LOCAL_C void HandleCreatePlayerL(CMMAPlayer* aPlayer,
       
    33                                  MMAFunctionServer* aEventSource,
       
    34                                  TInt* aHandle)
       
    35 {
       
    36     if (aPlayer)
       
    37     {
       
    38         // Player was created, add it to event source
       
    39         CleanupStack::PushL(aPlayer);
       
    40         aEventSource->AddPlayerL(aPlayer);
       
    41         *aHandle = reinterpret_cast<TInt>(aPlayer);
       
    42         CleanupStack::Pop(aPlayer);
       
    43     }
       
    44     else
       
    45     {
       
    46         // Data was not supported and there is no error.
       
    47         // Return KErrNone to java side
       
    48         *aHandle = KErrNone;
       
    49     }
       
    50 }
       
    51 
       
    52 /**
       
    53  * Local function that calls CMMAManager's CreatePlayerL method.
       
    54  */
       
    55 LOCAL_C void CreatePlayerHeaderDataL(CMMAManager* aManager,
       
    56                                      MMAFunctionServer* aEventSource,
       
    57                                      const TDesC8* aHeaderData,
       
    58                                      TInt* aHandle)
       
    59 {
       
    60     HandleCreatePlayerL(aManager->CreatePlayerL(*aHeaderData),
       
    61                         aEventSource,
       
    62                         aHandle);
       
    63 }
       
    64 
       
    65 /**
       
    66  * JNI function from NativePlayerFactory class.
       
    67  */
       
    68 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_NativePlayerFactory__1createPlayerHeaderData
       
    69 (JNIEnv* aJni, jclass,
       
    70  jint aEventSourceHandle, jint aManagerHandle,
       
    71  jbyteArray aHeaderData)
       
    72 {
       
    73     // Development time check.
       
    74     __ASSERT_DEBUG((aEventSourceHandle > 0) && (aManagerHandle > 0), User::Invariant());
       
    75 
       
    76     MMAFunctionServer* eventSource =
       
    77         reinterpret_cast< MMAFunctionServer *>(aEventSourceHandle);
       
    78     CMMAManager* manager = reinterpret_cast< CMMAManager *>(aManagerHandle);
       
    79 
       
    80     // Returning just KErrNone if there is no header data
       
    81     TInt playerHandle = KErrNone;
       
    82     if (aHeaderData)
       
    83     {
       
    84         // Get pointer to Java header data
       
    85         jbyte* data = aJni->GetByteArrayElements(aHeaderData, NULL);
       
    86         // if data is null Java data could not be obtained to native and
       
    87         // KErrNoMemory is returned to Java
       
    88         if (!data)
       
    89         {
       
    90             return KErrNoMemory;
       
    91         }
       
    92 
       
    93         TInt headerDataLength = aJni->GetArrayLength(aHeaderData);
       
    94         TPtrC8 headerData((TUint8*)data, headerDataLength);
       
    95         TInt err = eventSource->ExecuteTrap(&CreatePlayerHeaderDataL,
       
    96                                             manager,
       
    97                                             eventSource,
       
    98                                             (const TDesC8*)&headerData,
       
    99                                             &playerHandle);
       
   100 
       
   101         // release bytes got with GetByteArrayElements
       
   102         aJni->ReleaseByteArrayElements(aHeaderData,
       
   103                                        data,
       
   104                                        0);
       
   105         if (err != KErrNone)
       
   106         {
       
   107             // Leave occured return error code to Java
       
   108             playerHandle = err;
       
   109             ELOG1( EJavaMMAPI, "MMA::NativePlayerFactory createPlayerHeaderData err %d",
       
   110                       playerHandle);
       
   111         }
       
   112     }
       
   113     return playerHandle;
       
   114 }
       
   115 
       
   116 /**
       
   117  * Local function that calls CMMAManager's CreatePlayerL method.
       
   118  */
       
   119 LOCAL_C void CreatePlayerLocatorL(CMMAManager* aManager,
       
   120                                   MMAFunctionServer* aEventSource,
       
   121                                   const TDesC* aProtocol,
       
   122                                   const TDesC* aMiddlePart,
       
   123                                   const TDesC* aParameters,
       
   124                                   TInt* aHandle)
       
   125 {
       
   126     HandleCreatePlayerL(aManager->CreatePlayerL(*aProtocol,
       
   127                         *aMiddlePart,
       
   128                         *aParameters),
       
   129                         aEventSource,
       
   130                         aHandle);
       
   131 }
       
   132 
       
   133 /**
       
   134  * JNI function from NativePlayerFactory class.
       
   135  */
       
   136 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_NativePlayerFactory__1createPlayerLocator
       
   137 (JNIEnv* aJni, jclass,
       
   138  jint aEventSourceHandle, jint aManagerHandle,
       
   139  jstring aProtocol, jstring aMiddlePart, jstring aParameters)
       
   140 {
       
   141     // Development time check.
       
   142     __ASSERT_DEBUG((aEventSourceHandle > 0) && (aManagerHandle > 0), User::Invariant());
       
   143 
       
   144     MMAFunctionServer* eventSource =
       
   145     	reinterpret_cast< MMAFunctionServer* >(aEventSourceHandle);
       
   146     CMMAManager* manager = reinterpret_cast< CMMAManager *>(aManagerHandle);
       
   147 
       
   148     // Get Java strings to native
       
   149     JStringUtils protocol(*aJni, aProtocol);
       
   150     JStringUtils middlePart(*aJni, aMiddlePart);
       
   151 
       
   152     // If Java parameters is null, empty descriptor will be given to
       
   153     // CreatePlayerL method.
       
   154     TPtrC parameters(KNullDesC);
       
   155     JStringUtils* tmp = NULL;
       
   156     if (aParameters != NULL)
       
   157     {
       
   158         tmp = new JStringUtils(*aJni, aParameters);
       
   159         if (tmp)
       
   160         {
       
   161             parameters.Set(*tmp);
       
   162         }
       
   163         else
       
   164         {
       
   165             // failed to create string
       
   166             return KErrNoMemory;
       
   167         }
       
   168     }
       
   169 
       
   170     TInt playerHandle = KErrNoMemory;
       
   171     TInt err = eventSource->ExecuteTrap(&CreatePlayerLocatorL,
       
   172                                         manager,
       
   173                                         eventSource,
       
   174                                         (const TDesC*)&protocol,
       
   175                                         (const TDesC*)&middlePart,
       
   176                                         (const TDesC*)&parameters,
       
   177                                         &playerHandle);
       
   178     delete tmp;
       
   179     if (err != KErrNone)
       
   180     {
       
   181         // Leave occured return error code to Java
       
   182         playerHandle = err;
       
   183         ELOG1( EJavaMMAPI, "MMA::NativePlayerFactory createPlayerLocator err %d",
       
   184                   playerHandle);
       
   185     }
       
   186     return playerHandle;
       
   187 }
       
   188 
       
   189 /**
       
   190  * Local function that calls CMMAManager's CreatePlayerL method.
       
   191  */
       
   192 LOCAL_C void CreatePlayerContentTypeL(CMMAManager* aManager,
       
   193                                       MMAFunctionServer* aEventSource,
       
   194                                       const TDesC* aContentType,
       
   195                                       TInt* aHandle)
       
   196 {
       
   197     HandleCreatePlayerL(aManager->CreatePlayerL(*aContentType),
       
   198                         aEventSource,
       
   199                         aHandle);
       
   200 }
       
   201 
       
   202 /**
       
   203  * JNI function from NativePlayerFactory class.
       
   204  */
       
   205 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_NativePlayerFactory__1createPlayerContentType
       
   206 (JNIEnv* aJni, jclass,
       
   207  jint aEventSourceHandle, jint aManagerHandle,
       
   208  jstring aContentType)
       
   209 {
       
   210     // Development time check.
       
   211     __ASSERT_DEBUG((aEventSourceHandle > 0) && (aManagerHandle > 0), User::Invariant());
       
   212 
       
   213     MMAFunctionServer* eventSource =
       
   214         reinterpret_cast< MMAFunctionServer* >(aEventSourceHandle);
       
   215     CMMAManager* manager = reinterpret_cast< CMMAManager* >(aManagerHandle);
       
   216 
       
   217     JStringUtils contentType(*aJni, aContentType);
       
   218     TInt playerHandle = KErrNoMemory;
       
   219     TInt err = eventSource->ExecuteTrap(&CreatePlayerContentTypeL,
       
   220                                         manager,
       
   221                                         eventSource,
       
   222                                         (const TDesC*)&contentType,
       
   223                                         &playerHandle);
       
   224     if (err != KErrNone)
       
   225     {
       
   226         // Leave occured return error code to Java
       
   227         playerHandle = err;
       
   228         ELOG1( EJavaMMAPI, "MMA::NativePlayerFactory createPlayerContentType err %d",
       
   229                   playerHandle);
       
   230     }
       
   231     return playerHandle;
       
   232 }
       
   233 
       
   234 //  END OF FILE