Android(安卓)AOSP 环境下实现C++直接调用libmedia.so接口播放视频文件。
16lz
2021-01-24
最近需要hook android libmedia.so以取得播放进度。
没搞出来,先搞了个c++直接播放MP4的程序:
#include #include #include #define LOG_TAG "INJECT-lx"#include #include #include #include #include #include #include /#ifdef ANDROID_5#include #include #include #include #include #include #include #include // for FOURCC definition#include "android_runtime/AndroidRuntime.h"#include "jni.h"#include #include "android_media_BufferingParams.h"#include "android_media_MediaDataSource.h"#include "android_media_MediaMetricsJNI.h"#include "android_media_PlaybackParams.h"#include "android_media_SyncParams.h"#include "android_media_VolumeShaper.h"#else#include #include "jni.h"#include #include #include #include #include #include #include #include #include #include #include #include #include #endif/#ifndef int32_t#ifdef _WIN32typedef int status_t;#elsetypedef int32_t status_t;#endif#endif#define LOGD(fmt,args...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,fmt,##args);\printf(fmt,##args)extern "C" void *memcpy(void *dest, const void *src, size_t n);typedef status_t (*t_setDataSource1)(const char *url);typedef status_t (*pause_t)();typedef status_t (android::MediaPlayer::*t_setDataSource2)(int fd, int64_t offset, int64_t length);typedef status_t (android::MediaPlayer::*mp_pause_t)();struct fields_t { jfieldID context; jfieldID surface_texture; jmethodID post_event; jmethodID proxyConfigGetHost; jmethodID proxyConfigGetPort; jmethodID proxyConfigGetExclusionList;};//using std::string;extern "C"{int file_exists(char* path){FILE* fp = fopen(path, "r");if(fp==NULL) return 0;fclose(fp);return 1;}}using android::MediaPlayer;using android::MediaPlayerListener;namespace android{class MyMediaPlayerListener: public MediaPlayerListener{public:void notify(int msg, int ext1, int ext2, const Parcel *obj){LOGD("int msg=%d, int ext1=%d, int ext2=%d, const Parcel *obj=%p\n", msg, ext1, ext2, obj);}/*protected:~RefBase(){LOGD("%s->%s->%d\n", __FILE__, __FUNCTION__, __LINE__);}*/};}using android::sp;using android::ProcessState;using android::MyMediaPlayerListener;int main(int argc, char** argv) {LOGD(" [V003] ");char mp4file[256];strcpy(mp4file, "/sdcard/1.mp4");int i;char* str,*pstr;for(i=0;i proc = ProcessState::self();LOGD("App process: starting thread pool.\n");proc->startThreadPool(); int iRet = 0; MediaPlayer *p= new MediaPlayer(); MyMediaPlayerListener* ml = new MyMediaPlayerListener();LOGD("At %s, line %d\n", __FUNCTION__, __LINE__);FILE* fp = fopen(mp4file, "r");fseek(fp, 0, SEEK_END);int length = ftell(fp);rewind(fp);LOGD("At %s, line %d\n", __FUNCTION__, __LINE__);int fd = fileno(fp); iRet = p->setDataSource(fd, 0, length); if (0 != iRet) { LOGD("[setDataSource] iRet = %d\n", iRet); }LOGD("At %s, line %d\n", __FUNCTION__, __LINE__); iRet = p->setListener(ml); if (0 != iRet) { LOGD("[setListener] iRet = %d\n", iRet); } LOGD("At %s, line %d\n", __FUNCTION__, __LINE__); iRet = p->prepare(); if (0 != iRet) { LOGD("[prepare] iRet = %d\n", iRet); }LOGD("At %s, line %d\n", __FUNCTION__, __LINE__); iRet = p->start(); if (0 != iRet) { LOGD("[start] iRet = %d\n", iRet); }LOGD("At %s, line %d\n", __FUNCTION__, __LINE__);while(i++<10){LOGD("[Playing] %d...\n", i);sleep(3);}LOGD("At %s, line %d\n", __FUNCTION__, __LINE__); p->stop(); return 0;}
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) #下面这句是android5使用的附加库的方法LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog -landroid_runtime -lmedia -lm -lnativebridge -lnativehelper -lnativeloader -lstagefright -landroid -lbase -lbinder -lcutils -ldl -lmediandk -lutils -lsysutils -ljnigraphics -lz -ldl -lgcc#LOCAL_LDFLAGS := -Wl,--warn-unresolved-symbols #下面这句是android4.2使用的附加库的方法LOCAL_SHARED_LIBRARIES := liblog libcutils libutils libmedia libbinderLOCAL_C_INCLUDES := -I system/libhidl/base/include/ -I system/libhidl/transport/token/1.0/utils/include/ -I external/piex/ -I frameworks/base/media/jni/ -I system/core/base/include -I ./system/core/include/LOCAL_MODULE := myMediaPlayerLOCAL_SRC_FILES:= myMediaPlayer.cppinclude $(BUILD_EXECUTABLE)
执行程序能够听到视频的声音,看不到图像,这个不管了,应该需要某种方式接收图像数据然后显示。
QQ群:95303036
更多相关文章
- Android(安卓)一些指令
- Android(安卓)解决方法数 65536 (65k) 限制
- Android中WebView详解
- Android(安卓)跨应用调用Activity及Service
- android thread Interrupte使用方法
- android window.requestWindowFeature()常用方法(转)
- Android(安卓)Studio 用 JNI 实现与原生代码通信
- the bind value at index 2 is null
- Android隐藏标题栏的四种方法