使用FFMpeg 模仿Android MediaPlayer

搭建ffmpeg player(一)--搭建上层框架

2020-03-25  本文已影响0人  llm_5243

搭建ffmpeg player -- 整体思想介绍

工程上层结构

在前面的文章MediaPlayer--MediaPlayer基本框架MediaPlayer--Android MediaPlayer的使用方法,了解了搭建mediaplayer的基本信息,接下来开始搭建播放器的上层框架。

Android MediaPlayer考虑的因素会比较全面,而我们自己的播放器目前只考虑提供给单个应用使用,所以在层次上做了简化


FFMediaPlayer Struct.png ffmediaplayer_up_struct.png

工程搭建

首先在MediaPlayer.java中 实现MediaPlayer--Android MediaPlayer的使用方法文章列举的函数,所有方法暂时都未实现。这部分都挺简单,这里主要介绍下native层回调java的流程

Native层回调java层流程

Java层

java层实现了静态的 postEventFromNative, native层的回调会调用到该函数。 postEventFromNative 会将消息发送给handle去处理。

 //Jni 层回调的函数
    private static void postEventFromNative(Object mediaplayer_ref,
                                            int what, int arg1, int arg2)
    {
        final MediaPlayer mp = (MediaPlayer)((WeakReference)mediaplayer_ref).get();
        if (mp == null) {
            return;
        }

        if (mp.mEventHandler != null) {
            Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2);
            mp.mEventHandler.sendMessage(m);
        }
    }

handle,message在MediaPlayer的构造函数中创建,handle 会调用到对应的listener将消息传递给上层

native层 ---> postEventFromNative --->EventHandler --->listener
由于postEventFromNative是static函数,Mediaplayer的对象通过native层参数传递过来,而Meidaplayer的对象又是在播放器的创建过程中由java层传递给nativie层的

Jave层 -->native层初始化注册过程

1 加载so和native_init

static {
        System.loadLibrary("nativeffmpeg");
        native_init();
    }

System.loadLibrary会加载native 的so,调用到JNI_OnLoad,这里去做了方法注册,这部分是jni的知识,可以去参考其他的网络资料,Android基础--JNI这篇文章只是摘抄了我自己比较困惑的知识。 接下来看nativie_init函数

struct fields_t {
    jfieldID    context;     //将native层的player设置给java层
    jfieldID    surface_texture;

    jmethodID   post_event;
};
static fields_t fields;
...
static void pri_tool_MediaPlayer_native_init(JNIEnv *env) {
    jclass clazz;

    clazz = env->FindClass(className);
    if (clazz == NULL) {
        return;
    }
    ...
    fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
                                               "(Ljava/lang/Object;III)V");
    ...
    env->DeleteLocalRef(clazz);
}

这个函数主要是对fields 这个静态变量赋值,filelds获取的是java层对应的方法和变量,最终native会通过post_event回调到java层

2 MediaPlayer的创建和listener的传递
当上层应用通过 new MediaPlayer() 创建播放器时

public MediaPlayer() {
        Looper looper;
        if ((looper = Looper.myLooper()) != null) {
            mEventHandler = new EventHandler(this, looper);
        } else if ((looper = Looper.getMainLooper()) != null) {
            mEventHandler = new EventHandler(this, looper);
        } else {
            mEventHandler = null;
        }

        /* Native setup requires a weak reference to our object.
         * It's easier to create it here than in C++.
         */
        native_setup(new WeakReference<MediaPlayer>(this));
    }

在构造函数里,主要做了两件事,1是创建了EventHandler, 2是调用了native层的native_setup,并将java层的MediaPlayer实例传递给native层

static void pri_tool_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) {
    ALOGI("native_setup");
    FFMediaPlayer *mp = new FFMediaPlayer();
    if (mp == NULL) {
        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
        return;
    }

    // create new listener and give it to MediaPlayer
    JNIMediaPlayerListener *listener = new JNIMediaPlayerListener(env, thiz, weak_this);
    mp->setListener(listener);

    // Stow our new C++ MediaPlayer in an opaque field in the Java object.
    setMediaPlayer(env, thiz, mp);
}

native_setup做了几件事情,
1 创建native层播放器实例FFMediaPlayer()
2 创建了JNIMediaPlayerListener,将java的MediaPlayer实例传递进去
3 将JNIMediaPlayerListener 设置给FFMediaPlayer()
4 将FFMediaPlayer 设置给Java层的mNativeContext

后面native层的FFMediaPlayer 就通过JNIMediaPlayerListener 回调java层接口。JNIMediaPlayerListener 位于native_MediaPlayer.cpp中,能够访问全局变量 fields.post_event, 同时拥有 java的MediaPlayer实例,所以JNIMediaPlayerListener能够回调java层的postEventFromNative,并将MediaPlayer实例传递回去

顺便提一下,Android MediaPlayer原生的回调机制,除了上面的有相同之处外, 在native层的client和service端有notify方法, MediaPlayerService 在创建播放器时会往NuPlayer注册一个notify的回调函数

MediaPlayerFactory::createPlayer(playerType, this, notify, mPid);

notify为MediaPlayerService::Client::notify。后续NuPlayer通过notify回调消息,回调的流程大致如下:
NuPlayer -->MediaPlayerService::Client::notify--> MediaPlayer::notify--> JNIMediaPlayerListener::notify -->postEventFromNative(java)
这部分网上资料也很多

底层播放器接口和工厂的定义

#include <mutex>

#ifdef __cplusplus

enum player_type {
    FFMPEG_PLAYER = 0,
};

// callback mechanism for passing messages to MediaPlayer object
typedef void (*notify_callback_f)(int msg, int ext1, int ext2);


class MediaPlayerInterface {
public:
    virtual  ~MediaPlayerInterface() {}

    virtual int    setDataSource(int fd, int64_t offset, int64_t length) = 0;
    virtual int    prepareAsync() = 0;
    virtual int    start() = 0;
    virtual int    stop() = 0;
    virtual int    pause() = 0;
    virtual int    reset() = 0;
    virtual int    release() = 0;

    //设置回调函数
    void  setNotifyCallback(notify_callback_f notifyFunc) {
        std::lock_guard<std::mutex> lock(mNotifyLock);
        mNotify = notifyFunc;
    }

    //发送回调消息
    void sendEvent(int msg, int ex1 = 0, int ext2 = 0) {
        std::lock_guard<std::mutex> lock(mNotifyLock);
        if (mNotify) {
            mNotify(msg, ex1, ext2);
        }
    }

private:
    notify_callback_f mNotify = 0;
    std::mutex mNotifyLock;
};

#endif // __cplusplus


#endif //FFMEDIAPLAYER_MEDIAPLAYERINTERFACE_H
class FactoryInterface {
public:
    virtual ~FactoryInterface() {}
    virtual std::shared_ptr<MediaPlayerInterface> createPlayer() = 0;
};

这里用到了c++11 智能指针shared_ptr和自动释放锁的lock_guard

上一篇 下一篇

猜你喜欢

热点阅读