语言聊天中的压缩利器Speex
最近项目组在着手开发一个IM项目,即时聊天App.所以在工程师们正在搭建后台服务器的时候,我必须解决一些前端和后端交互数据的一些事宜,例如协议,心跳包等。关于语音聊天的难点在于语音的压缩加密。
参考了以下文章之后,决定是用Speex这个开源利器来压缩语音吧。
http://code.csdn.net/news/313194
Google了以下Speex,很多demo信息都是13年12年的,所以我决定去他们Speex官网下载一个新的下来重新经过NDK编译成.so的库。
配置Speex,参考:http://www.phonesdevelopers.com/1787731/
解压缩下载下来的压缩包,我下载的是:speex-1.2rc1.tar.gz,把文件中的include和libspeex文件夹复制到工程的jni中。
1.
在工程中新建Speex.java类,利用javah的命令生成头文件。
package com.park.speex;
public class Speex {
/* quality
* 1 : 4kbps (very noticeable artifacts, usually intelligible)
* 2 : 6kbps (very noticeable artifacts, good intelligibility)
* 4 : 8kbps (noticeable artifacts sometimes)
* 6 : 11kpbs (artifacts usually only noticeable with headphones)
* 8 : 15kbps (artifacts not usually noticeable)
*/
private static final int DEFAULT_COMPRESSION = 8;
public Speex() {
}
public void init() {
load();
open(DEFAULT_COMPRESSION);
}
private void load() {
try {
System.loadLibrary("speex");
} catch (Throwable e) {
e.printStackTrace();
}
}
public native int open(int compression);
public native int getFrameSize();
public native int decode(byte encoded[], short lin[], int size);
public native int encode(short lin[], int offset, byte encoded[], int size);
public native void close();
}
经过javaH之后生成了com_park_speex_Speex.h的头文件,接着编写speex.cpp文件用来真正实现压缩解压。
#include <jni.h>
#include <string.h>
#include <unistd.h>
#include <speex/speex.h>
static int codec_open = 0;
static int dec_frame_size;
static int enc_frame_size;
static SpeexBits ebits, dbits;
void *enc_state;
void *dec_state;
static JavaVM *gJavaVM;
extern "C"
JNIEXPORT jint JNICALL Java_com_park_speex_Speex_open
(JNIEnv *env, jobject obj, jint compression) {
int tmp;
if (codec_open++ != 0)
return (jint)0;
speex_bits_init(&ebits);
speex_bits_init(&dbits);
enc_state = speex_encoder_init(&speex_nb_mode);
dec_state = speex_decoder_init(&speex_nb_mode);
tmp = compression;
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &enc_frame_size);
speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &dec_frame_size);
return (jint)0;
}
extern "C"
JNIEXPORT jint Java_com_park_speex_Speex_encode
(JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) {
jshort buffer[enc_frame_size];
jbyte output_buffer[enc_frame_size];
int nsamples = (size-1)/enc_frame_size + 1;
int i, tot_bytes = 0;
if (!codec_open)
return 0;
speex_bits_reset(&ebits);
for (i = 0; i < nsamples; i++) {
env->GetShortArrayRegion(lin, offset + i*enc_frame_size, enc_frame_size, buffer);
speex_encode_int(enc_state, buffer, &ebits);
}
//env->GetShortArrayRegion(lin, offset, enc_frame_size, buffer);
//speex_encode_int(enc_state, buffer, &ebits);
tot_bytes = speex_bits_write(&ebits, (char *)output_buffer,
enc_frame_size);
env->SetByteArrayRegion(encoded, 0, tot_bytes,
output_buffer);
return (jint)tot_bytes;
}
extern "C"
JNIEXPORT jint JNICALL Java_com_park_speex_Speex_decode
(JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) {
jbyte buffer[dec_frame_size];
jshort output_buffer[dec_frame_size];
jsize encoded_length = size;
if (!codec_open)
return 0;
env->GetByteArrayRegion(encoded, 0, encoded_length, buffer);
speex_bits_read_from(&dbits, (char *)buffer, encoded_length);
speex_decode_int(dec_state, &dbits, output_buffer);
env->SetShortArrayRegion(lin, 0, dec_frame_size,
output_buffer);
return (jint)dec_frame_size;
}
extern "C"
JNIEXPORT jint JNICALL Java_com_park_speex_Speex_getFrameSize
(JNIEnv *env, jobject obj) {
if (!codec_open)
return 0;
return (jint)enc_frame_size;
}
extern "C"
JNIEXPORT void JNICALL Java_com_park_speex_Speex_close
(JNIEnv *env, jobject obj) {
if (--codec_open != 0)
return;
speex_bits_destroy(&ebits);
speex_bits_destroy(&dbits);
speex_decoder_destroy(dec_state);
speex_encoder_destroy(enc_state);
}
注意:cpp文件中的方法必须与头文件中的方法名称一致
在其他工程中复制Android.mk和Application.mk到jni文件夹下面。
Android.mk如下:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := speex
LOCAL_CFLAGS = -DFIXED_POINT -DUSE_KISS_FFT -DEXPORT="" -UHAVE_CONFIG_H
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := ./speex_jni.cpp ./libspeex/buffer.c ./libspeex/bits.c ./libspeex/cb_search.c ./libspeex/exc_10_16_table.c ./libspeex/exc_10_32_table.c ./libspeex/exc_20_32_table.c ./libspeex/exc_5_256_table.c ./libspeex/exc_5_64_table.c ./libspeex/exc_8_128_table.c ./libspeex/fftwrap.c ./libspeex/filterbank.c ./libspeex/filters.c ./libspeex/gain_table.c ./libspeex/gain_table_lbr.c ./libspeex/hexc_10_32_table.c ./libspeex/hexc_table.c ./libspeex/high_lsp_tables.c ./libspeex/jitter.c ./libspeex/kiss_fft.c ./libspeex/kiss_fftr.c ./libspeex/lpc.c ./libspeex/lsp.c ./libspeex/lsp_tables_nb.c ./libspeex/ltp.c ./libspeex/mdf.c ./libspeex/modes.c ./libspeex/modes_wb.c ./libspeex/nb_celp.c ./libspeex/preprocess.c ./libspeex/quant_lsp.c ./libspeex/resample.c ./libspeex/sb_celp.c ./libspeex/scal.c ./libspeex/smallft.c ./libspeex/speex.c ./libspeex/speex_callbacks.c ./libspeex/speex_header.c ./libspeex/stereo.c ./libspeex/vbr.c ./libspeex/vq.c ./libspeex/window.c
include $(BUILD_SHARED_LIBRARY)
注意:在LOCAL_SRC_FILES后面接的文件名称“\”后面不能跟空格,如下:
会发生这样的“jni/Android.mk:26: * missing separator. Stop.”的编译错误,这个错误说明在25行的“\”后面有空格了。
差不多了,可以进行编译了,利用cygdrive的ndk-build命令进行jni的编译,却发现报错:
jni/include/speex/speex_types.h:122:40: fatal error: speex/speex_config_types.h: No such file or directory
没有找到speex_config_types.h的文件。
因此就去include\speex看看,发现只有一个命名为:speex_config_types.h.in的文件。此时我心里暗喜了一下,这都给我找到,这次应该可以编译成功了吧。我把这个.in的文件直接复制命名为:speex_config_types.h,再NDK,再报错,我XXXXXXXX.
报错信息为:jni/include/speex/speex.h:325:35: error: ‘spx_int16_t’ has not been declared
说明这个文件里面的“spx_int16_t”还未进行声明就进行运用了。打开这个speex_config_types.h.in文件一看,果然存在一个叫“typedef @SIZE16@ spx_int16_t;”的东西,那么如何改呢?参考了上述的文章,我直接复制他们的speex_config_types.h文件内容过来,如下:
#ifndef __SPEEX_TYPES_H__
#define __SPEEX_TYPES_H__
/* these are filled in by configure */
typedef short spx_int16_t;
typedef unsigned short spx_uint16_t;
typedef int spx_int32_t;
typedef unsigned int spx_uint32_t;
#endif
再进行NDK的编译,终于顺利的生成了libspeex.so文件了。
如果想了解这个C写的Speex可以这篇文章来重新写压缩的和解压的算法,有注释说明。
【Tech-Speex】语言聊天中的压缩利器Speex【1】
原文地址:http://blog.csdn.net/a_asinceo/article/details/44804829