janus写自己的插件plugins的加载过程

举个例子,janus 加载videoroom这个插件,动态库的加载:

首先janus把所有的插件都放在了目录:/opt/janus/lib/janus/plugins/下,

我们下面的代码path的值就是这个路径,eventent->d_name的值就是:libjanus_videoroom.so, 而且需要加入的插件都是这样加载的, 这样就很清楚了


memset(eventpath, 0, 1024);
g_snprintf(eventpath, 1024, "%s/%s", path, eventent->d_name);
void *event = dlopen(eventpath, RTLD_NOW | RTLD_GLOBAL);
if (!event) {
		JANUS_LOG(LOG_ERR, "\tCouldn't load event handler plugin '%s': %s\n", eventent->d_name, dlerror());
} else {
		create_e *create = (create_e*) dlsym(event, "create");
		const char *dlsym_error = dlerror();
	    if (dlsym_error) {
				JANUS_LOG(LOG_ERR, "\tCouldn't load symbol 'create': %s\n", dlsym_error);
				continue;
		}
	    janus_eventhandler *janus_eventhandler = create();
		if(!janus_eventhandler) {
			JANUS_LOG(LOG_ERR, "\tCouldn't use function 'create'...\n");
			continue;
		}

两个关键的函数:dlopen,dlsym实现动态库的加载

所有只要我们把自己的插件编译好放在:/opt/janus/lib/janus/plugins/目录下就会加载进来,但问题来了:

1.假如我们想禁止某个插件怎么处理?

直接删除某库,或者在janus.jcfg的选项中plugins disabled 添加该库,开启disabled这功能

2.如何写插件?

步骤1:在plugins目录下创建一个janus_mytest.c 文件,这个就是我们的插件

//
// Created by root on 4/23/21.
//

#include "plugin.h"

#include 
#include 

#include "../debug.h"
#include "../apierror.h"
#include "../config.h"
#include "../mutex.h"
#include "../rtp.h"
#include "../rtpsrtp.h"
#include "../rtcp.h"
#include "../record.h"
#include "../sdp-utils.h"
#include "../utils.h"
#include "../ip-utils.h"
#include 
#include 


/* Plugin information */
#define JANUS_MYTEST_VERSION			9
#define JANUS_MYTEST_VERSION_STRING	    "0.0.9"
#define JANUS_MYTEST_DESCRIPTION		"This is a plugin implementing a videoconferencing SFU (Selective Forwarding Unit) for Janus, that is an test router."
#define JANUS_MYTEST_NAME			"JANUS Test plugin"
#define JANUS_MYTEST_AUTHOR			"Meetecho s.r.l."
#define JANUS_MYTEST_PACKAGE		"janus.plugin.mytest"

/* Plugin methods */
janus_plugin *create(void);
int janus_mytest_init(janus_callbacks *callback, const char *config_path);
void janus_mytest_destroy(void);
int janus_mytest_get_api_compatibility(void);
int janus_mytest_get_version(void);
const char *janus_mytest_get_version_string(void);
const char *janus_mytest_get_description(void);
const char *janus_mytest_get_name(void);
const char *janus_mytest_get_author(void);
const char *janus_mytest_get_package(void);
void janus_mytest_create_session(janus_plugin_session *handle, int *error);
struct janus_plugin_result *janus_mytest_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep);
json_t *janus_mytest_handle_admin_message(json_t *message);
void janus_mytest_setup_media(janus_plugin_session *handle);
void janus_mytest_incoming_rtp(janus_plugin_session *handle, janus_plugin_rtp *packet);
void janus_mytest_incoming_rtcp(janus_plugin_session *handle, janus_plugin_rtcp *packet);
void janus_mytest_incoming_data(janus_plugin_session *handle, janus_plugin_data *packet);
void janus_mytest_data_ready(janus_plugin_session *handle);
void janus_mytest_slow_link(janus_plugin_session *handle, int uplink, int video);
void janus_mytest_hangup_media(janus_plugin_session *handle);
void janus_mytest_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_mytest_query_session(janus_plugin_session *handle);

static janus_plugin janus_mytest_plugin =
        JANUS_PLUGIN_INIT (
                .init = janus_mytest_init,
                .destroy = janus_mytest_destroy,

                .get_api_compatibility = janus_mytest_get_api_compatibility,
                .get_version = janus_mytest_get_version,
                .get_version_string = janus_mytest_get_version_string,
                .get_description = janus_mytest_get_description,
                .get_name = janus_mytest_get_name,
                .get_author = janus_mytest_get_author,
                .get_package = janus_mytest_get_package,

                .create_session = janus_mytest_create_session,
                .handle_message = janus_mytest_handle_message,
                .handle_admin_message = janus_mytest_handle_admin_message,
                .setup_media = janus_mytest_setup_media,
                .incoming_rtp = janus_mytest_incoming_rtp,
                .incoming_rtcp = janus_mytest_incoming_rtcp,
                .incoming_data = janus_mytest_incoming_data,
                .data_ready = janus_mytest_data_ready,
                .slow_link = janus_mytest_slow_link,
                .hangup_media = janus_mytest_hangup_media,
                .destroy_session = janus_mytest_destroy_session,
                .query_session = janus_mytest_query_session,
        );

janus_plugin *create(void) {
    JANUS_LOG(LOG_VERB, "----test %s created!\n", JANUS_MYTEST_NAME);
    return &janus_mytest_plugin;
}

int janus_mytest_init(janus_callbacks *callback, const char *config_path) {
    JANUS_LOG(LOG_INFO, "----test %s initialized!\n", JANUS_MYTEST_NAME);
    return 0;
}

void janus_mytest_destroy(void) {
    JANUS_LOG(LOG_INFO, "----test %s destroyed!\n", JANUS_MYTEST_NAME);
}

int janus_mytest_get_api_compatibility(void) {
    return JANUS_PLUGIN_API_VERSION;
}

int janus_mytest_get_version(void) {
    return JANUS_MYTEST_VERSION;
}

const char *janus_mytest_get_version_string(void) {
    return JANUS_MYTEST_VERSION_STRING;
}

const char *janus_mytest_get_description(void) {
    return JANUS_MYTEST_DESCRIPTION;
}

const char *janus_mytest_get_name(void) {
    return JANUS_MYTEST_NAME;
}

const char *janus_mytest_get_author(void) {
    return JANUS_MYTEST_AUTHOR;
}

const char *janus_mytest_get_package(void) {
    return JANUS_MYTEST_PACKAGE;
}

void janus_mytest_create_session(janus_plugin_session *handle, int *error) {
    JANUS_LOG(LOG_INFO, "----test Session created.\n");
}

struct janus_plugin_result *janus_mytest_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep) {
    return janus_plugin_result_new(JANUS_PLUGIN_OK, NULL, json_object());
}

json_t *janus_mytest_handle_admin_message(json_t *message) {
    json_t *response = json_deep_copy(message);
    return response;
}

void janus_mytest_data_ready(janus_plugin_session *handle) {
    if(handle == NULL || g_atomic_int_get(&handle->stopped))
        return;
}

void janus_mytest_setup_media(janus_plugin_session *handle) {
    JANUS_LOG(LOG_INFO, "----test WebRTC media is now available.\n");
}

void janus_mytest_incoming_rtp(janus_plugin_session *handle, janus_plugin_rtp *packet) {
    JANUS_LOG(LOG_VERB, "----test Got an RTP message (%d bytes.)\n", packet->length);
}

void janus_mytest_incoming_rtcp(janus_plugin_session *handle, janus_plugin_rtcp *packet) {
    JANUS_LOG(LOG_VERB, "----test Got an RTCP message (%d bytes.)\n", packet->length);
}

void janus_mytest_incoming_data(janus_plugin_session *handle, janus_plugin_data *packet) {
    JANUS_LOG(LOG_VERB, "----test Got a DataChannel message (%d bytes.)\n", packet->length);
}

void janus_mytest_slow_link(janus_plugin_session *handle, int uplink, int video) {
    JANUS_LOG(LOG_VERB, "----test Slow link detected.\n");
}

void janus_mytest_hangup_media(janus_plugin_session *handle) {
    JANUS_LOG(LOG_INFO, "----test No WebRTC media anymore.\n");
}

void janus_mytest_destroy_session(janus_plugin_session *handle, int *error) {
    janus_refcount_decrease(&handle->ref);
    JANUS_LOG(LOG_INFO, "----test Session destroyed.\n");
}

json_t *janus_mytest_query_session(janus_plugin_session *handle) {
    JANUS_LOG(LOG_INFO, "----test No WebRTC media anymore.\n");
}

步骤2:再把这个文件单独编译成动态库,生成后是libjanus_test.so,再把这个库拷贝到/opt/janus/lib/janus/plugins/目录下,再运行janus,

运行结果如下:

janus写自己的插件plugins的加载过程_第1张图片

说明动态库加载成功了

你可能感兴趣的:(janus-gateway)