-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,099 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import URI from '../ppspp/uri'; | ||
import {Client} from '../client'; | ||
import {ConnManager} from '../wrtc'; | ||
import PlayButton from './PlayButton'; | ||
import {useTimeout, useAsync} from 'react-use'; | ||
import useQuery from '../hooks/useQuery'; | ||
import VideoPlayer from './VideoPlayer'; | ||
|
||
import './App.scss'; | ||
|
||
const NoiseLogger = React.lazy(() => import('./NoiseLogger')); | ||
const PubSubLogger = React.lazy(() => import('./PubSubLogger')); | ||
|
||
const getDefaultBootstrapAddress = () => { | ||
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws'; | ||
const host = process.env.NODE_ENV === 'development' | ||
? window.location.hostname + ':8080' | ||
: window.location.host; | ||
return `${proto}://${host}`; | ||
}; | ||
|
||
const useSwarm = ({ppsppClient} = {}) => { | ||
const [swarm, setSwarm] = useState(null); | ||
const join = uri => setSwarm(ppsppClient.joinSwarm(URI.parse(uri))); | ||
return [swarm, join]; | ||
}; | ||
|
||
const App = ({ | ||
location, | ||
match: {params}, | ||
clientTimeoutMs = 5000, | ||
}) => { | ||
const query = useQuery(location.search); | ||
const autoPlay = 'autoplay' in query; | ||
const bootstrapAddress = query.bootstrap || getDefaultBootstrapAddress(); | ||
const swarmName = params.name; | ||
|
||
const clientTimeout = useTimeout(clientTimeoutMs); | ||
const { | ||
loading: clientLoading, | ||
error: clientError, | ||
value: client, | ||
} = useAsync(() => Client.create(new ConnManager(bootstrapAddress)), []); | ||
|
||
const [swarm, joinSwarm] = useSwarm(client); | ||
|
||
const swarmDesc = client?.bootstrap.swarms.find(desc => desc.name === swarmName); | ||
const error = clientError || (autoPlay && clientTimeout) || !(clientLoading || swarmDesc); | ||
|
||
useEffect(() => { | ||
if (autoPlay && swarmDesc) { | ||
setImmediate(() => joinSwarm(swarmDesc.uri)); | ||
} | ||
}, [autoPlay, swarmDesc]); | ||
|
||
if (swarm) { | ||
const Component = { | ||
'application/octet-stream': NoiseLogger, | ||
'application/json': PubSubLogger, | ||
'video/mpeg-ts': VideoPlayer, | ||
}[swarmDesc.contentType]; | ||
|
||
return ( | ||
<Component swarm={swarm} /> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="idle"> | ||
<div className="noise"></div> | ||
</div> | ||
<PlayButton | ||
disabled={clientLoading || autoPlay || error} | ||
onClick={() => joinSwarm(swarmDesc.uri)} | ||
pulse={!clientLoading && !autoPlay} | ||
flicker={clientLoading || autoPlay} | ||
error={error} | ||
blur | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default App; |
Oops, something went wrong.