海康SDK语⾳转发实现(ubuntu)
海康的SDK技术⽂档⽐较坑,且不说介绍不全⾯,⽽且好多地⽅⾃相⽭盾,综合多⽅⾯研究才摸索出正确的语⾳转发⽅式。
注明:语⾳转发是为了实现海康摄像头与PC间的双向语⾳通信,不同于对讲,这个主要是为了实现播放⾃⼰的视频⽂件。
直接上代码:
#define  HPR_OK 0
#define  HPR_ERROR -1
void CALLBACK fVoiceDataCallBack(LONG lVoiceComHandle, char *pRecvDataBuffer, DWORD dwBufSize, BYTE byAudioFlag, void* pUser) {
static int icount = 0;
// printf("  pyd---%5d Get voice data. size:%d.\n", icount, dwBufSize);
icount++;
//Send data to device after getting data.
char pSendData[80] = {0};
//NET_DVR_VoiceComSendData(lVoiceComHandle, pSendData, 80);
}
int Demo_VoiceTrans(string camIp,string camUser,string camPwd,int camPort,string vedioPath)
{
long lUserID;
void* lVoiceInit;
bool isG722;
//login
NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
struLoginInfo.bUseAsynLogin = false;
struLoginInfo.wPort = camPort;
memcpy(struLoginInfo.sDeviceAddress,camIp.c_str(), NET_DVR_DEV_ADDRESS_MAX_LEN);
memcpy(struLoginInfo.sUserName, camUser.c_str(), NAME_LEN);
memcpy(struLoginInfo.sPassword, camPwd.c_str(), NAME_LEN);
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
if (lUserID < 0)
{
printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
return HPR_ERROR;
}
/**************Encode Audio in G.722 Mode**************/
LPVOID hEncInstance = 0;
NET_DVR_AUDIOENC_INFO info_param;
int blockcount= 0;
hEncInstance = NET_DVR_InitG722Encoder(&info_param); //初始化G722编码
if ((long)hEncInstance == -1)
{
printf("NET_DVR_InitG722Encoder fail,err %d!\n", NET_DVR_GetLastError());
return HPR_ERROR;
}
NET_DVR_AUDIOENC_PROCESS_PARAM  enc_proc_param;
FILE                *infile;
short encode_input[1280];  //20ms
unsigned char encoded_data[80];
infile=fopen(vedioPath.c_str(),"rb");
if(infile == NULL)
{
printf("infile open error.");
return HPR_ERROR;
}
enc_proc_param.in_buf  = (unsigned char *)encode_input;  //输⼊数据缓冲区,存放编码前PCM原始⾳频数据
enc_proc_param.out_buf  = (unsigned char *)encoded_data;  //输出数据缓冲区,存放编码后⾳频数据
long lVoiceHanle;
lVoiceHanle = NET_DVR_StartVoiceCom_MR_V30(lUserID, 1, fVoiceDataCallBack, NULL);
if (lVoiceHanle < 0)
{
printf("pyd---NET_DVR_StartVoiceCom_MR_V30 fail! %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
return HPR_ERROR;
}
while (1)
{
int encode_samples = fread((short *)enc_proc_param.in_buf,1,info_param.in_frame_size,infile);
if (encode_samples < 1280)
{
fclose(infile);
break;
}
blockcount++;
//PCM数据输⼊,编码成G722sdk
BOOL ret = NET_DVR_EncodeG722Frame(hEncInstance, &enc_proc_param);
if (ret != 1)
{
NET_DVR_Logout(lUserID);
return HPR_ERROR;
}
if(!NET_DVR_VoiceComSendData(lVoiceHanle,(char*)enc_proc_param.out_buf,enc_proc_param.out_frame_size))        {
printf("pyd---NET_DVR_VoiceComSendData fail! %d\n", NET_DVR_GetLastError());
continue;
}
sleep(0.02);
}
//释放G722编码资源
NET_DVR_ReleaseG722Encoder(hEncInstance);
sleep(5);
NET_DVR_StopVoiceCom(lVoiceHanle);
NET_DVR_Logout(lUserID);
return HPR_OK;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。