Skip to content

Commit

Permalink
Add timestamps to TranscriptionSegment. (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc authored Aug 28, 2024
1 parent 0929ca9 commit 25362ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 18 additions & 8 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
RegionUrlProvider? _regionUrlProvider;
String? _regionUrl;

// Agents
final Map<String, DateTime> _transcriptionReceivedTimes = {};

Room({
@Deprecated('deprecated, please use connectOptions in room.connect()')
ConnectOptions connectOptions = const ConnectOptions(),
Expand Down Expand Up @@ -790,24 +793,31 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
void _onTranscriptionEvent(EngineTranscriptionReceivedEvent event) {
final participant = getParticipantByIdentity(
event.transcription.transcribedParticipantIdentity);
if (participant == null) {
if (participant == null || event.transcription.segments.isEmpty) {
return;
}

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

var segments = event.transcription.segments.map((e) {
var segments = event.transcription.segments.map((segment) {
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,
text: segment.text,
id: segment.id,
firstReceivedTime:
_transcriptionReceivedTimes[segment.id] ?? DateTime.now(),
lastReceivedTime: DateTime.now(),
isFinal: segment.final_5,
language: segment.language,
);
}).toList();

for (var segment in segments) {
segment.isFinal
? _transcriptionReceivedTimes.remove(segment.id)
: _transcriptionReceivedTimes[segment.id] = DateTime.now();
}

final transcription = TranscriptionEvent(
participant: participant,
publication: publication,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,15 @@ class ParticipantPermissionsUpdatedEvent with RoomEvent, ParticipantEvent {
class TranscriptionSegment {
final String id;
final String text;
final DateTime startTime;
final DateTime endTime;
final DateTime firstReceivedTime;
final DateTime lastReceivedTime;
final bool isFinal;
final String language;
const TranscriptionSegment({
required this.id,
required this.text,
required this.startTime,
required this.endTime,
required this.firstReceivedTime,
required this.lastReceivedTime,
required this.isFinal,
required this.language,
});
Expand Down

0 comments on commit 25362ff

Please sign in to comment.