-
Notifications
You must be signed in to change notification settings - Fork 43
/
transcription_api.go
265 lines (221 loc) · 8.71 KB
/
transcription_api.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"fmt"
"github.com/mattermost/mattermost-plugin-calls/server/public"
"time"
"github.com/mattermost/calls-offloader/public/job"
"github.com/mattermost/mattermost/server/public/model"
)
const transcriptionJobStartTimeout = time.Minute
func (p *Plugin) transcriptionJobTimeoutChecker(callID, jobID string) {
time.Sleep(transcriptionJobStartTimeout)
state, err := p.lockCall(callID)
if err != nil {
p.LogError("failed to lock call", "err", err.Error())
return
}
defer p.unlockCall(callID)
trState, err := state.getTranscription()
if err != nil {
p.LogWarn("failed to get transcription state", "err", err.Error(), "callID", callID, "jobID", jobID)
return
}
var lcState *jobState
if cfg := p.getConfiguration(); cfg != nil && cfg.liveCaptionsEnabled() {
var err error
if lcState, err = state.getLiveCaptions(); err != nil {
p.LogError("failed to get live captions state", "error", err.Error())
// Note: not returning because we still want to finish ending the transcriber
}
}
// If the transcription hasn't started (bot hasn't joined yet) we notify the
// client.
if trState.StartAt == 0 {
if trState.JobID != jobID {
p.LogInfo("a new job has started in between, exiting", "callID", callID, "jobID", jobID)
return
}
p.LogError("timed out waiting for transcriber bot to join", "callID", callID, "jobID", jobID)
state.Call.Transcription = nil
if lcState != nil {
state.Call.LiveCaptions = nil
}
if err := p.kvSetChannelState(callID, state); err != nil {
p.LogError("failed to set channel state", "err", err.Error())
return
}
clientState := trState.getClientState()
clientState.Err = "failed to start transcriber job: timed out waiting for bot to join call"
clientState.EndAt = time.Now().UnixMilli()
if state.Call.Recording != nil && state.Call.Recording.EndAt == 0 {
recClientState := state.Call.Recording.getClientState()
if _, _, err := p.stopRecordingJob(state, callID); err != nil {
p.LogError("failed to stop recording job", "err", err.Error(), "callID", callID, "jobID", jobID)
}
// This is needed as we don't yet handle wsEventCallTranscriptionState on
// the client since jobs are coupled.
recClientState.Err = "failed to start transcriber job: timed out waiting for bot to join call"
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recClientState.toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
// MM-57224: deprecated, remove when not needed by mobile pre 2.14.0
p.publishWebSocketEvent(wsEventCallRecordingState, map[string]interface{}{
"callID": callID,
"recState": recClientState.toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
// MM-57265: This message is delayed and not shown to the client correctly.
jobState := clientState.toMap()
jobState["type"] = public.JobTypeTranscribing
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": jobState,
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}
func (p *Plugin) startTranscribingJob(state *channelState, callID, userID, trID string) (rerr error) {
if state.Call.Transcription != nil && state.Call.Transcription.EndAt == 0 {
return fmt.Errorf("transcription already in progress")
}
if trID == "" {
return fmt.Errorf("trID should not be empty")
}
trState := new(jobState)
trState.Type = public.JobTypeTranscribing
trState.ID = trID
trState.CreatorID = userID
trState.InitAt = time.Now().UnixMilli()
state.Call.Transcription = trState
liveCaptionsOn := false
if cfg := p.getConfiguration(); cfg != nil && cfg.liveCaptionsEnabled() {
liveCaptionsOn = true
lcState := new(jobState)
lcState.Type = public.JobTypeCaptioning
lcState.ID = trID
lcState.CreatorID = userID
lcState.InitAt = time.Now().UnixMilli()
state.Call.LiveCaptions = lcState
}
if err := p.kvSetChannelState(callID, state); err != nil {
return fmt.Errorf("failed to set channel state: %w", err)
}
defer func() {
// In case of any error we relay it to the client.
if rerr != nil && trState != nil {
trState.EndAt = time.Now().UnixMilli()
trState.Err = rerr.Error()
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": trState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}()
// Sending the event prior to making the API call to the job service
// since it could take a few seconds to complete and we want clients
// to get their local state updated as soon as it changes on the server.
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": trState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
// Note: We don't need to send the live captions event until we get the StartAt in the
// bot_api handleBotPostJobsStatus
// We don't want to keep the lock while making the API call to the service since it
// could take a while to return. We lock again as soon as this returns.
p.unlockCall(callID)
trJobID, jobErr := p.getJobService().RunJob(job.TypeTranscribing, callID, state.Call.PostID, trState.ID, p.botSession.Token)
state, err := p.lockCall(callID)
if err != nil {
return fmt.Errorf("failed to lock call: %w", err)
}
trState, err = state.getTranscription()
if err != nil {
return fmt.Errorf("failed to get transcription state: %w", err)
}
var lcState *jobState
if liveCaptionsOn {
lcState, err = state.getLiveCaptions()
if err != nil {
p.LogError("failed to get live captions state", "callID", callID,
"error", err)
}
}
if jobErr != nil {
state.Call.Transcription = nil
if lcState != nil {
state.Call.LiveCaptions = nil
}
if err := p.kvSetChannelState(callID, state); err != nil {
return fmt.Errorf("failed to set channel state: %w", err)
}
return fmt.Errorf("failed to create transcription job: %w", jobErr)
}
if trState.JobID != "" {
return fmt.Errorf("transcription job already in progress")
}
trState.JobID = trJobID
if lcState != nil {
lcState.JobID = trJobID
}
if err := p.kvSetChannelState(callID, state); err != nil {
return fmt.Errorf("failed to set channel state: %w", err)
}
p.LogDebug("transcription job started successfully", "jobID", trJobID, "callID", callID)
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": trState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
go p.transcriptionJobTimeoutChecker(callID, trJobID)
return nil
}
func (p *Plugin) stopTranscribingJob(state *channelState, callID string) (rerr error) {
p.LogDebug("stopping transcribing job", "callID", callID)
if state.Call.Transcription == nil || state.Call.Transcription.EndAt != 0 {
return fmt.Errorf("no transcription in progress")
}
trState, err := state.getTranscription()
if err != nil {
return fmt.Errorf("failed to get transcription state: %w", err)
}
trState.EndAt = time.Now().UnixMilli()
state.Call.Transcription = nil
lcState, err := state.getLiveCaptions()
if err == nil {
lcState.EndAt = time.Now().UnixMilli()
state.Call.LiveCaptions = nil
}
if err := p.kvSetChannelState(callID, state); err != nil {
return fmt.Errorf("failed to set channel state: %w", err)
}
defer func() {
// In case of any error we relay it to the client.
if rerr != nil {
trState.Err = rerr.Error()
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": trState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}()
if err := p.getJobService().StopJob(callID, trState.ID, p.getBotID(), trState.BotConnID); err != nil {
return fmt.Errorf("failed to stop transcription job: %w", err)
}
if state.Call.Recording != nil && state.Call.Recording.EndAt == 0 {
if _, _, err := p.stopRecordingJob(state, callID); err != nil {
p.LogError("failed to stop recording job", "callID", callID, "err", err.Error())
}
}
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": trState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
if lcState != nil {
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": lcState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
return nil
}