关闭

Android移植ffmpeg.so实现屏幕录制功能(上)

标签: Android移植ffmpeg.soffmpeg 图片声音合成视频android屏幕录制
4936人阅读 评论(13) 收藏 举报
分类:

   第一次写博客,为了提高自己技术水平,为了帮助更多走过弯路的人们,也为了以前自己开发过程过遇到的棘手的问题做个记录,希望各位路过的大牛别见笑了!

   前段日子android项目中需要做解码视频取帧播放,还有当前应用屏幕录制功能,一下子蒙了,之前没有做过多媒体之类的东西,亚历山大啊,没有办法,只能硬着头皮搞了,经过多方面的Google,度娘,YaHoo,sos...,终于经过万水千山,跋山涉水,万里长城~(废话多了,就是想表达不容易!)。好了废话不多说,上代码...

编译环境:ubuntu 12.10 32位(虚拟

NDK版本:android-ndk-r9b  

ffmpeg版本:1.2

一 ,环境配置

1、下载NDK同时配置NDK环境NDK下载下载ffmpeg-1.2源码ffmpeg-1.2下载

2、把ffmpeg解压到一个NDK文档的samples/hello-jni 目录(先删除原来 hello-jni.c 和Android.mk文件)  如下图:

3、在jni目录下添加如下文件Android.mk ,内容如下:

include $(all-subdir-makefiles) 

4,然后在jni/ffmpeg下建立Android.mk和av.mk文件,内容如下:

Android.mk

 LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
 PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg  
LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)   
 LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavfilter libavutil libpostproc libswscale libswresample  
 LOCAL_MODULE := ffmpeg 
include $(BUILD_SHARED_LIBRARY)  
include $(call all-makefiles-under,$(LOCAL_PATH))
av.mk
# LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale  
  
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak  
  
include $(LOCAL_PATH)/../config.mak  
  
OBJS :=  
  
OBJS-yes :=  
  
MMX-OBJS-yes :=  
  
include $(LOCAL_PATH)/Makefile  
  
# collect objects  
  
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)  
  
OBJS += $(OBJS-yes)  
  
FFNAME := lib$(NAME)  
  
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))  
  
FFCFLAGS  = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign  
  
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"  
  
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)  
  
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))  
  
ifneq ($(ALL_S_FILES),)  
  
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))  
  
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))  
  
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))  
  
else  
  
C_OBJS := $(OBJS)  
  
S_OBJS :=  
  
endif  
  
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))  
  
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))  
  
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))  


5,同样在jni/ffmpeg目录下建立build_android.sh文件,内容如下:

#!/bin/bash
PREBUILT=/home/lsw/android-ndk-r9b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
PLATFORM=/home/lsw/android-ndk-r9b/platforms/android-14/arch-arm

./configure --target-os=linux \
--arch=arm \
--enable-version3 \
--enable-gpl \
--enable-nonfree \
--enable-shared \
--enable-stripping \
--enable-ffmpeg \
--disable-ffplay \
--disable-ffserver \
--disable-ffprobe \
--enable-decoders \
--disable-symver \
--enable-encoders \
--enable-muxers \
--disable-devices \
--enable-protocols \
--enable-protocol=file \
--enable-avfilter \
--enable-network \
--disable-avdevice \
--disable-asm \
--enable-cross-compile \
--cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
--cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
--strip=$PREBUILT/bin/arm-linux-androideabi-strip \
--extra-cflags="-fPIC -DANDROID" \
--extra-ldflags="-Wl,-T,$PREBUILT/arm-linux-androideabi/lib/ldscripts/armelf_linux_eabi.x -Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtbegin.o $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtend.o -lc -lm -ldl" \

sed -i 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
sed -i 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
sed -i 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
sed -i 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
sed -i 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
sed -i 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
sed -i 's/HAVE_CBRT 0/HAVE_CBRT 1/g' config.h
sed -i 's/HAVE_CBRTF 0/HAVE_CBRTF 1/g' config.h
sed -i 's/HAVE_ISINF 0/HAVE_ISINF 1/g' config.h
sed -i 's/HAVE_ISNAN 0/HAVE_ISNAN 1/g' config.h
sed -i 's/HAVE_SINF 0/HAVE_SINF 1/g' config.h
sed -i 's/HAVE_RINT 0/HAVE_RINT 1/g' config.h
# collect objects OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)  OBJS += $(OBJS-yes) FFNAME := lib$(NAME) FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME)) FFCFLAGS  = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\" ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)  ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))ifneq ($(ALL_S_FILES),) ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES)) C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS)) S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))  else   C_OBJS := $(OBJS) S_OBJS := endif C_FILES := $(patsubst %.o,%.c,$(C_OBJS)) S_FILES := $(patsubst %.o,%.S,$(S_OBJS)) FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))<strong>
</strong>
上面PREBUILT,PLATFORM变量 的路径自己根据NDK的路径自己更改。

6,在jni/ffmpeg/libavformat下添加Android,mk内容如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES := \
$(LOCAL_PATH) \
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)



7,在jni/ffmpeg/libavcodec下添加Android,mk内容如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=        \
$(LOCAL_PATH)        \
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)


8,在jni/ffmpeg/libavutil libavfilter libpostproc libswscale libswresample 下添加Android,mk内容如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES := \
$(LOCAL_PATH) \
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

9,执行build_android.sh
A,chmod 755 build_android.sh  确保文件有权限被执行
B,然后执行如下命令 ./build_android.sh
就几秒时间在最后会出现如下文字:
WARNING: /home/lsw/android-ndk-r9b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-pkg-config not found, library detection may fail.
不用管它。
在看ffmpeg目录下生成config.h,config.log,config.mak文件
C,然后修改jni/ffmpeg/config.h下的 
#define avrestrict restrict 为  #define av_restrict  
D,把config.log中的restrict的关键字删掉
修改jni/ffmpeg/config.h下的
HAVE_ISINF 0
HAVE_ISNAN 0
HAVE_SINF 0
HAVE_RINT 0
HAVE_LRINT 0
HAVE_LRINTF 0 
HAVE_ROUND 0 
HAVE_ROUNDF 0
HAVE_TRUNCF 0 
HAVE_TRUNC 0
HAVE_CBRT 0
HAVE_CBRTF 0
的值修改为1,例如:HAVE_TRUNC 1 
同时相应的修改jni/ffmpeg/config.mak 的值 
例如:!HAVE_LRINT =yes为HAVE_LRINT =yes 
上面修改也可以用下面方法替换*注释掉/jni/ffmpeg/libavutil 目录中的 libm.h下的所有静态(static)方法 


10,删除 libavformat libavcodec libavutil libpostproc libswscale libswresample 目录下Makefile下的:
include $(SUBDIR)../config.mak
  删除libavcodec libavutil libswresample目录下Makefile下的 log2_tab.o \ 


11,把 ffmpeg/libavutil/time.h更名为avtime.h:
同时修改下面文件中的引用libavutil/time.h为libavutil/avtime.h 
ffmpeg/libavformat/avformat.h:211 
ffmpeg/libavformat/avio.c:25 
ffmpeg/libavformat/hls.c:33 
ffmpeg/libavformat/hlsproto.c:29
ffmpeg/libavformat/mux.c:39:30
ffmpeg/libavformat/utils.c:40 
ffmpeg/libavutil/time.c:36
ffmpeg/libavfilter/f_setpts.c:30:28:
注:上面需要修改avtime.h文件引用的部分文件。根据版本,环境不同可能还会出现其他的文件引用time.h,如果当你编译的时候说找不到time.h,你就可以根据日志显示的文件逐个修改。很好解决的。


12,可能会引发version.h   No such file or directory  问题,可执行如下命令生成version.h:
./version.sh . version.h


13,终于到最后编译so库了,好紧张!
cd 到 samples/hello-jni     然后执行 ndk-build   
这样就在libs/armeabi的文件夹,里面会有一个libffmpeg.so的文件


下面我介绍在编译过程中遇到的问题: 
出现/home/lsw/android-ndk-r9b/build/core/build-binary.mk:109 和build-all.mk:129 *** target file `clean' has both : and :: entries.  Stop.
解决办法:找到build-binary.mk提示错误那一行,把那一行注释掉。




1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30427次
    • 积分:439
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:6篇
    • 译文:1篇
    • 评论:20条
    文章分类
    最新评论