Skip to content

Commit

Permalink
WIP: rtp_relay: add support for recording in rtp_relay
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Dec 17, 2021
1 parent 9d61b9f commit 0783459
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 111 deletions.
3 changes: 3 additions & 0 deletions modules/rtp_relay/rtp_relay.c
Expand Up @@ -23,6 +23,7 @@
#include "../../mem/mem.h"
#include "../../ut.h"

#include "rtp_relay_load.h"
#include "rtp_relay_ctx.h"
#include "rtp_relay.h"

Expand Down Expand Up @@ -58,6 +59,8 @@ static cmd_export_t mod_cmds[] = {
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
{"register_rtp_relay", (cmd_function)rtp_relay_reg,
{{0,0,0}},0},
{"load_rtp_relay", (cmd_function)rtp_relay_load,
{{0,0,0}},0},
{0,0,{{0,0,0}},0}
};

Expand Down
10 changes: 10 additions & 0 deletions modules/rtp_relay/rtp_relay.h
Expand Up @@ -24,6 +24,7 @@
#include "../../str.h"
#include "../../sr_module.h"
#include "../../lib/list.h"
#include "rtp_relay_common.h"

#define RTP_RELAY_ALL_BRANCHES -1

Expand All @@ -50,6 +51,15 @@ struct rtp_relay_funcs {
str *flags, str *extra, str *body);
int (*delete)(struct rtp_relay_session *sess, struct rtp_relay_server *server,
str *flags, str *extra);

void *(*copy_create)(struct rtp_relay_session *sess,
struct rtp_relay_server *server, str *flags,
unsigned int copy_flags, str *ret);
int (*copy_start)(struct rtp_relay_session *sess,
struct rtp_relay_server *server, void *copy,
str *flags, str *body);
int (*copy_stop)(struct rtp_relay_session *sess,
struct rtp_relay_server *server, void *copy, str *flags);
};

struct rtp_relay {
Expand Down
31 changes: 31 additions & 0 deletions modules/rtp_relay/rtp_relay_binds.c
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "rtp_relay_load.h"

int rtp_relay_load(struct rtp_relay_binds *binds)
{
binds->get_ctx = rtp_relay_get_context;
binds->get_ctx_dlg = rtp_relay_get_context_dlg;
binds->copy_create = rtp_relay_copy_create;
binds->copy_start = rtp_relay_copy_start;
binds->copy_stop = rtp_relay_copy_stop;
return 1;
}
29 changes: 29 additions & 0 deletions modules/rtp_relay/rtp_relay_common.h
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef _RTP_RELAY_COMMON_H_
#define _RTP_RELAY_COMMON_H_

typedef void * rtp_copy_ctx;

#define RTP_COPY_MODE_SIPREC (1<<0)

#endif /* _RTP_RELAY_COMMON_H_ */
119 changes: 112 additions & 7 deletions modules/rtp_relay/rtp_relay_ctx.c
Expand Up @@ -19,6 +19,7 @@
*/

#include "rtp_relay_ctx.h"
#include "rtp_relay_load.h"
#include "../../mem/shm_mem.h"
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
Expand Down Expand Up @@ -133,6 +134,16 @@ struct rtp_relay_ctx *rtp_relay_get_ctx(void)
return ctx;
}

rtp_ctx rtp_relay_get_context(void)
{
return rtp_relay_try_get_ctx();
}

rtp_ctx rtp_relay_get_context_dlg(struct dlg_cell *dlg)
{
return RTP_RELAY_GET_DLG_CTX(dlg);
}

static void rtp_relay_ctx_free_sess(struct rtp_relay_sess *s)
{
int f;
Expand Down Expand Up @@ -167,6 +178,10 @@ void rtp_relay_ctx_free(void *param)

if (ctx->callid.s)
shm_free(ctx->callid.s);
if (ctx->from_tag.s)
shm_free(ctx->from_tag.s);
if (ctx->to_tag.s)
shm_free(ctx->to_tag.s);

list_for_each_safe(it, safe, &ctx->sessions)
rtp_relay_ctx_free_sess(list_entry(it, struct rtp_relay_sess, list));
Expand Down Expand Up @@ -636,8 +651,8 @@ static void rtp_relay_delete_dlg(struct dlg_cell *dlg,
struct rtp_relay_session info;
memset(&info, 0, sizeof info);
info.callid = &ctx->callid;
info.from_tag = &dlg->legs[DLG_CALLER_LEG].tag;
info.to_tag = &dlg->legs[callee_idx(dlg)].tag;
info.from_tag = &ctx->from_tag;
info.to_tag = &ctx->to_tag;
info.branch = sess->index;
rtp_relay_delete(&info, sess, NULL);
}
Expand Down Expand Up @@ -780,7 +795,11 @@ static void rtp_relay_indlg(struct dlg_cell* dlg, int type, struct dlg_cb_params
static int rtp_relay_dlg_callbacks(struct dlg_cell *dlg, struct rtp_relay_ctx *ctx)
{
if (shm_str_sync(&ctx->callid, &dlg->callid) < 0)
LM_ERR("could not store callid in dialog\n");
LM_ERR("could not store callid in context\n");
if (shm_str_sync(&ctx->from_tag, &dlg->legs[DLG_CALLER_LEG].tag) < 0)
LM_ERR("could not store from tag in context\n");
if (shm_str_sync(&ctx->to_tag, &dlg->legs[callee_idx(dlg)].tag) < 0)
LM_ERR("could not store to tag in context\n");

if (rtp_relay_dlg.register_dlgcb(dlg, DLGCB_MI_CONTEXT,
rtp_relay_dlg_mi, NULL, NULL) < 0)
Expand Down Expand Up @@ -1293,8 +1312,8 @@ static int rtp_relay_reinvite_reply(struct sip_msg *msg,
memset(&info, 0, sizeof info);
/* reversed tags */
info.callid = &tmp->ctx->callid;
info.from_tag = &tmp->dlg->legs[callee_idx(tmp->dlg)].tag;
info.to_tag = &tmp->dlg->legs[DLG_CALLER_LEG].tag;
info.from_tag = &tmp->ctx->to_tag;
info.to_tag = &tmp->ctx->from_tag;
info.branch = tmp->sess->index;
info.body = pbody;
info.msg = msg;
Expand Down Expand Up @@ -1384,8 +1403,8 @@ static int rtp_relay_update_reinvites(struct rtp_relay_tmp *tmp)

/* offer callee's SDP to get SDP for caller */
info.callid = &tmp->ctx->callid;
info.from_tag = &tmp->dlg->legs[callee_leg].tag;
info.to_tag = &tmp->dlg->legs[DLG_CALLER_LEG].tag;
info.from_tag = &tmp->ctx->to_tag;
info.to_tag = &tmp->ctx->from_tag;
info.branch = tmp->sess->index;
info.body = &body;
info.msg = get_dummy_sip_msg();
Expand Down Expand Up @@ -1671,3 +1690,89 @@ mi_response_t *mi_rtp_relay_update_callid(const mi_params_t *params,
rtp_relay_release_tmp(ctmp, 0);
return NULL;
}

rtp_copy_ctx rtp_relay_copy_create(rtp_ctx _ctx,
str *flags, unsigned int copy_flags, str *ret_body)
{
struct rtp_relay_session info;
struct rtp_relay_ctx *ctx = _ctx;
if (!ret_body) {
LM_ERR("no body to return!\n");
return NULL;
}
if (!ctx) {
LM_ERR("no context to use!\n");
return NULL;
}
if (!ctx->main || !(rtp_relay_ctx_engaged(ctx)) || !ctx->main->relay) {
LM_ERR("rtp not established!\n");
return NULL;
}
if (!ctx->main->relay->funcs.copy_create) {
LM_ERR("rtp does not support recording!\n");
return NULL;
}
memset(&info, 0, sizeof info);
info.callid = &ctx->callid;
info.from_tag = &ctx->from_tag;
info.to_tag = &ctx->to_tag;
info.branch = ctx->main->index;
return ctx->main->relay->funcs.copy_create(&info,
&ctx->main->server, flags, copy_flags, ret_body);
}

int rtp_relay_copy_start(rtp_ctx _ctx, rtp_copy_ctx copy,
str *flags, str *body)
{
struct rtp_relay_session info;
struct rtp_relay_ctx *ctx = _ctx;
if (!body) {
LM_ERR("no body to provide!\n");
return -1;
}
if (!ctx) {
LM_ERR("no context to use!\n");
return -1;
}
if (!ctx->main || !(rtp_relay_ctx_engaged(ctx)) || !ctx->main->relay) {
LM_ERR("rtp not established!\n");
return -1;
}
if (!ctx->main->relay->funcs.copy_start) {
LM_ERR("rtp does not support recording!\n");
return -1;
}
memset(&info, 0, sizeof info);
info.callid = &ctx->callid;
info.from_tag = &ctx->from_tag;
info.to_tag = &ctx->to_tag;
info.branch = ctx->main->index;
return ctx->main->relay->funcs.copy_start(
&info, &ctx->main->server, copy, flags, body);
}

int rtp_relay_copy_stop(rtp_ctx _ctx, rtp_copy_ctx copy, str *flags)
{
struct rtp_relay_session info;
struct rtp_relay_ctx *ctx = _ctx;

if (!ctx) {
LM_ERR("no context to use!\n");
return -1;
}
if (!ctx->main || !ctx->main->relay) {
LM_ERR("rtp not established!\n");
return -1;
}
if (!ctx->main->relay->funcs.copy_stop) {
LM_DBG("rtp does not support stop recording!\n");
return 1;
}
memset(&info, 0, sizeof info);
info.callid = &ctx->callid;
info.from_tag = &ctx->from_tag;
info.to_tag = &ctx->to_tag;
info.branch = ctx->main->index;
return ctx->main->relay->funcs.copy_stop(
&info, &ctx->main->server, copy, flags);
}
2 changes: 1 addition & 1 deletion modules/rtp_relay/rtp_relay_ctx.h
Expand Up @@ -96,7 +96,7 @@ struct rtp_relay_sess {
};

struct rtp_relay_ctx {
str callid;
str callid, from_tag, to_tag;
gen_lock_t lock;
unsigned int state;
struct rtp_relay_sess *main;
Expand Down
70 changes: 70 additions & 0 deletions modules/rtp_relay/rtp_relay_load.h
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef _RTP_RELAY_LOAD_H_
#define _RTP_RELAY_LOAD_H_

#include "../../str.h"
#include "../../sr_module.h"
#include "rtp_relay_common.h"
struct dlg_cell;

typedef void * rtp_ctx;

struct rtp_relay_binds {
rtp_ctx (*get_ctx)(void);
rtp_ctx (*get_ctx_dlg)(struct dlg_cell *);
rtp_copy_ctx (*copy_create)(rtp_ctx ctx, str *flags,
unsigned int copy_flags, str *ret_body);
int (*copy_start)(rtp_ctx ctx, rtp_copy_ctx copy,
str *flags, str *body);
int (*copy_stop)(rtp_ctx ctx, rtp_copy_ctx copy,
str *flags);
};

typedef int (*load_rtp_relay_f)(struct rtp_relay_binds *rtpb);

static inline int load_rtp_relay(struct rtp_relay_binds *rtpb)
{
load_rtp_relay_f load_rtp_relay;

/* import the rtp_relay auto-loading function */
if ( !(load_rtp_relay=(load_rtp_relay_f)find_export("load_rtp_relay", 0)))
return -1;

/* let the auto-loading function load all rtp_relay stuff */
if (load_rtp_relay(rtpb) == -1)
return -1;

return 0;
}

int rtp_relay_load(struct rtp_relay_binds *binds);
rtp_ctx rtp_relay_get_context(void);
rtp_ctx rtp_relay_get_context_dlg(struct dlg_cell *);
rtp_copy_ctx rtp_relay_copy_create(rtp_ctx ctx,
str *flags, unsigned int copy_flags, str *ret_body);
int rtp_relay_copy_start(rtp_ctx ctx, rtp_copy_ctx copy,
str *flags, str *body);
int rtp_relay_copy_stop(rtp_ctx ctx, rtp_copy_ctx copy,
str *flags);

#endif /* _RTP_RELAY_LOAD_H_ */

0 comments on commit 0783459

Please sign in to comment.