您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
6. 完成音频重采样IResample模块初始化代码~1
发布时间:2021-06-11 17:00:27编辑:雪饮阅读()
音频重采样,首先需要在继承Iobserver然后主要以实现Resample为主:cpp/IResample.h:
#define XPLAY_IRESAMPLE_H
#include "XParameter.h"
#include "IObserver.h"
class IResample: public IObserver
{
public:
virtual bool Open(XParameter in,XParameter out=XParameter()) = 0;
virtual XData Resample(XData indata) = 0;
virtual void Update(XData data);
int outChannels = 2;
int outFormat = 1;
};
#endif //XPLAY_IRESAMPLE_H
然后对于Open和Resample单独规划出来声明定义于cpp/FFResample.h:
然后最后就是具体的重采样实现cpp/FFResample.cpp:
然后用cpp/CMakeLists.txt重新链接下新的c++:
#include "XLog.h"
void IResample::Update(XData data)
{
XData d = this->Resample(data);
if(d.size > 0)
{
this->Notify(d);
}
}
#define XPLAY_FFRESAMPLE_H
#include "IResample.h"
struct SwrContext;
class FFResample: public IResample
{
public:
virtual bool Open(XParameter in,XParameter out=XParameter());
virtual XData Resample(XData indata);
protected:
SwrContext *actx = 0;
};
#endif //XPLAY_FFRESAMPLE_H
{
#include <libswresample/swresample.h>
}
#include "XLog.h"
#include <libavcodec/avcodec.h>
#include "FFResample.h"
bool FFResample::Open(XParameter in,XParameter out)
{
//音频重采样上下文初始化
actx = swr_alloc();
actx = swr_alloc_set_opts(actx,
av_get_default_channel_layout(2),
AV_SAMPLE_FMT_S16,in.para->sample_rate,
av_get_default_channel_layout(in.para->channels),
(AVSampleFormat)in.para->format,in.para->sample_rate,
0,0 );
int re = swr_init(actx);
if(re != 0)
{
XLOGE("swr_init failed!");
return false;
}
else
{
XLOGI("swr_init success!");
}
outChannels = in.para->channels;
outFormat = AV_SAMPLE_FMT_S16;
return true;
}
XData FFResample::Resample(XData indata)
{
if(indata.size<=0 || !indata.data) return XData();
if(!actx)
return XData();
//XLOGE("indata size is %d",indata.size);
AVFrame *frame = (AVFrame *)indata.data;
//输出空间的分配
XData out;
int outsize = outChannels * frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)outFormat);
if(outsize <=0)return XData();
out.Alloc(outsize);
uint8_t *outArr[2] = {0};
outArr[0] = out.data;
int len = swr_convert(actx,outArr,frame->nb_samples,(const uint8_t **)frame->data,frame->nb_samples);
if(len<=0)
{
out.Drop();
return XData();
}
XLOGE("swr_convert success = %d",len);
return out;
}
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("xplay")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#添加頭文件路徑(括號中的include是相對於本文件路徑)
include_directories(../../../include)
#設置ffmpeg庫所在路徑的變量,這裏的FF是自定義的一個名字
set(FF ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})
#avcodec這個是自定義的一個名字
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${FF}/libavutil.so)
add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${FF}/libswscale.so)
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
#src/main/cpp/native-lib.cpp
IDemux
FFDemux
XData
XLog
native-lib.cpp
XThread
IObserver
FFDecode
IDecode
XParameter
IVideoView
GLVideoView
XTexture
XEGL
XShader
FFResample
IResample
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
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 )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
GLESv2 EGL
OpenSLES
android
avcodec avformat avutil swscale swresample
# Links the target library to the log library
# included in the NDK.
${log-lib} ) Alloc暂时先不用实现。
关键字词:IResample
相关文章
-
无相关信息