UNPKG

mediasfu-reactnative-expo

Version:

mediasfu-reactnative-expo – Expo-managed React Native WebRTC SDK for video conferencing, webinars, live streaming, broadcast, screen sharing, whiteboard, chat, recording, live subtitles, translation, and AI agent rooms on iOS, Android, and web. Prebuilt r

119 lines 6.21 kB
import { createSendTransport as sharedCreateSendTransport } from 'mediasfu-shared'; const createLocalSendTransport = async ({ option, parameters, }) => { try { let { islevel, member, socket, localSocket, device, localProducerTransport, localTransportCreated, updateLocalProducerTransport, updateLocalTransportCreated, connectSendTransport, } = parameters; if (!localSocket || !localSocket.id || socket.id === localSocket.id) { return; } localSocket.emit('createWebRtcTransport', { consumer: false, islevel }, async ({ params }) => { if (params && params.error) { console.error('Error in local createWebRtcTransport:', params.error); return; } // Create local send transport localProducerTransport = await device.createSendTransport(params); if (updateLocalProducerTransport) { updateLocalProducerTransport(localProducerTransport); } // Handle local transport events localProducerTransport.on('connect', async ({ dtlsParameters }, callback, errback) => { try { localSocket.emit('transport-connect', { dtlsParameters }); callback(); } catch (error) { errback(error); } }); localProducerTransport.on('produce', async (parameters, callback, errback) => { try { localSocket.emit('transport-produce', { kind: parameters.kind, rtpParameters: parameters.rtpParameters, appData: parameters.appData, islevel, name: member, }, ({ id }) => callback({ id })); } catch (error) { errback(error); } }); localProducerTransport.on('connectionstatechange', (state) => { if (state === 'failed') { console.error('Local transport connection failed.'); if (localProducerTransport) { localProducerTransport.close(); } } }); // Mark local transport as created localTransportCreated = true; updateLocalTransportCreated?.(localTransportCreated); // connect local transport await connectSendTransport({ targetOption: 'local', option, parameters: { ...parameters, localProducerTransport: localProducerTransport }, }); }); } catch (error) { console.error('Error creating local send transport:', error); } }; /** * Creates a WebRTC send transport and sets up event handlers for the transport. * * Supports both primary and local transports with modular handling. * * @param {CreateSendTransportOptions} options - The options for creating the send transport. * @param {string} options.option - The type of transport to create. * @param {CreateSendTransportParameters} options.parameters - The parameters required for creating the transport. * @param {string} options.parameters.islevel - The level of the transport. * @param {string} options.parameters.member - The member name for the transport. * @param {Socket} options.parameters.socket - The primary socket instance. * @param {Device} options.parameters.device - The device instance. * @param {Transport | null} options.parameters.producerTransport - The primary producer transport object. * @param {boolean} options.parameters.transportCreated - The state of the primary transport creation. * @param {(transport: Transport | null) => void} options.parameters.updateProducerTransport - The function to update the primary transport object. * @param {(state: boolean) => void} options.parameters.updateTransportCreated - The function to update the primary transport creation state. * @param {Function} options.parameters.connectSendTransport - The function to connect the send transport. * @param {Function} options.parameters.getUpdatedAllParams - The function to get updated parameters. * @param {Socket} [options.parameters.localSocket] - The local socket instance. * @param {Transport | null} [options.parameters.localProducerTransport] - The local producer transport object. * @param {boolean} [options.parameters.localTransportCreated] - The state of the local transport creation. * @param {(localTransport: Transport | null) => void} [options.parameters.updateLocalProducerTransport] - The function to update the local transport object. * @param {(state: boolean) => void} [options.parameters.updateLocalTransportCreated] - The function to update the local transport creation state. * @returns {Promise<void>} A promise that resolves when the send transport is created and configured. * * @throws Will throw an error if there is an issue creating the send transport. * * @example * const options = { * option: 'video', * parameters: { * islevel: '1', * member: 'John Doe', * socket: socketInstance, * localSocket: localSocketInstance, * device: deviceInstance, * producerTransport: null, * localProducerTransport: null, * transportCreated: false, * localTransportCreated: false, * updateProducerTransport: (transport) => console.log('Primary transport updated:', transport), * updateLocalProducerTransport: (transport) => console.log('Local transport updated:', transport), * updateTransportCreated: (state) => console.log('Primary transport created:', state), * updateLocalTransportCreated: (state) => console.log('Local transport created:', state), * }, * }; * * createSendTransport(options) * .then(() => console.log('Send transport created successfully')) * .catch((error) => console.error('Error creating send transport:', error)); */ export const createSendTransport = async (options) => { await sharedCreateSendTransport(options); }; //# sourceMappingURL=createSendTransport.js.map