2021-03-02 03:07:10 +01:00
|
|
|
// @flow
|
2021-03-19 21:14:31 +01:00
|
|
|
import { BITWAVE_API } from 'constants/livestream';
|
2021-03-02 03:07:10 +01:00
|
|
|
import React from 'react';
|
|
|
|
import Page from 'component/page';
|
2021-03-15 15:32:51 +01:00
|
|
|
import LivestreamLayout from 'component/livestreamLayout';
|
|
|
|
import analytics from 'analytics';
|
2021-03-30 01:05:18 +02:00
|
|
|
import { Lbry } from 'lbry-redux';
|
2021-03-02 03:07:10 +01:00
|
|
|
|
|
|
|
type Props = {
|
2021-03-15 15:32:51 +01:00
|
|
|
uri: string,
|
|
|
|
claim: StreamClaim,
|
|
|
|
doSetPlayingUri: ({ uri: ?string }) => void,
|
|
|
|
isAuthenticated: boolean,
|
|
|
|
doUserSetReferrer: (string) => void,
|
2021-03-22 10:42:02 +01:00
|
|
|
channelClaim: ChannelClaim,
|
2021-03-02 03:07:10 +01:00
|
|
|
};
|
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
export default function LivestreamPage(props: Props) {
|
2021-03-22 23:48:56 +01:00
|
|
|
const { uri, claim, doSetPlayingUri, isAuthenticated, doUserSetReferrer, channelClaim } = props;
|
2021-03-15 15:32:51 +01:00
|
|
|
const [activeViewers, setActiveViewers] = React.useState(0);
|
2021-03-19 21:14:31 +01:00
|
|
|
const [isLive, setIsLive] = React.useState(false);
|
2021-03-22 10:42:02 +01:00
|
|
|
const livestreamChannelId = channelClaim && channelClaim.signing_channel && channelClaim.signing_channel.claim_id;
|
2021-03-30 01:05:18 +02:00
|
|
|
const [hasLivestreamClaim, setHasLivestreamClaim] = React.useState(false);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-03-30 01:05:18 +02:00
|
|
|
const STREAMING_POLL_INTERVAL_IN_MS = 10000;
|
|
|
|
const LIVESTREAM_CLAIM_POLL_IN_MS = 60000;
|
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
React.useEffect(() => {
|
2021-03-30 01:05:18 +02:00
|
|
|
let checkClaimsInterval;
|
|
|
|
function checkHasLivestreamClaim() {
|
|
|
|
Lbry.claim_search({
|
|
|
|
channel_ids: [livestreamChannelId],
|
|
|
|
has_no_source: true,
|
|
|
|
claim_type: ['stream'],
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (res && res.items && res.items.length > 0) {
|
|
|
|
setHasLivestreamClaim(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}
|
|
|
|
if (livestreamChannelId && !isLive) {
|
|
|
|
if (!checkClaimsInterval) checkHasLivestreamClaim();
|
|
|
|
checkClaimsInterval = setInterval(checkHasLivestreamClaim, LIVESTREAM_CLAIM_POLL_IN_MS);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (checkClaimsInterval) {
|
|
|
|
clearInterval(checkClaimsInterval);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [livestreamChannelId, isLive]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
let interval;
|
2021-03-19 21:14:31 +01:00
|
|
|
function checkIsLive() {
|
2021-03-22 23:48:56 +01:00
|
|
|
// $FlowFixMe Bitwave's API can handle garbage
|
2021-03-22 10:42:02 +01:00
|
|
|
fetch(`${BITWAVE_API}/${livestreamChannelId}`)
|
2021-03-19 21:14:31 +01:00
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => {
|
|
|
|
if (!res || !res.data) {
|
|
|
|
setIsLive(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setActiveViewers(res.data.viewCount);
|
|
|
|
|
2021-03-22 10:42:02 +01:00
|
|
|
if (res.data.hasOwnProperty('live')) {
|
|
|
|
setIsLive(res.data.live);
|
2021-03-19 21:14:31 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-30 01:05:18 +02:00
|
|
|
if (livestreamChannelId && hasLivestreamClaim) {
|
2021-03-22 10:42:02 +01:00
|
|
|
if (!interval) checkIsLive();
|
2021-03-30 01:05:18 +02:00
|
|
|
interval = setInterval(checkIsLive, STREAMING_POLL_INTERVAL_IN_MS);
|
2021-03-22 10:42:02 +01:00
|
|
|
|
2021-03-30 01:05:18 +02:00
|
|
|
return () => {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [livestreamChannelId, hasLivestreamClaim]);
|
2021-03-15 15:32:51 +01:00
|
|
|
|
|
|
|
const stringifiedClaim = JSON.stringify(claim);
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (uri && stringifiedClaim) {
|
|
|
|
const jsonClaim = JSON.parse(stringifiedClaim);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
if (jsonClaim) {
|
|
|
|
const { txid, nout, claim_id: claimId } = jsonClaim;
|
|
|
|
const outpoint = `${txid}:${nout}`;
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
analytics.apiLogView(uri, outpoint, claimId);
|
|
|
|
}
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
const uri = jsonClaim.signing_channel && jsonClaim.signing_channel.permanent_url;
|
|
|
|
if (uri) {
|
|
|
|
doUserSetReferrer(uri.replace('lbry://', ''));
|
|
|
|
}
|
2021-03-02 03:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 15:32:51 +01:00
|
|
|
}, [uri, stringifiedClaim, isAuthenticated]);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-03-15 15:32:51 +01:00
|
|
|
// Set playing uri to null so the popout player doesnt start playing the dummy claim if a user navigates back
|
|
|
|
// This can be removed when we start using the app video player, not a bitwave iframe
|
|
|
|
doSetPlayingUri({ uri: null });
|
|
|
|
}, [doSetPlayingUri]);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
|
|
|
return (
|
2021-03-15 15:32:51 +01:00
|
|
|
<Page className="file-page" filePage livestream>
|
2021-03-22 23:48:56 +01:00
|
|
|
<LivestreamLayout uri={uri} activeViewers={activeViewers} isLive={isLive} />
|
2021-03-02 03:07:10 +01:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|