ov-components: replace and improve recordingElapsedTime logic

master
Carlos Santos 2025-07-04 17:51:30 +02:00
parent 1be876678c
commit 91aa127dad
3 changed files with 46 additions and 42 deletions

View File

@ -700,7 +700,7 @@ export class ToolbarComponent implements OnInit, OnDestroy, AfterViewInit {
private subscribeToRecordingStatus() { private subscribeToRecordingStatus() {
this.recordingSubscription = this.recordingService.recordingStatusObs.subscribe((event: RecordingStatusInfo) => { this.recordingSubscription = this.recordingService.recordingStatusObs.subscribe((event: RecordingStatusInfo) => {
const { status, recordingElapsedTime } = event; const { status, startedAt } = event;
this.recordingStatus = status; this.recordingStatus = status;
if (status === RecordingStatus.STARTED) { if (status === RecordingStatus.STARTED) {
this.startedRecording = event.recordingList.find((rec) => rec.status === RecordingStatus.STARTED); this.startedRecording = event.recordingList.find((rec) => rec.status === RecordingStatus.STARTED);
@ -708,8 +708,8 @@ export class ToolbarComponent implements OnInit, OnDestroy, AfterViewInit {
this.startedRecording = undefined; this.startedRecording = undefined;
} }
if (recordingElapsedTime) { if (startedAt) {
this.recordingTime = recordingElapsedTime; this.recordingTime = startedAt;
} }
this.cd.markForCheck(); this.cd.markForCheck();
}); });

View File

@ -21,7 +21,7 @@ export enum RecordingOutputMode {
export interface RecordingStatusInfo { export interface RecordingStatusInfo {
status: RecordingStatus; status: RecordingStatus;
recordingList: RecordingInfo[]; recordingList: RecordingInfo[];
recordingElapsedTime?: Date; startedAt?: Date;
error?: string; error?: string;
} }

View File

@ -20,7 +20,7 @@ export class RecordingService {
private recordingStatus = <BehaviorSubject<RecordingStatusInfo>>new BehaviorSubject({ private recordingStatus = <BehaviorSubject<RecordingStatusInfo>>new BehaviorSubject({
status: RecordingStatus.STOPPED, status: RecordingStatus.STOPPED,
recordingList: [] as RecordingInfo[], recordingList: [] as RecordingInfo[],
recordingElapsedTime: new Date(0, 0, 0, 0, 0, 0) startedAt: new Date(0, 0, 0, 0, 0, 0)
}); });
private log: ILogger; private log: ILogger;
@ -41,12 +41,9 @@ export class RecordingService {
* @internal * @internal
*/ */
setRecordingStarted(recordingInfo?: RecordingInfo, startTimestamp?: number) { setRecordingStarted(recordingInfo?: RecordingInfo, startTimestamp?: number) {
// Register the start timestamp of the recording // Determine the actual start timestamp of the recording
// to calculate the elapsed time // Priority: startTimestamp parameter > recordingInfo.startedAt > current time
this.recordingStartTimestamp = recordingInfo?.startedAt || Date.now(); this.recordingStartTimestamp = startTimestamp || recordingInfo?.startedAt || Date.now();
// Initialize the recording elapsed time
this.startRecordingTimer();
const { recordingList } = this.recordingStatus.getValue(); const { recordingList } = this.recordingStatus.getValue();
let updatedRecordingList = [...recordingList]; let updatedRecordingList = [...recordingList];
@ -61,17 +58,22 @@ export class RecordingService {
updatedRecordingList = [recordingInfo, ...updatedRecordingList]; updatedRecordingList = [recordingInfo, ...updatedRecordingList];
} }
} }
// Calculate the elapsed time based on the actual start timestamp
const recordingElapsedTime = new Date(0, 0, 0, 0, 0, 0); const recordingElapsedTime = new Date(0, 0, 0, 0, 0, 0);
if (startTimestamp) { if (this.recordingStartTimestamp) {
const elapsedSeconds = Math.floor((Date.now() - startTimestamp) / 1000); const elapsedSeconds = Math.floor((Date.now() - this.recordingStartTimestamp) / 1000);
recordingElapsedTime.setSeconds(elapsedSeconds); recordingElapsedTime.setSeconds(Math.max(0, elapsedSeconds)); // Ensure non-negative
} }
this.updateStatus({ this.updateStatus({
status: RecordingStatus.STARTED, status: RecordingStatus.STARTED,
recordingList: updatedRecordingList, recordingList: updatedRecordingList,
recordingElapsedTime startedAt: recordingElapsedTime
}); });
// Start the timer after updating the initial state
this.startRecordingTimer();
} }
/** /**
@ -96,7 +98,7 @@ export class RecordingService {
this.updateStatus({ this.updateStatus({
status: RecordingStatus.STOPPED, status: RecordingStatus.STOPPED,
recordingList: updatedRecordingList, recordingList: updatedRecordingList,
recordingElapsedTime: new Date(0, 0, 0, 0, 0, 0) startedAt: new Date(0, 0, 0, 0, 0, 0)
}); });
this.recordingStartTimestamp = null; this.recordingStartTimestamp = null;
@ -107,11 +109,11 @@ export class RecordingService {
* The `started` stastus will be updated automatically when the recording is actually started. * The `started` stastus will be updated automatically when the recording is actually started.
*/ */
setRecordingStarting() { setRecordingStarting() {
const { recordingList, recordingElapsedTime } = this.recordingStatus.getValue(); const { recordingList, startedAt } = this.recordingStatus.getValue();
this.updateStatus({ this.updateStatus({
status: RecordingStatus.STARTING, status: RecordingStatus.STARTING,
recordingList, recordingList,
recordingElapsedTime startedAt
}); });
} }
@ -121,11 +123,11 @@ export class RecordingService {
*/ */
setRecordingFailed(error: string) { setRecordingFailed(error: string) {
this.stopRecordingTimer(); this.stopRecordingTimer();
const { recordingElapsedTime, recordingList } = this.recordingStatus.getValue(); const { startedAt, recordingList } = this.recordingStatus.getValue();
const statusInfo: RecordingStatusInfo = { const statusInfo: RecordingStatusInfo = {
status: RecordingStatus.FAILED, status: RecordingStatus.FAILED,
recordingList, recordingList,
recordingElapsedTime, startedAt,
error error
}; };
this.updateStatus(statusInfo); this.updateStatus(statusInfo);
@ -136,12 +138,12 @@ export class RecordingService {
* The `stopped` stastus will be updated automatically when the recording is actually stopped. * The `stopped` stastus will be updated automatically when the recording is actually stopped.
*/ */
setRecordingStopping() { setRecordingStopping() {
const { recordingElapsedTime, recordingList } = this.recordingStatus.getValue(); const { startedAt, recordingList } = this.recordingStatus.getValue();
this.updateStatus({ this.updateStatus({
status: RecordingStatus.STOPPING, status: RecordingStatus.STOPPING,
recordingList, recordingList,
recordingElapsedTime startedAt
}); });
} }
@ -199,14 +201,14 @@ export class RecordingService {
* @internal * @internal
*/ */
deleteRecording(recording: RecordingInfo) { deleteRecording(recording: RecordingInfo) {
const { recordingList, status, recordingElapsedTime } = this.recordingStatus.getValue(); const { recordingList, status, startedAt } = this.recordingStatus.getValue();
const updatedList = recordingList.filter((item) => item.id !== recording.id); const updatedList = recordingList.filter((item) => item.id !== recording.id);
if (updatedList.length !== recordingList.length) { if (updatedList.length !== recordingList.length) {
this.updateStatus({ this.updateStatus({
status, status,
recordingList: updatedList, recordingList: updatedList,
recordingElapsedTime startedAt
}); });
return true; return true;
} }
@ -219,11 +221,11 @@ export class RecordingService {
* @internal * @internal
*/ */
setRecordingList(recordings: RecordingInfo[]) { setRecordingList(recordings: RecordingInfo[]) {
const { status, recordingElapsedTime, error } = this.recordingStatus.getValue(); const { status, startedAt, error } = this.recordingStatus.getValue();
this.updateStatus({ this.updateStatus({
status, status,
recordingList: recordings, recordingList: recordings,
recordingElapsedTime, startedAt,
error error
}); });
} }
@ -233,19 +235,21 @@ export class RecordingService {
* @param status {@link RecordingStatus} * @param status {@link RecordingStatus}
*/ */
private updateStatus(statusInfo: RecordingStatusInfo) { private updateStatus(statusInfo: RecordingStatusInfo) {
const { status, recordingList, error, recordingElapsedTime } = statusInfo; const { status, recordingList, error, startedAt } = statusInfo;
this.recordingStatus.next({ this.recordingStatus.next({
status, status,
recordingList, recordingList,
recordingElapsedTime, startedAt,
error error
}); });
} }
private startRecordingTimer() { private startRecordingTimer() {
// Don't override the timestamp if it's already set correctly
if (this.recordingStartTimestamp === null) { if (this.recordingStartTimestamp === null) {
this.recordingStartTimestamp = Date.now(); this.recordingStartTimestamp = Date.now();
} }
if (this.recordingTimeInterval) { if (this.recordingTimeInterval) {
clearInterval(this.recordingTimeInterval); clearInterval(this.recordingTimeInterval);
} }
@ -253,29 +257,29 @@ export class RecordingService {
this.recordingTimeInterval = setInterval(() => { this.recordingTimeInterval = setInterval(() => {
if (!this.recordingStartTimestamp) return; if (!this.recordingStartTimestamp) return;
let { recordingElapsedTime } = this.recordingStatus.getValue(); // Calculate elapsed time based on the actual recording start timestamp
if (recordingElapsedTime) { const elapsedSeconds = Math.floor((Date.now() - this.recordingStartTimestamp) / 1000);
// Calculamos con precisión el tiempo transcurrido const startedAt = new Date(0, 0, 0, 0, 0, 0);
const elapsedSeconds = Math.floor((Date.now() - this.recordingStartTimestamp) / 1000); startedAt.setSeconds(Math.max(0, elapsedSeconds)); // Ensure non-negative
const updatedElapsedTime = new Date(0, 0, 0, 0, 0, 0);
updatedElapsedTime.setSeconds(elapsedSeconds);
const { recordingList, status } = this.recordingStatus.getValue(); const { recordingList, status } = this.recordingStatus.getValue();
this.updateStatus({ this.updateStatus({
status, status,
recordingList, recordingList,
recordingElapsedTime: updatedElapsedTime startedAt
}); });
}
}, 1000); }, 1000);
} }
private stopRecordingTimer() { private stopRecordingTimer() {
clearInterval(this.recordingTimeInterval); if (this.recordingTimeInterval) {
clearInterval(this.recordingTimeInterval);
}
const { recordingList, status, error } = this.recordingStatus.getValue(); const { recordingList, status, error } = this.recordingStatus.getValue();
const statusInfo: RecordingStatusInfo = { const statusInfo: RecordingStatusInfo = {
status, status,
recordingList, recordingList,
startedAt: new Date(0, 0, 0, 0, 0, 0), // Reset elapsed time when stopped
error error
}; };
this.updateStatus(statusInfo); this.updateStatus(statusInfo);