Files
Iris/src/js/components/Stream.js

88 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-03-20 08:15:15 +13: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';
2020-10-02 07:11:08 +13:00
import { SnapStream } from './SnapStream';
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) {
this.start();
}
}
start = () => {
const {
host,
port,
ssl,
username,
} = this.props;
if (this.snapstream) {
this.snapstream.play();
2020-10-02 07:11:08 +13:00
} else {
const protocol = (ssl ? 'wss' : 'ws');
this.snapstream = new SnapStream(protocol, host, port, 'Iris');
}
}
stop = () => {
if (this.snapstream) {
this.snapstream.stop();
2020-10-02 07:11:08 +13:00
this.snapstream = null;
}
}
componentDidUpdate = ({
streaming_enabled: prevStreamingEnabled,
}) => {
const {
enabled,
streaming_enabled,
} = this.props;
if (!prevStreamingEnabled && enabled && streaming_enabled) {
this.start();
2020-10-02 07:11:08 +13:00
}
if (!enabled || !streaming_enabled) {
this.stop();
}
}
render = () => null;
}
2020-03-20 08:15:15 +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);