2020-05-17 20:22:15 +12:00
|
|
|
import React from 'react';
|
2020-03-20 08:15:15 +13:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
|
import * as coreActions from '../services/core/actions';
|
2021-03-23 21:04:08 +13:00
|
|
|
import { SnapStream } from './SnapStream.tsx';
|
2020-03-20 08:15:15 +13:00
|
|
|
|
|
|
|
|
class Stream extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
2020-10-02 07:11:08 +13:00
|
|
|
if (props.enabled && props.streaming_enabled) {
|
2020-10-02 22:28:03 +13:00
|
|
|
this.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = () => {
|
|
|
|
|
const {
|
|
|
|
|
host,
|
|
|
|
|
port,
|
|
|
|
|
ssl,
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
if (this.snapstream) {
|
|
|
|
|
this.snapstream.play();
|
2020-10-02 07:11:08 +13:00
|
|
|
} else {
|
2021-03-23 21:04:08 +13:00
|
|
|
const baseUrl = `${ssl ? 'wss' : 'ws'}://${host}:${port}`;
|
|
|
|
|
this.snapstream = new SnapStream(baseUrl);
|
2020-10-02 22:28:03 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stop = () => {
|
|
|
|
|
if (this.snapstream) {
|
|
|
|
|
this.snapstream.stop();
|
2020-10-02 07:11:08 +13:00
|
|
|
this.snapstream = null;
|
2020-05-13 20:48:44 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 22:28:03 +13:00
|
|
|
componentDidUpdate = ({
|
|
|
|
|
streaming_enabled: prevStreamingEnabled,
|
|
|
|
|
}) => {
|
|
|
|
|
const {
|
|
|
|
|
enabled,
|
|
|
|
|
streaming_enabled,
|
|
|
|
|
} = this.props;
|
2020-05-17 20:22:15 +12:00
|
|
|
|
2020-10-02 22:28:03 +13:00
|
|
|
if (!prevStreamingEnabled && enabled && streaming_enabled) {
|
|
|
|
|
this.start();
|
2020-10-02 07:11:08 +13:00
|
|
|
}
|
2020-10-02 22:28:03 +13:00
|
|
|
if (!enabled || !streaming_enabled) {
|
|
|
|
|
this.stop();
|
2020-05-13 20:48:44 +12:00
|
|
|
}
|
2020-05-17 20:53:05 +12:00
|
|
|
}
|
|
|
|
|
|
2020-05-17 20:22:15 +12:00
|
|
|
render = () => null;
|
2020-04-08 09:15:45 +12:00
|
|
|
}
|
2020-03-20 08:15:15 +13:00
|
|
|
|
2020-10-02 22:28:03 +13:00
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
|
const {
|
|
|
|
|
snapcast: {
|
|
|
|
|
enabled,
|
|
|
|
|
streaming_enabled,
|
|
|
|
|
host,
|
|
|
|
|
port,
|
|
|
|
|
ssl,
|
|
|
|
|
},
|
|
|
|
|
pusher: {
|
|
|
|
|
username,
|
|
|
|
|
},
|
|
|
|
|
} = state;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
enabled,
|
|
|
|
|
streaming_enabled,
|
|
|
|
|
host,
|
|
|
|
|
port,
|
|
|
|
|
ssl,
|
|
|
|
|
username,
|
|
|
|
|
};
|
|
|
|
|
};
|
2020-03-20 08:15:15 +13:00
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
coreActions: bindActionCreators(coreActions, dispatch),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Stream);
|