/*
|
* Copyright(C) 2010,Custom Co., Ltd
|
* FileName: Voice.cpp
|
* Description:
|
* Version: 1.0
|
* Author: panyadong
|
* Create Date: 2010-6-7
|
* Modification History��
|
*/
|
|
#include "HCNetSDK.h"
|
#include "public.h"
|
#include "Voice.h"
|
#include <stdio.h>
|
//#include <map>
|
//#include <string>
|
|
#include <string.h>
|
#ifdef _WIN32
|
#elif defined(__linux__)
|
#include <unistd.h>
|
|
//std::map<std::string, char*> FileName2Byte;
|
|
#endif
|
|
#ifdef __cplusplus
|
extern "C" { // 即使这是一个C++程序,下列这个函数的实现也要以C约定的风格来搞!
|
#endif
|
|
void CALLBACK fdwVoiceDataCallBack(LONG lVoiceComHandle, char *pRecvDataBuffer, DWORD dwBufSize, BYTE byAudioFlag, DWORD pUser)
|
{
|
|
}
|
|
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. byAudioFlag:%d\n", icount, dwBufSize, byAudioFlag);
|
// icount++;
|
// //Send data to device after getting data.
|
// char pSendData[160] = {0};
|
//
|
// while(1){
|
//
|
// NET_DVR_VoiceComSendData(lVoiceComHandle, pcbdata + icount*160, 160);
|
// icount++;
|
// usleep(40000);
|
// }
|
}
|
|
int sendVoiceTrans(char *ip, char *userName, char *password, char *filename)
|
{
|
|
long lUserID;
|
//login
|
NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
|
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
|
struLoginInfo.bUseAsynLogin = false;
|
|
struLoginInfo.wPort = 8000;
|
memcpy(struLoginInfo.sDeviceAddress, ip, NET_DVR_DEV_ADDRESS_MAX_LEN);
|
memcpy(struLoginInfo.sUserName, userName, NAME_LEN);
|
memcpy(struLoginInfo.sPassword, password, NAME_LEN);
|
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
|
if (lUserID < 0)
|
{
|
printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
|
return HPR_ERROR;
|
}
|
|
//read g711a file
|
FILE *fp = fopen(filename,"rt");
|
#define AUDIOMAXLEN (1024*1024)
|
char pcbdata[AUDIOMAXLEN] = {0};
|
int len = fread(pcbdata,1,AUDIOMAXLEN,fp);
|
fclose(fp);
|
|
//start voice
|
long lVoiceHanle;
|
printf("NET_DVR_StartVoiceCom_MR_V30\n");
|
lVoiceHanle = NET_DVR_StartVoiceCom_MR_V30(lUserID, 0, 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;
|
}
|
//
|
// LPNET_DVR_COMPRESSION_AUDIO audioType;
|
// printf("audioType ret:%d \n", NET_DVR_GetCurrentAudioCompress(lUserID, audioType));
|
// printf("audioType->byAudioEncType:%d \n", audioType->byAudioEncType);
|
// printf("audioType->byAudioBitRate:%d \n", audioType->byAudioBitRate);
|
// printf("audioType->byAudioSamplingRate:%d \n", audioType->byAudioSamplingRate);
|
|
int icount = 0;
|
while(1){
|
if((icount+1)*160 > len){
|
break;
|
}
|
NET_DVR_VoiceComSendData(lVoiceHanle, pcbdata + icount*160, 160);
|
icount++;
|
usleep(2000);
|
}
|
|
if (!NET_DVR_StopVoiceCom(lVoiceHanle))
|
{
|
printf("pyd---NET_DVR_StopVoiceCom fail!\n");
|
NET_DVR_Logout(lUserID);
|
return HPR_ERROR;
|
}
|
|
NET_DVR_Logout(lUserID);
|
|
return HPR_OK;
|
}
|
|
int SendVoice(char *ip, char *userName, char *password, char *filename)
|
{
|
char libPath[128] = "/opt/vasystem/libs/hic";
|
|
NET_DVR_LOCAL_SDK_PATH struComPath = {0};
|
sprintf(struComPath.sPath, "%s", libPath); //HCNetSDKCom文件夹所在的路径
|
NET_DVR_SetSDKInitCfg(NET_SDK_INIT_CFG_SDK_PATH, &struComPath);
|
|
NET_DVR_Init();
|
if (sendVoiceTrans(ip, userName, password, filename) == HPR_ERROR)
|
{
|
NET_DVR_Cleanup();
|
return HPR_ERROR;
|
}
|
|
printf("pyd---Test Voice successfully!\n");
|
NET_DVR_Cleanup();
|
return HPR_OK;
|
}
|
//
|
//#define AUDIOMAXLEN (1024*1024)
|
//bool AddVoiceFile(std::string basePath, std::string fileName){
|
// std::string filePath = basePath + "/" + fileName;
|
// //read g711a file
|
// FILE *fp = fopen(filePath.c_str(),"rb");
|
// if(fp == NULL){
|
// return false;
|
// }
|
//
|
// fseek(fp, 0L, SEEK_END);
|
// int flen = (int)ftell(fp);
|
// if(flen > AUDIOMAXLEN){
|
// flen = AUDIOMAXLEN;
|
// }
|
//
|
// char * pfile = (char *)malloc(flen+1);
|
//
|
// fseek(fp, 0L, SEEK_SET);
|
// int len = fread(pfile,1,flen,fp);
|
// fclose(fp);
|
//
|
// FileName2Byte.emplace(fileName, pfile);
|
//
|
// return true;
|
//}
|
//
|
//bool DeleteVoiceFile(std::string fileName){
|
// auto iter = FileName2Byte.find(fileName);
|
//
|
// if(iter != FileName2Byte.end()){
|
// FileName2Byte.erase(fileName);
|
// }
|
//
|
// return true;
|
//}
|
|
|
|
#ifdef __cplusplus
|
}
|
#endif
|