-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon_transcribe_handler.go
163 lines (140 loc) · 3.8 KB
/
amazon_transcribe_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
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
package suzu
import (
"context"
"encoding/json"
"io"
"github.com/aws/aws-sdk-go/service/transcribestreamingservice"
zlog "github.com/rs/zerolog/log"
)
func init() {
NewServiceHandlerFuncs.register("aws", NewAmazonTranscribeHandler)
}
type AmazonTranscribeHandler struct {
Config Config
ChannelID string
ConnectionID string
SampleRate uint32
ChannelCount uint16
LanguageCode string
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
func NewAmazonTranscribeHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &AmazonTranscribeHandler{
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 AwsResult struct {
ChannelID *string `json:"channel_id,omitempty"`
IsPartial *bool `json:"is_partial,omitempty"`
TranscriptionResult
}
func NewAwsResult(err error) AwsResult {
return AwsResult{
TranscriptionResult: TranscriptionResult{
Type: "aws",
Error: err,
},
}
}
func (ar *AwsResult) WithChannelID(channelID string) *AwsResult {
ar.ChannelID = &channelID
return ar
}
func (ar *AwsResult) WithIsPartial(isPartial bool) *AwsResult {
ar.IsPartial = &isPartial
return ar
}
func (h *AmazonTranscribeHandler) Handle(ctx context.Context, reader io.Reader) (*io.PipeReader, error) {
at := NewAmazonTranscribe(h.Config, h.LanguageCode, int64(h.SampleRate), int64(h.ChannelCount))
oggReader, oggWriter := io.Pipe()
go func() {
defer oggWriter.Close()
if err := opus2ogg(ctx, reader, oggWriter, h.SampleRate, h.ChannelCount, h.Config); err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
oggWriter.CloseWithError(err)
return
}
}()
stream, err := at.Start(ctx, oggReader)
if err != nil {
return nil, err
}
r, w := io.Pipe()
go func() {
encoder := json.NewEncoder(w)
L:
for {
select {
case <-ctx.Done():
break L
case event := <-stream.Events():
switch e := event.(type) {
case *transcribestreamingservice.TranscriptEvent:
if h.OnResultFunc != nil {
if err := h.OnResultFunc(ctx, w, h.ChannelID, h.ConnectionID, h.LanguageCode, e.Transcript.Results); err != nil {
w.CloseWithError(err)
return
}
} else {
for _, res := range e.Transcript.Results {
if at.Config.FinalResultOnly {
// IsPartial: true の場合は結果を返さない
if *res.IsPartial {
continue
}
}
result := NewAwsResult(nil)
if at.Config.AwsResultIsPartial {
result.WithIsPartial(*res.IsPartial)
}
if at.Config.AwsResultChannelID {
result.WithChannelID(*res.ChannelId)
}
for _, alt := range res.Alternatives {
var message string
if alt.Transcript != nil {
message = *alt.Transcript
}
result.Message = message
if err := encoder.Encode(result); err != nil {
w.CloseWithError(err)
return
}
}
}
}
default:
break L
}
}
}
if err := stream.Err(); err != nil {
// 復帰が不可能なエラー以外は再接続を試みる
switch err.(type) {
case *transcribestreamingservice.LimitExceededException,
*transcribestreamingservice.InternalFailureException:
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
err = ErrServerDisconnected
default:
}
w.CloseWithError(err)
return
}
w.Close()
}()
return r, nil
}