没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Developing Multimedia
Applications with NDK
In this chapter, we will cover the following recipes:
f Porting the ffmpeg library to Android with NDK
f Using the ffmpeg library to get media info
f Decoding and displaying the video frame
f Separating decoding and drawing with two threads
f Seeking to playback and grabbing the frames
f Optimizing the performance of multimedia apps
Introduction
Many multimedia Android applications use NDK, especially video applications. If you go
through the top 50 apps under the Media and Video category, you will easily nd a few apps
using NDK, including MX Player, VLC for Android Beta, PPS, PPTV, and so on.
There are a few advantages for using NDK in multimedia apps. Firstly, multimedia apps, and
video apps in particular, are usually CPU-intensive. Developing in native languages offers the
potential to achieve better performance. Secondly, NDK allows us to detect CPU features and
you can take advantage of the CPU features offered by a particular device. For example, NEON
is a general-purpose Single Instruction Multiple Data (SIMD) engine available on many ARM
CPUs. It can speed up some media processing tasks (for example, color conversion) several
times. You can develop your apps in such a way that if NEON is available, it will use the NEON-
specic code. Thirdly, multimedia apps usually require manipulation of bits and bytes, moving
chunks of memory around, and so on. Native languages such as C/C++ are better at those
tasks than Java. Last but not least, Android only offers support for a few video/audio codecs.
There are a lot of open source codec libraries available and most of them are in native
languages. You can port them to Android using NDK to enable support for additional codecs.
Developing Multimedia Applications with NDK
2
This chapter will discuss how to develop multimedia apps in Android NDK. As multimedia
apps are usually complicated, you can only cover the development of a relatively simple
frame grabber application in the chapter. But, it should be a good starting point.
The frame grabber app you're going to develop will be able to play the video frames from
a video le and save the frames being displayed to pictures. It is based on the well-known
ffmpeg library. We will rst port the ffmpeg library to Android with NDK. After that, you will
introduce the basics of reading a video le and initializing the codec context. The next step
is to decode, scale, and display the video frame. We will see how to do this with the Android
SurfaceView class, bitmap, and ffmpeg. We will also illustrate dividing the decoding,
scale, and display tasks into two threads, performing random seek at playback, and
saving the video frame to pictures. The last recipe will discuss the techniques to
optimize the performance.
The frame grabber app uses the ActionBarSherlock library for the GUI. You will link to
the library at all your sample projects in this chapter. The ActionBarSherlock library is
available at http://actionbarsherlock.com/. The detailed instruction is covered in
the second recipe.
Porting the ffmpeg library to Android with
NDK
The ffmpeg library is not available on Android by default. You will need to port the ffmpeg
library in order to use it.
Getting ready
This recipe uses many of the techniques covered Chapter 8, Porting and Using Existing
Libraries with Android NDK. The following recipes should be reviewed rst in particular:
f Porting a library with its existing build system
f Using a library in multiple projects with import-module
How to do it...
The following steps describe how to build a ffmpeg library for Android with NDK:
1. Go to http://ffmpeg.org/download.html, and download the ffmpeg source
code. In this example, you used ffmpeg 1.0.1 release. Extract the downloaded archive
to the sources folder of our Android NDK directory. The extracted les will be put into
the sources/ffmpeg-1.0.1 folder.
Bonus Chapter 1
3
Note that the ffmpeg library is updated frequently. Hoyouver, the techniques
and scripts mentioned in this recipe should work on your versions of ffmpeg.
You should try it out on the latest stable release of the library.
2. Add a build_android.sh script under the sources/ffmpeg-1.0.1 folder to
compile the ffmpeg libraries. Note that you need to update the NDK variable in the
script to point to the correct location in our computer.
#!/bin/bash
NDK=/home/roman10/Desktop/android/android-ndk-r8b
SYSROOT=$NDK/platforms/android-8/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/
linux-x86
function build_one
{
./configure \
--prefix=$PREFIX \
--disable-shared \
--enable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--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
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
Developing Multimedia Applications with NDK
4
3. Start a command line shell, go to the sources/ffmpeg-1.0.1 folder, and enter the
following command to build ffmpeg:
$ chmod +x build_android.sh
$ ./build_android.sh
The build will take quite a while to nish. After it is done, you can nd the library
headers under the sources/ffmpeg-1.0.1/android/arm/include directory,
and library les under the sources/ffmpeg-1.0.1/android/arm/lib directory.
4. Add an Android.mk le under the sources/ffmpeg-1.0.1/android/arm folder:
LOCAL_PATH:= $(call my-dir)
#static version of libavcodec
include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec_static
LOCAL_SRC_FILES:= lib/libavcodec.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavformat
include $(CLEAR_VARS)
LOCAL_MODULE:= libavformat_static
LOCAL_SRC_FILES:= lib/libavformat.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
#static version of libswscale
include $(CLEAR_VARS)
LOCAL_MODULE:= libswscale_static
LOCAL_SRC_FILES:= lib/libswscale.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavutil
include $(CLEAR_VARS)
LOCAL_MODULE:= libavutil_static
LOCAL_SRC_FILES:= lib/libavutil.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
Bonus Chapter 1
5
How it works...
The previous steps show how to build ffmpeg for Android and prepare it to be used by
Android applications.
The idea is to build the ffmpeg library and allow multiple projects to link to it as prebuilt
libraries with import-module. You put the ffmpeg library source code under the sources folder
of the Android NDK directory. As discussed in the Using a library in multiple projects with
import-module recipe in Chapter 8, Porting and Using the Existing Libraries with Android
NDK, the sources directory is appended to NDK_MODULE_PATH by NDK build system.
The build script at the second step cross compiles ffmpeg library using the NDK toolchain
directly. The ffmpeg build scripts accept lots of conguration options for controlling the build
process. Our script enables static libraries and disables a few components you don't want. You
can execute the ./configure --help command under the ffmpeg-1.0.1 directory to
view all conguration options and ne-tune the build process.
The compilation will produce library les and headers for six ffmpeg libraries, including
libavcodec, libavlter, libavformat, libavutil, libswresample and libswscale.
After compilation is done, you provide an Android.mk le in step 4 for an Android application
project to easily import the ffmpeg libraries as static prebuilt library modules. You will see how
to use the ffmpeg libraries in subsequent recipes.
Using the ffmpeg library to get media info
Starting from this recipe, you will show how to use the ffmpeg libraries to develop multimedia
applications. This recipe will discuss how to obtain various information of a video le.
Getting ready
This recipe and subsequent recipes are based on a project template available from
the book's youbsite. Please download the source code for this book and locate the
FrameGrabberTemplate folder in the folder Bonus chapter 1.
剩余35页未读,继续阅读
lcl81
- 粉丝: 14
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1