1 #include "FfmpegHelper.h"
5 #define FLOGE(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
6 #define FLOGD(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
8 FfmpegHelper* FfmpegHelper::singleton = NULL;
9 bool FfmpegHelper::isInit = false;
12 FfmpegHelper::FfmpegHelper(JavaVM *vm, jclass cls)
14 , ai_suanzi_rtmpclient_FfmpegHelper(cls)
19 jint FfmpegHelper::nativeOnLoad(JavaVM * vm, void* reserved)
22 if(vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK){
27 if (env) local_ref = env->FindClass ("ai/suanzi/rtmpclient/FfmpegHelper");
28 jclass global_ref = reinterpret_cast<jclass> (env->NewGlobalRef (local_ref));
29 singleton = new FfmpegHelper(vm, global_ref);
30 return JNI_VERSION_1_6;
33 void FfmpegHelper::av_log_cb (void *ptr, int level, const char* fmt, va_list vl)
35 static int print_prefix = 1;
37 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
39 if (level <= AV_LOG_WARNING){
40 if (singleton) singleton->javaPrint(line);
42 if (singleton) singleton->javaPrint(line);
47 void FfmpegHelper::javaPrint(const char* str)
50 if(this->jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK){
53 jmethodID mid = env->GetStaticMethodID(ai_suanzi_rtmpclient_FfmpegHelper, "javaPrint", "(Ljava/lang/String;)V");
54 jstring jstr = env->NewStringUTF(str);
55 env->CallStaticVoidMethod(ai_suanzi_rtmpclient_FfmpegHelper, mid, jstr);
56 env->DeleteLocalRef(jstr);
59 void FfmpegHelper::init()
61 av_log_set_callback(av_log_cb);
62 av_log_set_level(AV_LOG_DEBUG);
63 FLOGE("########## Ffmpeg Init ##########");
64 unsigned int v = avutil_version();
65 FLOGE("libavutil - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
66 v = avcodec_version();
67 FLOGE("libavcodec - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
68 v = avformat_version();
69 FLOGE("libavformat - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
70 v = avdevice_version();
71 FLOGE("libavdevice - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
74 //avdevice_register_all();
76 if((ret = avformat_network_init()) != 0){
77 FLOGE("avformat_network_init, error:%s(%d)", av_err2str(ret), ret);
82 int FfmpegHelper::initEncoder(int width, int height, const char* outpath)
85 FLOGE("initEncoder - width=%d, height=%d, url=%s", width, height, outpath);
91 avformat_alloc_output_context2(&formatCtx, NULL, "flv", outpath);
94 if((codec = avcodec_find_encoder(AV_CODEC_ID_H264)) == NULL){
95 FLOGE("Can not find encoder!\n");
98 codecCtx = avcodec_alloc_context3(codec);
99 codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
100 codecCtx->width = width;
101 codecCtx->height = height;
102 codecCtx->time_base.num = 1;
103 codecCtx->time_base.den = 30;
104 codecCtx->bit_rate = 800000;
105 codecCtx->gop_size = 300;
106 if (formatCtx->oformat->flags & AVFMT_GLOBALHEADER) /* Some formats want stream headers to be separate. */
107 codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
109 //pCodecCtx->me_range = 16;
110 //pCodecCtx->max_qdiff = 4;
111 //pCodecCtx->qcompress = 0.6;
115 codecCtx->max_b_frames = 3;
116 // Set H264 preset and tune
117 AVDictionary *param = 0;
118 av_dict_set(¶m, "preset", "ultrafast", 0);
119 av_dict_set(¶m, "tune", "zerolatency", 0);
120 if ((ret = avcodec_open2(codecCtx, codec, ¶m)) < 0){
121 LOGE("Failed to open encoder!, error:%s(%d)\n", av_err2str(ret), ret);
125 //Add a new stream to output,should be called by the user before avformat_write_header() for muxing
127 if ((vStream = avformat_new_stream(formatCtx, codec)) == NULL){
128 FLOGE("avformat_new_stream - error");
131 vStream->time_base.num = 1;
132 vStream->time_base.den = 30;
133 vStream->codec = codecCtx;
135 //Open output URL,set before avformat_write_header() for muxing
136 if (( ret = avio_open(&formatCtx->pb, outpath, AVIO_FLAG_READ_WRITE)) < 0){
137 LOGE("Failed to open output file! error :%s(%d)\n", av_err2str(ret), ret);
141 if((ret = avformat_write_header(formatCtx, NULL)) != 0){ //Write File Header
142 LOGE("avformat_write_header error :%s(%d)\n", av_err2str(ret), ret);
145 startTime = av_gettime();
149 int FfmpegHelper::processFrame(uint8_t* data)
152 AVFrame *pFrame, *pFrameYUV;
153 pFrameYUV = av_frame_alloc();
154 int y_length = pWidth * pHeight;
155 uint8_t *outBuf = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, codecCtx->width, codecCtx->height, 1));
156 av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, outBuf, AV_PIX_FMT_YUV420P, codecCtx->width, codecCtx->height, 1);
158 memcpy(pFrameYUV->data[0], data, y_length);
159 for (int i = 0; i < y_length / 4; i++){
160 *(pFrameYUV->data[2] + i) = *(data + y_length * 2);
161 *(pFrameYUV->data[1] + i) = *(data + y_length * 2 + 1);
164 pFrameYUV->format = AV_PIX_FMT_YUV420P;
165 pFrameYUV->width = pWidth;
166 pFrameYUV->height = pHeight;
172 av_init_packet(&encPkt);
175 avcodec_encode_video2(codecCtx, &encPkt, pFrameYUV, &got_frame);
176 av_frame_free(&pFrameYUV);
179 if (frameCnt % (15 * 60) == 0){
180 FLOGE("Succeed to encode frame: %5d\tsize:%5d\n", frameCnt, encPkt.size);
183 encPkt.stream_index = vStream->index;
186 AVRational time_base = formatCtx->streams[0]->time_base;//{ 1, 1000 };
187 AVRational r_framerate = {60, 2 };//{ 50, 2 };
188 AVRational time_base_q = { 1, AV_TIME_BASE };
189 //Duration between 2 frames (us)
190 int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate)); //内部时间戳
192 //enc_pkt.pts = (double)(framecnt*calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
193 encPkt.pts = av_rescale_q(frameCnt*calc_duration, time_base_q, time_base);
194 encPkt.dts = encPkt.pts;
195 encPkt.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));
199 int64_t pts_time = av_rescale_q(encPkt.dts, time_base, time_base_q);
200 int64_t now_time = av_gettime() - startTime;
201 if (pts_time > now_time)
202 av_usleep(pts_time - now_time);
204 ret = av_interleaved_write_frame(formatCtx, &encPkt);
205 av_packet_unref(&encPkt);
211 int FfmpegHelper::close()
214 avcodec_close(vStream->codec);
215 avio_close(formatCtx->pb);
216 avformat_free_context(formatCtx);
221 jint FfmpegHelper::nativeInitialEncoder(JNIEnv *env, jclass cls, jint width, jint height, jstring url)
223 const char* output = env->GetStringUTFChars(url, 0);
225 if (singleton) ret = singleton->initEncoder(width, height, output);
226 env->ReleaseStringUTFChars(url, output);
230 jint FfmpegHelper::nativeProcessFrame(JNIEnv *env, jclass cls, jbyteArray data)
232 jbyte* buf = (jbyte*)env->GetByteArrayElements(data, 0);
234 if(singleton) ret = singleton->processFrame((uint8_t *)buf);
238 jint FfmpegHelper::nativeClose()
240 if(singleton) return singleton->close();