#include <android/native_window.h>
#include <android/native_window_jni.h>
#include "log.h"
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
extern "C" {
#include "libavformat/avformat.h"
AVPacket enc_pkt;
AVFrame *pFrameYUV;
+void javaPrint(JNIEnv *env, jobject obj, const char* str)
+{
+ jclass clazz = (*env).GetObjectClass(obj);
+ jobject mobj = env->NewGlobalRef(obj);
+ jmethodID mmid = env->GetMethodID(clazz, "print", "(Ljava/lang/String;)V");
+ jstring jstr = env->NewStringUTF(str);
+ env->CallVoidMethod(mobj, mmid, jstr);
+ env->DeleteLocalRef(jstr);
+}
-void custom_log(void *ptr, int level, const char* fmt, va_list vl){
+JNIEnv *g_env;
+jobject g_obj;
+
+void custom_log222 (void *ptr, int level, const char* fmt, va_list vl){
+ static int print_prefix = 1;
+ char line[1024];
+ av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
+ if (level <= AV_LOG_WARNING){
+ LOGE("%s", line);
+ javaPrint(g_env, g_obj, line);
+ } else {
+ //LOGE("%s", line);
+ //javaPrint(g_env, g_obj, line);
+ }
+}
+void custom_log(void *ptr, int level, const char* fmt, va_list vl){
//To TXT file
-
/*FILE *fp=fopen("/storage/emulated/0/av_log.txt","a+");
if(fp){
- vfprintf(fp,fmt,vl);
- fflush(fp);
- fclose(fp);
+ vfprintf(fp,fmt,vl);
+ fflush(fp);
+ fclose(fp);
} */
-
-
//To Logcat
- // LOGE(fmt, vl);
-
+ // LOGE(fmt, vl);
+ static int print_prefix = 1;
+ //static char prev[1024];
+ char line[1024];
- static int print_prefix = 1;
- static int count;
- static char prev[1024];
- char line[1024];
- static int is_atty;
-
- av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
-
- strcpy(prev, line);
- //sanitize((uint8_t *)line);
-
- if (level <= AV_LOG_WARNING)
- {
- LOGE("%s", line);
- }
- else
- {
- LOGE("%s", line);
- }
+ av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
+ //strcpy(prev, line);
+ //sanitize((uint8_t *)line);
+ if (level <= AV_LOG_WARNING){
+ LOGE("%s", line);
+ } else {
+// LOGE("%s", line);
+ }
}
int y_length;
int uv_length;
+JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_init (JNIEnv *env, jobject obj ){
+ LOGE("########## Ffmpeg Init ##########");
+ unsigned int v = avutil_version();
+ LOGE("libavutil - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
+ v = avcodec_version();
+ LOGE("libavcodec - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
+ v = avformat_version();
+ LOGE("libavformat - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
+ v = avdevice_version();
+ LOGE("libavdevice - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
+
+ //system("su -c chmod 666 /dev/video0");
+ system("/system/xbin/su -c echo 'wowo' >> /data/local/test");
+ system("echo 'wowow' >> /sdcard/peng/test");
+
+ av_log_set_level(AV_LOG_TRACE);
+ av_register_all();
+ avdevice_register_all();
+ avformat_network_init();
+ av_log_set_callback(custom_log);
+}
JNIEXPORT jstring JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_getVersion (JNIEnv *env, jobject obj) {
jint v = avformat_version();
+ LOGE("######### Ffmpeg JNI version i= %d", v);
+
+ system("su -c chmod 666 /dev/video0");
+
LOGE("######### Ffmpeg JNI version i= %d", v);
return env->NewStringUTF("====== Ffmpeg call =======");
}
-JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_init (JNIEnv *env, jobject obj, jint width, jint height) {
+//const char* out_path;
+
+JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_setRtmpUrl (JNIEnv *env, jobject obj, jstring url){
+ LOGE("set rtmpurl");
+ //const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
+ //out_path = env->GetStringUTFChars(url, 0);
+}
+
+
+
+//#defind JLOGE(s) javaPrint(env, obj, (s));
+
+
+JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jobject obj, jint width, jint height, jstring url)
+{
+ const char * out_path= env->GetStringUTFChars(url, 0);
+ LOGE("Ffmpeg init, width=%d, heigh=%d, url=%s", width, height, out_path);
+ javaPrint(env, obj, "Ffmpeg init");
+
+ yuv_width=width;
+ yuv_height=height;
+ y_length=width*height;
+ uv_length=width*height/4;
+
+
+ av_register_all();
+ avformat_network_init();
+ g_env = env;
+ g_obj = obj;
+ av_log_set_callback(custom_log222);
+
+
+ //output initialize
+ avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
+ //output encoder initialize
+ pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
+ if (!pCodec){
+ LOGE("Can not find encoder!\n");
+ javaPrint(env, obj, "Can not find encoder!");
+ return -1;
+ }
+ pCodecCtx = avcodec_alloc_context3(pCodec);
+ pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
+ pCodecCtx->width = width;
+ pCodecCtx->height = height;
+ pCodecCtx->time_base.num = 1;
+ pCodecCtx->time_base.den = 30;
+ pCodecCtx->bit_rate = 800000;
+ pCodecCtx->gop_size = 300;
+ /* Some formats want stream headers to be separate. */
+ if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
+ pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
+ //H264 codec param
+ //pCodecCtx->me_range = 16;
+ //pCodecCtx->max_qdiff = 4;
+ //pCodecCtx->qcompress = 0.6;
+ pCodecCtx->qmin = 10;
+ pCodecCtx->qmax = 51;
+ //Optional Param
+ pCodecCtx->max_b_frames = 3;
+ // Set H264 preset and tune
+ AVDictionary *param = 0;
+ av_dict_set(¶m, "preset", "ultrafast", 0);
+ av_dict_set(¶m, "tune", "zerolatency", 0);
+
+ if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0){
+ LOGE("Failed to open encoder!\n");
+ javaPrint(env, obj, "Failed to open encoder!");
+ return -1;
+ }
+
+ //Add a new stream to output,should be called by the user before avformat_write_header() for muxing
+ video_st = avformat_new_stream(ofmt_ctx, pCodec);
+ if (video_st == NULL){
+ return -1;
+ }
+ video_st->time_base.num = 1;
+ video_st->time_base.den = 30;
+ video_st->codec = pCodecCtx;
+
+ //Open output URL,set before avformat_write_header() for muxing
+ jint ret = 0;
+ if (( ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
+ LOGE("Failed to open output file! return :%s(%d)\n", av_err2str(ret),ret);
+ javaPrint(env, obj, "Failed to open output file! return!");
+ return -1;
+ }
+
+ //Write File Header
+ avformat_write_header(ofmt_ctx, NULL);
+
+ start_time = av_gettime();
+ env->ReleaseStringUTFChars(url, out_path);
+ return 0;
+
+}
+
+JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_inithaha (JNIEnv *env, jobject obj, jint width, jint height) {
//const char* out_path = "/storage/emulated/0/Movies/output.flv";
- const char* out_path = "rtmp://192.168.1.35:1935/myapp/suanzi";
+ //const char* out_path = "rtmp://192.168.1.35:1935/myapp/suanzi";
+ const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
+
// const char* out_path = "/storage/sdcard0/output.flv";
return 0;
}
+
+
+JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_processnew (JNIEnv *env, jobject obj, jbyteArray yuv){
+
+}
+
+
+
JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_process (JNIEnv *env, jobject obj, jbyteArray yuv){
int ret;
int enc_got_frame=0;
av_frame_free(&pFrameYUV);
if (enc_got_frame == 1){
- //LOGE("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
+ if (framecnt % (15 * 60) == 0){
+ LOGE("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
+ javaPrint(env, obj, "Succeed to encode frame:");
+
+ }
framecnt++;
enc_pkt.stream_index = video_st->index;
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
av_free_packet(&enc_pkt);
}
+ av_free(out_buffer);
return 0;
}
return 0;
}
-JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_push (JNIEnv *env, jobject obj, jobject surface) {
+JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_push (JNIEnv *env, jobject obj, jobject surface, jstring url){
+ /*
av_log_set_level(AV_LOG_TRACE);
av_register_all();
avformat_network_init();
avdevice_register_all();
+ */
LOGE("====push=====");
- av_log_set_callback(custom_log);
+// av_log_set_callback(custom_log);
+ // Open Output
+ //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
+ const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
+ //const char* out_path = env->GetStringUTFChars(url, 0);
+ //const char * file_name = env->GetStringUTFChars(fname, 0);
+
int ret = 0;
/// Open Input
AVInputFormat *ifmt = av_find_input_format("video4linux2");
if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
+ // if((ret = avformat_open_input(&pFormatCtx, "/dev/bus/usb/003/007", ifmt, NULL)) != 0) {
+
LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
return -1;
}
// Open Output
- const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
+ //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
+ //const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
AVFormatContext *ofmt_ctx;
avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
av_free_packet(&enc_pkt);
//av_packet_unref(&enc_pkt);
-
-
}
-
-
}
}
av_packet_unref(packet);
}
JNIEXPORT jstring JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_getPerfectDevice (JNIEnv *env, jobject obj) {
+ int ret;
+ LOGE("getPerfectDevice");
+ AVFormatContext *pFormatCtx = avformat_alloc_context();
+ AVInputFormat *ifmt = av_find_input_format("video4linux2");
+ if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
+ LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
+ //return ;
+ }
+ if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
+ LOGE( "could not find stream info");
+ //return -1;
+ }
+ av_dump_format(pFormatCtx, 0, "0", 0);
+ avformat_free_context(pFormatCtx);
+ //system("su -c \"find / -perm -2000 -o -perm -4000; ps; ls\"");
+ system("touch /storage/sdcard0/aa");
+
+ return env->NewStringUTF("====== Ffmpeg call =======");
+}
+
+
+
+
+JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_test (JNIEnv *env, jobject obj, jint fd){
+ char path[512] = {0};
+ char* real_path = NULL;
+
+ LOGE("=================");
+ //system("su -c chmod 666 /dev/video0");
+ /*
+#ifdef ANDROID_USB_CAMERA
+ //MY_USB_CAMER_FD = fd;
+ avdevice_set_android_usb_fd(fd);
+
+ //LOGE("MY camer fd is %d", MY_USB_CAMER_FD);
+#endif
+
+ sprintf(path, "/proc/%d/fd/%d", getpid(), fd);
+ if(path[0] != '\0'){
+ LOGE("fd path is %s.", path);
+ real_path = realpath(path, NULL);
+ if(real_path != NULL){
+ LOGE("get full path from fd %s.", real_path);
+ free(real_path);
+ }
+ }
+*/
+
+/*
+
+
+
+ LOGE("====push=====");
+// av_log_set_callback(custom_log);
+
+ int ret = 0;
+ /// Open Input
+ AVFormatContext *pFormatCtx = avformat_alloc_context();
+
+ AVInputFormat *ifmt = av_find_input_format("video4linux2");
+ //if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
+ if((ret = avformat_open_input(&pFormatCtx, real_path, ifmt, NULL)) != 0) {
+
+ LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
+ return -1;
+ }
+
+ if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
+ LOGE( "could not find stream info");
+ return -1;
+ }
+
+ av_dump_format(pFormatCtx, 0, "0", 0);
+
+ AVCodec *dec;
+ int video_index = -1;
+ if((video_index = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0)) < 0){
+ LOGE( "error");
+ return -1;
+ }
+
+ AVCodecContext *pCodecCtx = pFormatCtx->streams[video_index]->codec;
+ if(avcodec_open2(pCodecCtx, dec, NULL) <0){
+ LOGE( "eee");
+ return -1;
+ }
+
+
+ // Open Output
+ //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
+ const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
+
+ AVFormatContext *ofmt_ctx;
+ avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
+ AVCodec *oDec = avcodec_find_encoder(AV_CODEC_ID_H264);
+ if (!oDec) {
+ LOGE("Can not find endoder");
+ return -1;
+ }
+
+ AVCodecContext *oCodecCtx = avcodec_alloc_context3(oDec);
+ oCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
+ oCodecCtx->width = pCodecCtx->width;
+ oCodecCtx->height = pCodecCtx->height;
+ oCodecCtx->time_base.num = 1;
+ oCodecCtx->time_base.den = 30;
+ oCodecCtx->bit_rate = 800000;
+ oCodecCtx->gop_size = 300;
+ if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
+ oCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ oCodecCtx->qmin = 10;
+ oCodecCtx->qmax = 51;
+ oCodecCtx->max_b_frames = 3;
+
+ AVDictionary *params = 0;
+ av_dict_set(¶ms, "preset", "ultrafast", 0);
+ av_dict_set(¶ms, "tune", "zerolatency", 0);
+
+ if (avcodec_open2(oCodecCtx, oDec, ¶ms) < 0){
+ LOGE("Failed to open encoder");
+ return -1;
+ }
+
+ AVStream *videoStream = avformat_new_stream(ofmt_ctx, oDec);
+ if (videoStream == NULL){
+ return -1;
+ }
+
+ videoStream->time_base.num = 1;
+ videoStream->time_base.den = 30;
+ videoStream->codec = oCodecCtx;
+
+ if((ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
+ LOGE("Failed open out file22 erro=%d, ==%s==", ret, av_err2str(ret) );
+ //LOGE("Failed open out file22 erro=%d", ret);
+ return -1;
+ }
+
+ avformat_write_header(ofmt_ctx, NULL);
+ /////////////
+
+
+
+
+ //
+ AVFrame *pFrame, *pFrameYUV;
+ pFrame = av_frame_alloc();
+ pFrameYUV = av_frame_alloc();
+
+ int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
+ uint8_t *buffer = (uint8_t *)av_malloc(num_bytes * sizeof(uint8_t));
+ av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
+
+ pFrameYUV->format = AV_PIX_FMT_YUV420P;
+ pFrameYUV->width = pCodecCtx->width;
+ pFrameYUV->height = pCodecCtx->height;
+
+ struct SwsContext *img_convert_ctx;
+ img_convert_ctx = sws_getContext(pCodecCtx->width,
+ pCodecCtx->height,
+ pCodecCtx->pix_fmt,
+ pCodecCtx->width,
+ pCodecCtx->height,
+ AV_PIX_FMT_YUV420P,
+ SWS_BICUBIC,
+ NULL, NULL, NULL);
+
+ AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
+ int got_picture = 0;
+
+ AVPacket enc_pkt ;
+
+ int64_t framecnt = 0;
+
+ while(av_read_frame(pFormatCtx, packet) >= 0){
+ if (packet->stream_index == video_index){
+ ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
+ if (ret < 0){
+ LOGE("Decode Error.");
+ return -1;
+ }
+ if (got_picture){
+ sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
+
+ enc_pkt.data = NULL;
+ enc_pkt.size = 0;
+ av_init_packet(&enc_pkt);
+ int enc_got_frame = 0;
+ ret = avcodec_encode_video2(oCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
+ if (enc_got_frame == 1){
+
+ framecnt++;
+ enc_pkt.stream_index = videoStream->index;
+
+ // write PTS
+ AVRational time_base = ofmt_ctx->streams[0]->time_base;
+ AVRational r_framerate1 = {60, 2};
+ AVRational time_base_q = {1, AV_TIME_BASE};
+
+ int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳
+ enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
+ enc_pkt.dts = enc_pkt.pts;
+ enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
+ enc_pkt.pos = -1;
+
+ int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
+
+ ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
+ //av_frame_free(&pFrameYUV);
+ //av_packet_unref(packet);
+
+ av_free_packet(&enc_pkt);
+ //av_packet_unref(&enc_pkt);
+ }
+ }
+ }
+ av_packet_unref(packet);
+ }
+
+ sws_freeContext(img_convert_ctx);
+ av_free(pFrameYUV);
+ av_free(pFrame);
+ avcodec_close(pCodecCtx);
+ avformat_close_input(&pFormatCtx);
+
+
+*/
+
+
+
+
+
+
+
+
+
+
}