参考:https://github.com/wangzuxing/MyFFmpegH264H265YUVOpenGL/blob/master/MyFFmpegH264H265YUVOpenGL/jni/streamer.cpp
返回字符串:
string result="aaaaa";
char *p = const_cast<char*>(result.c_str());
jstring jres = env->NewStringUTF(p);
return jres;
string转换
#include <jni.h>
#include <android/log.h>
#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh>
#include <pthread.h>
#include "DynamicRTSPServer.hh"
#include "version.hh"
#include "RTSPStream.h"
#include "H264VideoSource.h"
#include "H264VideoServerMediaSubsession.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOG_TAG "streamer"
#define LOGW(a ) __android_log_write(ANDROID_LOG_WARN,LOG_TAG,a)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
void play();
void afterPlaying(void* /*clientData*/);
//}
UsageEnvironment* uEnv;
H264VideoStreamFramer* videoSource;
RTPSink* videoSink;
char quit;
char* url;
int port_number;
FILE *file;
char const *inputFilename;
Boolean reuseFirstSource = False;
Boolean reuseFirstSource0 = True;
pthread_mutex_t mutex;
//将const char类型转换成jstring类型
jstring CStr2Jstring0(JNIEnv* env, const char* pat )
{
// 定义java String类 strClass
jclass strClass = (env)->FindClass("java/lang/String;");
// 获取java String类方法String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
// 建立byte数组
jbyteArray bytes = (env)->NewByteArray((jsize)strlen(pat));
// 将char* 转换为byte数组
(env)->SetByteArrayRegion(bytes, 0, (jsize)strlen(pat), (jbyte*)pat);
//设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = (env)->NewStringUTF("GB2312");
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
}
char * Jstring2CStr0( JNIEnv * env, jstring jstr )
{
char * rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("GB2312");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr,mid,strencode);
jsize alen = env->GetArrayLength(barr);
jbyte * ba = env->GetByteArrayElements(barr,JNI_FALSE);
if(alen > 0)
{
rtn = (char*)malloc(alen+1); //new char[alen+1];
memcpy(rtn,ba,alen);
rtn[alen]=0;
}
env->ReleaseByteArrayElements(barr,ba,0);
return rtn;
}
jstring CharTojstring0(JNIEnv* env, char* str)
{
jsize len = strlen(str);
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("GB2312");
jmethodID mid = env->GetMethodID(clsstring,"<init>","([BLjava/lang/String;)V");
jbyteArray barr = env-> NewByteArray(len);
env-> SetByteArrayRegion(barr,0,len,(jbyte*)str);
return (jstring)env-> NewObject(clsstring,mid,barr,strencode);
}
void doExecuteVideoViewShow0(JNIEnv * env, jobject obj, jstring sdp) {
// 1.找到java的MainActivity的class
jclass clazz = env->FindClass("com/example/mylive55servermediacodec2/StreamerActivity2");
if (clazz == 0) {
LOGI("can't find clazz");
}
LOGI(" find clazz");
//2 找到class 里面的方法定义
jmethodID methodid = env->GetMethodID(clazz, "PlayRtspStream", "(Ljava/lang/String;)V");
if (methodid == 0) {
LOGI("can't find methodid");
}
LOGI(" find methodid");
//3 .调用方法
env->CallVoidMethod(clazz, methodid, sdp);
}
static void announceStream0(RTSPServer* rtspServer, ServerMediaSession* sms,
char const* streamName, char const* inputFileName)
{
char* url = rtspServer->rtspURL(sms);
char buf[255] = { 0 };
UsageEnvironment& env = rtspServer->envir();
sprintf(buf,
"%s-->stream, from the file-->%s,Play this stream using the URL:%s",
streamName, inputFileName, url);
LOGW(buf);
delete[] url;
}