ffmpeg 要支持ogg编解码的话必须集成libogg库和libvorbis库,其中libvorbis 要拆分为libvorbis和libvorbisenc两个库,因为ffmpeg 编译时需要这两个,其中编译这三个库的eclipse工程下载地址为
:https://github.com/XIAIBIANCHENG/AndroidLibvorbis
ffmpeg 集成libogg、libvorbis和libvorbisenc编译文件:
#!/bin/bash
NDK="/home/wh/android-ndk-r12b"
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
export PREFIX=/home/wh/faac-1.28/install
function build_one
{
./configure --prefix=$PREFIX \
--disable-shared \
--enable-static \
--disable-debug \
--disable-doc \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--disable-encoders \
--disable-muxers \
--disable-demuxers \
--disable-parsers \
--disable-bsfs \
--disable-protocols \
--disable-indevs \
--disable-outdevs \
--disable-filters \
--disable-decoders \
--enable-small \
--enable-asm \
--enable-nonfree \
--enable-neon \
--enable-gpl \
--enable-encoder=libfdk_aac \
--enable-decoder=libfdk_aac \
--enable-libfdk-aac \
--enable-muxer=adts \
--enable-encoder=vorbis \
--enable-muxer=ogg \
--enable-libvorbis \
--disable-encoder=libfaac \
--enable-protocol=file \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=android \
--arch=arm \
--disable-libfaac \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS -mfpu=neon" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm -I/home/wh/FFmpeg-n3.1.3/fdkacc/include/ -I/home/wh/FFmpeg-n3.1.3/libvorbis/include/ -DANDROID"
ADDI_LDFLAGS="-L/home/wh/FFmpeg-n3.1.3/fdkacc/lib/ -L/home/wh/FFmpeg-n3.1.3/libvorbis/lib/ "
CPU=armv5te
OPTIMIZE_CFLAGS="-marm -march=$CPU"
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG="--disable-armv6 --disable-armv6t2 --disable-vfp --disable-neon"
build_one
#arm v7n
CPU=armv7-a
OPTIMIZE_CFLAGS="-marm -march=$CPU"
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=--enable-neon
build_one
将相应的链接文件路径改成你的路径其中包含了fdkaac的集成,fdkaac的集成编译参见:http://blog.csdn.net/xiaibiancheng/article/details/71293606,如果你不需要fdkaac可以在上面的编译文件中disable 掉相应的aac编译项。include 目录存放的文件如下:
这几个头文件就是上面工程里面的,直接拷贝过来。
lib存放的文件如下:
这三个静态库文件就是上面工程编译产生的。直接拷贝过来。
将上面生成ffmpeg 库拷贝到工程里面进行使用。
Audiorecord 初始化代码
minBufferSize = 4096;
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
采集
while (isRecording == true) {
byte[] bytes = new byte[minBufferSize];
if (audioRecord == null)
return;
int res = audioRecord.read(bytes, 0, minBufferSize);
if (res > 0 && isRecording == true) {
AacRecord.writeAudioData(bytes,bytes.length);
}
}
底层编码代码
int Java_com_example_aacrecord_AacRecord_writeAudioData(JNIEnv* env, jobject thiz, jbyteArray pcmData,jint len){
uint8_t* srcBuf = (uint8_t*) (*env)->GetByteArrayElements(env, pcmData, 0);
av_opt_set_int(swr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
av_opt_set_int(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
av_opt_set_int(swr, "in_sample_rate", 16000, 0);
av_opt_set_int(swr, "out_sample_rate", 16000, 0);
av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
swr_init(swr);
uint8_t *outs[2];
outs[0]=(uint8_t *)malloc(len);
outs[1]=(uint8_t *)malloc(len);
int count=swr_convert(swr,&outs,len*4,&srcBuf,len/4);
audioFrame->data[0] =(uint8_t*)outs[0];
audioFrame->data[1] =(uint8_t*)outs[1];
int got_frame = 0;
int ret = avcodec_encode_audio2(audioStream->codec, &audioPkt, audioFrame, &got_frame);
if (ret < 0){
LOGE("encoder failed");
}
if (got_frame==1){
audioPkt.stream_index = 0;
ret = av_interleaved_write_frame(outputCtx, &audioPkt);
}
av_free_packet(&audioPkt);
(*env)->ReleaseByteArrayElements(env, pcmData, srcBuf, 0);
free(outs[0]);
free(outs[1]);
return 1;
}
源码下载地址:
https://github.com/XIAIBIANCHENG/OggRecord/tree/master