-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathPlayer.js
More file actions
208 lines (175 loc) · 5.66 KB
/
Player.js
File metadata and controls
208 lines (175 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, {Component} from 'react'
import {Link} from 'react-router-dom'
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import {faPlay, faPause, faSpinner} from '@fortawesome/free-solid-svg-icons'
import MediaStreamer from './MediaStreamer'
import Spectrogram from './Spectrogram'
import {feedType} from 'types/feedType'
import {storeCurrentFeed, getCurrentFeed} from 'utils/feedStorage'
import 'styles/player.scss'
export default class Player extends Component {
static propTypes = {
feed: feedType,
}
constructor(props) {
super(props)
const currentFeed = this.props.currentFeed || getCurrentFeed() || {}
this.state = {
currentFeed,
timestamp: '',
isLoading: true,
isPlaying: false,
intervalId: null,
debugInfo: {
playerTime: 0,
latencyHistory: [0],
},
play: () => {},
pause: () => {},
playPause: () => {},
}
}
isEmpty = object => Object.keys(object).length === 0
startTimestampFetcher = () => {
var {currentFeed, intervalId} = this.state
if (intervalId) {
clearInterval(intervalId)
}
if (currentFeed && Object.keys(currentFeed).length > 0) {
this.fetchTimestamp(currentFeed.nodeName)
intervalId = setInterval(
() => this.fetchTimestamp(currentFeed.nodeName),
10000,
)
this.setState({intervalId})
}
}
componentDidMount() {
this.startTimestampFetcher()
}
test(){
spec = this.spectrogram('viewport')
navigator.getUserMedia({audio: true}, spec.gotStream, function() { console.log("error"); })
}
componentWillUnmount() {
const {intervalId} = this.state
clearInterval(intervalId)
}
componentDidUpdate(prevProps) {
const {currentFeed} = this.props
if (currentFeed !== prevProps.currentFeed) {
storeCurrentFeed(currentFeed)
this.setState({currentFeed}, this.startTimestampFetcher)
}
}
playIconOpts = ({isLoading, isPlaying}) => {
if (isLoading) return {icon: faSpinner, pulse: true}
if (isPlaying) return {icon: faPause}
return {icon: faPlay}
}
debugInfo = (hlsUri, awsConsoleUri) => (
<div className="ml-auto">
<span className="mr-2" title="Stream Latency">
{Math.round(this.getStreamLatency())}
</span>
<span className="mr-2" title="Total Latency">
{Math.round(this.getTotalLatency())}
</span>
<a href={hlsUri} className="mx-2" target="_blank">
HLS
</a>
<a href={awsConsoleUri} className="mx-2" target="_blank">
AWS
</a>
</div>
)
getStreamLatency = () => {
return this.state.debugInfo.latencyHistory[
this.state.debugInfo.latencyHistory.length - 1
]
}
getTotalLatency = () => {
return (
Math.floor(Date.now() / 1000) -
(+this.state.timestamp + this.state.debugInfo.playerTime)
)
}
getHlsUri = (timestamp, feed) =>
`https://s3-us-west-2.amazonaws.com/dev-streaming-orcasound-net/${feed}/hls/${timestamp}/live.m3u8`
getAwsConsoleUri = (timestamp, nodeName) =>
`https://s3.console.aws.amazon.com/s3/buckets/dev-streaming-orcasound-net/${nodeName}/hls/${timestamp}/`
fetchTimestamp = feed => {
const timestampURI = `https://s3-us-west-2.amazonaws.com/dev-streaming-orcasound-net/${feed}/latest.txt`
const xhr = new XMLHttpRequest()
xhr.open('GET', timestampURI)
xhr.onload = () => {
if (xhr.status === 200) {
const timestamp = xhr.responseText.trim()
if (ENV.development) console.log('Latest timestamp: ' + timestamp)
if (timestamp != this.state.timestamp) {
this.setState({
timestamp: timestamp,
hlsURI: this.getHlsUri(timestamp, feed),
})
if (ENV.development)
console.log(
'New stream instance: ' + this.getHlsUri(timestamp, feed),
)
}
}
}
xhr.send()
}
setControls = controls => this.setState({...controls})
render() {
const {hlsURI, playPause, currentFeed} = this.state
const awsConsoleUri = this.getAwsConsoleUri(
this.state.timestamp,
currentFeed.nodeName,
)
if (currentFeed && Object.keys(currentFeed).length !== 0) {
return (
<div className="audio-player text-light d-flex align-items-center justify-content-start">
<FontAwesomeIcon
size="3x"
{...this.playIconOpts(this.state)}
className="mr-3"
onClick={playPause}
/>
{currentFeed.slug && (
<Link to={currentFeed.slug} className="text-light">
{currentFeed.name}
</Link>
)}
{hlsURI && (
<MediaStreamer
src={hlsURI}
onReady={this.setControls}
onLoading={() => this.setState({isLoading: true})}
onPlaying={() =>
this.setState({isLoading: false, isPlaying: true})
}
onPaused={() =>
this.setState({isLoading: false, isPlaying: false})
}
onLatencyUpdate={(newestLatency, playerTime) =>
this.setState({
debugInfo: {
playerTime: playerTime,
latencyHistory: this.state.debugInfo.latencyHistory.concat(
newestLatency,
),
},
})
}
/>
)}
{document.getElementsByTagName('audio')[0] && (<Spectrogram componentId='viewport' audioElement={document.getElementsByTagName('audio')[0]}
src={hlsURI}/>)}
{this.debugInfo(hlsURI, awsConsoleUri)}
</div>
)
}
return <div />
}
}