From 2db3c6339d97b906adaf5579f1b65e9d13e78c31 Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Mon, 17 Aug 2020 22:07:29 +0800 Subject: [PATCH] update ptt codec. --- client/builders.go | 4 ++-- client/client.go | 10 ++++++++-- utils/audio.go | 31 ------------------------------- 3 files changed, 10 insertions(+), 35 deletions(-) delete mode 100644 utils/audio.go diff --git a/client/builders.go b/client/builders.go index dce2e7a3..2f70e0f8 100644 --- a/client/builders.go +++ b/client/builders.go @@ -596,7 +596,7 @@ func (c *QQClient) buildImageUploadPacket(data, updKey []byte, commandId int32, } // PttStore.GroupPttUp -func (c *QQClient) buildGroupPttStorePacket(groupCode int64, md5 []byte, size, voiceLength int32) (uint16, []byte) { +func (c *QQClient) buildGroupPttStorePacket(groupCode int64, md5 []byte, size, codec, voiceLength int32) (uint16, []byte) { seq := c.nextSeq() req := &pb.D388ReqBody{ NetType: 3, @@ -614,7 +614,7 @@ func (c *QQClient) buildGroupPttStorePacket(groupCode int64, md5 []byte, size, v InnerIp: 0, BuildVer: "6.5.5.663", VoiceLength: voiceLength, - Codec: 0, + Codec: codec, VoiceType: 1, BoolNewUpChan: true, }, diff --git a/client/client.go b/client/client.go index 95bd0b9b..170b3222 100644 --- a/client/client.go +++ b/client/client.go @@ -1,6 +1,7 @@ package client import ( + "bytes" "crypto/md5" "encoding/hex" "errors" @@ -502,8 +503,13 @@ func (c *QQClient) uploadPrivateImage(target int64, img []byte, count int) (*mes func (c *QQClient) UploadGroupPtt(groupCode int64, voice []byte) (*message.GroupVoiceElement, error) { h := md5.Sum(voice) - voiceLength := utils.GetAmrDuration(voice) - seq, pkt := c.buildGroupPttStorePacket(groupCode, h[:], int32(len(voice)), voiceLength) + codec := func() int32 { + if bytes.HasPrefix(voice, []byte("#!AMR")) { + return 0 + } + return 1 + }() + seq, pkt := c.buildGroupPttStorePacket(groupCode, h[:], int32(len(voice)), codec, int32(len(voice))) r, err := c.sendAndWait(seq, pkt) if err != nil { return nil, err diff --git a/utils/audio.go b/utils/audio.go deleted file mode 100644 index c567ef0a..00000000 --- a/utils/audio.go +++ /dev/null @@ -1,31 +0,0 @@ -package utils - -// GetAmrDuration - return .amr file duration form bytesData -func GetAmrDuration(bytesData []byte) int32 { - duration := -1 - packedSize := [16]int{12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0} - - length := len(bytesData) - pos := 6 - var datas []byte - frameCount := 0 - packedPos := -1 - - for pos <= length { - datas = bytesData[pos : pos+1] - if len(datas) != 1 { - if length > 0 { - duration = (length - 6) / 650 - } else { - duration = 0 - } - break - } - packedPos = int((datas[0] >> 3) & 0x0F) - pos += packedSize[packedPos] + 1 - frameCount++ - } - - duration += frameCount * 20 - return int32(duration/1000 + 1) -}