您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
5. 完成XThread线程类IDemux继承后在线程中读取帧数据~1
发布时间:2021-06-09 11:10:34编辑:雪饮阅读()
用线程去读取帧数据,首先要在CMakeLists.txt中配置线程cpp
# 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
)
# 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} )
以及线程头文件: XThread.h:
然后要对待使用线程的类定义一个main方法:IDemux.cpp:
头文件IDemux.h中自是必不可少:
然后在入口cpp中测试线程的开启:native-lib.cpp:
魅族16T编译运行通过
#include "XLog.h"
#include <thread>
using namespace std;
//启动线程
void XThread::Start()
{
thread th(&XThread::ThreadMain,this);
th.detach();
}
void XThread::ThreadMain()
{
XLOGI("线程函数进入");
Main();
XLOGI("线程函数退出");
}
//通过控制isExit安全停止线程(不一定成功)
void XThread::Stop()
{
}
#define XPLAY_XTHREAD_H
//c++ 11 线程库
class XThread
{
public:
//启动线程
virtual void Start();
//通过控制isExit安全停止线程(不一定成功)
virtual void Stop();
//入口主函数
virtual void Main() {}
private:
void ThreadMain();
};
#endif //XPLAY_XTHREAD_H
#include "XLog.h"
void IDemux::Main()
{
for(;;)
{
XData d = Read();
XLOGI("IDemux Read %d",d.size);
if(d.size<=0)break;
}
}
#define XPLAY_IDEMUX_H
#include "XData.h"
#include "XThread.h"
//解封装接口
class IDemux:public XThread{
public:
//打开文件,或者流媒体 rmtp http rtsp
virtual bool Open(const char *url) = 0;
//读取一帧数据,数据由调用者清理
virtual XData Read() = 0;
//总时长(毫秒)
int totalMs = 0;
protected:
virtual void Main();
};
#endif //XPLAY_IDEMUX_H
#include <string>
#include "FFDemux.h"
#include "XLog.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
IDemux *de = new FFDemux();
de->Open("/sdcard/1080.mp4");
de->Start();
/*
for(;;)
{
XData d = de->Read();
XLOGI("Read data size is %d",d.size);
}
*/
return env->NewStringUTF(hello.c_str());
}
关键字词:XThread
相关文章
-
无相关信息