Skip to content

Commit

Permalink
Modification based on comments
Browse files Browse the repository at this point in the history
- Add bitrate config on SDP, allowing different bitrate on local and remote
- Fix some compile warning
- Set [--with-lyra]/model_coeffs as default to PJMEDIA_CODEC_LYRA_DEFAULT_MODEL_PATH
  • Loading branch information
trengginas committed May 16, 2024
1 parent 408014a commit 688174c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 18 deletions.
3 changes: 3 additions & 0 deletions aconfigure
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ ac_webrtc_instset
ac_no_webrtc
ac_no_yuv
ac_no_srtp
ac_lyra_model_path
ac_no_lyra_codec
ac_no_bcg729
opus_present
Expand Down Expand Up @@ -10138,6 +10139,7 @@ if test "x$ac_cross_compile" != "x" -a "x$with_lyra" = "xno"; then
fi



# Check whether --enable-lyra was given.
if test ${enable_lyra+y}
then :
Expand Down Expand Up @@ -10201,6 +10203,7 @@ _ACEOF
if ac_fn_cxx_try_link "$LINENO"
then :

ac_lyra_model_path="$LYRA_PREFIX/model_coeffs"
printf "%s\n" "#define PJMEDIA_HAS_LYRA_CODEC 1" >>confdefs.h

LYRA_CPPFLAGS="'-DGLOG_DEPRECATED=__attribute__((deprecated))' '-DGLOG_EXPORT=__attribute__((visibility(\"default\")))' '-DGLOG_NO_EXPORT=__attribute__((visibility(\"default\")))'"
Expand Down
2 changes: 2 additions & 0 deletions aconfigure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,7 @@ fi

dnl # lyra
AC_SUBST(ac_no_lyra_codec)
AC_SUBST(ac_lyra_model_path)
AC_ARG_ENABLE(lyra,
AS_HELP_STRING([--disable-lyra],
[Disable lyra (default: not disabled)]),
Expand Down Expand Up @@ -2369,6 +2370,7 @@ AC_ARG_ENABLE(lyra,
[std::unique_ptr<chromemedia::codec::LyraDecoder> dec = chromemedia::codec::LyraDecoder::Create(8000,1,"");]
)],
[
[ac_lyra_model_path="$LYRA_PREFIX/model_coeffs"]
AC_DEFINE(PJMEDIA_HAS_LYRA_CODEC,1)
dnl # use single quoted preprocessor definition
LYRA_CPPFLAGS="'-DGLOG_DEPRECATED=__attribute__((deprecated))' '-DGLOG_EXPORT=__attribute__((visibility(\"default\")))' '-DGLOG_NO_EXPORT=__attribute__((visibility(\"default\")))'"
Expand Down
1 change: 1 addition & 0 deletions pjmedia/build/os-auto.mak.in
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ ifeq ($(AC_NO_LYRA_CODEC),1)
export CFLAGS += -DPJMEDIA_HAS_LYRA_CODEC=0
else
export CODEC_OBJS += lyra.o
export CFLAGS += -DPJMEDIA_CODEC_LYRA_DEFAULT_MODEL_PATH='"@ac_lyra_model_path@"'
endif

#
Expand Down
11 changes: 11 additions & 0 deletions pjmedia/include/pjmedia-codec/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@
# define PJMEDIA_CODEC_LYRA_DEFAULT_BIT_RATE 3200
#endif

/**
* Lyra default model path containing lyra_config.binarypb, lyragan.tflite,
* quantizer.tflite and soundstream_encoder.tflite file. If autoconf is used,
* it will be set to "[lyra src folder]/model_coeffs".
*
* Default: "model_coeffs"
*/
#ifndef PJMEDIA_CODEC_LYRA_DEFAULT_MODEL_PATH
# define PJMEDIA_CODEC_LYRA_DEFAULT_MODEL_PATH "model_coeffs"
#endif

/**
* Specify if FFMPEG codecs are available.
*
Expand Down
9 changes: 6 additions & 3 deletions pjmedia/include/pjmedia-codec/lyra.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@

PJ_BEGIN_DECL

/**
* Lyra codec setting;
*/
typedef struct pjmedia_codec_lyra_config
{
unsigned bit_rate;
pj_str_t model_path;
unsigned bit_rate; /**< The expected bit rate from remote. */
pj_str_t model_path; /**< The path to the model files. */
} pjmedia_codec_lyra_config;

/**
Expand Down Expand Up @@ -80,7 +83,7 @@ pjmedia_codec_lyra_get_config( pjmedia_codec_lyra_config *cfg);
* @return PJ_SUCCESS on success.
*/
PJ_DECL(pj_status_t)
pjmedia_codec_lyra_set_default_param(const pjmedia_codec_lyra_config *cfg);
pjmedia_codec_lyra_set_config(const pjmedia_codec_lyra_config *cfg);


PJ_END_DECL
Expand Down
75 changes: 64 additions & 11 deletions pjmedia/src/pjmedia-codec/lyra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#define LYRA_MAX_PATH_LEN 64
#define LYRA_DEFAULT_PATH "model_coeffs"

static char LYRA_STR[] = "lyra";
static char BIT_RATE_STR[] = "bitrate";

using namespace chromemedia::codec;

/* Prototypes for lyra factory */
Expand Down Expand Up @@ -131,6 +134,8 @@ struct lyra_data
pj_bool_t vad_enabled;
pj_bool_t plc_enabled;
unsigned samples_per_frame;
unsigned enc_bit_rate;
unsigned dec_bit_rate;

std::unique_ptr<LyraEncoder> enc;
std::unique_ptr<LyraDecoder> dec;
Expand Down Expand Up @@ -198,7 +203,8 @@ PJ_DEF(pj_status_t) pjmedia_codec_lyra_init(pjmedia_endpt *endpt)
}

lyra_cfg.bit_rate = PJMEDIA_CODEC_LYRA_DEFAULT_BIT_RATE;
pj_ansi_strxcpy(lyra_factory.model_path_buf, LYRA_DEFAULT_PATH,
pj_ansi_strxcpy(lyra_factory.model_path_buf,
PJMEDIA_CODEC_LYRA_DEFAULT_MODEL_PATH ,
sizeof(lyra_factory.model_path_buf));
lyra_cfg.model_path = pj_str(lyra_factory.model_path_buf);

Expand Down Expand Up @@ -249,7 +255,6 @@ PJ_DEF(pj_status_t) pjmedia_codec_lyra_deinit(void)
static pj_status_t lyra_test_alloc(pjmedia_codec_factory *factory,
const pjmedia_codec_info *info)
{
const pj_str_t lyra_tag = { "lyra", 4 };
unsigned i;

PJ_UNUSED_ARG(factory);
Expand All @@ -259,7 +264,7 @@ static pj_status_t lyra_test_alloc(pjmedia_codec_factory *factory,
return PJMEDIA_CODEC_EUNSUP;

/* Check encoding name. */
if (pj_stricmp(&info->encoding_name, &lyra_tag) != 0)
if (pj_strcmp2(&info->encoding_name, LYRA_STR) != 0)
return PJMEDIA_CODEC_EUNSUP;

/* Check clock-rate */
Expand All @@ -273,6 +278,23 @@ static pj_status_t lyra_test_alloc(pjmedia_codec_factory *factory,
return PJMEDIA_CODEC_EUNSUP;
}

static char *get_bit_rate_val(unsigned bit_rate)
{
static char buf_3200[] = "3200";
static char buf_6000[] = "6000";
static char buf_9200[] = "9200";

switch (bit_rate) {
case 3200:
return buf_3200;
case 6000:
return buf_6000;
case 9200:
return buf_9200;
}
return 0;
}

static pj_status_t lyra_default_attr(pjmedia_codec_factory *factory,
const pjmedia_codec_info *id,
pjmedia_codec_param *attr)
Expand Down Expand Up @@ -301,6 +323,10 @@ static pj_status_t lyra_default_attr(pjmedia_codec_factory *factory,
attr->info.pcm_bits_per_sample = 16;
attr->info.frm_ptime = 20;
attr->setting.frm_per_pkt = 1;
attr->setting.dec_fmtp.cnt = 1;
pj_strset2(&attr->setting.dec_fmtp.param[0].name, BIT_RATE_STR);
pj_strset2(&attr->setting.dec_fmtp.param[0].val,
get_bit_rate_val(lyra_cfg.bit_rate));

/* Default flags. */
attr->setting.cng = 0;
Expand Down Expand Up @@ -332,7 +358,7 @@ static pj_status_t lyra_enum_codecs(pjmedia_codec_factory *factory,
continue;

pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
codecs[*count].encoding_name = pj_str("lyra");
pj_strset2(&codecs[*count].encoding_name, LYRA_STR);
codecs[*count].pt = lyra_factory.param[i].pt;
codecs[*count].type = PJMEDIA_TYPE_AUDIO;
codecs[*count].clock_rate = lyra_factory.param[i].clock_rate;
Expand Down Expand Up @@ -369,6 +395,7 @@ static pj_status_t lyra_alloc_codec(pjmedia_codec_factory *factory,
}

lyra_data->pool = pool;
lyra_data->dec_bit_rate = lyra_cfg.bit_rate;

codec->op = &lyra_op;
codec->codec_data = lyra_data;
Expand Down Expand Up @@ -414,12 +441,32 @@ static pj_status_t lyra_codec_open(pjmedia_codec *codec,

pj_mutex_lock(lyra_data->mutex);

PJ_LOG(4, (THIS_FILE, "Codec opening, model_path=%.*s",
(int)lyra_cfg.model_path.slen, lyra_cfg.model_path.ptr));

/* Get encoder bit_rate */
for (unsigned i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
if (pj_strcmp2(&attr->setting.enc_fmtp.param[i].name,
BIT_RATE_STR) == 0)
{
lyra_data->enc_bit_rate = (pj_uint16_t)
pj_strtoul(&attr->setting.enc_fmtp.param[i].val);
break;
}
}
if (lyra_data->enc_bit_rate == 0 ||
(lyra_data->enc_bit_rate != 3200 && lyra_data->enc_bit_rate != 6000 &&
lyra_data->enc_bit_rate != 9200))
{
lyra_data->enc_bit_rate = PJMEDIA_CODEC_LYRA_DEFAULT_BIT_RATE;
}

lyra_data->vad_enabled = (attr->setting.vad != 0);
lyra_data->plc_enabled = (attr->setting.plc != 0);

lyra_data->enc = LyraEncoder::Create(attr->info.clock_rate,
attr->info.channel_cnt,
lyra_cfg.bit_rate,
lyra_data->enc_bit_rate,
lyra_data->vad_enabled,
model_path);
if (lyra_data->enc == nullptr) {
Expand All @@ -440,10 +487,11 @@ static pj_status_t lyra_codec_open(pjmedia_codec *codec,
attr->info.clock_rate / lyra_data->enc->frame_rate();

PJ_LOG(4, (THIS_FILE, "Codec opened, model_path=%.*s, chan_cnt=%d, "
"bitrate=%d, clockrate=%d, vad=%d, frame_rate=%d, "
"samples_per_frame=%d",
lyra_cfg.model_path.slen, lyra_cfg.model_path.ptr,
attr->info.channel_cnt, lyra_cfg.bit_rate, attr->info.clock_rate,
"enc_bit_rate=%d, dec_bit_rate=%d, clockrate=%d, vad=%d, "
"frame_rate=%d, samples_per_frame=%d",
(int)lyra_cfg.model_path.slen, lyra_cfg.model_path.ptr,
attr->info.channel_cnt, lyra_data->enc_bit_rate,
lyra_data->dec_bit_rate, attr->info.clock_rate,
lyra_data->vad_enabled, lyra_data->enc->frame_rate(),
lyra_data->samples_per_frame));

Expand Down Expand Up @@ -487,8 +535,8 @@ static pj_status_t lyra_codec_parse(pjmedia_codec *codec,
pjmedia_frame frames[])
{
unsigned count = 0;
unsigned frm_size = lyra_cfg.bit_rate / (50 * 8);
struct lyra_data* lyra_data = (struct lyra_data*)codec->codec_data;
unsigned frm_size = lyra_data->dec_bit_rate / (50 * 8);

PJ_UNUSED_ARG(codec);

Expand Down Expand Up @@ -649,8 +697,13 @@ pjmedia_codec_lyra_get_config(pjmedia_codec_lyra_config *cfg)
}

PJ_DEF(pj_status_t)
pjmedia_codec_lyra_set_default_param(const pjmedia_codec_lyra_config *cfg)
pjmedia_codec_lyra_set_config(const pjmedia_codec_lyra_config *cfg)
{
if (cfg->bit_rate != 3200 && cfg->bit_rate != 6000 &&
cfg->bit_rate != 9200)
{
return PJ_EINVAL;
}
lyra_cfg.bit_rate = cfg->bit_rate;
pj_strncpy_with_null(&lyra_cfg.model_path, &cfg->model_path,
PJ_ARRAY_SIZE(lyra_factory.model_path_buf));
Expand Down
6 changes: 3 additions & 3 deletions pjsip/include/pjsua2/media.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2642,12 +2642,12 @@ struct CodecOpusConfig
};

/**
* Lyra codec parameters setting;
* Lyra codec setting;
*/
struct CodecLyraConfig
{
unsigned bit_rate; /**< Codec bit rate. */
string model_path; /**< Model path. */
unsigned bit_rate; /**< The expected bit rate from remote. */
string model_path; /**< The path to the model files. */

pjmedia_codec_lyra_config toPj() const;
void fromPj(const pjmedia_codec_lyra_config &config);
Expand Down
2 changes: 1 addition & 1 deletion pjsip/src/pjsua2/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2548,7 +2548,7 @@ void Endpoint::setCodecLyraConfig(const CodecLyraConfig &lyra_cfg)
pjmedia_codec_lyra_config new_lyra_cfg;
new_lyra_cfg = lyra_cfg.toPj();

PJSUA2_CHECK_EXPR(pjmedia_codec_lyra_set_default_param(&new_lyra_cfg));
PJSUA2_CHECK_EXPR(pjmedia_codec_lyra_set_config(&new_lyra_cfg));
#else
PJ_UNUSED_ARG(lyra_cfg);

Expand Down

0 comments on commit 688174c

Please sign in to comment.