useRemoteMedia

The useRemoteMedia hook is used to fetch media from a remote peers. It gives an ability to stop and start consuming.

NameDescriptionReturn Type
stateThe current state of the media from remote peer'playable' | 'unavailable' | 'stopped' | 'paused' | 'available'
trackMediaStreamTrack of the remote peerMediaStreamTrack | null
streamMediaStream of the remote peerMediaStream | null
stopConsumingMethod to stop consuming media from remote peervoid
startConsumingMethod to start consuming media from remote peerPromise<void>

Example usage

import { useRemoteMedia } from '@huddle01/react/hooks';
 
const { 
    state, 
    track, 
    stream, 
    stopConsuming, 
    startConsuming 
  } = useRemoteMedia({
    peerId: 'YOUR_PEER_ID',
    label: 'YOUR_MEDIA_LABEL',
    onClose() (),
    onPlayable(data: {
        track: MediaStreamTrack;
        stream: MediaStream;
        label: string;
    }) (),
    onStopped(reason: {
        code: number;
        tag: string;
        message: string;
    }) ()
  })
 
// Stop consuming media from remote peer
const stop = (peerId: string) => {
  stopConsuming(peerId);
}
 
// Start consuming media from remote peer
const start = async (peerId: string) => {
  await startConsuming();
}

Props

The useRemoteMedia hook accepts the following props:

peerId

Required
DescriptionType
PeerId of the remote peerstring

Example usage

// Get state of media from remote peer
const { state } = useRemoteMedia({
    peerId,
    label: 'YOUR_MEDIA_LABEL',
})

label

Required
DescriptionType
Label of the media to consume from remote peerstring

Example usage

// Get state of media from remote peer
const { state } = useRemoteMedia({
    peerId: 'YOUR_PEER_ID',
    label,
})

onClose

Optional
DescriptionType
Callback function to be called when the media is closedvoid

Example usage

useRemoteMedia({
    peerId: 'YOUR_PEER_ID',
    label: 'YOUR_MEDIA_LABEL',
    onClose() {
        console.log('Media closed')
        // Your code here
    }
})

onPlayable

Optional
DescriptionType
Callback function to be called when the media is playable, i.e. start consumingvoid

Example usage

useRemoteMedia({
    peerId: 'YOUR_PEER_ID',
    label: 'YOUR_MEDIA_LABEL',
    onPlayable(data: {
        track: MediaStreamTrack;
        stream: MediaStream;
        label: string;
    }) {
        console.log('Media is playable')
        // Your code here
    }
})

onStopped

Optional
DescriptionType
Callback function to be called when we stop consuming mediavoid

Example usage

useRemoteMedia({
    peerId: 'YOUR_PEER_ID',
    label: 'YOUR_MEDIA_LABEL',
    onStopped(reason: {
        code: number;
        tag: string;
        message: string;
    }) {
        console.log('Media is stopped')
        // Your code here
    }
})
Audio/Video Infrastructure designed for developers to empower them to ship simple yet powerful Audio/Video Apps.
support
company
Copyright © 2024 Graphene 01, Inc. All Rights Reserved.