您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
3. 完成Builder模式的IPlayerBuilder构建IPlayer对象~1
发布时间:2021-06-12 21:39:47编辑:雪饮阅读()
其實吧爲了實現建造者模式,這裏現在需要新增IPlayerBuilder和FFPlayerBuilder以用來對之前的結構進行進一步的封裝優化,那麽首先在cpp/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
IObserver
FFDecode
IDecode
XParameter
IVideoView
GLVideoView
XTexture
XEGL
XShader
FFResample
IResample
SLAudioPlay
IAudioPlay
IPlayer
IPlayerBuilder
FFPlayerBuilder
)
# 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} )
其具體實現如:cpp/FFPlayerBuilder.cpp:
那麽同樣的對於入口,我們需要再次調整下調用cpp/native-lib.cpp:
然後建造者接口這裏先聲明和實現cpp/IPlayerBuilder.cpp:中進行實現:
Cpp/IPlayerBuilder.h中進行聲明:
以雷電4的運行結果來看,其實問題還是不算大的
#define XPLAY_FFPLAYERBUILDER_H
#include "IPlayerBuilder.h"
class FFPlayerBuilder:public IPlayerBuilder
{
public:
static void InitHard(void *vm);
static FFPlayerBuilder *Get()
{
static FFPlayerBuilder ff;
return &ff;
}
protected:
FFPlayerBuilder(){};
virtual IDemux *CreateDemux();
virtual IDecode *CreateDecode();
virtual IResample *CreateResample();
virtual IVideoView *CreateVideoView();
virtual IAudioPlay *CreateAudioPlay();
virtual IPlayer *CreatePlayer(unsigned char index=0);
};
#endif //XPLAY_FFPLAYERBUILDER_H
#include "FFDemux.h"
#include "FFdecode.h"
#include "FFResample.h"
#include "GLVideoView.h"
#include "SLAudioPlay.h"
IDemux *FFPlayerBuilder::CreateDemux()
{
IDemux *ff = new FFDemux();
return ff;
}
IDecode *FFPlayerBuilder::CreateDecode()
{
IDecode *ff = new FFDecode();
return ff;
}
IResample *FFPlayerBuilder::CreateResample()
{
IResample *ff = new FFResample();
return ff;
}
IVideoView *FFPlayerBuilder::CreateVideoView()
{
IVideoView *ff = new GLVideoView();
return ff;
}
IAudioPlay *FFPlayerBuilder::CreateAudioPlay()
{
IAudioPlay *ff = new SLAudioPlay();
return ff;
}
IPlayer *FFPlayerBuilder::CreatePlayer(unsigned char index)
{
return IPlayer::Get(index);
}
void FFPlayerBuilder::InitHard(void *vm)
{
FFDecode::InitHard(vm);
}
#include <string>
#include <android/native_window_jni.h>
#include "XLog.h"
#include "FFPlayerBuilder.h"
//IVideoView *view = NULL;
static IPlayer *player = NULL;
extern "C"
JNIEXPORT
jint JNI_OnLoad(JavaVM *vm,void *res)
{
//FFDecode::InitHard(vm);
FFPlayerBuilder::InitHard(vm);
///////////////////////////////////
///测试用代码
player = FFPlayerBuilder::Get()->BuilderPlayer();
player->Open("/sdcard/v1080.mp4");
player->Start();
//de->Start();
//vdecode->Start();
//adecode->Start();
return JNI_VERSION_1_4;
}
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
//XLOGI("S begin!");
//XSleep(3000);
//XLOGI("S end!");
//return env->NewStringUTF(hello.c_str());
//XSleep(3000);
//de->Stop();
/*for(;;)
{
XData d = de->Read();
XLOGI("Read data size is %d",d.size);
}*/
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_XPlay_InitView(JNIEnv *env, jobject instance, jobject surface) {
// TODO
ANativeWindow *win = ANativeWindow_fromSurface(env,surface);
//view->SetRender(win);
if(player)
player->InitView(win);
//XEGL::Get()->Init(win);
//XShader shader;
//shader.Init();
}
#include "IVideoView.h"
#include "IResample.h"
#include "IDecode.h"
#include "IAudioPlay.h"
#include "IDemux.h"
IPlayer *IPlayerBuilder::BuilderPlayer(unsigned char index)
{
IPlayer *play = CreatePlayer(index);
//解封装
IDemux *de = CreateDemux();
//视频解码
IDecode *vdecode = CreateDecode();
//音频解码
IDecode *adecode = CreateDecode();
//解码器观察解封装
de->AddObs(vdecode);
de->AddObs(adecode);
//显示观察视频解码器
IVideoView *view = CreateVideoView();
vdecode->AddObs(view);
//重采样观察音频解码器
IResample *resample = CreateResample();
adecode->AddObs(resample);
//音频播放观察重采样
IAudioPlay *audioPlay = CreateAudioPlay();
resample->AddObs(audioPlay);
play->demux = de;
play->adecode = adecode;
play->vdecode = vdecode;
play->videoView = view;
play->resample = resample;
play->audioPlay = audioPlay;
return play;
}
#define XPLAY_IPLAYERBUILDER_H
#include "IPlayer.h"
class IPlayerBuilder
{
public:
virtual IPlayer *BuilderPlayer(unsigned char index=0);
protected:
virtual IDemux *CreateDemux() = 0;
virtual IDecode *CreateDecode() = 0;
virtual IResample *CreateResample() = 0;
virtual IVideoView *CreateVideoView() = 0;
virtual IAudioPlay *CreateAudioPlay() = 0;
virtual IPlayer *CreatePlayer(unsigned char index=0) = 0;
};
#endif //XPLAY_IPLAYERBUILDER_H
关键字词:Builder
相关文章
-
无相关信息