Adding Opus Codec to PJSIP | How to add new codec to pjsip

How to add custom media codec to pjsip

Hi, In this post, I will show you how to add a custom codec to pjsip 2.3

Introduction:

http://www.pjsip.org/
PJSIP is a very strong and widely used in voip. you can combine pjsip with kamailio,opensips, stund, turn server, free switch to build chat application like Skype with many well feature like message, call, conversation.v.v.

http://www.opus-codec.org/
Opus is a lightweight media codec with very good quality but also cost minimum bandwidth (you can customize it bitrate yourself)

To port opus codec you should choose a codec and follow it config (gsm for example)

A little bit you need to know about pjsip build system (make file)

PJSIP uses a set of make files to build, if you familiar with gnumake, it’s very easy to
understand PJSIP. But if you are not, here are some tips to help you easily maintain PJSIP and solve
the bugs when you are buiding project.
Notice the “build” dir, it’s contain make file you need to configure.

files *.in is input files, run configure and it will generate *.mak files based on *.in files

Porting opus with pjsip interface

1) Add opus source code:
you must write api for opus to work with pjmedia, ex: (view resource file for more)
PJ_DECL(pj_status_t) pjmedia_codec_opus_init(pjmedia_endpt *endpt);

PJ_DECL(pj_status_t) pjmedia_codec_opus_deinit(void);

+ after write opus.h and opus.c file for pjmedia. put it in:
header file: pjmedia/include/pjmedia-codec
source file: pjmedia/src/pjmedia-codec

+ add opus source to third_party. 
+ int third_party/build create new opus dir, write Makefile and config for all c file in opus

2) Config pjsip build system, find appropriate place to port this code, often after g7221 config, i may miss few files but you just follow the g7221 codec configuration. 

+ aconfigure.ac

line 846:
dnl # Include opus codec
AC_SUBST(ac_has_opus_codec)
AC_ARG_ENABLE(opus_codec,
AC_HELP_STRING([–enable-opus-codec],
[Enable OPUS support (derived from ITU implementation).]),
[ac_has_opus_codec=1]
AC_DEFINE(PJMEDIA_HAS_OPUS_CODEC,1)
AC_MSG_RESULT([Checking if opus codec is disabled…no]),
[ac_has_opus_codec=]
AC_MSG_RESULT([Checking if opus codec is disabled…yes])
)

+ pjsip/build.mak.in

ifeq (@ac_has_opus_codec@,1)
APP_THIRD_PARTY_LIB_FILES += $(PJ_DIR)/third_party/lib/libopuscodec-$(LIB_SUFFIX)
ifeq ($(PJ_SHARED_LIBRARIES),)
APP_THIRD_PARTY_LIBS += -lopuscodec-$(TARGET_NAME)
else
APP_THIRD_PARTY_LIBS += -lopuscodec
APP_THIRD_PARTY_LIB_FILES += $(PJ_DIR)/third_party/lib/libopuscodec.$(SHLIB_SUFFIX).$(PJ_VERSION_MAJOR) $(PJ_DIR)/third_party/lib/libopuscodec.$(SHLIB_SUFFIX)
endif
endif

+ pjmedia/build/os-auto.mak.in

AC_HAS_OPUS_CODEC=@ac_has_opus_codec@
ifeq ($(AC_HAS_OPUS_CODEC),1)
export CFLAGS += -I$(THIRD_PARTY)/opus/include -DPJMEDIA_HAS_OPUS_CODEC=1
export CODEC_OBJS += opus.o
endif

+ third_party/build/os-auto.mak.in

ifeq (@ac_has_opus_codec@,1)
DIRS += opus
endif
+ pjmedia/include/pjmedia-codec/config.h

/**
 * Unless specified otherwise, OPUS codec is not included by default.
 */
#ifndef PJMEDIA_HAS_OPUS_CODEC
#   define PJMEDIA_HAS_OPUS_CODEC    1
#endif

+ pjmedia/include/pjmedia-codec/config_auto.h

/* OPUS codec */
#ifndef PJMEDIA_HAS_OPUS_CODEC
#define PJMEDIA_HAS_OPUS_CODEC 1
#endif

+ pjmedia/include/pjmedia-codec/config_auto.h.in

/* OPUS codec */
#ifndef PJMEDIA_HAS_OPUS_CODEC
#undef PJMEDIA_HAS_OPUS_CODEC
#endif

+ pjmedia/src/pjmedia-codec/audio_codecs.c

#if PJMEDIA_HAS_OPUS_CODEC
    status = pjmedia_codec_opus_init(endpt);
    if (status != PJ_SUCCESS) {
        return status;
    }
#endif /* PJMEDIA_HAS_OPUS_CODEC */

3) run config and build pjsip

autoconf acconfigure.ac > acconfigure
configure –with-ssl –enable-opus-codec
make-dep

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. aben says:

    Hello Venkatesh,
    Very helpful Tutorial!
    Thank you a lot!

  2. Unknown says:

    Hi,
    I'm looking for help in compiling PJ with g792 codec for iOS.
    Is there a why I Can contact you for help?
    I'm struggling with it for few days…
    Thanks.

Leave a Reply