Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio streaming header 関連処理の整理 #189

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 54 additions & 70 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
)

const (
FrameSize = 1024 * 10
FrameSize = 1024 * 10
HeaderLength = 20
MaxPayloadLength = 0xffff
)

var (
Expand Down Expand Up @@ -231,6 +233,8 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
}
}

const ()

func readPacketWithHeader(reader io.Reader) (io.Reader, error) {
r, w := io.Pipe()

Expand All @@ -240,7 +244,7 @@ func readPacketWithHeader(reader io.Reader) (io.Reader, error) {
var payload []byte

for {
buf := make([]byte, 20+0xffff)
buf := make([]byte, HeaderLength+MaxPayloadLength)
n, err := reader.Read(buf)
if err != nil {
w.CloseWithError(err)
Expand All @@ -250,81 +254,61 @@ func readPacketWithHeader(reader io.Reader) (io.Reader, error) {
payload = append(payload, buf[:n]...)
length += n

if length > 20 {
// timestamp(64), sequence number(64), length(32)
h := payload[0:20]
p := payload[20:length]
// ヘッダー分のデータが揃っていないので、次の読み込みへ
if length < HeaderLength {
continue
}

payloadLength = int(binary.BigEndian.Uint32(h[16:20]))
// timestamp(64), sequence number(64), length(32)
h := payload[:HeaderLength]
p := payload[HeaderLength:]

if length == (20 + payloadLength) {
if _, err := w.Write(p); err != nil {
w.CloseWithError(err)
return
}
payload = []byte{}
length = 0
continue
}
payloadLength = int(binary.BigEndian.Uint32(h[16:HeaderLength]))

// payload が足りないので、次の読み込みへ
if length < (HeaderLength + payloadLength) {
continue
}

// payload が足りないのでさらに読み込む
if length < (20 + payloadLength) {
// 前の payload へ追加して次へ
payload = append(payload, p...)
continue
if _, err := w.Write(p[:payloadLength]); err != nil {
w.CloseWithError(err)
return
}

payload = p[payloadLength:]
length = len(payload)

// 全てのデータを書き込んだ場合は次の読み込みへ
if length == 0 {
continue
}

// 次の frame が含まれている場合
for {
// ヘッダー分のデータが揃っていないので、次の読み込みへ
if length < HeaderLength {
break
}

// 次の frame が含まれている場合
if length > (20 + payloadLength) {
if _, err := w.Write(p[:payloadLength]); err != nil {
w.CloseWithError(err)
return
}
// 次の payload 処理へ
payload = p[payloadLength:]
length = len(payload)

// 次の payload がすでにある場合の処理
for {
if length > 20 {
h = payload[0:20]
p = payload[20:length]

payloadLength = int(binary.BigEndian.Uint32(h[16:20]))

// すでに次の payload が全てある場合
if length == (20 + payloadLength) {
if _, err := w.Write(p); err != nil {
w.CloseWithError(err)
return
}
payload = []byte{}
length = 0
continue
}

if length > (20 + payloadLength) {
if _, err := w.Write(p[:payloadLength]); err != nil {
w.CloseWithError(err)
return
}

// 次の payload 処理へ
payload = p[payloadLength:]
length = len(payload)
continue
}
} else {
// payload が足りないので、次の読み込みへ
break
}
}
h = payload[:HeaderLength]
p = payload[HeaderLength:]

continue
payloadLength = int(binary.BigEndian.Uint32(h[16:HeaderLength]))

// payload が足りないので、次の読み込みへ
if length < (HeaderLength + payloadLength) {
break
}
} else {
// ヘッダー分に足りなければ次の読み込みへ
continue

// データが足りているので payloadLength まで書き込む
if _, err := w.Write(p[:payloadLength]); err != nil {
w.CloseWithError(err)
return
}

// 残りの処理へ
payload = p[payloadLength:]
length = len(payload)
}
}
}()
Expand Down
Loading