-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_handler.go
129 lines (110 loc) · 2.79 KB
/
test_handler.go
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
package suzu
import (
"context"
"encoding/json"
"fmt"
"io"
"sync"
zlog "github.com/rs/zerolog/log"
)
func init() {
NewServiceHandlerFuncs.register("test", NewTestHandler)
}
type TestHandler struct {
Config Config
ChannelID string
ConnectionID string
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
func NewTestHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &TestHandler{
Config: config,
ChannelID: channelID,
ConnectionID: connectionID,
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
type TestResult struct {
ChannelID *string `json:"channel_id,omitempty"`
TranscriptionResult
}
func NewTestResult(channelID, message string) TestResult {
return TestResult{
TranscriptionResult: TranscriptionResult{
Type: "test",
Message: message,
},
ChannelID: &channelID,
}
}
func (h *TestHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
}
func (h *TestHandler) GetRetryCount() int {
return h.RetryCount
}
func (h *TestHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
}
func (h *TestHandler) Handle(ctx context.Context, opusCh chan opusChannel, header soraHeader) (*io.PipeReader, error) {
r, w := io.Pipe()
reader := opusChannelToIOReadCloser(ctx, opusCh)
go func() {
encoder := json.NewEncoder(w)
for {
buf := make([]byte, FrameSize)
n, err := reader.Read(buf)
if err != nil {
if err != io.EOF {
if err := encoder.Encode(NewSuzuErrorResponse(err)); err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
}
}
w.CloseWithError(err)
return
}
if n > 0 {
message := fmt.Sprintf("n: %d", n)
channelID := &[]string{"ch_0"}[0]
result := NewTestResult(*channelID, message)
if h.OnResultFunc != nil {
if err := h.OnResultFunc(ctx, w, h.ChannelID, h.ConnectionID, h.LanguageCode, result); err != nil {
if err := encoder.Encode(NewSuzuErrorResponse(err)); err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
}
w.CloseWithError(err)
return
}
} else {
if err := encoder.Encode(result); err != nil {
w.CloseWithError(err)
return
}
}
}
}
}()
return r, nil
}