Skip to content

Commit

Permalink
feat: add Transcription Event. (#531)
Browse files Browse the repository at this point in the history
* feat: add Transcription Event.

* dart format.

* remove deprecated API.

* print seg.text in example app.
  • Loading branch information
cloudwebrtc authored Jun 12, 2024
1 parent 8c74825 commit a478e23
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example/lib/widgets/participant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,26 @@ abstract class _ParticipantWidgetState<T extends ParticipantWidget>
TrackPublication? get videoPublication;
TrackPublication? get audioPublication;
bool get isScreenShare => widget.type == ParticipantTrackType.kScreenShare;
EventsListener<ParticipantEvent>? _listener;

@override
void initState() {
super.initState();
_listener = widget.participant.createListener();
_listener?.on<TranscriptionEvent>((e) {
for (var seg in e.segments) {
print('Transcription: ${seg.text} ${seg.isFinal}');
}
});

widget.participant.addListener(_onParticipantChanged);
_onParticipantChanged();
}

@override
void dispose() {
widget.participant.removeListener(_onParticipantChanged);
_listener?.dispose();
super.dispose();
}

Expand Down
12 changes: 12 additions & 0 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,18 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
packet: dp.user,
kind: dp.kind,
));
} else if (dp.whichValue() == lk_models.DataPacket_Value.transcription) {
// Transcription packet
events.emit(EngineTranscriptionReceivedEvent(
transcription: dp.transcription,
));
} else if (dp.whichValue() == lk_models.DataPacket_Value.sipDtmf) {
// SIP DTMF packet
events.emit(EngineSipDtmfReceivedEvent(
dtmf: dp.sipDtmf,
));
} else {
logger.warning('Unknown data packet type: ${dp.whichValue()}');
}
}

Expand Down
32 changes: 32 additions & 0 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
..on<EngineActiveSpeakersUpdateEvent>(
(event) => _onEngineActiveSpeakersUpdateEvent(event.speakers))
..on<EngineDataPacketReceivedEvent>(_onDataMessageEvent)
..on<EngineTranscriptionReceivedEvent>(_onTranscriptionEvent)
..on<AudioPlaybackStarted>((event) {
_handleAudioPlaybackStarted();
})
Expand Down Expand Up @@ -683,6 +684,37 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
}
}

void _onTranscriptionEvent(EngineTranscriptionReceivedEvent event) {
final participant = getParticipantByIdentity(
event.transcription.transcribedParticipantIdentity);
if (participant == null) {
return;
}

final publication =
participant.getTrackPublicationBySid(event.transcription.trackId);

var segments = event.transcription.segments.map((e) {
return TranscriptionSegment(
text: e.text,
id: e.id,
startTime: DateTime.fromMillisecondsSinceEpoch(e.startTime.toInt()),
endTime: DateTime.fromMillisecondsSinceEpoch(e.endTime.toInt()),
isFinal: e.final_5,
language: e.language,
);
}).toList();

final transcription = TranscriptionEvent(
participant: participant,
publication: publication,
segments: segments,
);

participant.events.emit(transcription);
events.emit(transcription);
}

void _onDataMessageEvent(EngineDataPacketReceivedEvent dataPacketEvent) {
// participant may be null if data is sent from Server-API
final senderSid = dataPacketEvent.packet.participantSid;
Expand Down
29 changes: 29 additions & 0 deletions lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,35 @@ class ParticipantPermissionsUpdatedEvent with RoomEvent, ParticipantEvent {
'(participant: ${participant}, permissions: ${permissions})';
}

class TranscriptionSegment {
final String id;
final String text;
final DateTime startTime;
final DateTime endTime;
final bool isFinal;
final String language;
const TranscriptionSegment({
required this.id,
required this.text,
required this.startTime,
required this.endTime,
required this.isFinal,
required this.language,
});
}

/// Transcription event received from the server.
class TranscriptionEvent with RoomEvent, ParticipantEvent {
final Participant participant;
final TrackPublication<Track>? publication;
final List<TranscriptionSegment> segments;
const TranscriptionEvent({
required this.participant,
required this.publication,
required this.segments,
});
}

class ParticipantNameUpdatedEvent with RoomEvent, ParticipantEvent {
final Participant participant;
final String name;
Expand Down
16 changes: 16 additions & 0 deletions lib/src/internal/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ class EngineDataPacketReceivedEvent with EngineEvent, InternalEvent {
});
}

@internal
class EngineTranscriptionReceivedEvent with EngineEvent, InternalEvent {
final lk_models.Transcription transcription;
const EngineTranscriptionReceivedEvent({
required this.transcription,
});
}

@internal
class EngineSipDtmfReceivedEvent with EngineEvent, InternalEvent {
final lk_models.SipDTMF dtmf;
const EngineSipDtmfReceivedEvent({
required this.dtmf,
});
}

@internal
abstract class DataChannelStateUpdatedEvent with EngineEvent, InternalEvent {
final bool isPrimary;
Expand Down

0 comments on commit a478e23

Please sign in to comment.