From e473f9eeb0233afcbd38de5de975dea4d131026d Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期三, 16 八月 2017 15:26:27 +0800
Subject: [PATCH] fix Remote client bug

---
 VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h                 |   15 +++-
 VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncClient.cpp         |    3 
 VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperServer.cpp      |    4 
 VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.cpp      |   75 ++++++++++++++++--------
 VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.h        |    7 +
 VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncServer.cpp         |    1 
 VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNIClient.cpp |   29 ++++++---
 7 files changed, 88 insertions(+), 46 deletions(-)

diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.cpp
index 32b79f4..6d52346 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.cpp
@@ -48,7 +48,13 @@
         running = false;
         return false;
     }
-
+    ret = pthread_create(&jni_call_thid, NULL, CameraWrapper::jni_call_thd, this);
+    if(ret != 0)
+    {
+        LOGP(ERROR, "pthread_create jni_call_thid: %s/n", strerror(ret));
+        running = false;
+        return false;
+    }
     return true;
 }
 
@@ -209,31 +215,8 @@
 
 void CameraWrapper::fireFaceCount(int faceCount)
 {
-    // double check it's all ok
-    LOG_ERROR <<"client fireFaceCount start!!!!!!!!"<< LOG_ENDL;
-    int getEnvStat = javaVM->GetEnv((void **)&(javaEnv), JNI_VERSION_1_6);
-    if (getEnvStat == JNI_EDETACHED)
-    {
-        //LOG_WARN << "GetEnv: not attached" << std::endl;
-        if (javaVM->AttachCurrentThread(&(javaEnv), NULL) != 0)
-            LOG_WARN << "Failed to attach" << LOG_ENDL;
-        else
-            getEnvStat = JNI_OK;
-    }
-    else if (getEnvStat == JNI_OK){
-    }
-    else if (getEnvStat == JNI_EVERSION)
-        LOG_WARN << "GetEnv: version not supported" << LOG_ENDL;
-    else if (getEnvStat == JNI_ERR)
-        LOG_WARN << "GetEnv: JNI_ERR" << LOG_ENDL;
-    LOG_ERROR <<"client fireFaceCount end!!!!!!!!!!!!"<< LOG_ENDL;
-
-    javaEnv->CallStaticVoidMethod(faceCallbackClazz, faceCallbackFunc, cameraIdx, faceCount);
-
-    if (javaEnv->ExceptionCheck())
-        javaEnv->ExceptionDescribe();
-
-    javaVM->DetachCurrentThread();
+    this->faceCount = faceCount;
+    this->faceCountChanged = true;
 }
 
 bool cw_pm_breaker_ptr_paint(const PipeMaterial* pm, void* args)
@@ -393,3 +376,43 @@
         }
     }
 }
+
+void *CameraWrapper::jni_call_thd(void *arg) {
+
+    CameraWrapper* cameraWrapper = (CameraWrapper*)arg;
+    while(cameraWrapper->running)
+    {
+        if(cameraWrapper->faceCountChanged){
+            // double check it's all ok
+            LOG_ERROR <<"client fireFaceCount start!!!!!!!!"<< LOG_ENDL;
+            int getEnvStat = cameraWrapper->javaVM->GetEnv((void **)&(cameraWrapper->javaEnv), JNI_VERSION_1_6);
+            if (getEnvStat == JNI_EDETACHED)
+            {
+                //LOG_WARN << "GetEnv: not attached" << std::endl;
+                if (cameraWrapper->javaVM->AttachCurrentThread(&(cameraWrapper->javaEnv), NULL) != 0)
+                    LOG_WARN << "Failed to attach" << LOG_ENDL;
+                else
+                    getEnvStat = JNI_OK;
+            }
+            else if (getEnvStat == JNI_OK){
+            }
+            else if (getEnvStat == JNI_EVERSION)
+                LOG_WARN << "GetEnv: version not supported" << LOG_ENDL;
+            else if (getEnvStat == JNI_ERR)
+                LOG_WARN << "GetEnv: JNI_ERR" << LOG_ENDL;
+            LOG_ERROR <<"client fireFaceCount end!!!!!!!!!!!!"<< LOG_ENDL;
+
+            cameraWrapper->javaEnv->CallStaticVoidMethod(cameraWrapper->faceCallbackClazz,
+                                                         cameraWrapper->faceCallbackFunc,
+                                                         cameraWrapper->cameraIdx,
+                                                         cameraWrapper->faceCount);
+
+            if (cameraWrapper->javaEnv->ExceptionCheck())
+                cameraWrapper->javaEnv->ExceptionDescribe();
+            cameraWrapper->javaVM->DetachCurrentThread();
+            cameraWrapper->faceCountChanged = false;
+        }
+        usleep(10000);
+    }
+
+}
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.h b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.h
index fef9b72..4cd8077 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.h
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperClient.h
@@ -30,8 +30,9 @@
 	PL_AndroidMediaCodecDecoder_Config amcdConfig;
 	PL_AndroidSurfaceViewRender_Config asvrConfig;
 	PL_Scale_Config plScaleCfg;
-
+	int faceCount;
 	int cameraIdx;
+	bool faceCountChanged;
 	JavaVM* javaVM;
 	JNIEnv* javaEnv;
 	jclass faceCallbackClazz;
@@ -41,6 +42,7 @@
 
 	pthread_t decoder_thid;
 	pthread_t live_daemon_thid;
+	pthread_t jni_call_thid;
 	pthread_mutex_t live_daemon_mut;
 	bool running;
 	volatile bool killed;
@@ -59,7 +61,7 @@
 	CameraWrapper() : 
 		pipeLineDecoderDetector(nullptr), pipeLineRender(nullptr), rtspConfig(), amcdConfig(), asvrConfig(),
 		cameraIdx(0), javaVM(nullptr), javaEnv(nullptr), faceCallbackClazz(0), faceCallbackFunc(0), windowRender(nullptr), windowDecode(nullptr),
-		decoder_thid(0), live_daemon_thid(0), live_daemon_mut(), running(false), killed(false), lastAliveTime(0),
+		decoder_thid(0), live_daemon_thid(0),jni_call_thid(0), live_daemon_mut(), running(false), killed(false), lastAliveTime(0),
 		faceCacheLocked(false), faceCache(),
 		plplContext(), faceLabels(), fontPath()
 	{
@@ -79,6 +81,7 @@
 private:
 	static void* decoder_thd(void *arg);
 	static void* live_daemon_thd(void *arg);
+	static void* jni_call_thd(void *arg);
 
 	bool resetPl();
 	bool initPl_DecoderPl();
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperServer.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperServer.cpp
index 7266e3d..95cb0ec 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperServer.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/CameraWrapperServer.cpp
@@ -397,10 +397,10 @@
         auto  request = rClient.fireFaceCountListenerRequest();
         request.setCameraIndex(cameraWrapper.cameraIdx);
         request.setFaceCount(cameraWrapper.faceCache.getFaceCount(*pm));
+        LOG_INFO <<cameraWrapper.cameraIdx << "+" << cameraWrapper.faceCache.getFaceCount(*pm)<< LOG_ENDL;
         LOG_INFO <<"call client : i have face!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<< LOG_ENDL;
         auto sendAct = request.send();
-
-        sendAct.wait(waitScope);
+        sendAct.ignoreResult().wait(waitScope);
     }
     catch (const kj::Exception& e)
     {
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h
index 85e3adb..f5b2bd2 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h
@@ -57,10 +57,17 @@
 
 	static void *rServer_thd(void *arg)
 	{
-		RemoteServer &rs = *(RemoteServer *) arg;
-		capnp::EzRpcServer trpcServer(kj::heap<T>(), rs.host, rs.port);
-		auto &serverLoop = trpcServer.getWaitScope();
-		kj::NEVER_DONE.wait(serverLoop);
+		try {
+			RemoteServer &rs = *(RemoteServer *) arg;
+			capnp::EzRpcServer trpcServer(kj::heap<T>(), rs.host, rs.port);
+			auto &serverLoop = trpcServer.getWaitScope();
+			kj::NEVER_DONE.wait(serverLoop);
+		}
+		catch (kj::Exception e){
+			LOG_ERROR<<e.getLine()<<LOG_ENDL;
+		}
+
+
 	}
 
 	pthread_t server_thid;
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncClient.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncClient.cpp
index 778428a..623fdc0 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncClient.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncClient.cpp
@@ -78,6 +78,7 @@
         {
             //#todo ip modify
             rpcClient = new capnp::EzRpcClient("192.168.1.94", 8112);
+            //rpcClient = new capnp::EzRpcClient("192.168.1.71", 8112);
         }
         catch (const kj::Exception& e)
         {
@@ -94,6 +95,6 @@
 	auto results = context.getResults();
     LOG_ERROR << "!!!!!!!!!!!!!!!!!!!!!WHO  find face!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << LOG_ENDL;
 	remoteFireFaceCountListener(context.getParams(), results);
-	context.setResults(results);
+    context.setResults(results);
     return kj::READY_NOW;
 }
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncServer.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncServer.cpp
index 5d7a9a0..3c77217 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncServer.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFuncServer.cpp
@@ -12,6 +12,7 @@
 
 //#todo 淇敼鏈嶅姟绾跨▼鏋勯�犲弬鏁颁紶鍏ユ柟寮�
 RemoteServer<RtspFaceViewImpl> remoteServer("192.168.1.94", 8112);
+//RemoteServer<RtspFaceViewImpl> remoteServer("192.168.1.97", 8112);
 static RtspFaceDetectClient* rtspFaceDetectClient = nullptr;
 
 void startRemoteServer()
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNIClient.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNIClient.cpp
index 474f89b..10ea530 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNIClient.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNIClient.cpp
@@ -39,17 +39,24 @@
 const size_t _faceImagesSize = MAX_FACE * MAX_FACE_WIDTH * MAX_FACE_HEIGHT * 3;
 uint8_t _faceImages[_faceImagesSize]; // android stack is small
 
+// #todo optimize not copy data here, set data to jbyteArray directly
+//Java_cn_com_basic_face_util_RtspFaceNative_getFaceList
+// Y channel of YUV420p, packed in protobuf
+static uint8_t buffer[MAX_FACE * MAX_FACE_WIDTH * MAX_FACE_HEIGHT] = {0};
+static size_t buffSize = sizeof(buffer);
+
 void remoteFireFaceCountListener(const RtspFaceDetect::FireFaceCountListenerParams::Reader& params, RtspFaceDetect::FireFaceCountListenerResults::Builder& results){
     int count = params.getFaceCount();
     int index = params.getCameraIndex();
-    g_CameraWrappers[index].fireFaceCount(count);
+    LOG_INFO <<count<< "+" << index<< LOG_ENDL;
+    g_CameraWrappers[index - 1].fireFaceCount(count);
 }
 extern "C"
 {
 
 void Java_cn_com_basic_face_util_RtspFaceNative_init(JNIEnv *env, jclass clazz)
 {
-    g_logger.set_level(INFO);
+    g_logger.set_level(VERBOSE);
 
     cpu_sched();
 
@@ -363,10 +370,7 @@
     cameraIdx -= 1;
     CameraWrapper &cameraWrapper(g_CameraWrappers[cameraIdx]);
 
-    // #todo optimize not copy data here, set data to jbyteArray directly
-    // Y channel of YUV420p, packed in protobuf
-    uint8_t buffer[MAX_FACE * MAX_FACE_WIDTH * MAX_FACE_HEIGHT] = {0};
-    size_t buffSize = sizeof(buffer);
+
 
     bool ret = false;
 
@@ -379,21 +383,24 @@
         LOG_ERROR <<"client : i want getFaceList!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<< LOG_ENDL;
         RtspFaceViewClient* client = getRtspFaceViewClient();
         RtspFaceView::Client rClient = client->getEzRpcClient()->getMain<RtspFaceView>();
+        //auto& waitScope =client->getEzRpcClient()->getWaitScope();
         auto& waitScope =client->getWaitScope();
-
         auto  request = rClient.getFaceListRequest();
         request.setCameraIndex(cameraIdx);
         auto sendAct = request.send();
-        auto  result =sendAct.wait(waitScope);
+        auto  result = sendAct.wait(waitScope);
         ret = result.getFaceList().size() > 0;
         LOG_DEBUG << "@@@ getFaceList get data" << LOG_ENDL;
-        memcpy(pBufPB,result.getFaceList().begin(),buffSize);
+        memcpy(pBufPB,result.getFaceList().begin(),result.getFaceList().size());
     }
-    catch (const kj::Exception& e)
-    {
+    catch (const kj::Exception& e){
         LOG_ERROR <<e.getDescription().cStr()<< LOG_ENDL;
         ret = false;
     }
+    catch (...)
+    {
+        ret = false;
+    }
     //-------end-------
     LOG_ERROR <<"client : i want getFaceList   end!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<< LOG_ENDL;
     //ret = cameraWrapper.faceCache.getFaceListPb(pBufPB, buffSize);

--
Gitblit v1.8.0