本文的示例将实现:读取安卓手机摄像头数据并使用H.264编码格式实时编码保存为flv文件。示例包含了
1、编译适用于安卓平台的ffmpeg库
2、在java中通过JNI使用ffmpeg
3、读取安卓摄像头数据并在后台线程中使用ffmpeg进行编码的基本流程
具有较强的综合性。
编译适用于安卓平台的ffmpeg库
- SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
- LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
- SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
- SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'
修改为
- SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
- LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
- SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
- SLIB_INSTALL_LINKS='$(SLIBNAME)'
之后进行常规的configure配置,make, make install步骤即可,下面给出一个常规的脚本示例
- make clean
- export NDK=xxxx/android-ndk-r10e
- export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt
- export PLATFORM=$NDK/platforms/android-8/arch-arm
- export PREFIX=../android_ffmpeglib
- ./configure --target-os=linux --prefix=$PREFIX \
- --enable-cross-compile \
- --enable-runtime-cpudetect \
- --disable-asm \
- --arch=arm \
- --cc=$PREBUILT/windows/bin/arm-linux-androideabi-gcc \
- --cross-prefix=$PREBUILT/windows/bin/arm-linux-androideabi- \
- --disable-stripping \
- --nm=$PREBUILT/windows/bin/arm-linux-androideabi-nm \
- --sysroot=$PLATFORM \
- --enable-gpl --enable-shared --disable-static --enable-small \
- --disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \
- --extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"
- make
- make install
成功编译后,可以看到如下类库
在java中通过JNI使用ffmpeg
该协议用来沟通Java代码和外部的本地C/C++代码, 通过该协议 Java代码可以调用外部的本地代码, 外部的C/C++
代码可以调用Java代码。简单来说,就是将一个C语言的方法映射到Java的某个方法上;
-- native : Java语言中修饰本地方法的修饰符, 被该修饰符修饰的方法没有方法体;
-- Native方法 : 在Java语言中被native关键字修饰的方法是Native方法;
-- JNI层 : Java声明Native方法的部分;
-- JNI函数 : JNIEnv提供的函数, 这些函数在jni.h中进行定义;
-- JNI方法 : Native方法对应的JNI层实现的 C/C++方法, 即在jni目录中实现的那些C语言代码;
具体流程可以参照ndk的sample目录下的hellojni项目,总结起来有如下几个步骤
- jstring
- Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
- jobject thiz )
jstring 是 Java语言中的String类型, 方法名格式为 : Java_完整包名类名_方法名();
-- JNIEnv参数 : 代表的是Java环境, 通过这个环境可以调用Java里面的方法;
-- jobject参数 : 调用C语言方法的对象, thiz对象表示当前的对象, 即调用JNI方法所在的类;
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := hello-jni
- LOCAL_SRC_FILES := hello-jni.c
- include $(BUILD_SHARED_LIBRARY)
-- LOCAL_PATH : 代表mk文件所在的目录;
-- include $(CLEAR_VARS) : 编译工具函数, 通过该函数可以进行一些初始化操作;
-- LOCAL_MODULE : 编译后的 .so 后缀文件叫什么名字;
-- LOCAL_SRC_FILES: 指定编译的源文件名称;
-- include $(BUILD_SHARED_LIBRARY) : 告诉编译器需要生成动态库;
- static {
- System.loadLibrary("hello-jni");
- }
参照上面的基本流程,具体到利用ffmpeg实现安卓摄像头数据的编码
- //JNI
- //初始化,读取待编码数据的宽和高
- public native int initial(int width,int height);
- //读取yuv数据进行编码
- public native int encode(byte[] yuvimage);
- //清空缓存的帧
- public native int flush();
- //清理
- public native int close();
第二步:对应的c文件内容如下,基本就是一个将yuv数据编码为H.264的flv文件的流程,唯一需要注意的就是安卓摄像头拍摄数据的像素格式为NV21,需要转换为YUV420P才能进行编码
- /**
- * 基于FFmpeg安卓摄像头编码
- * FFmpeg Android Camera Encoder
- *
- * 张晖 Hui Zhang
- * zhanghuicuc@gmail.com
- * 中国传媒大学/数字电视技术
- * Communication University of China / Digital TV Technology
- *
- *
- */
- #include <stdio.h>
- #include <time.h>
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- #include "libswscale/swscale.h"
- #include "libavutil/log.h"
- #ifdef ANDROID
- #include <jni.h>
- #include <android/log.h>
- #define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
- #define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, "(=_=)", format, ##__VA_ARGS__)
- #else
- #define LOGE(format, ...) printf("(>_<) " format "\n", ##__VA_ARGS__)
- #define LOGI(format, ...) printf("(^_^) " format "\n", ##__VA_ARGS__)
- #endif
- AVFormatContext *ofmt_ctx;
- AVStream* video_st;
- AVCodecContext* pCodecCtx;
- AVCodec* pCodec;
- AVPacket enc_pkt;
- AVFrame *pFrameYUV;
- int framecnt = 0;
- int yuv_width;
- int yuv_height;
- int y_length;
- int uv_length;
- int64_t start_time;
- //Output FFmpeg's av_log()
- void custom_log(void *ptr, int level, const char* fmt, va_list vl){
- FILE *fp=fopen("/storage/emulated/0/av_log.txt","a+");
- if(fp){
- vfprintf(fp,fmt,vl);
- fflush(fp);
- fclose(fp);
- }
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_initial
- (JNIEnv *env, jobject obj,jint width,jint height)
- {
- const char* out_path = "/sdcard/zhanghui/testffmpeg.flv";
- yuv_width=width;
- yuv_height=height;
- y_length=width*height;
- uv_length=width*height/4;
- //FFmpeg av_log() callback
- av_log_set_callback(custom_log);
- av_register_all();
- //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");
- return -1;
- }
- pCodecCtx = avcodec_alloc_context3(pCodec);
- pCodecCtx->pix_fmt = 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");
- 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
- if (avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE) < 0){
- LOGE("Failed to open output file!\n");
- return -1;
- }
- //Write File Header
- avformat_write_header(ofmt_ctx, NULL);
- start_time = av_gettime();
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_encode
- (JNIEnv *env, jobject obj, jbyteArray yuv)
- {
- int ret;
- int enc_got_frame=0;
- int i=0;
- pFrameYUV = avcodec_alloc_frame();
- uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
- avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- //安卓摄像头数据为NV21格式,此处将其转换为YUV420P格式
- jbyte* in= (jbyte*)(*env)->GetByteArrayElements(env,yuv,0);
- memcpy(pFrameYUV->data[0],in,y_length);
- for(i=0;i<uv_length;i++)
- {
- *(pFrameYUV->data[2]+i)=*(in+y_length+i*2);
- *(pFrameYUV->data[1]+i)=*(in+y_length+i*2+1);
- }
- pFrameYUV->format = AV_PIX_FMT_YUV420P;
- pFrameYUV->width = yuv_width;
- pFrameYUV->height = yuv_height;
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = avcodec_encode_video2(pCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
- av_frame_free(&pFrameYUV);
- if (enc_got_frame == 1){
- LOGI("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
- framecnt++;
- enc_pkt.stream_index = video_st->index;
- //Write PTS
- AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
- AVRational r_framerate1 = {60, 2 };//{ 50, 2 };
- AVRational time_base_q = { 1, AV_TIME_BASE };
- //Duration between 2 frames (us)
- int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳
- //Parameters
- //enc_pkt.pts = (double)(framecnt*calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
- 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;
- //Delay
- int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
- int64_t now_time = av_gettime() - start_time;
- if (pts_time > now_time)
- av_usleep(pts_time - now_time);
- ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
- av_free_packet(&enc_pkt);
- }
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_flush
- (JNIEnv *env, jobject obj)
- {
- int ret;
- int got_frame;
- AVPacket enc_pkt;
- if (!(ofmt_ctx->streams[0]->codec->codec->capabilities &
- CODEC_CAP_DELAY))
- return 0;
- while (1) {
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = avcodec_encode_video2(ofmt_ctx->streams[0]->codec, &enc_pkt,
- NULL, &got_frame);
- if (ret < 0)
- break;
- if (!got_frame){
- ret = 0;
- break;
- }
- LOGI("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);
- //Write PTS
- AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
- AVRational r_framerate1 = { 60, 2 };
- AVRational time_base_q = { 1, AV_TIME_BASE };
- //Duration between 2 frames (us)
- int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1)); //内部时间戳
- //Parameters
- 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);
- //转换PTS/DTS(Convert PTS/DTS)
- enc_pkt.pos = -1;
- framecnt++;
- ofmt_ctx->duration = enc_pkt.duration * framecnt;
- /* mux encoded frame */
- ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
- if (ret < 0)
- break;
- }
- //Write file trailer
- av_write_trailer(ofmt_ctx);
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_zhanghui_test_MainActivity_close
- (JNIEnv *env, jobject obj)
- {
- if (video_st)
- avcodec_close(video_st->codec);
- avio_close(ofmt_ctx->pb);
- avformat_free_context(ofmt_ctx);
- return 0;
- }
第三步:编写Android.mk如下,这里要在jni目录下放入ffmpeg的头文件,就跟平时的调用方法一样
- LOCAL_PATH := $(call my-dir)
- # FFmpeg library
- include $(CLEAR_VARS)
- LOCAL_MODULE := avcodec
- LOCAL_SRC_FILES := libavcodec-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avdevice
- LOCAL_SRC_FILES := libavdevice-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avfilter
- LOCAL_SRC_FILES := libavfilter-5.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avformat
- LOCAL_SRC_FILES := libavformat-56.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := avutil
- LOCAL_SRC_FILES := libavutil-54.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := postproc
- LOCAL_SRC_FILES := libpostproc-53.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := swresample
- LOCAL_SRC_FILES := libswresample-1.so
- include $(PREBUILT_SHARED_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := swscale
- LOCAL_SRC_FILES := libswscale-3.so
- include $(PREBUILT_SHARED_LIBRARY)
- # Program
- include $(CLEAR_VARS)
- LOCAL_MODULE := encode
- LOCAL_SRC_FILES :=encode.c
- LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
- LOCAL_LDLIBS := -llog -lz
- LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale
- include $(BUILD_SHARED_LIBRARY)
第四步:使用cygwin切换到当前项目的jni目录下,输入ndk-build进行编译即可,如下
- static{
- System.loadLibrary("avutil-54");
- System.loadLibrary("swresample-1");
- System.loadLibrary("avcodec-56");
- System.loadLibrary("avformat-56");
- System.loadLibrary("swscale-3");
- System.loadLibrary("postproc-53");
- System.loadLibrary("avfilter-5");
- System.loadLibrary("avdevice-56");
- System.loadLibrary("encode");
- }
至此,就可以在java中调用我们声明的四个方法了。
java部分获取摄像头数据并编码的流程
- package com.zhanghui.test;
- import java.io.IOException;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.pm.PackageManager;
- import android.hardware.Camera;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- @SuppressWarnings("deprecation")
- public class MainActivity extends Activity {
- private static final String TAG= "MainActivity";
- private Button mTakeButton;
- private Camera mCamera;
- private SurfaceView mSurfaceView;
- private SurfaceHolder mSurfaceHolder;
- private boolean isRecording = false;
- private class StreamTask extends AsyncTask<Void, Void, Void>{
- private byte[] mData;
- //构造函数
- StreamTask(byte[] data){
- this.mData = data;
- }
- @Override
- protected Void doInBackground(Void... params) {
- // TODO Auto-generated method stub
- if(mData!=null){
- Log.i(TAG, "fps: " + mCamera.getParameters().getPreviewFrameRate());
- encode(mData);
- }
- return null;
- }
- }
- private StreamTask mStreamTask;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final Camera.PreviewCallback mPreviewCallbacx=new Camera.PreviewCallback() {
- @Override
- public void onPreviewFrame(byte[] arg0, Camera arg1) {
- // TODO Auto-generated method stub
- if(null != mStreamTask){
- switch(mStreamTask.getStatus()){
- case RUNNING:
- return;
- case PENDING:
- mStreamTask.cancel(false);
- break;
- }
- }
- mStreamTask = new StreamTask(arg0);
- mStreamTask.execute((Void)null);
- }
- };
- mTakeButton=(Button)findViewById(R.id.take_button);
- PackageManager pm=this.getPackageManager();
- boolean hasCamera=pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
- pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT) ||
- Build.VERSION.SDK_INT<Build.VERSION_CODES.GINGERBREAD;
- if(!hasCamera)
- mTakeButton.setEnabled(false);
- mTakeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if(mCamera!=null)
- {
- if (isRecording) {
- mTakeButton.setText("Start");
- mCamera.setPreviewCallback(null);
- Toast.makeText(MainActivity.this, "encode done", Toast.LENGTH_SHORT).show();
- isRecording = false;
- }else {
- mTakeButton.setText("Stop");
- initial(mCamera.getParameters().getPreviewSize().width,mCamera.getParameters().getPreviewSize().height);
- mCamera.setPreviewCallback(mPreviewCallbacx);
- isRecording = true;
- }
- }
- }
- });
- mSurfaceView=(SurfaceView)findViewById(R.id.surfaceView1);
- SurfaceHolder holder=mSurfaceView.getHolder();
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- holder.addCallback(new SurfaceHolder.Callback() {
- @Override
- public void surfaceDestroyed(SurfaceHolder arg0) {
- // TODO Auto-generated method stub
- if(mCamera!=null)
- {
- mCamera.stopPreview();
- mSurfaceView = null;
- mSurfaceHolder = null;
- }
- }
- @Override
- public void surfaceCreated(SurfaceHolder arg0) {
- // TODO Auto-generated method stub
- try{
- if(mCamera!=null){
- mCamera.setPreviewDisplay(arg0);
- mSurfaceHolder=arg0;
- }
- }catch(IOException exception){
- Log.e(TAG, "Error setting up preview display", exception);
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
- if(mCamera==null) return;
- Camera.Parameters parameters=mCamera.getParameters();
- parameters.setPreviewSize(640,480);
- parameters.setPictureSize(640,480);
- mCamera.setParameters(parameters);
- try{
- mCamera.startPreview();
- mSurfaceHolder=arg0;
- }catch(Exception e){
- Log.e(TAG, "could not start preview", e);
- mCamera.release();
- mCamera=null;
- }
- }
- });
- }
- @TargetApi(9)
- @Override
- protected void onResume(){
- super.onResume();
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD){
- mCamera=Camera.open(0);
- }else
- {
- mCamera=Camera.open();
- }
- }
- @Override
- protected void onPause(){
- super.onPause();
- flush();
- close();
- if(mCamera!=null){
- mCamera.release();
- mCamera=null;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- //JNI
- public native int initial(int width,int height);
- public native int encode(byte[] yuvimage);
- public native int flush();
- public native int close();
- static{
- System.loadLibrary("avutil-54");
- System.loadLibrary("swresample-1");
- System.loadLibrary("avcodec-56");
- System.loadLibrary("avformat-56");
- System.loadLibrary("swscale-3");
- System.loadLibrary("postproc-53");
- System.loadLibrary("avfilter-5");
- System.loadLibrary("avdevice-56");
- System.loadLibrary("encode");
- }
- }
至此,就完成了全部功能的开发,程序截图如下
转自: http://blog.csdn.net/nonmarking/article/details/48601317