mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
102 lines • 4.12 kB
JavaScript
/**
* Connects the receiving transport to consume media from a remote producer.
*
* @param {ConnectRecvTransportOptions} options - The options for connecting the receiving transport.
* @param {Transport} options.consumerTransport - The transport used for consuming media.
* @param {string} options.remoteProducerId - The ID of the remote producer.
* @param {string} options.serverConsumerTransportId - The ID of the server consumer transport.
* @param {Socket} options.nsock - The socket used for communication.
* @param {ConnectRecvTransportParameters} options.parameters - The parameters for the connection.
*
* @returns {Promise<void>} A promise that resolves when the connection is established.
*
* @throws Will throw an error if the connection or consumption fails.
*
* @example
* ```typescript
* const options = {
* consumerTransport,
* remoteProducerId: 'producer-id',
* serverConsumerTransportId: 'transport-id',
* nsock: socket,
* parameters: connectRecvTransportOptions,
* };
*
* connectRecvTransport(options)
* .then(() => {
* console.log('Transport connected and consuming media');
* })
* .catch((error) => {
* console.error('Error connecting transport:', error);
* });
* ```
*/
export const connectRecvTransport = async ({ consumerTransport, remoteProducerId, serverConsumerTransportId, nsock, parameters, }) => {
parameters = parameters.getUpdatedAllParams();
const { device, consumerTransports, updateConsumerTransports, consumerResume, } = parameters;
try {
// Emit 'consume' event to signal consumption initiation
nsock.emit('consume', {
rtpCapabilities: device.rtpCapabilities,
remoteProducerId,
serverConsumerTransportId,
}, async ({ params }) => {
if (params.error) {
// Handle error
console.log('consume error', params.error);
return;
}
try {
// Consume media using received parameters
const consumer = await consumerTransport.consume({
id: params.id,
producerId: params.producerId,
kind: params.kind,
rtpParameters: params.rtpParameters,
});
// Update consumerTransports array with the new consumer
consumerTransports.push({
consumerTransport,
serverConsumerTransportId: params.id,
producerId: remoteProducerId,
consumer,
socket_: nsock,
});
updateConsumerTransports(consumerTransports);
// Extract track from the consumer
const { track } = consumer;
// Emit 'consumer-resume' event to signal consumer resumption
nsock.emit('consumer-resume', { serverConsumerId: params.serverConsumerId }, async ({ resumed }) => {
if (resumed) {
// Consumer resumed and ready to be used
try {
await consumerResume({
track,
kind: params.kind,
remoteProducerId,
params,
parameters,
nsock,
consumer,
});
}
catch (error) {
// Handle error
console.log('consumerResume error', error);
}
}
});
}
catch (error) {
// Handle error
console.log('consume error', error);
return;
}
});
}
catch (error) {
// Handle error
console.log('connectRecvTransport error', error);
}
};
//# sourceMappingURL=connectRecvTransport.js.map