Init Session in Android

This commit is contained in:
Florian Märkl 2019-09-14 18:30:03 +02:00
commit cd50874705
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 113 additions and 8 deletions

View file

@ -4,6 +4,9 @@
#include <chiaki/common.h>
#include <chiaki/log.h>
#include <chiaki/session.h>
#include <string.h>
#define LOG_TAG "Chiaki"
@ -31,7 +34,7 @@ static void log_cb_android(ChiakiLogLevel level, const char *msg, void *user)
prio = ANDROID_LOG_INFO;
break;
}
__android_log_print(prio, LOG_TAG, "%s", msg);
__android_log_write(prio, LOG_TAG, msg);
}
ChiakiLog log_ctx;
@ -45,7 +48,75 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
return JNI_VERSION_1_2;
}
JNIEXPORT jstring JNICALL Java_com_metallic_chiaki_lib_Chiaki_stringFromJNI(JNIEnv *env, jobject thiz)
#define E (*env)
JNIEXPORT jstring JNICALL Java_com_metallic_chiaki_lib_ChiakiNative_errorCodeToString(JNIEnv *env, jobject obj, jint value)
{
return (*env)->NewStringUTF(env, chiaki_error_string(CHIAKI_ERR_SUCCESS));
return E->NewStringUTF(env, chiaki_error_string((ChiakiErrorCode)value));
}
JNIEXPORT void JNICALL Java_com_metallic_chiaki_lib_ChiakiNative_createSession(JNIEnv *env, jobject obj, jobject result, jobject connect_info_obj)
{
ChiakiSession *session = NULL;
ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;
char *host_str = NULL;
jclass result_class = E->GetObjectClass(env, result);
jclass connect_info_class = E->GetObjectClass(env, connect_info_obj);
jstring host_string = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "host", "Ljava/lang/String;"));
jbyteArray regist_key_array = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "registKey", "[B"));
jbyteArray morning_array = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "morning", "[B"));
jobject connect_video_profile_obj = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "videoProfile", "Lcom/metallic/chiaki/lib/ConnectVideoProfile;"));
jclass connect_video_profile_class = E->GetObjectClass(env, connect_video_profile_obj);
ChiakiConnectInfo connect_info;
const char *str_borrow = E->GetStringUTFChars(env, host_string, NULL);
connect_info.host = host_str = strdup(str_borrow);
E->ReleaseStringUTFChars(env, host_string, str_borrow);
if(!connect_info.host)
{
err = CHIAKI_ERR_MEMORY;
goto beach;
}
if(E->GetArrayLength(env, regist_key_array) != sizeof(connect_info.regist_key))
{
CHIAKI_LOGE(&log_ctx, "Regist Key passed from Java has invalid length");
err = CHIAKI_ERR_INVALID_DATA;
goto beach;
}
jbyte *bytes = E->GetByteArrayElements(env, regist_key_array, NULL);
memcpy(connect_info.regist_key, bytes, sizeof(connect_info.regist_key));
E->ReleaseByteArrayElements(env, regist_key_array, bytes, JNI_ABORT);
if(E->GetArrayLength(env, morning_array) != sizeof(connect_info.morning))
{
CHIAKI_LOGE(&log_ctx, "Morning passed from Java has invalid length");
err = CHIAKI_ERR_INVALID_DATA;
goto beach;
}
bytes = E->GetByteArrayElements(env, morning_array, NULL);
memcpy(connect_info.morning, bytes, sizeof(connect_info.morning));
E->ReleaseByteArrayElements(env, morning_array, bytes, JNI_ABORT);
connect_info.video_profile.width = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "width", "I"));
connect_info.video_profile.height = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "height", "I"));
connect_info.video_profile.max_fps = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "maxFPS", "I"));
connect_info.video_profile.bitrate = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "bitrate", "I"));
session = CHIAKI_NEW(ChiakiSession);
if(!session)
{
err = CHIAKI_ERR_MEMORY;
goto beach;
}
err = chiaki_session_init(session, &connect_info, &log_ctx);
beach:
free(host_str);
E->SetIntField(env, result, E->GetFieldID(env, result_class, "errorCode", "I"), (jint)err);
E->SetLongField(env, result, E->GetFieldID(env, result_class, "sessionPtr", "J"), (jlong)session);
}

View file

@ -2,7 +2,10 @@ package com.metallic.chiaki
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.metallic.chiaki.lib.Chiaki
import com.metallic.chiaki.lib.ChiakiNative
import com.metallic.chiaki.lib.ConnectInfo
import com.metallic.chiaki.lib.ConnectVideoProfile
import com.metallic.chiaki.lib.ErrorCode
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
@ -12,6 +15,12 @@ class MainActivity : AppCompatActivity()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
testTextView.text = Chiaki.stringFromJNI()
val result = ChiakiNative.CreateSessionResult(0, 0)
ChiakiNative.createSession(result, ConnectInfo("host",
ByteArray(0x10) { 0 },
ByteArray(0x10) { 0 },
ConnectVideoProfile(0, 0, 0, 0)))
testTextView.text = "${ErrorCode(0).toString()}\n${result}\n${ErrorCode(result.errorCode)}"
}
}

View file

@ -1,14 +1,39 @@
package com.metallic.chiaki.lib
class Chiaki
data class ConnectVideoProfile(
val width: Int,
val height: Int,
val maxFPS: Int,
val bitrate: Int
)
data class ConnectInfo(
val host: String,
val registKey: ByteArray,
val morning: ByteArray,
val videoProfile: ConnectVideoProfile
)
class ChiakiNative
{
data class CreateSessionResult(var errorCode: Int, var sessionPtr: Long)
companion object
{
init
{
System.loadLibrary("chiaki-jni")
}
@JvmStatic external fun stringFromJNI(): String
@JvmStatic external fun errorCodeToString(value: Int): String
@JvmStatic external fun createSession(result: CreateSessionResult, connectInfo: ConnectInfo)
}
}
class ErrorCode(val value: Int)
{
override fun toString() = ChiakiNative.errorCodeToString(value)
}
class Session
{
private val nativePtr: Long = 0
}