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

[MM-59866] Prevent failure if track context has no samples #29

Merged
merged 1 commit into from
Jul 25, 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
29 changes: 22 additions & 7 deletions cmd/transcriber/call/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,28 @@ func (t *Transcriber) processLiveTrack(track trackRemote, sessionID string) {
ctx.user = user
ctx.filename = filepath.Join(getDataDir(), fmt.Sprintf("%s_%s.ogg", user.Id, track.ID()))

var prevArrivalTime time.Time
var prevRTPTimestamp uint32

slog.Debug("processing voice track",
slog.String("username", user.Username),
slog.String("sessionID", sessionID),
slog.String("trackID", ctx.trackID))
slog.Debug("start reading loop for track", slog.String("trackID", ctx.trackID))
defer func() {
slog.Debug("exiting reading loop for track", slog.String("trackID", ctx.trackID))
select {
case t.trackCtxs <- ctx:
default:
slog.Error("failed to enqueue track context", slog.Any("ctx", ctx))

// Only send the track context if we processed at least one audio packet.
if !prevArrivalTime.IsZero() {
select {
case t.trackCtxs <- ctx:
default:
slog.Error("failed to enqueue track context", slog.Any("ctx", ctx))
}
} else {
slog.Debug("nothing to send", slog.String("trackID", ctx.trackID))
}

t.liveTracksWg.Done()
}()

Expand All @@ -131,8 +141,6 @@ func (t *Transcriber) processLiveTrack(track trackRemote, sessionID string) {
}

// Read track audio:
var prevArrivalTime time.Time
var prevRTPTimestamp uint32
for {
pkt, _, readErr := track.ReadRTP()
if readErr != nil {
Expand Down Expand Up @@ -181,7 +189,7 @@ func (t *Transcriber) processLiveTrack(track trackRemote, sessionID string) {
}

var gap uint64
if ctx.startTS == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this since, in theory, startTS could be still 0 even after setting it the first time if we are very unlucky and the start time was set just now. Very unlikely but you never know :)

if prevArrivalTime.IsZero() {
ctx.startTS = time.Since(*t.startTime.Load()).Milliseconds()
slog.Debug("start offset for track",
slog.Duration("offset", time.Duration(ctx.startTS)*time.Millisecond),
Expand Down Expand Up @@ -363,6 +371,7 @@ func (ctx trackContext) decodeAudio() ([]trackTimedSamples, error) {
if err != nil {
slog.Error("failed to decode audio data",
slog.String("err", err.Error()),
slog.Any("data", data),
slog.String("trackID", ctx.trackID))
}

Expand Down Expand Up @@ -420,6 +429,12 @@ func (t *Transcriber) transcribeTrack(ctx trackContext) (transcribe.TrackTranscr

var speechSamples []trackTimedSamples
for _, ts := range samples {
if len(ts.pcm) == 0 {
slog.Warn("unexpected empty audio samples",
slog.String("trackID", ctx.trackID))
continue
}

// We need to reset the speech detector's state from one chunk of samples
// to the next.
if err := sd.Reset(); err != nil {
Expand Down
133 changes: 118 additions & 15 deletions cmd/transcriber/call/transcriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func setupTranscriberForTest(t *testing.T) *Transcriber {
require.NoError(t, err)
require.NotNil(t, tr)

dir, err := os.MkdirTemp("", "data")
if err != nil {
require.NoError(t, err)
}
os.Setenv("DATA_DIR", dir)
t.Cleanup(func() {
os.Unsetenv("DATA_DIR")
os.RemoveAll(dir)
})
Comment on lines +51 to +59
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed both to simplify local testing and also to fix an existing test that was passing purely because of a failure creating the OGG file.


return tr
}

Expand Down Expand Up @@ -176,10 +186,6 @@ func TestProcessLiveTrack(t *testing.T) {

sessionID := "sessionID"

dataDir := os.Getenv("DATA_DIR")
os.Setenv("DATA_DIR", os.TempDir())
defer os.Setenv("DATA_DIR", dataDir)

tr.liveTracksWg.Add(1)
tr.startTime.Store(newTimeP(time.Now().Add(-time.Second)))
tr.processLiveTrack(track, sessionID)
Expand Down Expand Up @@ -281,10 +287,6 @@ func TestProcessLiveTrack(t *testing.T) {

sessionID := "sessionID"

dataDir := os.Getenv("DATA_DIR")
os.Setenv("DATA_DIR", os.TempDir())
defer os.Setenv("DATA_DIR", dataDir)

tr.liveTracksWg.Add(1)
tr.startTime.Store(newTimeP(time.Now().Add(-time.Second)))
tr.processLiveTrack(track, sessionID)
Expand Down Expand Up @@ -385,10 +387,6 @@ func TestProcessLiveTrack(t *testing.T) {

sessionID := "sessionID"

dataDir := os.Getenv("DATA_DIR")
os.Setenv("DATA_DIR", os.TempDir())
defer os.Setenv("DATA_DIR", dataDir)

tr.liveTracksWg.Add(1)
tr.startTime.Store(newTimeP(time.Now().Add(-time.Second)))
tr.processLiveTrack(track, sessionID)
Expand Down Expand Up @@ -450,12 +448,117 @@ func TestProcessLiveTrack(t *testing.T) {
Body: io.NopCloser(strings.NewReader(`{"id": "userID", "username": "testuser"}`)),
}, nil).Once()

track := &trackRemoteMock{
id: "trackID",
}

pkts := []*rtp.Packet{
{
Header: rtp.Header{
Timestamp: 1000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 2000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 3000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 4000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
}

var i int
track.readRTP = func() (*rtp.Packet, interceptor.Attributes, error) {
if i >= len(pkts) {
return nil, nil, io.EOF
}
defer func() { i++ }()
return pkts[i], nil, nil
}

tr.liveTracksWg.Add(1)
tr.startTime.Store(newTimeP(time.Now().Add(-time.Second)))
tr.processLiveTrack(&trackRemoteMock{
id: "trackID",
}, "sessionID")
tr.processLiveTrack(track, "sessionID")

close(tr.trackCtxs)
require.Len(t, tr.trackCtxs, 1)
})

t.Run("should not queue contexes with no samples", func(t *testing.T) {
tr := setupTranscriberForTest(t)

mockClient := &mocks.MockAPIClient{}
tr.apiClient = mockClient

defer mockClient.AssertExpectations(t)

mockClient.On("DoAPIRequest", mock.Anything, http.MethodGet,
"http://localhost:8065/plugins/com.mattermost.calls/bot/calls/8w8jorhr7j83uqr6y1st894hqe/sessions/sessionID/profile", "", "").
Return(&http.Response{
Body: io.NopCloser(strings.NewReader(`{"id": "userID", "username": "testuser"}`)),
}, nil).Once()

track := &trackRemoteMock{
id: "trackID",
}

pkts := []*rtp.Packet{
{
Header: rtp.Header{
Timestamp: 1000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 2000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 3000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
{
Header: rtp.Header{
Timestamp: 4000,
},
Payload: []byte{0x45, 0x45, 0x45},
},
}

var i int
track.readRTP = func() (*rtp.Packet, interceptor.Attributes, error) {
if i >= len(pkts) {
return nil, nil, io.EOF
}

defer func() { i++ }()

if i == 3 {
time.Sleep(2 * time.Second)
}

return pkts[i], nil, nil
}

tr.liveTracksWg.Add(1)
tr.processLiveTrack(track, "sessionID")
close(tr.trackCtxs)
require.Empty(t, tr.trackCtxs)
})
}
Loading