您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
7. 观察者Observer模式的代码实现并使用IDemux进行测试~1
发布时间:2021-06-09 14:50:56编辑:雪饮阅读()
先实现观察者服务好吧。
# 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
)
# 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} )
然后在观察者的应用实体中每读取一帧数据就进行通知:IDemux.cpp:
那么实体自然要继承自观察者:IDemux.h:
然后是观察者服务的具体实现cpp/IObserver.cpp:
虽然这里obss.push_back可能会报红,意思是push_back在obss中不存在该函数,但是该函数是c++中的,个人认为这里没有问题,可能是因为vector导致的android studio不太识别。但是实际编译运行是没有问题的。 最后编译运行如在魅族16T中是没有问题的。
#include <string>
#include "FFDemux.h"
#include "XLog.h"
class TestObs:public IObserver
{
public:
void Update(XData d)
{
XLOGI("TestObs Update data size is %d",d.size);
}
};
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
TestObs *tobs = new TestObs();
IDemux *de = new FFDemux();
de->AddObs(tobs);
de->Open("/sdcard/1080.mp4");
de->Start();
XSleep(3000);
de->Stop();
return env->NewStringUTF(hello.c_str());
}
#include "XLog.h"
void IDemux::Main()
{
while(!isExit)
{
XData d = Read();
if(d.size > 0)
Notify(d);
//XLOGI("IDemux Read %d",d.size);
//if(d.size<=0)break;
}
}
#define XPLAY_IDEMUX_H
#include "XData.h"
#include "XThread.h"
#include "IObserver.h"
//解封装接口
class IDemux: public IObserver {
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
//主体函数添加观察者
void IObserver::AddObs(IObserver *obs)
{
if(!obs)return;
mux.lock();
obss.push_back(obs);
mux.unlock();
}
//通知所有观察者
void IObserver::Notify(XData data)
{
mux.lock();
for(int i =0; i < obss.size(); i++)
{
obss[i]->Update(data);
}
mux.unlock();
}
#define XPLAY_IOBSERVER_H
#include "XData.h"
#include "XThread.h"
#include <vector>
#include <mutex>
//观察者和主体
class IObserver:public XThread
{
public:
//观察者接收数据函数
virtual void Update(XData data) {}
//主体函数添加观察者(线程安全)
void AddObs(IObserver *obs);
//通知所有观察者(线程安全)
void Notify(XData data);
protected:
std::vector<IObserver *>obss;
std::mutex mux;
};
#endif //XPLAY_IOBSERVER_H
关键字词:Observer
相关文章
-
无相关信息