// // Created by 倪路朋 on 4/13/25. // #include "live_view_call.h" #include "server_global.h" LiveViewCall::LiveViewCall(JNIEnv *env_, jobject instance_) { this->env = env_; this->env->GetJavaVM(&javaVM); this->instance = env->NewGlobalRef(instance_); jclass localClazz = env->FindClass("com/runt/live/cpp/LiveMiniView"); if (localClazz == nullptr || env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); return; } // 拿一个 global ref:关键!! //jobject,jclass等对象在方法执行完后就会被回收 miniClazz = (jclass)env->NewGlobalRef(localClazz); jmd_draw_text = env->GetStaticMethodID(miniClazz,"drawText","([BIII)[B"); }; LiveViewCall::~LiveViewCall() { javaVM = 0 ; env->DeleteGlobalRef(instance); env->DeleteGlobalRef(miniClazz); instance = 0 ; } uint8_t *LiveViewCall::drawText(int stream_code, uint8_t *yuvData[3],int width,int height) { //******转为rgba int size = width * height * 4; uint8_t *rgba_data = yuvToRGBA(yuvData,width,height); //转为java字节数组 jint res = javaVM->AttachCurrentThread(&env, nullptr); jbyteArray java_array = env->NewByteArray(size); env->SetByteArrayRegion(java_array, 0, size, (jbyte *)rgba_data); delete[] rgba_data; jbyteArray rgbaArray = (jbyteArray)env->CallStaticObjectMethod(miniClazz, jmd_draw_text,java_array,stream_code,width,height); env->DeleteLocalRef(java_array); // 2. 分配 native buffer uint8_t* rgba_copy = new uint8_t[size]; // 3. 拷贝数据 env->GetByteArrayRegion(rgbaArray, 0, size, reinterpret_cast(rgba_copy)); env->DeleteLocalRef(rgbaArray); javaVM->DetachCurrentThread(); return rgba_copy; }