Android开发学习之路--Android Studio cmake编译ffmpeg
最新的android studio2.2引入了cmake可以很好地实现ndk的编写。这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路–NDK、JNI之初体验。
1.ffmpeg编译
进入正题,既然是ffmpeg的移植编译,那么就先下载ffmpeg,https://ffmpeg.org/download.html#releases。这里下载的是3.0.3版本。
新建ffmpeg文件夹,然后新建脚本用来编译ffmpeg,命名为build.sh,脚本如下:
#!/bin/bash
cd ffmpeg
export TMPDIR=/Users/jared/Desktop/jared/study/external/ffmpeg/tempdir
NDK=/Users/jared/Desktop/jared/software/sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-16/arch-arm/
TOOLCHAIN=/Users/jared/Desktop/jared/software/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=arm
PREFIX=/Users/jared/Desktop/jared/study/external/ffmpeg/output
ADDI_CFLAGS="-marm"
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-doc \
--disable-symver \
--enable-small \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
cd ../
注意的是这里的路径是对应的ndk的相关路径。
然后chmod 777 build.sh,然后执行source build.sh。开始编译ffmpeg。小憩片刻之后,编译完成,在output目录下就会生成对应的so文件:
➜ ffmpeg cd output
➜ output ls
include lib
➜ output cd lib
➜ lib ls
libavcodec-57.so libavfilter-6.so libavutil-55.so libswscale-4.so
libavcodec.so libavfilter.so libavutil.so libswscale.so
libavdevice-57.so libavformat-57.so libswresample-2.so pkgconfig
libavdevice.so libavformat.so libswresample.so
➜ lib
编译完成,这里只是用了arm的平台,如果需要mips,或者x86需要修改build.sh脚本的arch和路径这里不赘述了。
既然库文件都已经编译出来了那就可以android开搞了。
2.android环境搭建
首先需要最新的android studio2.2,并且安装好cmake和ndk。然后新建工程,可以开始了。
新建工程取名为helloffmpeg,然后选中include c++ support,然后下一步直到新建完成为止。如下图:
然后比一般的工程多了些东西,首先是build.gradle了,
CMake的cpp的flags
externalNativeBuild {
cmake {
cppFlags ""
}
}
CMake的路径
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
然后看CMakeLists.txt文件,这里主要讲解下配置。先看下已经配置好的库文件:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
add_library( avutil-55
SHARED
IMPORTED )
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavutil-55.so )
add_library( swresample-2
SHARED
IMPORTED )
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswresample-2.so )
add_library( avcodec-57
SHARED
IMPORTED )
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavcodec-57.so )
add_library( avfilter-6
SHARED
IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavfilter-6.so )
add_library( swscale-4
SHARED
IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswscale-4.so )
add_library( avdevice-57
SHARED
IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavdevice-57.so )
add_library( avformat-57
SHARED
IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavformat-57.so )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
include_directories(libs/include)
#target_include_directories(native-lib PRIVATE libs/include)
target_link_libraries( native-lib swresample-2 avcodec-57 avfilter-6 swscale-4 avdevice-57 avformat-57
${log-lib} )
cmake_minimum_required(VERSION 3.4.1):表示cmake的最低版本是3.4.1。
add_library():添加库,分为两种,一种是需要编译为库的代码,一种是已经编译好的库文件。
比如上面编译好的ffmpeg的库,
add_library( avutil-55
SHARED
IMPORTED )
这里avutil-55表示库的名称,SHARED表示是共享库,一般.so文件,还有STATIC,一般.a文件。IMPORTED表示引用的不是生成的。
接着是这里用到的需要生产的.so文件
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
最后的参数是源码的路径,如果有更多的源码就接下去写上。
set_target_properties:对于已经编译好的so文件需要引入,所以需要设置。
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavutil-55.so )
这里avutil-55是名字,然后是PROPERTIES IMPORTED_LOCATION加上库的路径。
include_directories:一般外面引入的库文件需要头文件,所以可以通过这个来引入:
include_directories(libs/include)
target_link_libraries:链接,把需要的so文件链接起来,这里native-lib需要链接ffmpeg的库文件。
最后看下和java,res同级的cpp目录下有native-lib.cpp文件,系统自动生成的。
首先是native-lib.cpp内容:
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_jared_helloffmpeg_MainActivity_stringFromJNI(
JNIEnv *env, jobject) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
这里有一个函数Java_com_jared_helloffmpeg_MainActivity_stringFromJNI,命名方式也可以知道,Java开头,com_jared_helloffmpeg为包名,MainActivity是调用的java类,最后stringFromJNI是方法名。然后返回了hello from c++字符。
看下java的代码:
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(avformatinfo());
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
// Used to load the 'native-lib' library on application startup.
static {
loadLibrary("native-lib");
}
也很好理解这里的stringFromJNI的native方法,具体需要看jni的基本概念了,这里就不多赘述了。好了基本上最简单的通了,那么接下去就是ffmpeg的使用了。
3.修改使用ffmpeg
接着来修改界面显示ffmpeg的一些基本信息。如下:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="MainBinding"></data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jared.helloffmpeg.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_protocol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Protocol"
android:textAllCaps="false" />
<Button
android:id="@+id/btn_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Format"
android:textAllCaps="false" />
<Button
android:id="@+id/btn_codec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Codec"
android:textAllCaps="false" />
<Button
android:id="@+id/btn_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Filter"
android:textAllCaps="false" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
</ScrollView>
</LinearLayout>
</layout>
主要是实现了四个button,分别是protocol,format,codec和filter。如下图所示:
修改jni部分的代码,也就是native-lib.cpp的代码如下:
#include <jni.h>
#include <string>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
jstring
Java_com_jared_helloffmpeg_MainActivity_stringFromJNI(
JNIEnv *env, jobject) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_com_jared_helloffmpeg_MainActivity_urlprotocolinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
av_register_all();
struct URLProtocol *pup = NULL;
struct URLProtocol **p_temp = &pup;
avio_enum_protocols((void **) p_temp, 0);
while ((*p_temp) != NULL) {
sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 0));
}
pup = NULL;
avio_enum_protocols((void **) p_temp, 1);
while ((*p_temp) != NULL) {
sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 1));
}
return env->NewStringUTF(info);
}
jstring
Java_com_jared_helloffmpeg_MainActivity_avformatinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
while (if_temp != NULL) {
sprintf(info, "%sInput: %s\n", info, if_temp->name);
if_temp = if_temp->next;
}
while (of_temp != NULL) {
sprintf(info, "%sOutput: %s\n", info, of_temp->name);
of_temp = of_temp->next;
}
return env->NewStringUTF(info);
}
jstring
Java_com_jared_helloffmpeg_MainActivity_avcodecinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
av_register_all();
AVCodec *c_temp = av_codec_next(NULL);
while (c_temp != NULL) {
if (c_temp->decode != NULL) {
sprintf(info, "%sdecode:", info);
} else {
sprintf(info, "%sencode:", info);
}
switch (c_temp->type) {
case AVMEDIA_TYPE_VIDEO:
sprintf(info, "%s(video):", info);
break;
case AVMEDIA_TYPE_AUDIO:
sprintf(info, "%s(audio):", info);
break;
default:
sprintf(info, "%s(other):", info);
break;
}
sprintf(info, "%s[%10s]\n", info, c_temp->name);
c_temp = c_temp->next;
}
return env->NewStringUTF(info);
}
jstring
Java_com_jared_helloffmpeg_MainActivity_avfilterinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
avfilter_register_all();
AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);
while(f_temp != NULL) {
sprintf(info, "%s%s\n", info, f_temp->name);
f_temp = f_temp->next;
}
return env->NewStringUTF(info);
}
}
这里要特别注意ffmpeg的头文件已经调用的代码要extern “C”里面,要不然会报错误的。
最后就是java调用代码了。如下所示:
package com.jared.helloffmpeg;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.jared.helloffmpeg.databinding.MainBinding;
import static java.lang.System.loadLibrary;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.btnProtocol.setOnClickListener(this);
binding.btnCodec.setOnClickListener(this);
binding.btnFilter.setOnClickListener(this);
binding.btnFormat.setOnClickListener(this);
// Example of a call to a native method
//TextView tv = (TextView) findViewById(R.id.sample_text);
//tv.setText(avformatinfo());
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_protocol:
binding.tvInfo.setText(urlprotocolinfo());
break;
case R.id.btn_format:
binding.tvInfo.setText(avformatinfo());
break;
case R.id.btn_codec:
binding.tvInfo.setText(avcodecinfo());
break;
case R.id.btn_filter:
binding.tvInfo.setText(avfilterinfo());
break;
default:
break;
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String urlprotocolinfo();
public native String avformatinfo();
public native String avcodecinfo();
public native String avfilterinfo();
// Used to load the 'native-lib' library on application startup.
static {
loadLibrary("native-lib");
}
}
都准备好了,那么就继续运行看看结果了:
基本上Android Studio下的cmake编译ffmpeg就到此结束了,那么接下去就可以在android手机上使用ffmpeg来做一些音视频相关的东西了。
Android开发学习之路--Android Studio cmake编译ffmpeg的更多相关文章
- Android开发学习之路--Android系统架构初探
环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...
- Android开发学习之路-Android Studio开发小技巧
上一次发过了一个介绍Studio的,这里再发一个补充下. 我们都知道,Android Studio的功能是非常强大的,也是很智能的.如果有人告诉你学Android开发要用命令行,你可以告诉他Andro ...
- Android开发学习之路--Android Studio项目目录结构简介
既然已经搭建好环境了,那就对Android Studio中项目目录结构做个简单的了解了,这里以最简单的Hello工程为例子,新建好工程后看如下三个工程视图: 1.Android工程 manifests ...
- Android开发学习之路-Android中使用RxJava
RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...
- Android开发学习之路--逆向分析反编译
一般情况下我们想要了解别人的app怎么实现这个动画,这个效果的时候,总是会想到反编译一下,看下布局,看下代码实现.对,这对于有经验的玩家确实手到擒来了,但是初学者,根本就不知道怎么反编译,怎么看代 ...
- Android开发学习之路-Android N新特性-多窗口模式
我们都知道,在最新的Android N系统中,加入了一个新的功能,就是多窗口模式.多窗口模式允许我们在屏幕上显示两个窗口,每个窗口显示的内容不同,也就是说,我们可以一遍看电视剧,一边聊微信. 这里我们 ...
- Android开发学习之路-Android Design Support Library使用(CoordinatorLayout的使用)
效果图: 上面的这个图有两个效果是,一个是顶部的图片,在上滑之后会隐藏起来并且显示出一个ToolBar(ToolBar类似于ActionBar,但是只有ToolBar是兼容Material Desig ...
- Android开发学习之路-Android Studio真神器!
放假之后电脑配置升级就开始用Android Studio(下面简称AS)了,那个酸爽真的不是一般的啊,这里开一篇博客来记录下AS里面各种酷炫的功能,有更好玩的,大家不要吝啬,评论告诉我吧! 最近And ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
随机推荐
- mysql新建表设置为utf8
CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- 学习React系列(一)——React.Component 生命周期
挂载中(只执行一次) 以下方法在组件实例正被创建和插入到DOM中时调用 constructor()一般用于初始化state和方法的this绑定 componentWillMount() render( ...
- java基本数据类型的包装类
基本类型对应的包装类 byte(Byte).short(Short).int(Integer).long(Long).float(Float).double(Double).char(Characte ...
- python学习笔记1--python简介和第一行代码编写
一.什么是python? python是一种面向对象,解释型语言,它语法简介,容易学习.本节博客就来说说本人学习python的心得体会. 二.python环境安装 目前python版本有python2 ...
- [NOI 2005]聪聪和可可
Description 题库链接 一只猫和一只老鼠在一张 \(n\) 个节点和 \(m\) 条边的无向图上,初始位置不同.对于每一时刻,猫会先走,它走的方向为靠近老鼠的方向:若多个节点可选,则选字典序 ...
- [Vijos 1143]三取方格数
Description 设有N*N的方格图,我们将其中的某些方格填入正整数, 而其他的方格中放入0. 某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角. 在走过的路上,他取走了方格中的数 ...
- Conjugate
1.1Conjugate问题描述在不存在的 noip day3 里,小 w ⻅到了一堆堆的谜题.比如这题为什么会叫共轭?他并不知道答案.有 n 堆谜题,每堆有 a i 个,小 w 每次从剩下的谜题中选 ...
- ●POJ 2284 That Nice Euler Circuit
题链: http://poj.org/problem?id=2284 题解: 计算几何,平面图的欧拉定理 欧拉定理:设平面图的定点数为v,边数为e,面数为f,则有 v+f-e=2 即 f=e-v+2 ...
- 后缀自动机模板(SPOJ1811)
用后缀自动机实现求两个串的最长公共子串. #include <cstdio> #include <algorithm> ; char s[N]; ]; int main() { ...
- splay模板(BZOJ3224)
用splay实现二叉搜索树的模板,支持插入,删除,找前缀后缀,x的排名以及第x名的数. #include <cstdio> #define l(x) t[x].s[0] #define r ...