Skip to content

Commit

Permalink
[FCE-390] Remove metadata from track info (#82)
Browse files Browse the repository at this point in the history
## Description

Remove metadata from tracks returned from SDK

## Motivation and Context

These values should be opaque to user.
  • Loading branch information
mironiasty authored Aug 23, 2024
1 parent d9194f5 commit 5fde67b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
10 changes: 3 additions & 7 deletions examples/video-chat/components/VideosGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ type Props = {
tracks: GridTrack[];
};

type GridTrack = Track<Metadata> & {
type GridTrack = Track & {
isLocal: boolean;
userName: string | undefined;
};

const { VIDEO_CELL } = roomScreenLabels;

export function parsePeersToTracks(
peers: Peer<Metadata, Metadata, Metadata>[],
): GridTrack[] {
export function parsePeersToTracks(peers: Peer<Metadata>[]): GridTrack[] {
return peers
.sort((peer) => (peer.isLocal ? -1 : 1))
.flatMap((peer) =>
Expand All @@ -34,9 +32,7 @@ export function parsePeersToTracks(
isLocal: peer.isLocal,
userName: peer.metadata?.name,
}))
.filter(
(track) => track.type === 'Video' && (track.metadata.active ?? true),
),
.filter((track) => track.type === 'Video' && track.isActive),
);
}

Expand Down
8 changes: 2 additions & 6 deletions packages/react-native-client/src/RNFishjamClientModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ type RNFishjamClient = {
screencastOptions: Partial<ScreencastOptions<MetadataType>>,
) => Promise<void>;
isScreencastOn: boolean;
getPeers: <
PeerMetadataType extends Metadata,
VideoTrackMetadataType extends Metadata,
AudioTrackMetadataType extends Metadata,
>() => Promise<
Peer<PeerMetadataType, VideoTrackMetadataType, AudioTrackMetadataType>[]
getPeers: <PeerMetadataType extends Metadata>() => Promise<
Peer<PeerMetadataType>[]
>;
updatePeerMetadata: <MetadataType extends Metadata>(
metadata: MetadataType,
Expand Down
77 changes: 35 additions & 42 deletions packages/react-native-client/src/hooks/usePeers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';

import { Metadata, SimulcastConfig, TrackEncoding } from '../types';
import { Metadata, TrackEncoding, TrackMetadata } from '../types';
import RNFishjamClientModule from '../RNFishjamClientModule';
import { ReceivableEvents, eventEmitter } from '../common/eventEmitter';

Expand All @@ -14,19 +14,26 @@ export type TrackType = 'Audio' | 'Video';
*/
export type VadStatus = 'silence' | 'speech';

export type Track<MetadataType extends Metadata> = {
type TrackBase = {
id: string;
type: TrackType;
metadata: MetadataType;
vadStatus: VadStatus;
isActive: boolean;
};

export type AudioTrack = TrackBase & {
type: 'Audio';
vadStatus: VadStatus | undefined;
};

export type VideoTrack = TrackBase & {
type: 'Video';
// Encoding that is currently received. Only present for remote tracks.
encoding: TrackEncoding | null;
// Information about simulcast, if null simulcast is not enabled
simulcastConfig: SimulcastConfig | null;
// The reason of currently selected encoding. Only present for remote tracks.
encodingReason: EncodingReason | null;
};

export type Track = VideoTrack | AudioTrack;
/**
* Type describing possible reasons of currently selected encoding.
*
Expand All @@ -36,23 +43,11 @@ export type Track<MetadataType extends Metadata> = {
*/
export type EncodingReason = 'other' | 'encoding_inactive' | 'low_bandwidth';

export type PeersUpdateEvent<
MetadataType extends Metadata,
VideoTrackMetadataType extends Metadata,
AudioTrackMetadataType extends Metadata,
> = {
PeersUpdate: Peer<
MetadataType,
VideoTrackMetadataType,
AudioTrackMetadataType
>[];
export type PeersUpdateEvent<MetadataType extends Metadata> = {
PeersUpdate: Peer<MetadataType>[];
};

export type Peer<
MetadataType extends Metadata,
VideoTrackMetadataType extends Metadata,
AudioTrackMetadataType extends Metadata,
> = {
export type Peer<MetadataType extends Metadata> = {
/**
* id used to identify a peer
*/
Expand All @@ -68,40 +63,38 @@ export type Peer<
/**
* a list of peers's video and audio tracks
*/
tracks: Track<VideoTrackMetadataType | AudioTrackMetadataType>[];
tracks: Track[];
};

function addIsActiveToTracks<MetadataType extends Metadata>(
peers: ReadonlyArray<Peer<MetadataType>>,
): Peer<MetadataType>[] {
return peers.map((peer) => ({
...peer,
tracks: peer.tracks.map((track) => ({
...track,
isActive:
(track as { metadata?: TrackMetadata })?.metadata?.active ?? true,
})),
}));
}
/**
* This hook provides live updates of room peers.
* @returns An array of room peers.
*/
export function usePeers<
MetadataType extends Metadata,
VideoTrackMetadataType extends Metadata,
AudioTrackMetadataType extends Metadata,
>() {
const [peers, setPeers] = useState<
Peer<MetadataType, VideoTrackMetadataType, AudioTrackMetadataType>[]
>([]);
export function usePeers<MetadataType extends Metadata>() {
const [peers, setPeers] = useState<Peer<MetadataType>[]>([]);

useEffect(() => {
async function updatePeers() {
const peers = await RNFishjamClientModule.getPeers<
MetadataType,
VideoTrackMetadataType,
AudioTrackMetadataType
>();
setPeers(peers);
const peers = await RNFishjamClientModule.getPeers<MetadataType>();
setPeers(addIsActiveToTracks(peers));
}

const eventListener = eventEmitter.addListener<
PeersUpdateEvent<
MetadataType,
VideoTrackMetadataType,
AudioTrackMetadataType
>
PeersUpdateEvent<MetadataType>
>(ReceivableEvents.PeersUpdate, (event) => {
setPeers(event.PeersUpdate);
setPeers(addIsActiveToTracks(event.PeersUpdate));
});

updatePeers();
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type SimulcastBandwidthLimit = Record<TrackEncoding, BandwidthLimit>;
*/
export type TrackBandwidthLimit = BandwidthLimit | SimulcastBandwidthLimit;

export type TrackMetadata = {
active: boolean;
type: 'audio' | 'camera' | 'screensharing';
};

// branded types are useful for restricting where given value can be passed
declare const brand: unique symbol;
export type Brand<T, TBrand extends string> = T & { [brand]: TBrand };

0 comments on commit 5fde67b

Please sign in to comment.