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

feat: add call duration in the call state #1528

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions packages/client/src/store/CallState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class CallState {
private callStatsReportSubject = new BehaviorSubject<
CallStatsReport | undefined
>(undefined);
private durationSubject = new BehaviorSubject<number>(0);

// These are tracks that were delivered to the Subscriber's onTrack event
// that we couldn't associate with a participant yet.
Expand Down Expand Up @@ -285,6 +286,11 @@ export class CallState {
*/
thumbnails$: Observable<ThumbnailResponse | undefined>;

/**
* Will provide the count of seconds since the call started.
*/
duration$: Observable<number>;

readonly logger = getLogger(['CallState']);

/**
Expand Down Expand Up @@ -390,6 +396,10 @@ export class CallState {
this.participantCount$ = duc(this.participantCountSubject);
this.recording$ = duc(this.recordingSubject);
this.transcribing$ = duc(this.transcribingSubject);
this.duration$ = duc(this.durationSubject);
setInterval(() => {
this.setDuration((d) => d + 1);
}, 1000);
kristian-mkd marked this conversation as resolved.
Show resolved Hide resolved

this.eventHandlers = {
// these events are not updating the call state:
Expand Down Expand Up @@ -515,6 +525,23 @@ export class CallState {
return this.setCurrentValue(this.participantCountSubject, count);
};

/**
* The number of seconds since the start of the call.
*/
get duration() {
return this.getCurrentValue(this.duration$);
}

/**
* Sets the number of seconds since the start of the call.
*
* @internal
* @param duration the duration of the call in seconds.
*/
setDuration = (duration: Patch<number>) => {
return this.setCurrentValue(this.durationSubject, duration);
};

/**
* The time the call session actually started.
* Useful for displaying the call duration.
Expand Down
10 changes: 10 additions & 0 deletions packages/react-bindings/src/hooks/callStateHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ export const useParticipantCount = () => {
return useObservableValue(participantCount$);
};

/**
* Returns the duration of the call in seconds.
*
* @category Call State
*/
export const useCallDuration = () => {
const { duration$ } = useCallState();
return useObservableValue(duration$);
};

/**
* Returns the approximate anonymous participant count of the active call.
* The regular participants are not included in this count. It is computed on the server.
Expand Down
Loading