UNPKG

@api.video/react-native-livestream

Version:

RTMP live streaming library from api.video

168 lines 5.62 kB
import React, { forwardRef, useImperativeHandle, useRef } from 'react'; import NativeApiVideoLiveStreamView, { Commands as NativeLiveStreamCommands } from './NativeApiVideoLiveStreamView'; const LIVE_STREAM_PROPS_DEFAULTS = { style: {}, camera: 'back', video: { bitrate: 2000000, fps: 30, resolution: { width: 1280, height: 720 }, gopDuration: 1 }, isMuted: false, audio: { bitrate: 128000, sampleRate: 44100, isStereo: true }, zoomRatio: 1.0, enablePinchedZoom: true }; const getDefaultBitrate = resolution => { var numOfPixels = resolution.width * resolution.height; switch (true) { case numOfPixels <= 102240: // for 4/3 and 16/9 240p return 800000; case numOfPixels <= 230400: // for 16/9 360p return 1000000; case numOfPixels <= 409920: // for 4/3 and 16/9 480p return 1300000; case numOfPixels <= 921600: // for 4/3 600p, 4/3 768p and 16/9 720p return 2000000; default: return 3500000; // for 16/9 1080p } }; function resolveResolution(resolution) { const predefinedResolutions = { '1080p': { width: 1920, height: 1080 }, '720p': { width: 1280, height: 720 }, '480p': { width: 854, height: 480 }, '360p': { width: 640, height: 360 }, '240p': { width: 352, height: 240 } }; if (typeof resolution === 'string') { const predefined = predefinedResolutions[resolution]; if (!predefined) { throw new Error('Unknown resolution ' + resolution); } return predefined; } return { width: Math.max(resolution.height, resolution.width), height: Math.min(resolution.height, resolution.width) }; } const ApiVideoLiveStreamView = /*#__PURE__*/forwardRef((props, forwardedRef) => { var _props$video; const resolution = resolveResolution(((_props$video = props.video) === null || _props$video === void 0 ? void 0 : _props$video.resolution) || '720p'); const nativeLiveStreamProps = { ...LIVE_STREAM_PROPS_DEFAULTS, ...props, video: { ...LIVE_STREAM_PROPS_DEFAULTS.video, bitrate: getDefaultBitrate(resolution), ...props.video, resolution }, audio: { ...LIVE_STREAM_PROPS_DEFAULTS.audio, ...props.audio }, onConnectionSuccess: props.onConnectionSuccess ? () => { var _props$onConnectionSu; (_props$onConnectionSu = props.onConnectionSuccess) === null || _props$onConnectionSu === void 0 || _props$onConnectionSu.call(props); } : undefined, onConnectionFailed: props.onConnectionFailed ? event => { var _props$onConnectionFa; (_props$onConnectionFa = props.onConnectionFailed) === null || _props$onConnectionFa === void 0 || _props$onConnectionFa.call(props, event.nativeEvent.code); } : undefined, onDisconnect: props.onDisconnect ? () => { var _props$onDisconnect; (_props$onDisconnect = props.onDisconnect) === null || _props$onDisconnect === void 0 || _props$onDisconnect.call(props); } : undefined, onPermissionsDenied: props.onPermissionsDenied ? event => { var _props$onPermissionsD; (_props$onPermissionsD = props.onPermissionsDenied) === null || _props$onPermissionsD === void 0 || _props$onPermissionsD.call(props, event.nativeEvent.permissions); } : undefined, onStartStreaming: event => { const { requestId, result, error } = event.nativeEvent; const promise = _requestMap.current.get(requestId); if (result) { promise === null || promise === void 0 || promise.resolve(result); } else { promise === null || promise === void 0 || promise.reject(error); } _requestMap.current.delete(requestId); } }; const nativeRef = useRef(null); let _nextRequestId = useRef(1); const _requestMap = useRef(new Map()); useImperativeHandle(forwardedRef, () => ({ startStreaming: (streamKey, url) => { if (nativeRef.current) { const requestId = _nextRequestId.current++; const requestMap = _requestMap; const promise = new Promise((resolve, reject) => { requestMap.current.set(requestId, { resolve, reject }); }); NativeLiveStreamCommands.startStreaming(nativeRef.current, requestId, streamKey, url); return promise; } else { return new Promise((resolve, reject) => { reject('Native component is not mounted'); }); } }, stopStreaming: () => nativeRef.current && NativeLiveStreamCommands.stopStreaming(nativeRef.current), setZoomRatio: zoomRatio => nativeRef.current && NativeLiveStreamCommands.setZoomRatioCommand(nativeRef.current, zoomRatio) })); return /*#__PURE__*/React.createElement(NativeApiVideoLiveStreamView, { style: nativeLiveStreamProps.style, camera: nativeLiveStreamProps.camera, video: nativeLiveStreamProps.video, isMuted: nativeLiveStreamProps.isMuted, audio: nativeLiveStreamProps.audio, zoomRatio: nativeLiveStreamProps.zoomRatio, enablePinchedZoom: nativeLiveStreamProps.enablePinchedZoom, onConnectionSuccess: nativeLiveStreamProps.onConnectionSuccess, onConnectionFailed: nativeLiveStreamProps.onConnectionFailed, onDisconnect: nativeLiveStreamProps.onDisconnect, onPermissionsDenied: nativeLiveStreamProps.onPermissionsDenied, onStartStreaming: nativeLiveStreamProps.onStartStreaming, ref: nativeRef }); }); export { ApiVideoLiveStreamView }; //# sourceMappingURL=index.js.map