STATUS KinesisVideoClientWrapper::createStreamFunc()

in src/JNI/com/amazonaws/kinesis/video/producer/jni/KinesisVideoClientWrapper.cpp [1800:1896]


STATUS KinesisVideoClientWrapper::createStreamFunc(UINT64 customData,
                                             PCHAR deviceName,
                                             PCHAR streamName,
                                             PCHAR contentType,
                                             PCHAR kmsKeyId,
                                             UINT64 retention,
                                             PServiceCallContext pCallbackContext)
{
    DLOGS("TID 0x%016" PRIx64 " createStreamFunc called.", GETTID());

    KinesisVideoClientWrapper *pWrapper = FROM_WRAPPER_HANDLE(customData);
    CHECK(pWrapper != NULL);

    // Get the ENV from the JavaVM
    JNIEnv *env;
    BOOL detached = FALSE;
    STATUS retStatus = STATUS_SUCCESS;
    jstring jstrDeviceName = NULL, jstrStreamName = NULL, jstrContentType = NULL, jstrKmsKeyId = NULL;
    jbyteArray authByteArray = NULL;

    INT32 envState = pWrapper->mJvm->GetEnv((PVOID*) &env, JNI_VERSION_1_6);
    if (envState == JNI_EDETACHED) {
        ATTACH_CURRENT_THREAD_TO_JVM(env);

        // Store the detached so we can detach the thread after the call
        detached = TRUE;
    }

    // Call the Java func
    jstrDeviceName = env->NewStringUTF(deviceName);
    jstrStreamName = env->NewStringUTF(streamName);
    jstrContentType = env->NewStringUTF(contentType);
    if (kmsKeyId != NULL) {
        jstrKmsKeyId = env->NewStringUTF(kmsKeyId);
    }

    authByteArray = env->NewByteArray(pCallbackContext->pAuthInfo->size);
    if (jstrContentType == NULL ||
            jstrDeviceName == NULL ||
            jstrStreamName == NULL ||
            authByteArray == NULL) {
        retStatus = STATUS_NOT_ENOUGH_MEMORY;
        goto CleanUp;
    }

    // Copy the bits into the managed array
    env->SetByteArrayRegion(authByteArray,
                            0,
                            pCallbackContext->pAuthInfo->size,
                            (const jbyte*) pCallbackContext->pAuthInfo->data);

    // Invoke the callback
    retStatus = env->CallIntMethod(pWrapper->mGlobalJniObjRef,
                                   pWrapper->mCreateStreamMethodId,
                                   jstrDeviceName,
                                   jstrStreamName,
                                   jstrContentType,
                                   jstrKmsKeyId,
                                   retention,
                                   pCallbackContext->callAfter,
                                   pCallbackContext->timeout,
                                   authByteArray,
                                   pCallbackContext->pAuthInfo->type,
                                   pCallbackContext->customData
    );

    CHK_JVM_EXCEPTION(env);

CleanUp:

    if (jstrDeviceName != NULL) {
        env->DeleteLocalRef(jstrDeviceName);
    }

    if (jstrStreamName != NULL) {
        env->DeleteLocalRef(jstrStreamName);
    }

    if (jstrContentType != NULL) {
        env->DeleteLocalRef(jstrContentType);
    }

    if (jstrKmsKeyId != NULL) {
        env->DeleteLocalRef(jstrKmsKeyId);
    }

    if (authByteArray != NULL) {
        env->DeleteLocalRef(authByteArray);
    }

    // Detach the thread if we have attached it to JVM
    if (detached) {
        pWrapper->mJvm->DetachCurrentThread();
    }

    return retStatus;
}