-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathVideoPlayer.js
177 lines (169 loc) · 4.27 KB
/
VideoPlayer.js
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
import React from 'react'
import FontAwesome from 'react-fontawesome'
import { connect } from 'react-redux'
import {
requestAdFullscreen,
requestAdMuted,
requestAdPaused,
requestAdSkip,
requestVideoElementUpdate,
requestVpaidDomUpdate
} from '../actions'
import {
VIDEO_ELEMENT_ID,
VPAID_IFRAME_HTML,
VPAID_IFRAME_ID
} from '../constants/dom'
const kLoadedSrc = Symbol('loadedSrc')
const acceptVideoRef = ({ shared, videoMuted }, callback) => element => {
if (element == null) {
return
}
// React only sets the muted property, not the attribute
// See https://github.com/facebook/react/issues/6544
element.defaultMuted = videoMuted
// Load video source if it's not being controlled by a VPAID unit
if (
!shared &&
element[kLoadedSrc] !== element.src &&
typeof element.load === 'function'
) {
element.load()
element[kLoadedSrc] = element.src
}
// Dispatch
callback(element)
}
const acceptIframeRef = iframe => {
if (iframe == null) {
return
}
const doc = iframe.contentWindow.document
doc.open()
doc.write(VPAID_IFRAME_HTML)
doc.close()
}
const VideoPlayer = ({
shared,
videoSrc,
videoMuted,
adActive,
adPaused,
adMuted,
adFullscreen,
onVideoElementRef,
onVpaidIframeLoaded,
onSetAdPaused,
onSetAdMuted,
onSetAdFullscreen,
onSkipAd
}) => (
<div className="video-player">
<div className="video-player-main">
<div className="video-player-inner">
<video
id={VIDEO_ELEMENT_ID}
src={videoSrc}
muted={videoMuted}
playsInline
ref={acceptVideoRef({ shared, videoMuted }, onVideoElementRef)}
/>
{shared
? (
<iframe
id={VPAID_IFRAME_ID}
src="about:blank"
className="vpaid-host"
title="VPAID Host"
ref={acceptIframeRef}
onLoad={onVpaidIframeLoaded}
/>
)
: null}
</div>
</div>
<div className="video-player-controls">
<nav>
<ul className="ad-controls">
<li>
<button
title="Skip Ad"
onClick={adActive ? onSkipAd : null}
className={adActive ? '' : 'disabled'}
>
<FontAwesome name="close" />
</button>
</li>
</ul>
<ul className="playback-controls">
<li>
<button
title={adPaused ? 'Resume Ad' : 'Pause Ad'}
onClick={adActive ? () => onSetAdPaused(!adPaused) : null}
className={adActive ? '' : 'disabled'}
>
<FontAwesome name={adPaused ? 'play' : 'pause'} />
</button>
</li>
<li>
<button
title={adMuted ? 'Unmute Audio' : 'Mute Audio'}
onClick={adActive ? () => onSetAdMuted(!adMuted) : null}
className={adActive ? '' : 'disabled'}
>
<FontAwesome name={adMuted ? 'volume-off' : 'volume-up'} />
</button>
</li>
<li>
<button
title={adFullscreen ? 'Exit Fullscreen' : 'Enter Fullscreen'}
onClick={adActive ? () => onSetAdFullscreen(!adFullscreen) : null}
className={adActive ? '' : 'disabled'}
>
<FontAwesome name={adFullscreen ? 'compress' : 'expand'} />
</button>
</li>
</ul>
</nav>
</div>
</div>
)
const mapStateToProps = ({
video: { shared, src: videoSrc, muted: videoMuted, paused: videoPaused },
ad: {
active: adActive,
paused: adPaused,
muted: adMuted,
fullscreen: adFullscreen
}
}) => ({
shared,
videoSrc,
videoPaused,
videoMuted,
adActive,
adPaused,
adMuted,
adFullscreen
})
const mapDispatchToProps = dispatch => ({
onVideoElementRef: () => {
dispatch(requestVideoElementUpdate())
},
onVpaidIframeLoaded: () => {
dispatch(requestVpaidDomUpdate())
},
onSetAdPaused: value => {
dispatch(requestAdPaused(value))
},
onSetAdMuted: value => {
dispatch(requestAdMuted(value))
},
onSetAdFullscreen: value => {
dispatch(requestAdFullscreen(value))
},
onSkipAd: () => {
dispatch(requestAdSkip())
}
})
export default connect(mapStateToProps, mapDispatchToProps)(VideoPlayer)