chenshijun
2020-06-28 6b94b6095a0cda41eb01fa4211e7ce0235ab5289
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * 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