import React, { Component } from 'react'; import { channelNames } from 'muse-js'; // eslint-disable-next-line no-unused-vars import Chart from 'chart.js/auto'; import { Bar } from 'react-chartjs-2'; import './Broadcast.css'; import { Badge, Button, ButtonGroup, Card, Col, Row } from 'react-bootstrap'; import BrainSummary from '../components/BrainSummary'; const { IVSPlayer } = window; const { create: createMediaPlayer, isPlayerSupported, PlayerEventType, PlayerState } = IVSPlayer; const { ENDED, PLAYING, READY, BUFFERING } = PlayerState; const { TEXT_METADATA_CUE, ERROR } = PlayerEventType; // eslint-disable-next-line no-undef const STREAM_URL = process.env.REACT_APP_STREAM_URL; export class Playback extends Component { constructor() { super(); this.chartReferenceCh0 = React.createRef(); this.chartReferenceCh1 = React.createRef(); this.chartReferenceCh2 = React.createRef(); this.chartReferenceCh3 = React.createRef(); this.videoRef = React.createRef(); this.playerRef = React.createRef(); this.state = { status: 'Disconnected', isPlaying: false, currentChannel: 0, ch0: { labels: ['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma'], datasets: [ { label: 'Channel 0', fillColor: 'rgba(220,220,220,0.2)', strokeColor: 'rgba(220,220,220,1)', pointColor: 'rgba(220,220,220,1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(220,220,220,1)', data: [0, 0, 0, 0, 0] } ] }, ch1: { labels: ['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma'], datasets: [ { label: 'Channel 1', fillColor: 'rgba(220,220,220,0.2)', strokeColor: 'rgba(220,220,220,1)', pointColor: 'rgba(220,220,220,1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(220,220,220,1)', data: [0, 0, 0, 0, 0] } ] }, ch2: { labels: ['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma'], datasets: [ { label: 'Channel 2', fillColor: 'rgba(220,220,220,0.2)', strokeColor: 'rgba(220,220,220,1)', pointColor: 'rgba(220,220,220,1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(220,220,220,1)', data: [0, 0, 0, 0, 0] } ] }, ch3: { labels: ['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma'], datasets: [ { label: 'Channel 3', fillColor: 'rgba(220,220,220,0.2)', strokeColor: 'rgba(220,220,220,1)', pointColor: 'rgba(220,220,220,1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(220,220,220,1)', data: [0, 0, 0, 0, 0] } ] } }; } showChannel = (channel) => { this.setState({ currentChannel: channel }); }; componentWillUnmount() { if (!this.playerRef.current) return; this.playerRef.current.removeEventListener(PLAYING, this.onPlayerStateChange); this.playerRef.current.removeEventListener(ENDED, this.onPlayerStateChange); this.playerRef.current.removeEventListener(ERROR, this.onPlayerError); this.playerRef.current.pause(); this.playerRef.current.delete(); this.playerRef.current = null; this.videoRef.current?.removeAttribute('src'); // remove possible stale src } componentDidMount() { if (!isPlayerSupported) { console.warn('IVS Player is not supported!'); } this.playerRef.current = createMediaPlayer(); this.playerRef.current.attachHTMLVideoElement(this.videoRef.current); this.playerRef.current.load(STREAM_URL); this.playerRef.current.play(); this.playerRef.current.addEventListener(READY, this.onPlayerStateChange); this.playerRef.current.addEventListener(PLAYING, this.onPlayerStateChange); this.playerRef.current.addEventListener(BUFFERING, this.onPlayerStateChange); this.playerRef.current.addEventListener(ENDED, this.onPlayerStateChange); this.playerRef.current.addEventListener(ERROR, this.onPlayerError); this.playerRef.current.addEventListener(TEXT_METADATA_CUE, this.onPlayerMetadata); } onPlayerMetadata = (e) => { const data = JSON.parse(e.text); this.setState(state => { state.ch0.datasets[0].data = data[0]; state.ch1.datasets[0].data = data[1]; state.ch2.datasets[0].data = data[2]; state.ch3.datasets[0].data = data[3]; this.chartReferenceCh0.current.data.datasets[0].data = state.ch0.datasets[0].data; this.chartReferenceCh1.current.data.datasets[0].data = state.ch1.datasets[0].data; this.chartReferenceCh2.current.data.datasets[0].data = state.ch2.datasets[0].data; this.chartReferenceCh3.current.data.datasets[0].data = state.ch3.datasets[0].data; return ({ ch0: state.ch0, ch1: state.ch1, ch2: state.ch2, ch3: state.ch3 }); }); }; onPlayerError = (e) => { console.error(e); }; onPlayerStateChange = () => { this.setState({ isPlaying: this.playerRef.current.getState() === PLAYING, }); if(this.playerRef.current.getState() === ENDED) { this.videoRef.current.setAttribute('src', null); } }; render() { return (