houxiao
2017-03-21 29179252e3e9619738f36d3ddc37c88073870388
VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp
@@ -1,7 +1,5 @@
#include "RtspNativeCodecJNI.h"
#include <PipeLine.h>
#include <PL_RTSPClient.h>
#include <PL_AndroidMediaCodecDecoder.h>
#include "CameraWrapper.h"
#include <logger.h>
#include <Logger/src/logger.hpp>
@@ -15,103 +13,6 @@
std::stringstream logss;
Logger g_logger(logss);
class CameraWrapper
{
public:
   PipeLine* pipeLine;
   PL_RTSPClient_Config rtspConfig;
   PL_AndroidMediaCodecDecoder_Config amcdConfig;
   jmethodID faceCallback;
   ANativeWindow* window;
   pthread_t live_daemon_thid;
   bool running;
   CameraWrapper() :
      pipeLine(nullptr), rtspConfig(), amcdConfig(), faceCallback(0), window(nullptr),
      live_daemon_thid(0), running(false)
   {
   }
   ~CameraWrapper()
   {
      stop();
      delete pipeLine;
   }
   bool start()
   {
      LOG_INFO << "CameraWrapper::start" << LOG_ENDL;
      running = true;
      int ret = pthread_create(&live_daemon_thid, NULL, CameraWrapper::live_daemon_thd, this);
      if(ret != 0)
      {
         LOGP(ERROR, "pthread_create: %s/n", strerror(ret));
         running = false;
         return false;
      }
      return true;
   }
   void stop()
   {
      LOG_INFO << "CameraWrapper::stop" << LOG_ENDL;
      if (!running)
         return;
      running = false;
      pthread_join(live_daemon_thid, NULL);
   }
   bool initPl()
   {
      PL_RTSPClient* rtspClient = (PL_RTSPClient*)pipeLine->push_elem("PL_RTSPClient");
      bool ret = rtspClient->init(&rtspConfig);
      if (!ret)
      {
         LOG_ERROR << "rtspClient.init error" << std::endl;
         return  false;
      }
      PL_AndroidMediaCodecDecoder* amcDecoder = (PL_AndroidMediaCodecDecoder*)pipeLine->push_elem("PL_AndroidMediaCodecDecoder");
      ret = amcDecoder->init(&amcdConfig);
      if (!ret)
      {
         LOG_ERROR << "amcDecoder.init error" << std::endl;
         return  false;
      }
      return true;
   }
   static void* live_daemon_thd(void* arg)
   {
      LOG_INFO << "CameraWrapper::live_daemon_thd start" << LOG_ENDL;
      CameraWrapper& cameraWrapper = *(CameraWrapper*)arg;
      while(cameraWrapper.running)
      {
         PipeLineElem* last = cameraWrapper.pipeLine->pipe();
         bool ret = cameraWrapper.pipeLine->check_pipe_complete(last);
         LOG_DEBUG << "pipe ret=" << ret << LOG_ENDL;
         if (ret)
         {
            PipeMaterial pm;
            last->gain(pm);
         }
      }
      LOG_INFO << "CameraWrapper::live_daemon_thd stop, ret=" << LOG_ENDL;
   }
};
CameraWrapper g_CameraWrappers[CAMERA_COUNT];
extern "C"
@@ -123,9 +24,12 @@
   
   PipeLine::register_global_elem_creator("PL_RTSPClient", create_PL_RTSPClient);
   PipeLine::register_global_elem_creator("PL_AndroidMediaCodecDecoder", create_PL_AndroidMediaCodecDecoder);
   PipeLine::register_global_elem_creator("PL_SensetimeFaceTrack", create_PL_SensetimeFaceTrack);
   
   for (size_t i = 0; i < CAMERA_COUNT; i++)
   {
      g_CameraWrappers[i].cameraIdx = i + 1;
      g_CameraWrappers[i].pipeLine = new PipeLine;
      PipeLine& pipeLine(*(g_CameraWrappers[i].pipeLine));
   }
@@ -177,6 +81,14 @@
      cameraWrapper.amcdConfig.windowSurface = cameraWrapper.window;
      cameraWrapper.amcdConfig.releaseOutputBuffIdx = true;
   }
   {
      cameraWrapper.sftConfig.point_size = 21;
      cameraWrapper.sftConfig.detect_face_cnt_limit = MAX_FACE;
      cameraWrapper.sftConfig.draw_face_rect = false;
      cameraWrapper.sftConfig.draw_face_feature_point = false;
      cameraWrapper.sftConfig.generate_face_feature = true;
   }
   bool ret = cameraWrapper.initPl();
   if (ret)
@@ -201,13 +113,12 @@
    LOG_DEBUG << "@@@ Java_com_example_nativecodec_NativeCodec_setFaceCallback" << LOG_ENDL;
   assert(cameraIdx <= CAMERA_COUNT);
   cameraIdx -= 1;
   CameraWrapper& cameraWrapper(g_CameraWrappers[cameraIdx]);
    jclass cls = env->GetObjectClass(clazz);
    const char *utfFunc = env->GetStringUTFChars(func, NULL);
    cameraWrapper.faceCallback = env->GetMethodID(cls, utfFunc, "(II)V"); // Java_FaceCallback_func
    cameraWrapper.faceCallback = env->GetMethodID(cls, utfFunc, "(II)V"); // Java_com_example_nativecodec_NativeCodec_FaceCallback_func
    env->ReleaseStringUTFChars(func, utfFunc);
   
   // call:
@@ -216,26 +127,52 @@
void Java_com_example_nativecodec_NativeCodec_lockFace(JNIEnv* env, jclass clazz, jint cameraIdx)
{
   LOG_DEBUG << "@@@ Java_com_example_nativecodec_NativeCodec_lockFace" << LOG_ENDL;
   assert(cameraIdx <= CAMERA_COUNT);
   cameraIdx -= 1;
   CameraWrapper& cameraWrapper(g_CameraWrappers[cameraIdx]);
   cameraWrapper.lockFace();
}
void Java_com_example_nativecodec_NativeCodec_releaseFace(JNIEnv* env, jclass clazz, jint cameraIdx)
{
   LOG_DEBUG << "@@@ Java_com_example_nativecodec_NativeCodec_releaseFace" << LOG_ENDL;
   assert(cameraIdx <= CAMERA_COUNT);
   cameraIdx -= 1;
   CameraWrapper& cameraWrapper(g_CameraWrappers[cameraIdx]);
   cameraWrapper.releaseFace();
}
jboolean Java_com_example_nativecodec_NativeCodec_getFaceList(JNIEnv* env, jclass clazz, jint cameraIdx, jbyteArray faceListPb)
{
   LOG_DEBUG << "@@@ Java_com_example_nativecodec_NativeCodec_getFaceList" << LOG_ENDL;
   assert(cameraIdx <= CAMERA_COUNT);
   cameraIdx -= 1;
   CameraWrapper& cameraWrapper(g_CameraWrappers[cameraIdx]);
   // Y channel of YUV420p, packed in protobuf
   uint8_t buffer[MAX_FACE * MAX_FACE_WIDTH * MAX_FACE_HEIGHT];
   size_t buffSize = sizeof(buffer);
   bool ret = cameraWrapper.faceCache.getFaceListPb(buffer, buffSize);
   if (!ret)
   {
      LOG_INFO << "No face captured" << LOG_ENDL;
      return JNI_FALSE;
   }
   uint8_t* _faceListPb = (uint8_t*)faceListPb;
   LOG_DEBUG << _faceListPb[0] << "  "<< _faceListPb[1] << "  "<< _faceListPb[2] << "  " <<LOG_ENDL;
}
jboolean Java_com_example_nativecodec_NativeCodec_getFaceImages(JNIEnv* env, jclass clazz, jint cameraIdx, jbyteArray faceImagesIdx, jbyteArray faceImages)
jboolean Java_com_example_nativecodec_NativeCodec_getFaceImages(JNIEnv* env, jclass clazz, jint cameraIdx, jintArray faceImagesIdx, jbyteArray faceImages)
{
   LOG_DEBUG << "@@@ Java_com_example_nativecodec_NativeCodec_getFaceImages" << LOG_ENDL;
   assert(cameraIdx <= CAMERA_COUNT);
   cameraIdx -= 1;
   CameraWrapper& cameraWrapper(g_CameraWrappers[cameraIdx]);
}