Android中如何通过JNI调用C文件中的main函数?

2024-03-30

我正在开发一个应用程序,我必须使用Live555在 Android 4.4.2 上。

Live555是一个 C 源代码Media Streaming,我必须这样称呼它JNI.

我已经构建了 .soLive555就像下面的图片:

我想通过JNI调用C源代码

The question 1

我应该使用哪些库来加载System.loadLibrary就像下面的代码一样JAVA ?

static {
        System.loadLibrary("What libraries name should I load ?");
    }

The question 2

如何致电main函数于java通过使用public native String main; (主要功能在jni/mediaServer/live555MediaServer.cpp)

的C源代码live555MediaServer.cpp就像下面这样:

#include <BasicUsageEnvironment.hh>
#include "DynamicRTSPServer.hh"
#include "version.hh"

int main(int argc, char** argv) {
  // Begin by setting up our usage environment:
  TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);

  UserAuthenticationDatabase* authDB = NULL;
#ifdef ACCESS_CONTROL
  // To implement client access control to the RTSP server, do the following:
  authDB = new UserAuthenticationDatabase;
  authDB->addUserRecord("username1", "password1"); // replace these with real strings
  // Repeat the above with each <username>, <password> that you wish to allow
  // access to the server.
#endif

  // Create the RTSP server.  Try first with the default port number (554),
  // and then with the alternative port number (8554):
  RTSPServer* rtspServer;
  portNumBits rtspServerPortNum = 554;
  rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  if (rtspServer == NULL) {
    rtspServerPortNum = 8554;
    rtspServer = DynamicRTSPServer::createNew(*env, rtspServerPortNum, authDB);
  }
  if (rtspServer == NULL) {
    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
    exit(1);
  }

  *env << "LIVE555 Media Server\n";
  *env << "\tversion " << MEDIA_SERVER_VERSION_STRING
       << " (LIVE555 Streaming Media library version "
       << LIVEMEDIA_LIBRARY_VERSION_STRING << ").\n";

  char* urlPrefix = rtspServer->rtspURLPrefix();
  *env << "Play streams from this server using the URL\n\t"
       << urlPrefix << "<filename>\nwhere <filename> is a file present in the current directory.\n";
  *env << "Each file's type is inferred from its name suffix:\n";
  *env << "\t\".264\" => a H.264 Video Elementary Stream file\n";
  *env << "\t\".aac\" => an AAC Audio (ADTS format) file\n";
  *env << "\t\".ac3\" => an AC-3 Audio file\n";
  *env << "\t\".amr\" => an AMR Audio file\n";
  *env << "\t\".dv\" => a DV Video file\n";
  *env << "\t\".m4e\" => a MPEG-4 Video Elementary Stream file\n";
  *env << "\t\".mkv\" => a Matroska audio+video+(optional)subtitles file\n";
  *env << "\t\".mp3\" => a MPEG-1 or 2 Audio file\n";
  *env << "\t\".mpg\" => a MPEG-1 or 2 Program Stream (audio+video) file\n";
  *env << "\t\".ts\" => a MPEG Transport Stream file\n";
  *env << "\t\t(a \".tsx\" index file - if present - provides server 'trick play' support)\n";
  *env << "\t\".vob\" => a VOB (MPEG-2 video with AC-3 audio) file\n";
  *env << "\t\".wav\" => a WAV Audio file\n";
  *env << "\t\".webm\" => a WebM audio(Vorbis)+video(VP8) file\n";
  *env << "See http://www.live555.com/mediaServer/ for additional documentation.\n";

  // Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.
  // Try first with the default HTTP port (80), and then with the alternative HTTP
  // port numbers (8000 and 8080).

  if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) {
    *env << "(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling, or for HTTP live streaming (for indexed Transport Stream files only).)\n";
  } else {
    *env << "(RTSP-over-HTTP tunneling is not available.)\n";
  }

  env->taskScheduler().doEventLoop(); // does not return

  return 0; // only to prevent compiler warning
}

- - - - - - - - - - - - - - - -编辑 - - - - - - - - - ------------------

jni/live555.cpp:33:11: error: 'argc' was not declared in this scope
jni/live555.cpp:33:17: error: 'argv' was not declared in this scope
jni/live555.cpp:33:21: error: 'main' was not declared in this scope

static {
        // order matters when you have more ".so" files.
        System.loadLibrary("live555");
    }

现在用参数调用主函数。创建一个采用字符串数组的 JNI 方法,然后简单地调用该方法。在 JNI 中将字符串数组转换为 C 字符串数组,就完成了。

JNIEXPORT return_type JNICALL Java_full_package_name_classname_functionname(JNIEnv *env, jobject object, Jobject stringArray){
    // your argc
    int size = env->GetArrayLength(stringArray);
    char **argv = new char*[size];
    for (int i=0; i < size; ++i) 
    {
        jstring string = env->GetObjectArrayElement(stringArrays, i);
        const char* cString = env->GetStringUTFChars(string, 0);
        argv[i] = strdup(cString);
        env->ReleaseStringUTFChars(string, myarray);
        env->DeleteLocalRef(string);
    }
    // you will need to modify main so it does properly cleanup
    main(argc, argv);
    // clean up
    for(int i = 0; i < size; ++i)
        free(argv[i]);
    delete [] argv;
}

你应该尝试一些 hello world 应用程序来让 Javac++ 代码在 Android 上运行,其余的应该是相当明显的。检查 Android 示例。他们有很好的 JNI 样本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android中如何通过JNI调用C文件中的main函数? 的相关文章

随机推荐