Skip to content

Commit

Permalink
feat: joinedAt in HMSPeer (#331)
Browse files Browse the repository at this point in the history
* feat(sdk): joinedAt in HMSPeer

* feat(store): joinedAt in HMSPeer

* fix(sdk): local peer joined at same as room joined at
  • Loading branch information
eswarclynn authored Mar 17, 2022
1 parent 7b83f17 commit cf06365
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/hms-video-store/src/core/hmsSDKStore/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class SDKToHMS {
customerUserId: sdkPeer.customerUserId,
customerDescription: sdkPeer.metadata,
metadata: sdkPeer.metadata,
joinedAt: sdkPeer.joinedAt,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/hms-video-store/src/core/schema/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface HMSPeer {
auxiliaryTracks: HMSTrackID[];
customerUserId?: string;
metadata?: string;
joinedAt?: Date;
/**
* @deprecated
* Use metadata field instead.
Expand Down
1 change: 1 addition & 0 deletions packages/hms-video-web/src/interfaces/peer/hms-peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface HMSPeer {
peerId: string;
name: string;
isLocal: boolean;
joinedAt?: Date;
customerUserId?: string;
metadata?: string;
audioTrack?: HMSAudioTrack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface PeerNotification {
peer_id: string;
info: Info;
role: string;
joined_at?: number;
tracks: {
[track_id: string]: TrackState;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IStore } from '../../sdk/store';
import HMSLogger from '../../utils/logger';
import { PeerNotification } from '../HMSNotifications';
import { TrackManager } from './TrackManager';
import { convertDateNumToDate } from '../../utils/date';

/**
* Handles:
Expand Down Expand Up @@ -123,6 +124,7 @@ export class PeerManager {
customerUserId: peer.info.user_id,
metadata: peer.info.data,
role: this.store.getPolicyForRole(peer.role),
joinedAt: convertDateNumToDate(peer.joined_at) || new Date(),
});

this.store.addPeer(hmsPeer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { HMSNotificationMethod } from '../HMSNotificationMethod';
import { HMSUpdateListener, HMSRoomUpdate, HMSHLS, HMSHLSRecording } from '../../interfaces';
import { IStore } from '../../sdk/store';
import { convertDateNumToDate } from '../../utils/date';

export class RoomUpdateManager {
constructor(private store: IStore, public listener?: HMSUpdateListener) {}
Expand Down Expand Up @@ -53,20 +54,16 @@ export class RoomUpdateManager {
room.recording.server.running = !!recording?.sfu.enabled;
room.recording.browser.running = !!recording?.browser.enabled;
room.rtmp.running = !!streaming?.rtmp?.enabled;
room.rtmp.startedAt = this.getAsDate(streaming?.rtmp?.started_at);
room.recording.server.startedAt = this.getAsDate(recording?.sfu.started_at);
room.recording.browser.startedAt = this.getAsDate(recording?.browser.started_at);
room.rtmp.startedAt = convertDateNumToDate(streaming?.rtmp?.started_at);
room.recording.server.startedAt = convertDateNumToDate(recording?.sfu.started_at);
room.recording.browser.startedAt = convertDateNumToDate(recording?.browser.started_at);
room.recording.hls = this.getPeerListHLSRecording(recording);
room.hls = this.convertHls(streaming?.hls);
room.sessionId = session_id;
room.startedAt = this.getAsDate(started_at);
room.startedAt = convertDateNumToDate(started_at);
this.listener?.onRoomUpdate(HMSRoomUpdate.RECORDING_STATE_UPDATED, room);
}

private getAsDate(dateNum?: number): Date | undefined {
return dateNum ? new Date(dateNum) : undefined;
}

private onRTMPStart(notification: RTMPNotification) {
this.setRTMPStatus(!notification.error?.code, notification);
}
Expand Down Expand Up @@ -105,7 +102,7 @@ export class RoomUpdateManager {
meetingURL: variant.meeting_url,
url: variant.url,
metadata: variant.metadata,
startedAt: this.getAsDate(variant.started_at),
startedAt: convertDateNumToDate(variant.started_at),
});
});
return hls;
Expand All @@ -118,7 +115,7 @@ export class RoomUpdateManager {
running: !!hlsNotification?.enabled,
singleFilePerLayer: !!hlsNotification.hls_recording?.single_file_per_layer,
hlsVod: !!hlsNotification.hls_recording?.hls_vod,
startedAt: this.getAsDate(hlsNotification?.variants?.[0].started_at),
startedAt: convertDateNumToDate(hlsNotification?.variants?.[0].started_at),
error: hlsNotification?.error?.code ? hlsNotification.error : undefined,
};
}
Expand All @@ -129,7 +126,7 @@ export class RoomUpdateManager {
const hlsNotification = recording?.hls;
return {
running: !!hlsNotification?.enabled,
startedAt: this.getAsDate(hlsNotification?.started_at),
startedAt: convertDateNumToDate(hlsNotification?.started_at),
singleFilePerLayer: !!hlsNotification?.config?.single_file_per_layer,
hlsVod: !!hlsNotification?.config?.hls_vod,
};
Expand All @@ -141,14 +138,14 @@ export class RoomUpdateManager {
if (notification.type === 'sfu') {
room.recording.server = {
running,
startedAt: running ? this.getAsDate(notification.started_at) : undefined,
startedAt: running ? convertDateNumToDate(notification.started_at) : undefined,
error: notification.error?.code ? notification.error : undefined,
};
action = HMSRoomUpdate.SERVER_RECORDING_STATE_UPDATED;
} else {
room.recording.browser = {
running,
startedAt: running ? this.getAsDate(notification.started_at) : undefined,
startedAt: running ? convertDateNumToDate(notification.started_at) : undefined,
error: notification.error?.code ? notification.error : undefined,
};
action = HMSRoomUpdate.BROWSER_RECORDING_STATE_UPDATED;
Expand All @@ -160,7 +157,7 @@ export class RoomUpdateManager {
const room = this.store.getRoom();
room.rtmp = {
running,
startedAt: running ? this.getAsDate(notification.started_at) : undefined,
startedAt: running ? convertDateNumToDate(notification.started_at) : undefined,
error: notification.error?.code ? notification.error : undefined,
};
this.listener?.onRoomUpdate(HMSRoomUpdate.RTMP_STREAMING_STATE_UPDATED, room);
Expand Down
3 changes: 3 additions & 0 deletions packages/hms-video-web/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ export class HMSSdk implements HMSInterface {
const localPeer = this.store.getLocalPeer();
const room = this.store.getRoom();
room.joinedAt = new Date();
if (localPeer) {
localPeer.joinedAt = room.joinedAt;
}

if (localPeer?.role) {
this.listener?.onJoin(room);
Expand Down
5 changes: 4 additions & 1 deletion packages/hms-video-web/src/sdk/models/peer/HMSPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type HMSPeerInit = {
customerUserId?: string;
metadata?: string;
role?: HMSRole;
joinedAt?: Date;
};

export class HMSPeer implements IHMSPeer {
Expand All @@ -21,13 +22,15 @@ export class HMSPeer implements IHMSPeer {
videoTrack?: HMSVideoTrack;
auxiliaryTracks: HMSTrack[] = [];
role?: HMSRole;
joinedAt?: Date;

constructor({ peerId, name, isLocal, customerUserId, metadata, role }: HMSPeerInit) {
constructor({ peerId, name, isLocal, customerUserId, metadata, role, joinedAt }: HMSPeerInit) {
this.name = name;
this.peerId = peerId;
this.isLocal = isLocal;
this.customerUserId = customerUserId;
this.metadata = metadata;
this.joinedAt = joinedAt;

if (role) {
this.role = role;
Expand Down
3 changes: 3 additions & 0 deletions packages/hms-video-web/src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const convertDateNumToDate = (dateNum?: number): Date | undefined => {
return dateNum ? new Date(dateNum) : undefined;
};

0 comments on commit cf06365

Please sign in to comment.