海康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数据输入,编码成G722
        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;
}

 

你可能感兴趣的:(C++,ubuntu)