Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix un-initialized JNI ClientInfo members #1161

Merged
merged 18 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ jobs:
Move-Item -Path "D:\a\amazon-kinesis-video-streams-producer-sdk-cpp\amazon-kinesis-video-streams-producer-sdk-cpp\*" -Destination "C:\amazon-kinesis-video-streams-producer-sdk-cpp"
- name: Install dependencies
run: |
choco install pkgconfiglite
choco install nasm strawberryperl
choco install gstreamer --version=1.22.8
choco install gstreamer-devel --version=1.22.8
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ open-source/
outputs
tags
dependency

.vs
.vscode/
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,9 @@ BOOL KinesisVideoClientWrapper::setCallbacks(JNIEnv* env, jobject thiz)
mClientCallbacks.clientShutdownFn = NULL;
mClientCallbacks.streamShutdownFn = NULL;

// TODO: Currently we set the shutdown callbacks to NULL.
// We need to expose these in the near future

// Extract the method IDs for the callbacks and set a global reference
jclass thizCls = env->GetObjectClass(thiz);
if (thizCls == NULL) {
Expand Down
196 changes: 191 additions & 5 deletions src/JNI/com/amazonaws/kinesis/video/producer/jni/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ BOOL setClientInfo(JNIEnv *env, jobject clientInfo, PClientInfo pClientInfo) {
STATUS retStatus = STATUS_SUCCESS;
jmethodID methodId = NULL;
const char *retChars;
jobject kvsRetryStrategyCallbacks = NULL;
jobject kvsRetryStrategy = NULL;

CHECK(env != NULL && clientInfo != NULL && pClientInfo != NULL);

Expand All @@ -168,7 +170,7 @@ BOOL setClientInfo(JNIEnv *env, jobject clientInfo, PClientInfo pClientInfo) {
CHK(FALSE, STATUS_INVALID_OPERATION);
}

// Retrieve the methods and call it
// Retrieve the methods and call them
methodId = env->GetMethodID(cls, "getVersion", "()I");
if (methodId == NULL) {
DLOGW("Couldn't find method id getVersion");
Expand Down Expand Up @@ -201,17 +203,17 @@ BOOL setClientInfo(JNIEnv *env, jobject clientInfo, PClientInfo pClientInfo) {
CHK_JVM_EXCEPTION(env);
}

methodId = env->GetMethodID(cls, "getServiceConnectionTimeout", "()J");
methodId = env->GetMethodID(cls, "getServiceCallConnectionTimeout", "()J");
if (methodId == NULL) {
DLOGW("Couldn't find method id getServiceConnectionTimeout");
DLOGW("Couldn't find method id getServiceCallConnectionTimeout");
} else {
pClientInfo->serviceCallConnectionTimeout = env->CallLongMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
}

methodId = env->GetMethodID(cls, "getServiceCompletionTimeout", "()J");
methodId = env->GetMethodID(cls, "getServiceCallCompletionTimeout", "()J");
if (methodId == NULL) {
DLOGW("Couldn't find method id getServiceCompletionTimeout");
DLOGW("Couldn't find method id getServiceCallCompletionTimeout");
} else {
pClientInfo->serviceCallCompletionTimeout = env->CallLongMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
Expand Down Expand Up @@ -249,10 +251,194 @@ BOOL setClientInfo(JNIEnv *env, jobject clientInfo, PClientInfo pClientInfo) {
CHK_JVM_EXCEPTION(env);
}

methodId = env->GetMethodID(cls, "getMetricLoggingPeriod", "()J");
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
if (methodId == NULL) {
DLOGW("Couldn't find method id getMetricLoggingPeriod");
pClientInfo->metricLoggingPeriod = 0;
} else {
pClientInfo->metricLoggingPeriod = env->CallLongMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
}

methodId = env->GetMethodID(cls, "getReservedCallbackPeriod", "()J");
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
if (methodId == NULL) {
DLOGW("Couldn't find method id getReservedCallbackPeriod");
pClientInfo->reservedCallbackPeriod = 0;
} else {
pClientInfo->reservedCallbackPeriod = env->CallLongMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
}

methodId = env->GetMethodID(cls, "getKvsRetryStrategy", "()Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategy;");
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
if (methodId == NULL) {
DLOGW("Couldn't find method id getKvsRetryStrategy");
} else {
kvsRetryStrategy = (jobject) env->CallObjectMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
}
setKvsRetryStrategy(env, kvsRetryStrategy, &pClientInfo->kvsRetryStrategy);
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved

methodId = env->GetMethodID(cls, "getKvsRetryStrategyCallbacks", "()Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategyCallbacks;");
if (methodId == NULL) {
DLOGW("Couldn't find method id getKvsRetryStrategyCallbacks");
} else {
kvsRetryStrategyCallbacks = (jobject) env->CallObjectMethod(clientInfo, methodId);
CHK_JVM_EXCEPTION(env);
}
setKvsRetryStrategyCallbacks(env, kvsRetryStrategyCallbacks, &pClientInfo->kvsRetryStrategyCallbacks);
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved

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

VOID setKvsRetryStrategy(JNIEnv *env, jobject kvsRetryStrategy, PKvsRetryStrategy pKvsRetryStrategy)
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
{
jmethodID methodId = NULL;
jclass cls = NULL;
BOOL nullKvsRetryStrategy = FALSE;

CHECK(env != NULL && pKvsRetryStrategy != NULL);

if (kvsRetryStrategy == NULL) {
nullKvsRetryStrategy = TRUE;
goto CleanUp;
}

cls = env->GetObjectClass(kvsRetryStrategy);
if (cls == NULL) {
DLOGW("Failed to create Java kvsRetryStrategy class.");
nullKvsRetryStrategy = TRUE;
goto CleanUp;
}

methodId = env->GetMethodID(cls, "getRetryStrategy", "()Lcom/amazonaws/kinesisvideo/producer/RetryStrategy");
if (methodId == NULL) {
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
DLOGW("Couldn't find method id getRetryStrategy, setting pRetryStrategy to null.");
pKvsRetryStrategy->pRetryStrategy = NULL;
} else {
if (env->CallLongMethod(kvsRetryStrategy, methodId) == 0) {
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
pKvsRetryStrategy->pRetryStrategy = NULL;
} else {
// TODO: Implement once we have support for this in Java, setting to null for safety.
pKvsRetryStrategy->pRetryStrategy = NULL;
}
}

methodId = env->GetMethodID(cls, "getRetryStrategyConfig", "()Lcom/amazonaws/kinesisvideo/producer/RetryStrategyConfig");
if (methodId == NULL) {
DLOGW("Couldn't find method id getRetryStrategyConfig, setting pRetryStrategyConfig to null.");
pKvsRetryStrategy->pRetryStrategyConfig = NULL;
} else {
if (env->CallLongMethod(kvsRetryStrategy, methodId) == 0) {
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
pKvsRetryStrategy->pRetryStrategyConfig = NULL;
} else {
// TODO: Implement once we have support for this in Java, setting to null for safety.
pKvsRetryStrategy->pRetryStrategyConfig = NULL;
}
}

methodId = env->GetMethodID(cls, "getRetryStrategyType", "()Lcom/amazonaws/kinesisvideo/producer/RetryStrategyType");
if (methodId == NULL) {
DLOGW("Couldn't find method id getRetryStrategyType, setting retryStrategyType to EXPONENTIAL_BACKOFF_WAIT.");
pKvsRetryStrategy->retryStrategyType = KVS_RETRY_STRATEGY_EXPONENTIAL_BACKOFF_WAIT;
} else {
pKvsRetryStrategy->retryStrategyType = (KVS_RETRY_STRATEGY_TYPE) env->CallIntMethod(kvsRetryStrategy, methodId);
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
}


CleanUp:
if (nullKvsRetryStrategy) {
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
DLOGI("Setting kvsRetryStrategy members to NULL/default values.");
pKvsRetryStrategy->pRetryStrategy = NULL;
pKvsRetryStrategy->pRetryStrategyConfig = NULL;
pKvsRetryStrategy->retryStrategyType = KVS_RETRY_STRATEGY_EXPONENTIAL_BACKOFF_WAIT;
}
}

VOID setKvsRetryStrategyCallbacks(JNIEnv *env, jobject kvsRetryStrategyCallbacks, PKvsRetryStrategyCallbacks pKvsRetryStrategyCallbacks)
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved
{
jmethodID methodId = NULL;
jclass cls = NULL;
BOOL nullSetCallbacks = FALSE;
stefankiesz marked this conversation as resolved.
Show resolved Hide resolved

CHECK(env != NULL && pKvsRetryStrategyCallbacks != NULL);

if (kvsRetryStrategyCallbacks == NULL) {
nullSetCallbacks = TRUE;
goto CleanUp;
}

cls = env->GetObjectClass(kvsRetryStrategyCallbacks);
if (cls == NULL) {
DLOGW("Failed to create Java kvsRetryStrategyCallbacks class.");
nullSetCallbacks = TRUE;
goto CleanUp;
}

/* *** */
/* NOTE: If ANY of the kvsRetryStrategyCallbacks are NULL, then they will ALL be set to defaults in PIC. */
/* So, below, if any are NULL, we will set them all to NULL in cleanup */
/* *** */

methodId = env->GetMethodID(cls, "createRetryStrategyFn", "(Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategyCallbacks;)V");
if (methodId == NULL) {
DLOGW("Couldn't find method id createRetryStrategyFn, setting to NULL");
nullSetCallbacks = TRUE;
goto CleanUp;
} else {
// Use Java-provided callback once we implement Java callback invocation and support setting KvsRetryStrategyCallbacks
// from the Producer Java SDK.
nullSetCallbacks = TRUE;
goto CleanUp;
}

methodId = env->GetMethodID(cls, "getCurrentRetryAttemptNumberFn", "(Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategyCallbacks;I)V");
if (methodId == NULL) {
DLOGW("Couldn't find method id getCurrentRetryAttemptNumberFn, setting to NULL");
nullSetCallbacks = TRUE;
goto CleanUp;
} else {
// Use Java-provided callback once we implement Java callback invocation and support setting KvsRetryStrategyCallbacks
// from the Producer Java SDK.
nullSetCallbacks = TRUE;
goto CleanUp;
}

methodId = env->GetMethodID(cls, "freeRetryStrategyFn", "(Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategyCallbacks;)V");
if (methodId == NULL) {
DLOGW("Couldn't find method id freeRetryStrategyFn, setting to NULL");
nullSetCallbacks = TRUE;
goto CleanUp;
} else {
// Use Java-provided callback once we implement Java callback invocation and support setting KvsRetryStrategyCallbacks
// from the Producer Java SDK.
nullSetCallbacks = TRUE;
goto CleanUp;
}

methodId = env->GetMethodID(cls, "executeRetryStrategyFn", "(Lcom/amazonaws/kinesisvideo/producer/KvsRetryStrategyCallbacks;J)V");
if (methodId == NULL) {
DLOGW("Couldn't find method id executeRetryStrategyFn, setting to NULL");
nullSetCallbacks = TRUE;
goto CleanUp;
} else {
// Use Java-provided callback once we implement Java callback invocation and support setting KvsRetryStrategyCallbacks
// from the Producer Java SDK.
nullSetCallbacks = TRUE;
goto CleanUp;
}

CleanUp:

// NOTE: If ANY RetryStrategyCallbacks are NULL, then ALL of them will be set to default callbacks in PIC.
if (nullSetCallbacks) {
DLOGI("Setting kvsRetryStrategyCallbacks functions to NULL to be set to defaults callbacks in PIC");
pKvsRetryStrategyCallbacks->createRetryStrategyFn = NULL;
pKvsRetryStrategyCallbacks->getCurrentRetryAttemptNumberFn = NULL;
pKvsRetryStrategyCallbacks->freeRetryStrategyFn = NULL;
pKvsRetryStrategyCallbacks->executeRetryStrategyFn = NULL;
}
}

BOOL setTags(JNIEnv *env, jobjectArray tagArray, PTag* ppTags, PUINT32 pTagCount)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ BOOL setStreamDataBuffer(JNIEnv* env, jobject dataBuffer, UINT32 offset, PBYTE*
BOOL releaseStreamDataBuffer(JNIEnv* env, jobject dataBuffer, UINT32 offset, PBYTE pBuffer);
BOOL setTags(JNIEnv *env, jobjectArray tagArray, PTag* ppTags, PUINT32 pTagCount);
VOID releaseTags(PTag tags);
VOID setKvsRetryStrategyCallbacks(JNIEnv *env, jobject kvsRetryStrategyCallbacks, PKvsRetryStrategyCallbacks pKvsRetryStrategyCallbacks);
VOID setKvsRetryStrategy(JNIEnv *env, jobject kvsRetryStrategyCallbacks, PKvsRetryStrategy pKvsRetryStrategy);

#endif // __KINESIS_VIDEO_PARAMETERS_CONVERSION_H__
Loading