Skip to content

Commit

Permalink
Add OnTranscription Handler (#571)
Browse files Browse the repository at this point in the history
* add transcription handler

* allow current participant receive transcription
  • Loading branch information
duongnv129 authored Dec 4, 2024
1 parent c458dcd commit 59f3bc1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ParticipantCallback struct {
OnTrackUnpublished func(publication *RemoteTrackPublication, rp *RemoteParticipant)
OnDataReceived func(data []byte, params DataReceiveParams) // Deprecated: Use OnDataPacket instead
OnDataPacket func(data DataPacket, params DataReceiveParams)
OnTranscriptionReceived func(transcriptionSegments []*TranscriptionSegment, p Participant, publication TrackPublication)
}

func NewParticipantCallback() *ParticipantCallback {
Expand All @@ -66,6 +67,7 @@ func NewParticipantCallback() *ParticipantCallback {
OnTrackUnpublished: func(publication *RemoteTrackPublication, rp *RemoteParticipant) {},
OnDataReceived: func(data []byte, params DataReceiveParams) {},
OnDataPacket: func(data DataPacket, params DataReceiveParams) {},
OnTranscriptionReceived: func(transcriptionSegments []*TranscriptionSegment, p Participant, publication TrackPublication) {},
}
}

Expand Down Expand Up @@ -115,6 +117,9 @@ func (cb *ParticipantCallback) Merge(other *ParticipantCallback) {
if other.OnDataPacket != nil {
cb.OnDataPacket = other.OnDataPacket
}
if other.OnTranscriptionReceived != nil {
cb.OnTranscriptionReceived = other.OnTranscriptionReceived
}
}

type DisconnectionReason string
Expand Down
5 changes: 5 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type RTCEngine struct {
OnRestarted func(*livekit.JoinResponse)
OnResuming func()
OnResumed func()
OnTranscription func(*livekit.Transcription)

// callbacks to get data
CbGetLocalParticipantSID func() string
Expand Down Expand Up @@ -561,6 +562,10 @@ func (e *RTCEngine) handleDataPacket(msg webrtc.DataChannelMessage) {
if e.OnDataPacket != nil {
e.OnDataPacket(identity, msg.SipDtmf)
}
case *livekit.DataPacket_Transcription:
if e.OnTranscription != nil {
e.OnTranscription(msg.Transcription)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func NewRoom(callback *RoomCallback) *Room {
engine.OnResumed = r.handleResumed
engine.OnLocalTrackUnpublished = r.handleLocalTrackUnpublished
engine.OnTrackRemoteMuted = r.handleTrackRemoteMuted
engine.OnTranscription = r.handleTranscriptionReceived

// callbacks engine can use to get data
engine.CbGetLocalParticipantSID = r.getLocalParticipantSID
Expand Down Expand Up @@ -715,6 +716,25 @@ func (r *Room) handleLocalTrackUnpublished(msg *livekit.TrackUnpublishedResponse
}
}

func (r *Room) handleTranscriptionReceived(transcription *livekit.Transcription) {
var (
p Participant
publication TrackPublication
)

if transcription.TranscribedParticipantIdentity == r.LocalParticipant.Identity() {
p = r.LocalParticipant
publication = r.LocalParticipant.getPublication(transcription.TrackId)
} else {
rp := r.GetParticipantByIdentity(transcription.TranscribedParticipantIdentity)
publication = rp.getPublication(transcription.TrackId)
p = rp
}
transcriptionSegments := ExtractTranscriptionSegments(transcription)

r.callback.OnTranscriptionReceived(transcriptionSegments, p, publication)
}

func (r *Room) sendSyncState() {
subscriber, ok := r.engine.Subscriber()
if !ok || subscriber.pc.RemoteDescription() == nil {
Expand Down
47 changes: 47 additions & 0 deletions transcription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lksdk

import (
"github.com/livekit/protocol/livekit"
)

type TranscriptionSegment struct {
ID string
Text string
Language string
StartTime uint64
EndTime uint64
Final bool
}

func ExtractTranscriptionSegments(transcription *livekit.Transcription) []*TranscriptionSegment {
var segments []*TranscriptionSegment
if transcription == nil {
return segments
}
segments = make([]*TranscriptionSegment, len(transcription.Segments))
for i := range transcription.Segments {
segments[i] = &TranscriptionSegment{
ID: transcription.Segments[i].Id,
Text: transcription.Segments[i].Text,
Language: transcription.Segments[i].Language,
StartTime: transcription.Segments[i].StartTime,
EndTime: transcription.Segments[i].EndTime,
Final: transcription.Segments[i].Final,
}
}
return segments
}

0 comments on commit 59f3bc1

Please sign in to comment.