在Hi3516ev300上调通PJSIP 的音频通话过程记录

关键词:pjsip、hi3516ev300、voip

呱牛笔记

第一步:pjsip在hi3516ev300的移植:

这个工作量相对较小,配置host为arm-himix100-linux编译都能顺利通过

./configure --host=arm-himix100-linux  --prefix=$PWD/install  --disable-libwebrtc --disable-libyuv --disable-v4l2 --disable-sound  --disable-opencore-amrnb --disable-speex-codec --disable-speex-aec
make

在/mpp/sample/路径下创建一个pjsip_app的路径;

简单的将simple_pjsua.c拷贝到这个路径下,修改Makefile,注意pjsip相关a库的引用顺序,参考:

https://blog.csdn.net/qq_40170041/article/details/124327913

Makefile参考:

# Hisilicon Hi35xx sample Makefile

include ../Makefile.param

SRCS := $(wildcard *.c)
#TARGET := $(SRCS:%.c=%)
SRCS += $(wildcard $(PWD)/adp/*.c)
SRCS += $(wildcard $(PWD)/g711/*.c)
SRCS += $(PWD)/opus/libopus.a
SRCS += $(wildcard $(PWD)/pjsip/*.c)
SRCS += $(PWD)/pjsip/lib/libpjsua-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjsip-ua-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjsip-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjsua2-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjsip-simple-arm-himix100-linux-gnu.a 
SRCS += $(PWD)/pjsip/lib/libpjmedia-codec-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjmedia-videodev-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjmedia-arm-himix100-linux-gnu.a 
SRCS += $(PWD)/pjsip/lib/libpjmedia-audiodev-arm-himix100-linux-gnu.a 
SRCS += $(PWD)/pjsip/lib/libpjnath-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjlib-util-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libresample-arm-himix100-linux-gnu.a 
SRCS += $(PWD)/pjsip/lib/libsrtp-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libgsmcodec-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libspeex-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libilbccodec-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libg7221codec-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpjsdp-arm-himix100-linux-gnu.a
SRCS += $(PWD)/pjsip/lib/libpj-arm-himix100-linux-gnu.a

CFLAGS += -DPJ_AUTOCONF -DPJ_LINUX=1 -L$(REL_LIB) -I./ -I/pjsip/ -I./opus/include/ -I./pjsip/include/  -Wl,-rpath=/usr/lib

TARGET := pjsip_app
OBJ := $(SRC:%.c=%.o) 
TARGET_PATH := $(PWD)

# compile linux or HuaweiLite
include $(PWD)/../$(ARM_ARCH)_$(OSTYPE).mak


第二步:

打通pjsip的音频录制、编码发送逻辑,网上有说法是使用null_dev.c ,在编译pjsip时,修改config_site.h,增加:

#define PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO      1


这样pjsip的库里面就支持null_dev录音设备了,但这样不便于修改,所以不修改config_site.h,而是将null_dev.c拷贝到上面pjsip_app的路径下,然后在pjsua_init函数后面,动态注册设备:

//register null audio factory

{

pjmedia_aud_register_factory(&pjmedia_null_audio_factory);

}




参考lagency_audio.c 修改null_dev.c,打通录制和播放即可;

/* $Id$ */
/*
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 *
 * This program 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.
 *
 * This program 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <pjmedia-audiodev/audiodev_imp.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/os.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include "audio_ringfifo.h"
#if 1

#define THIS_FILE		"null_dev.c"

/* null_audio device info */
struct null_audio_dev_info
{
    pjmedia_aud_dev_info	 info;
    unsigned			 dev_id;
};

/* null_audio factory */
struct null_audio_factory
{
    pjmedia_aud_dev_factory	 base;
    pj_pool_t			*pool;
    pj_pool_factory		*pf;

    unsigned			 dev_count;
    struct null_audio_dev_info	*dev_info;
};

/* Sound stream. */
struct null_audio_stream
{
    pjmedia_aud_stream	 base;		    /**< Base stream	       */
    pjmedia_aud_param	 param;		    /**< Settings	       */
    pj_pool_t           *pool;              /**< Memory pool.          */
    
    int                  rec_id;      	/* Capture device id		*/
    int                  quit;

    pjmedia_aud_rec_cb   rec_cb;            /**< Capture callback.     */
    unsigned             pb_buf_size;
    char		*pb_buf;
    pj_thread_t		*pb_thread;

    
    pjmedia_aud_play_cb  play_cb;           /**< Playback callback.    */
    unsigned             ca_buf_size;
    char		    *ca_buf;
    pj_thread_t		*ca_thread;
    
    void                *user_data;         /**< Application data.     */
};


/* Prototypes */
static pj_status_t null_factory_init(pjmedia_aud_dev_factory *f);
static pj_status_t null_factory_destroy(pjmedia_aud_dev_factory *f);
static pj_status_t null_factory_refresh(pjmedia_aud_dev_factory *f);
static unsigned    null_factory_get_dev_count(pjmedia_aud_dev_factory *f);
static pj_status_t null_factory_get_dev_info(pjmedia_aud_dev_factory *f,
					     unsigned index,
					     pjmedia_aud_dev_info *info);
static pj_status_t null_factory_default_param(pjmedia_aud_dev_factory *f,
					      unsigned index,
					      pjmedia_aud_param *param);
static pj_status_t null_factory_create_stream(pjmedia_aud_dev_factory *f,
					      const pjmedia_aud_param *param,
					      pjmedia_aud_rec_cb rec_cb,
					      pjmedia_aud_play_cb play_cb,
					      void *user_data,
					      pjmedia_aud_stream **p_aud_strm);

static pj_status_t null_stream_get_param(pjmedia_aud_stream *strm,
					 pjmedia_aud_param *param);
static pj_status_t null_stream_get_cap(pjmedia_aud_stream *strm,
				       pjmedia_aud_dev_cap cap,
				       void *value);
static pj_status_t null_stream_set_cap(pjmedia_aud_stream *strm,
				       pjmedia_aud_dev_cap cap,
				       const void *value);
static pj_status_t null_stream_start(pjmedia_aud_stream *strm);
static pj_status_t null_stream_stop(pjmedia_aud_stream *strm);
static pj_status_t null_stream_destroy(pjmedia_aud_stream *strm);

/* Operations */
static pjmedia_aud_dev_factory_op factory_op =
{
    &null_factory_init,
    &null_factory_destroy,
    &null_factory_get_dev_count,
    &null_factory_get_dev_info,
    &null_factory_default_param,
    &null_factory_create_stream,
    &null_factory_refresh
};

static pjmedia_aud_stream_op stream_op =
{
    &null_stream_get_param,
    &null_stream_get_cap,
    &null_stream_set_cap,
    &null_stream_start,
    &null_stream_stop,
    &null_stream_destroy
};


/****************************************************************************
 * Factory operations
 */
/*
 * Init null_audio audio driver.
 */
pjmedia_aud_dev_factory* pjmedia_null_audio_factory(pj_pool_factory *pf)
{
    struct null_audio_factory *f;
    pj_pool_t *pool;

    pool = pj_pool_create(pf, "null audio", 1000, 1000, NULL);
    f = PJ_POOL_ZALLOC_T(pool, struct null_audio_factory);
    f->pf = pf;
    f->pool = pool;
    f->base.op = &factory_op;

    return &f->base;
}


/* API: init factory */
static pj_status_t null_factory_init(pjmedia_aud_dev_factory *f)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;
    struct null_audio_dev_info *ndi;

    /* Initialize input and output devices here */
    nf->dev_count = 1;
    nf->dev_info = (struct null_audio_dev_info*)
 		   pj_pool_calloc(nf->pool, nf->dev_count,
 				  sizeof(struct null_audio_dev_info));
    ndi = &nf->dev_info[0];
    pj_bzero(ndi, sizeof(*ndi));
    strcpy(ndi->info.name, "null device");
    strcpy(ndi->info.driver, "null");
    ndi->info.input_count = 1;
    ndi->info.output_count = 1;
    ndi->info.default_samples_per_sec = 16000;
    /* Set the device capabilities here */
    ndi->info.caps = 0;

    PJ_LOG(4, (THIS_FILE, "null audio initialized"));

    return PJ_SUCCESS;
}

/* API: destroy factory */
static pj_status_t null_factory_destroy(pjmedia_aud_dev_factory *f)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;

    pj_pool_safe_release(&nf->pool);

    return PJ_SUCCESS;
}

/* API: refresh the list of devices */
static pj_status_t null_factory_refresh(pjmedia_aud_dev_factory *f)
{
    PJ_UNUSED_ARG(f);
    return PJ_SUCCESS;
}

/* API: get number of devices */
static unsigned null_factory_get_dev_count(pjmedia_aud_dev_factory *f)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;
    return nf->dev_count;
}

/* API: get device info */
static pj_status_t null_factory_get_dev_info(pjmedia_aud_dev_factory *f,
					     unsigned index,
					     pjmedia_aud_dev_info *info)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;

    PJ_ASSERT_RETURN(index < nf->dev_count, PJMEDIA_EAUD_INVDEV);

    pj_memcpy(info, &nf->dev_info[index].info, sizeof(*info));

    return PJ_SUCCESS;
}

/* API: create default device parameter */
static pj_status_t null_factory_default_param(pjmedia_aud_dev_factory *f,
					      unsigned index,
					      pjmedia_aud_param *param)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;
    struct null_audio_dev_info *di = &nf->dev_info[index];

    PJ_ASSERT_RETURN(index < nf->dev_count, PJMEDIA_EAUD_INVDEV);

    pj_bzero(param, sizeof(*param));
    if (di->info.input_count && di->info.output_count) {
	param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
	param->rec_id = index;
	param->play_id = index;
    } else if (di->info.input_count) {
	param->dir = PJMEDIA_DIR_CAPTURE;
	param->rec_id = index;
	param->play_id = PJMEDIA_AUD_INVALID_DEV;
    } else if (di->info.output_count) {
	param->dir = PJMEDIA_DIR_PLAYBACK;
	param->play_id = index;
	param->rec_id = PJMEDIA_AUD_INVALID_DEV;
    } else {
	return PJMEDIA_EAUD_INVDEV;
    }

    /* Set the mandatory settings here */
    /* The values here are just some examples */
    param->clock_rate = di->info.default_samples_per_sec;
    param->channel_count = 1;
    param->samples_per_frame = di->info.default_samples_per_sec * 20 / 1000;
    param->bits_per_sample = 16;

    /* Set the device capabilities here */
    param->flags = 0;

    return PJ_SUCCESS;
}

/* API: create stream */
static pj_status_t null_factory_create_stream(pjmedia_aud_dev_factory *f,
					      const pjmedia_aud_param *param,
					      pjmedia_aud_rec_cb rec_cb,
					      pjmedia_aud_play_cb play_cb,
					      void *user_data,
					      pjmedia_aud_stream **p_aud_strm)
{
    struct null_audio_factory *nf = (struct null_audio_factory*)f;
    pj_pool_t *pool;
    struct null_audio_stream *strm;

    /* Create and Initialize stream descriptor */
    pool = pj_pool_create(nf->pf, "null_audio-dev", 1000, 1000, NULL);
    PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);

    strm = PJ_POOL_ZALLOC_T(pool, struct null_audio_stream);
    pj_memcpy(&strm->param, param, sizeof(*param));
    strm->pool = pool;
    strm->rec_cb = rec_cb;
    strm->play_cb = play_cb;
    strm->user_data = user_data;
    strm->quit      = 0;

    /* Create player stream here */
    if (param->dir & PJMEDIA_DIR_PLAYBACK) {
        strm->pb_buf_size = 720;
	strm->pb_buf = (char*) pj_pool_alloc (strm->pool, strm->pb_buf_size);

    }

    /* Create capture stream here */
    if (param->dir & PJMEDIA_DIR_CAPTURE) {
        /* Set our buffer */
        strm->ca_buf_size = 720;
        strm->ca_buf = (char*) pj_pool_alloc (strm->pool, strm->ca_buf_size);

    }

    /* Apply the remaining settings */
    /* Below is an example if you want to set the output volume */
    if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
	null_stream_set_cap(&strm->base,
		            PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
		            &param->output_vol);
    }

    /* Done */
    strm->base.op = &stream_op;
    *p_aud_strm = &strm->base;

    return PJ_SUCCESS;
}

/* API: Get stream info. */
static pj_status_t null_stream_get_param(pjmedia_aud_stream *s,
					 pjmedia_aud_param *pi)
{
    struct null_audio_stream *strm = (struct null_audio_stream*)s;

    PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);

    pj_memcpy(pi, &strm->param, sizeof(*pi));

    /* Example: Update the volume setting */
    if (null_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
			    &pi->output_vol) == PJ_SUCCESS)
    {
        pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
    }

    return PJ_SUCCESS;
}

/* API: get capability */
static pj_status_t null_stream_get_cap(pjmedia_aud_stream *s,
				       pjmedia_aud_dev_cap cap,
				       void *pval)
{
    struct null_audio_stream *strm = (struct null_audio_stream*)s;

    PJ_UNUSED_ARG(strm);

    PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);

    /* Example: Get the output's volume setting */
    if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING)
    {
	/* Output volume setting */
	*(unsigned*)pval = 0; // retrieve output device's volume here
	return PJ_SUCCESS;
    } else {
	return PJMEDIA_EAUD_INVCAP;
    }
}

/* API: set capability */
static pj_status_t null_stream_set_cap(pjmedia_aud_stream *s,
				       pjmedia_aud_dev_cap cap,
				       const void *pval)
{
    struct null_audio_stream *strm = (struct null_audio_stream*)s;

    PJ_UNUSED_ARG(strm);

    PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);

    /* Example */
    if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING)
    {
	/* Output volume setting */
	// set output's volume level here
	return PJ_SUCCESS;
    }

    return PJMEDIA_EAUD_INVCAP;
}


extern void start_pcm_record(int sample_rate, int payload_type);
extern void stop_pcm_record();
static int pb_thread_func (void *arg)
{
    struct null_audio_stream* stream = (struct null_audio_stream*) arg;
    void* user_data            = stream->user_data;
    pj_timestamp tstamp;
    int result;
    int size                   = stream->pb_buf_size;
    char* buf 		       = stream->pb_buf;

    tstamp.u64 = 0;    

    while (!stream->quit) {
    	pjmedia_frame frame;

    	frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
    	frame.buf = buf;
    	frame.size = 160;
    	frame.timestamp.u64 = tstamp.u64;
    	frame.bit_info = 0;
 
#if 0
    	result = stream->play_cb (user_data, &frame);
    	if (result != PJ_SUCCESS || stream->quit)
    	    break;

    	if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
    	    pj_bzero (buf, size);
#endif//no value.
#if 0
    	result = snd_pcm_writei (pcm, buf, nframes);
    	if (result == -EPIPE) {
    	    PJ_LOG (4,(THIS_FILE, "pb_thread_func: underrun!"));
    	    snd_pcm_prepare (pcm);
    	} else if (result < 0) {
    	    PJ_LOG (4,(THIS_FILE, "pb_thread_func: error writing data!"));
    	}

#endif//
    	tstamp.u64 += 160;/* samples_per_frame		*/
        usleep(1000*2000);
    }

    //TRACE_((THIS_FILE, "pb_thread_func: Stopped"));
    return PJ_SUCCESS;
}

static int ca_thread_func (void *arg)
{
    struct null_audio_stream* stream = (struct null_audio_stream*) arg;
    void* user_data            = stream->user_data;
    pj_timestamp tstamp;    
    int result;
    struct audio_ringbuf temp_audio_ringinfo;
    int audio_ringbufflen  = 0;
    struct timespec ts = {0,33333};// 1|\A8\ACs = 1000ns  33us?

    tstamp.u64 = 0;

    while (!stream->quit) {
    	pjmedia_frame frame;
        
        nanosleep(&ts, NULL);
        
        //get audio
        audio_ringbufflen = audio_ringget(&temp_audio_ringinfo);
        if(audio_ringbufflen ==0){
            continue ;
        }
    	if (stream->quit)
    	    break;
        if (stream->ca_buf_size > audio_ringbufflen){
            memcpy(stream->ca_buf, temp_audio_ringinfo.buffer, audio_ringbufflen);

        }else{
            continue;
        }
        //PJ_LOG(4, (THIS_FILE, "get pcm buffer:%d", audio_ringbufflen));

    	frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
    	frame.buf = (void*) stream->ca_buf;
    	frame.size = audio_ringbufflen;
    	frame.timestamp.u64 = tstamp.u64;
    	frame.bit_info = 0;

    	result = stream->rec_cb(user_data, &frame);
    	if (result != PJ_SUCCESS || stream->quit){
         
    	    break;
        }

    	tstamp.u64 += 160;/* samples_per_frame		*/
    }

    //TRACE_((THIS_FILE, "ca_thread_func: Stopped"));

    return PJ_SUCCESS;
}

/* API: Start stream. */
static pj_status_t null_stream_start(pjmedia_aud_stream *strm)
{
    struct null_audio_stream *stream = (struct null_audio_stream*)strm;
    pj_status_t status = PJ_SUCCESS;

    PJ_LOG(4, (THIS_FILE, "Starting null audio stream"));


    stream->quit = 0;
    if (stream->param.dir & PJMEDIA_DIR_CAPTURE_PLAYBACK){
    	status = pj_thread_create (stream->pool,
    				   "hisi_playback",
    				   pb_thread_func,
    				   stream,
    				   0, //ZERO,
    				   0,
    				   &stream->pb_thread);
    	if (status != PJ_SUCCESS)
    	    return status;
    	status = pj_thread_create (stream->pool,
    				   "hisi_capture",
    				   ca_thread_func,
    				   stream,
    				   0, //ZERO,
    				   0,
    				   &stream->ca_thread);
    	if (status != PJ_SUCCESS) {
    	    stream->quit = PJ_TRUE;
    	    pj_thread_join(stream->pb_thread);
    	    pj_thread_destroy(stream->pb_thread);
    	    stream->pb_thread = NULL;
    	}
    }else if (stream->param.dir & PJMEDIA_DIR_PLAYBACK) {
    	status = pj_thread_create (stream->pool,
    				   "hisi_playback",
    				   pb_thread_func,
    				   stream,
    				   0, //ZERO,
    				   0,
    				   &stream->pb_thread);
    	if (status != PJ_SUCCESS)
    	    return status;
    }else if (stream->param.dir & PJMEDIA_DIR_CAPTURE) {
    	status = pj_thread_create (stream->pool,
    				   "hisi_capture",
    				   ca_thread_func,
    				   stream,
    				   0, //ZERO,
    				   0,
    				   &stream->ca_thread);
    	if (status != PJ_SUCCESS) {
    	    stream->quit = PJ_TRUE;
            if (stream->pb_thread != NULL){
        	    pj_thread_join(stream->pb_thread);
        	    pj_thread_destroy(stream->pb_thread);
            }
    	    stream->pb_thread = NULL;
    	}
    }
    //open one thread to get pcm buffer ?
    start_pcm_record(8000, 1);
    //start read from fifo
    

    return PJ_SUCCESS;
}

/* API: Stop stream. */
static pj_status_t null_stream_stop(pjmedia_aud_stream *strm)
{
    struct null_audio_stream *stream = (struct null_audio_stream*)strm;

    stream->quit = 1;
    PJ_LOG(4, (THIS_FILE, "null_stream_stop enter!\n"));

    if (stream->pb_thread) {
    	//TRACE_((THIS_FILE, "null_audio_stream_stop Waiting for playback to stop."));
    	pj_thread_join (stream->pb_thread);
    	//TRACE_((THIS_FILE,  "null_audio_stream_stop: playback stopped."));
    	pj_thread_destroy(stream->pb_thread);
	    stream->pb_thread = NULL;
    }
    PJ_LOG(4, (THIS_FILE, "Stopping null audio capture stream"));

    if (stream->ca_thread) {
    	//TRACE_((THIS_FILE, "null_audio_stream_stop Waiting for capture to stop."));
    	pj_thread_join (stream->ca_thread);
    	//TRACE_((THIS_FILE, "null_audio_stream_stop(%u): capture stopped."));
    	pj_thread_destroy(stream->ca_thread);
    	stream->ca_thread = NULL;
    }

    PJ_LOG(4, (THIS_FILE, "Stopping null audio stream"));
    stop_pcm_record();

    return PJ_SUCCESS;
}


/* API: Destroy stream. */
static pj_status_t null_stream_destroy(pjmedia_aud_stream *strm)
{
    struct null_audio_stream *stream = (struct null_audio_stream*)strm;
    PJ_LOG(4, (THIS_FILE, "null_stream_destroy enter!\n"));

    PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);

    null_stream_stop(strm);

    pj_pool_release(stream->pool);

    return PJ_SUCCESS;
}

#endif	/* PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO */

关键是null_stream_start 和null_stream_stop适配的两个线程,用于将录音的数据发给编码器,和将接收到的数据喂给播放器!



呱牛笔记

图片来源:https://blog.csdn.net/jack909633117/article/details/103656966

本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com

请先登录后发表评论
  • 最新评论
  • 总共0条评论