private string StopSpeech() { try { _sendbuff = new byte[1000]; //请求 var rtn = EvrcProtocal.proto_make_release_mic_packet(PubUser.default_group, ref _sendbuff[0]); _clientSocket.Send(_sendbuff, rtn, SocketFlags.None); _recvbuff = new byte[1000]; int len = _clientSocket.Receive(_recvbuff, 1000, SocketFlags.None); if (len == 0) { //ShowMsg("停止喊话req:没有接收到服务器响应。"); return(JsonConvert.SerializeObject(new Rst { Result = "没有接收到服务器响应" })); } uint vlen = 0; var checkedrtn = CrcCheck(_recvbuff, len, ref vlen);//CRC校验 //解包请求 var entgack = EvrcProtocal.proto_unpack_release_mic_ack(ref checkedrtn[5], vlen - 5); StopCollectSound(); //ShowMsg("停止喊话res:" + (entgack != 0)); return(JsonConvert.SerializeObject(new Rst { Result = "Success" })); } catch (Exception ex) { return(JsonConvert.SerializeObject(new Rst { Result = ex.Message })); } }
private void DataProc(uint br, IntPtr data) { //byte[] ucEvrc = new byte[AUDIOPACKLENGTH]; //ucEvrc[0] = 3;//语音数据标记 ////音频数据压缩,数据长度:输出EVRC 10 输入PCM 320,测试bug在此处后面 //EvrcProtocal.evrc_encode(ref ucEvrc[1], ref data);//todo:原有Evrc.dll方法 byte[] recv = new byte[23]; var rtn = EvrcProtocal.CCSEncode(data);//todo:替换为CCS.dll方法 Marshal.Copy(rtn, recv, 0, 23); //复制编码结果到音频流缓存区EvrcBuff中 for (var i = 0; i < 11; i++) { EvrcBuff[EvrcPtr, i] = recv[i]; } EvrcPtr++; //音频流缓存区EvrcBuff满则发UDP包至服务器 if (EvrcPtr >= 10) { SendEvrcPacket(10); EvrcPtr = 0;//重置音频流行计算偏移量 } }
/// <summary> /// 开始喊话 /// </summary> private void StartCollectSound() { try { if (IsRecoding) { return; } ////初始化音频编码解码 //EvrcProtocal.InitEncoder(); //EvrcProtocal.InitDecoder(); EvrcProtocal.StartSpeech(); var rtn = 0; hRecord = IntPtr.Zero; rtn = PcmAudio.waveInOpen(out hRecord, -1, ref lpFormat, _wd, 0, 0x00030000); if (rtn == 0)//成功返回0 { //将准备好的buffer提供给音频输入设备 for (int i = 0; i < MAXRECBUFFER; i++) { //准备一个bufrer给输入设备 PcmAudio.waveInPrepareHeader(hRecord, ref globalhdr[i], (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); //发送一个buffer给指定的输入设备,当buffer填满将会通知程序 PcmAudio.waveInAddBuffer(hRecord, ref globalhdr[i], (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); } //开启指定的输入采集设备 rtn = PcmAudio.waveInStart(hRecord); if (rtn == 0) { IsRecoding = true; //ShowMsg("正在采集音频数据..."); } else { //ShowMsg("采集音频失败"); } } } catch (Exception ex) { //ShowMsg(ex.Message); } }
/// <summary> /// CRC校验 /// </summary> /// <param name="recvbuf"></param> /// <param name="recvlength"></param> /// <param name="vLen"></param> /// <returns></returns> private static byte[] CrcCheck(byte[] recvbuf, int recvlength, ref uint vLen) { uint uiValidLen = (((uint)recvbuf[0]) << 24) | (((uint)recvbuf[1]) << 16) | (((uint)recvbuf[2]) << 8) | ((uint)recvbuf[3]); vLen = uiValidLen; if ((uiValidLen + 4) > recvlength || recvlength < 9) { return(null); } var uiReCrc = (((uint)recvbuf[uiValidLen]) << 24) | (((uint)recvbuf[uiValidLen + 1]) << 16) | (((uint)recvbuf[uiValidLen + 2]) << 8) | ((uint)recvbuf[uiValidLen + 3]); if (uiReCrc != EvrcProtocal.ZTE_CRC(ref recvbuf[0], uiValidLen)) { return(null); } return(recvbuf); }
private void Udptimer_Elapsed(object sender, ElapsedEventArgs e) { if (_voiceSocket == null || !_voiceSocket.Connected) { return; } if (gRunPar.UdpHbCnt++ >= 4) { //以下发送心跳包 var udpsndbuf = new byte[100]; var uiSndLen = EvrcProtocal.proto_make_ping_packet((uint)user.uid, ref udpsndbuf[0]);//测试ping //ShowMsg("ping服务器:" + uiSndLen); if (uiSndLen > 0) { SendUdpPacket(_voiceSocket, user.iSetPar.ip, POC_NET_UDP_PORT, udpsndbuf, uiSndLen); //ShowMsg("已发送UDP心跳包。"); } gRunPar.UdpHbCnt = 0; } }
private string Login() { try { //UDP socket _voiceSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { ReceiveTimeout = 30 * 1000, SendTimeout = 30 * 1000 }; _voiceSocket.Connect(new IPEndPoint(IPAddress.Parse(IP), POC_NET_UDP_PORT)); //开始UDP心跳轮询 udptimer = new System.Timers.Timer(); udptimer.Elapsed += Udptimer_Elapsed; udptimer.Interval = 6 * 1000; udptimer.Enabled = true; udptimer.Start(); //TCP socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { ReceiveTimeout = 30 * 1000, SendTimeout = 30 * 1000 }; _clientSocket.Connect(new IPEndPoint(IPAddress.Parse(IP), POC_NET_TCP_PORT)); //登录参数 user = new EvrcProtocal.stUser { iSetPar = { ip = IP, account = Usr, passwd = Pwd } }; //1、获取登录 _sendbuff = new byte[1000]; var loginpack = EvrcProtocal.proto_make_login_packet(ref user, ref _sendbuff[0]); _clientSocket.Send(_sendbuff, loginpack, SocketFlags.None); _recvbuff = new byte[1000]; int len = _clientSocket.Receive(_recvbuff, 1000, SocketFlags.None); uint vlen = 0; byte[] checkedrtn = CrcCheck(_recvbuff, len, ref vlen);//CRC校验 //登录解包(此处回填登录参数) var loginunpack = EvrcProtocal.proto_unpack_login_ack(ref user, ref checkedrtn[5], vlen - 5); var name = TrimStr(Encoding.UTF8.GetString(user.username)); //ShowMsg("登陆解包res:" + (loginunpack != 0) + ";uid=" + user.uid + ";username="******";defaultgroup=" + user.default_group); var rtnmsg = new ServerMsg { uid = user.uid.ToString(), username = name, defaultgroup = user.default_group.ToString(), groups = new List <GroupMsg>() }; //2、获取组 _sendbuff = new byte[1000]; var reqgroup = EvrcProtocal.proto_make_query_group_packet(ref _sendbuff[0]); //ShowMsg("获取组结果:" + reqgroup); _clientSocket.Send(_sendbuff, reqgroup, SocketFlags.None); _recvbuff = new byte[1000]; len = _clientSocket.Receive(_recvbuff, 1000, SocketFlags.None); checkedrtn = CrcCheck(_recvbuff, len, ref vlen);//CRC校验 //解包组 var upkgroup = EvrcProtocal.proto_unpack_query_group_ack(ref checkedrtn[5], vlen - 5); //ShowMsg("获取组解包res:" + (upkgroup != 0)); //0、进入群组 _sendbuff = new byte[1000]; var entg = EvrcProtocal.proto_make_enter_group_packet(user.default_group, ref _sendbuff[0]); //ShowMsg("进入群组结果:"+entg); _clientSocket.Send(_sendbuff, entg, SocketFlags.None); _recvbuff = new byte[1000]; len = _clientSocket.Receive(_recvbuff, 1000, SocketFlags.None); checkedrtn = CrcCheck(_recvbuff, len, ref vlen);//CRC校验 //解包进入群组 var entgack = EvrcProtocal.proto_unpack_enter_group_ack(ref checkedrtn[5], vlen - 5); //ShowMsg("进入群组解包res:" + (entgack != 0)); //3、解析组 var gl = EvrcProtocal.proto_get_group_list(); var grouplist = (EvrcProtocal.stGroupList)Marshal.PtrToStructure(gl, typeof(EvrcProtocal.stGroupList)); //ShowMsg("获取组结果:组数" + grouplist.uiTotal); var nowhead = grouplist.pHead; var now = (EvrcProtocal.stGroupNode)Marshal.PtrToStructure(nowhead, typeof(EvrcProtocal.stGroupNode)); while (true) { if (now == null) { break; } var name1 = TrimStr(Encoding.UTF8.GetString(now.iGroup.gname)); //ShowMsg("解析组:gid=" + now.iGroup.gid + ";gname=" + name1 + ";number_n=" + now.iGroup.number_n); GroupMsg gm = new GroupMsg { gid = now.iGroup.gid.ToString(), gname = name1, numbers = now.iGroup.number_n.ToString(), users = new List <UserMsg>() }; //4、根据组获取用户 _sendbuff = new byte[1000]; var mbr = EvrcProtocal.proto_make_query_member_packet(now.iGroup.gid, ref _sendbuff[0]);//now.iGroup.gid|user.default_group _clientSocket.Send(_sendbuff, mbr, SocketFlags.None); _recvbuff = new byte[1000]; len = _clientSocket.Receive(_recvbuff, 1000, SocketFlags.None); checkedrtn = CrcCheck(_recvbuff, len, ref vlen);//CRC校验 var dembr = EvrcProtocal.proto_unpack_query_member_ack(ref checkedrtn[5], vlen - 5); //ShowMsg("解包用户res:" + (dembr != 0)); var ml = EvrcProtocal.proto_get_user_list(); var mbrlist = (EvrcProtocal.stUserList)Marshal.PtrToStructure(ml, typeof(EvrcProtocal.stUserList)); //ShowMsg("获取用户结果:用户数" + mbrlist.uiTotal); var head = mbrlist.pHead; var mbrnow = (EvrcProtocal.stUserNode)Marshal.PtrToStructure(head, typeof(EvrcProtocal.stUserNode)); while (true) { if (mbrnow == null) { break; } var name2 = TrimStr(Encoding.UTF8.GetString(mbrnow.uname)); //ShowMsg("解析用户:uid=" + mbrnow.uid + ";online=" + mbrnow.online + ";uname=" + name2); UserMsg um = new UserMsg { uid = mbrnow.uid.ToString(), online = mbrnow.online.ToString(), uname = name2 }; gm.users.Add(um); mbrnow = (EvrcProtocal.stUserNode)Marshal.PtrToStructure(mbrnow.pNext, typeof(EvrcProtocal.stUserNode)); } rtnmsg.groups.Add(gm); now = (EvrcProtocal.stGroupNode)Marshal.PtrToStructure(now.pNext, typeof(EvrcProtocal.stGroupNode)); } PubUser = user; return(JsonConvert.SerializeObject(rtnmsg)); } catch (Exception ex) { return(JsonConvert.SerializeObject(ex.Message)); } }