BOOL setDeviceInfo()

in src/JNI/com/amazonaws/kinesis/video/producer/jni/Parameters.cpp [8:155]


BOOL setDeviceInfo(JNIEnv *env, jobject deviceInfo, PDeviceInfo pDeviceInfo)
{
    STATUS retStatus = STATUS_SUCCESS;
    jmethodID methodId = NULL;
    const char *retChars;

    CHECK(env != NULL && deviceInfo != NULL && pDeviceInfo != NULL);

    // Load DeviceInfo
    jclass cls = env->GetObjectClass(deviceInfo);
    if (cls == NULL) {
        DLOGE("Failed to create DeviceInfo class.");
        CHK(FALSE, STATUS_INVALID_OPERATION);
    }

    // Retrieve the methods and call it
    methodId = env->GetMethodID(cls, "getVersion", "()I");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getVersion");
    } else {
        pDeviceInfo->version = env->CallIntMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getName");
    } else {
        jstring retString = (jstring) env->CallObjectMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);

        if (retString != NULL) {
            retChars = env->GetStringUTFChars(retString, NULL);
            STRNCPY(pDeviceInfo->name, retChars, MAX_DEVICE_NAME_LEN + 1);

            // Ensure we null terminate it
            pDeviceInfo->name[MAX_DEVICE_NAME_LEN] = '\0';
            env->ReleaseStringUTFChars(retString, retChars);
        } else {
            pDeviceInfo->name[0] = '\0';
        }
    }

    methodId = env->GetMethodID(cls, "getStreamCount", "()I");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getStreamCount");
    } else {
        pDeviceInfo->streamCount = env->CallIntMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getStorageInfoVersion", "()I");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getStorageInfoVersion");
    } else {
        pDeviceInfo->storageInfo.version = env->CallIntMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getDeviceStorageType", "()I");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getDeviceStorageType");
    } else {
        pDeviceInfo->storageInfo.storageType = (DEVICE_STORAGE_TYPE) env->CallIntMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getSpillRatio", "()I");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getSpillRatio");
    } else {
        pDeviceInfo->storageInfo.spillRatio = env->CallIntMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getStorageSize", "()J");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getStorageSize");
    } else {
        pDeviceInfo->storageInfo.storageSize = env->CallLongMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);
    }

    methodId = env->GetMethodID(cls, "getRootDirectory", "()Ljava/lang/String;");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getRootDirectory");
    } else {
        jstring retString = (jstring) env->CallObjectMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);

        if (retString != NULL) {
            retChars = env->GetStringUTFChars(retString, NULL);
            STRNCPY(pDeviceInfo->storageInfo.rootDirectory, retChars, MAX_PATH_LEN + 1);
            pDeviceInfo->storageInfo.rootDirectory[MAX_PATH_LEN] = '\0';
            env->ReleaseStringUTFChars(retString, retChars);
        } else {
            pDeviceInfo->storageInfo.rootDirectory[0] = '\0';
        }
    }

    // Set the tags to empty first
    pDeviceInfo->tagCount = 0;
    pDeviceInfo->tags = NULL;
    methodId = env->GetMethodID(cls, "getTags", "()[Lcom/amazonaws/kinesisvideo/producer/Tag;");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getTags");
    } else {
        jobjectArray array = (jobjectArray) env->CallObjectMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);

        if (!setTags(env, array, &pDeviceInfo->tags, &pDeviceInfo->tagCount)) {
            DLOGW("Failed getting/setting tags.");
        }
    }

    methodId = env->GetMethodID(cls, "getClientId", "()Ljava/lang/String;");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getClientId");
    } else {
        jstring retString = (jstring) env->CallObjectMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);

        if (retString != NULL) {
            retChars = env->GetStringUTFChars(retString, NULL);
            STRNCPY(pDeviceInfo->clientId, retChars, MAX_CLIENT_ID_STRING_LENGTH + 1);
            pDeviceInfo->clientId[MAX_CLIENT_ID_STRING_LENGTH] = '\0';
            env->ReleaseStringUTFChars(retString, retChars);
        } else {
            STRNCPY(pDeviceInfo->clientId, (PCHAR) "JNI", MAX_CLIENT_ID_STRING_LENGTH + 1);
        }
    }

    // Set the client info to empty first
    methodId = env->GetMethodID(cls, "getClientInfo", "()Lcom/amazonaws/kinesisvideo/producer/ClientInfo;");
    if (methodId == NULL) {
        DLOGW("Couldn't find method id getClientInfo");
    } else {
        jobject clientInfo = (jobject) env->CallObjectMethod(deviceInfo, methodId);
        CHK_JVM_EXCEPTION(env);

        if (!setClientInfo(env, clientInfo, &pDeviceInfo->clientInfo)) {
            DLOGW("Failed getting/setting client info.");
        }
    }

CleanUp:
    return STATUS_FAILED(retStatus) ? FALSE : TRUE;
}