@api.video/react-native-livestream
Version:
RTMP live streaming library from api.video
175 lines (174 loc) • 6.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ApiVideoLiveStreamView = void 0;
var _react = _interopRequireWildcard(require("react"));
var _NativeApiVideoLiveStreamView = _interopRequireWildcard(require("./NativeApiVideoLiveStreamView"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 = exports.ApiVideoLiveStreamView = /*#__PURE__*/(0, _react.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 = (0, _react.useRef)(null);
let _nextRequestId = (0, _react.useRef)(1);
const _requestMap = (0, _react.useRef)(new Map());
(0, _react.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
});
});
_NativeApiVideoLiveStreamView.Commands.startStreaming(nativeRef.current, requestId, streamKey, url);
return promise;
} else {
return new Promise((resolve, reject) => {
reject('Native component is not mounted');
});
}
},
stopStreaming: () => nativeRef.current && _NativeApiVideoLiveStreamView.Commands.stopStreaming(nativeRef.current),
setZoomRatio: zoomRatio => nativeRef.current && _NativeApiVideoLiveStreamView.Commands.setZoomRatioCommand(nativeRef.current, zoomRatio)
}));
return /*#__PURE__*/_react.default.createElement(_NativeApiVideoLiveStreamView.default, {
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
});
});
//# sourceMappingURL=index.js.map