Improve livestream external embeds

This commit is contained in:
Rafael 2022-03-15 13:47:36 -03:00 committed by Thomas Zarebczan
parent b096aad70e
commit b7f9152dca
5 changed files with 104 additions and 44 deletions

View file

@ -94,8 +94,6 @@ type Props = {
fetchModAmIList: () => void, fetchModAmIList: () => void,
}; };
export const SocketContext = React.createContext<any>();
function App(props: Props) { function App(props: Props) {
const { const {
theme, theme,
@ -139,8 +137,6 @@ function App(props: Props) {
const previousHasVerifiedEmail = usePrevious(hasVerifiedEmail); const previousHasVerifiedEmail = usePrevious(hasVerifiedEmail);
const previousRewardApproved = usePrevious(isRewardApproved); const previousRewardApproved = usePrevious(isRewardApproved);
const [socketConnected, setSocketConnection] = React.useState(false);
const [gdprRequired, setGdprRequired] = usePersistedState('gdprRequired'); const [gdprRequired, setGdprRequired] = usePersistedState('gdprRequired');
const [localeLangs, setLocaleLangs] = React.useState(); const [localeLangs, setLocaleLangs] = React.useState();
const [localeSwitchDismissed] = usePersistedState('locale-switch-dismissed', false); const [localeSwitchDismissed] = usePersistedState('locale-switch-dismissed', false);
@ -567,13 +563,11 @@ function App(props: Props) {
/> />
) : ( ) : (
<React.Fragment> <React.Fragment>
<SocketContext.Provider value={socketConnected}>
<Router /> <Router />
</SocketContext.Provider>
<ModalRouter /> <ModalRouter />
<React.Suspense fallback={null}>{renderFiledrop && <FileDrop />}</React.Suspense> <React.Suspense fallback={null}>{renderFiledrop && <FileDrop />}</React.Suspense>
<FileRenderFloating setSocketConnection={setSocketConnection} /> <FileRenderFloating />
<React.Suspense fallback={null}> <React.Suspense fallback={null}>
{isEnhancedLayout && <Yrbl className="yrbl--enhanced" />} {isEnhancedLayout && <Yrbl className="yrbl--enhanced" />}

View file

@ -0,0 +1,23 @@
// @flow
import React from 'react';
import { formatLbryChannelName } from 'util/url';
export default function useSocketConnect(
isLivestreamClaim: boolean,
claimId: boolean,
channelUrl: ?string,
canonicalUrl: boolean,
doCommentSocketConnect: (string, string, string) => void,
doCommentSocketDisconnect: (string, string) => void
) {
// Establish web socket connection for viewer count.
React.useEffect(() => {
if (!isLivestreamClaim || !claimId || !channelUrl || !canonicalUrl) return;
const channelName = formatLbryChannelName(channelUrl);
doCommentSocketConnect(canonicalUrl, channelName, claimId);
return () => doCommentSocketDisconnect(claimId, channelName);
}, [canonicalUrl, channelUrl, claimId, doCommentSocketConnect, doCommentSocketDisconnect, isLivestreamClaim]);
}

View file

@ -1,33 +1,46 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import EmbedWrapperPage from './view'; import EmbedWrapperPage from './view';
import { makeSelectClaimForUri, selectIsUriResolving } from 'redux/selectors/claims'; import { selectClaimForUri, selectIsUriResolving } from 'redux/selectors/claims';
import { makeSelectStreamingUrlForUri } from 'redux/selectors/file_info'; import { makeSelectStreamingUrlForUri } from 'redux/selectors/file_info';
import { doResolveUri } from 'redux/actions/claims'; import { doResolveUri } from 'redux/actions/claims';
import { buildURI } from 'util/lbryURI'; import { buildURI } from 'util/lbryURI';
import { doPlayUri } from 'redux/actions/content'; import { doPlayUri } from 'redux/actions/content';
import { selectCostInfoForUri, doFetchCostInfoForUri, selectBlackListedOutpoints } from 'lbryinc'; import { selectCostInfoForUri, doFetchCostInfoForUri, selectBlackListedOutpoints } from 'lbryinc';
import { doCommentSocketConnect, doCommentSocketDisconnect } from 'redux/actions/websocket';
import { doFetchActiveLivestreams, doFetchChannelLiveStatus } from 'redux/actions/livestream';
import { selectIsActiveLivestreamForUri, selectActiveLivestreams } from 'redux/selectors/livestream';
import { isStreamPlaceholderClaim } from 'util/claim';
const select = (state, props) => { const select = (state, props) => {
const { match } = props; const { match } = props;
const { params } = match; const { params } = match;
const { claimName, claimId } = params; const { claimName, claimId } = params;
const uri = claimName ? buildURI({ claimName, claimId }) : ''; const uri = claimName ? buildURI({ claimName, claimId }) : '';
const claim = selectClaimForUri(state, uri);
const { canonical_url: canonicalUrl } = claim || {};
return { return {
uri, uri,
claim: makeSelectClaimForUri(uri)(state), claim,
costInfo: selectCostInfoForUri(state, uri), costInfo: selectCostInfoForUri(state, uri),
streamingUrl: makeSelectStreamingUrlForUri(uri)(state), streamingUrl: makeSelectStreamingUrlForUri(uri)(state),
isResolvingUri: selectIsUriResolving(state, uri), isResolvingUri: selectIsUriResolving(state, uri),
blackListedOutpoints: selectBlackListedOutpoints(state), blackListedOutpoints: selectBlackListedOutpoints(state),
isCurrentClaimLive: canonicalUrl && selectIsActiveLivestreamForUri(state, canonicalUrl),
isLivestreamClaim: isStreamPlaceholderClaim(claim),
activeLivestreams: selectActiveLivestreams(state),
}; };
}; };
const perform = (dispatch) => { const perform = {
return { doResolveUri,
resolveUri: (uri) => dispatch(doResolveUri(uri)), doPlayUri,
doPlayUri: (uri) => dispatch(doPlayUri(uri)), doFetchCostInfoForUri,
doFetchCostInfoForUri: (uri) => dispatch(doFetchCostInfoForUri(uri)), doFetchChannelLiveStatus,
}; doCommentSocketConnect,
doCommentSocketDisconnect,
doFetchActiveLivestreams,
}; };
export default connect(select, perform)(EmbedWrapperPage); export default connect(select, perform)(EmbedWrapperPage);

View file

@ -1,51 +1,70 @@
// @flow // @flow
import { SITE_NAME } from 'config'; import { SITE_NAME } from 'config';
import React, { useEffect } from 'react'; import React from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
import FileRender from 'component/fileRender'; import FileRender from 'component/fileRender';
import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle'; import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle';
import Spinner from 'component/spinner'; import Spinner from 'component/spinner';
import Button from 'component/button'; import Button from 'component/button';
import Card from 'component/common/card'; import Card from 'component/common/card';
import { formatLbryUrlForWeb } from 'util/url'; import { formatLbryUrlForWeb, formatLbryChannelName } from 'util/url';
import { useHistory } from 'react-router'; import { useHistory } from 'react-router';
import useSocketConnect from 'effects/use-socket-connect';
const LIVESTREAM_STATUS_CHECK_INTERVAL = 30000;
type Props = { type Props = {
uri: string, uri: string,
resolveUri: (string) => void, claim: ?any,
claim: Claim,
doPlayUri: (string) => void,
costInfo: any, costInfo: any,
streamingUrl: string, streamingUrl: string,
doFetchCostInfoForUri: (string) => void,
isResolvingUri: boolean, isResolvingUri: boolean,
blackListedOutpoints: Array<{ blackListedOutpoints: Array<{ txid: string, nout: number }>,
txid: string, isCurrentClaimLive: boolean,
nout: number, isLivestreamClaim: boolean,
}>, activeLivestreams: ?any,
doResolveUri: (uri: string) => void,
doPlayUri: (uri: string) => void,
doFetchCostInfoForUri: (uri: string) => void,
doFetchChannelLiveStatus: (string) => void,
doCommentSocketConnect: (string, string, string) => void,
doCommentSocketDisconnect: (string, string) => void,
doFetchActiveLivestreams: () => void,
}; };
export const EmbedContext = React.createContext<any>(); export const EmbedContext = React.createContext<any>();
const EmbedWrapperPage = (props: Props) => { const EmbedWrapperPage = (props: Props) => {
const { const {
resolveUri,
claim,
uri, uri,
doPlayUri, claim,
costInfo, costInfo,
streamingUrl, streamingUrl,
doFetchCostInfoForUri,
isResolvingUri, isResolvingUri,
blackListedOutpoints, blackListedOutpoints,
isCurrentClaimLive,
isLivestreamClaim: liveClaim,
activeLivestreams,
doResolveUri,
doPlayUri,
doFetchCostInfoForUri,
doFetchChannelLiveStatus,
doCommentSocketConnect,
doCommentSocketDisconnect,
doFetchActiveLivestreams,
} = props; } = props;
const { const {
location: { search }, location: { search },
} = useHistory(); } = useHistory();
const { claim_id: claimId, canonical_url: canonicalUrl, signing_channel: channelClaim } = claim || {};
const channelUrl = channelClaim && formatLbryChannelName(channelClaim.canonical_url);
const urlParams = new URLSearchParams(search); const urlParams = new URLSearchParams(search);
const embedLightBackground = urlParams.get('embedBackgroundLight'); const embedLightBackground = urlParams.get('embedBackgroundLight');
const haveClaim = Boolean(claim); const haveClaim = Boolean(claim);
const readyToDisplay = claim && streamingUrl; const readyToDisplay = isCurrentClaimLive || (claim && streamingUrl);
const loading = !claim && isResolvingUri; const loading = !claim && isResolvingUri;
const noContentFound = !claim && !isResolvingUri; const noContentFound = !claim && !isResolvingUri;
const isPaidContent = costInfo && costInfo.cost > 0; const isPaidContent = costInfo && costInfo.cost > 0;
@ -60,21 +79,38 @@ const EmbedWrapperPage = (props: Props) => {
(outpoint.txid === claim.txid && outpoint.nout === claim.nout) (outpoint.txid === claim.txid && outpoint.nout === claim.nout)
); );
useEffect(() => { React.useEffect(doFetchActiveLivestreams, [doFetchActiveLivestreams]);
if (resolveUri && uri && !haveClaim) {
resolveUri(uri); useSocketConnect(liveClaim, claimId, channelUrl, canonicalUrl, doCommentSocketConnect, doCommentSocketDisconnect);
React.useEffect(() => {
if (doResolveUri && uri && !haveClaim) {
doResolveUri(uri);
} }
if (uri && haveClaim && costInfo && costInfo.cost === 0) { if (uri && haveClaim && costInfo && costInfo.cost === 0) {
doPlayUri(uri); doPlayUri(uri);
} }
}, [resolveUri, uri, doPlayUri, haveClaim, costInfo]); }, [doResolveUri, uri, doPlayUri, haveClaim, costInfo]);
useEffect(() => { React.useEffect(() => {
if (haveClaim && uri && doFetchCostInfoForUri) { if (haveClaim && uri && doFetchCostInfoForUri) {
doFetchCostInfoForUri(uri); doFetchCostInfoForUri(uri);
} }
}, [uri, haveClaim, doFetchCostInfoForUri]); }, [uri, haveClaim, doFetchCostInfoForUri]);
// Find out current channels status + active live claim every 30 seconds
React.useEffect(() => {
if (!channelClaim || !activeLivestreams) return;
const { claim_id: channelClaimId } = channelClaim || {};
doFetchChannelLiveStatus(channelClaimId);
const intervalId = setInterval(() => doFetchChannelLiveStatus(channelClaimId), LIVESTREAM_STATUS_CHECK_INTERVAL);
return () => clearInterval(intervalId);
}, [activeLivestreams, channelClaim, doFetchChannelLiveStatus]);
if (isClaimBlackListed) { if (isClaimBlackListed) {
return ( return (
<Card <Card
@ -92,11 +128,7 @@ const EmbedWrapperPage = (props: Props) => {
} }
return ( return (
<div <div className={classnames('embed__wrapper', { 'embed__wrapper--light-background': embedLightBackground })}>
className={classnames('embed__wrapper', {
'embed__wrapper--light-background': embedLightBackground,
})}
>
<EmbedContext.Provider value> <EmbedContext.Provider value>
{readyToDisplay ? ( {readyToDisplay ? (
<FileRender uri={uri} embedded /> <FileRender uri={uri} embedded />

View file

@ -18,9 +18,7 @@ Lbry.setDaemonConnectionString(PROXY_URL);
async function getClaim(requestUrl) { async function getClaim(requestUrl) {
const uri = requestUrl.replace(`${URL}/`, 'lbry://'); const uri = requestUrl.replace(`${URL}/`, 'lbry://');
let claim; let claim, error;
let error;
try { try {
const response = await Lbry.resolve({ urls: [uri] }); const response = await Lbry.resolve({ urls: [uri] });
if (response && response[uri] && !response[uri].error) { if (response && response[uri] && !response[uri].error) {
@ -33,7 +31,7 @@ async function getClaim(requestUrl) {
} else { } else {
const { value_type, value } = claim; const { value_type, value } = claim;
if (value_type !== 'stream' || value.stream_type !== 'video') { if (value_type !== 'stream' || (value.stream_type !== 'video' && value.stream_type !== undefined)) {
error = 'The URL is not associated with a video claim.'; error = 'The URL is not associated with a video claim.';
} }
} }