-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp-media-video.js
More file actions
294 lines (262 loc) · 8.98 KB
/
app-media-video.js
File metadata and controls
294 lines (262 loc) · 8.98 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import {IronResizableBehavior} from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js';
import {html} from '@polymer/polymer/lib/utils/html-tag.js';
import {Polymer} from '@polymer/polymer/polymer-legacy.js';
/**
`app-media-video` is an element that converts a video source into a nicely
scaled video that is displayed to the viewer of the page.
@demo demo/index.html
*/
Polymer({
/** @override */
_template: html`
<style>
:host {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: nowrap;
width: 100%;
height: 100%;
background: var(--app-media-video-background, #000);
overflow: hidden;
}
video {
flex: 0 0 auto;
transform-origin: center center;
position: relative;
will-change: transform;
}
</style>
<video id="videoElement" on-loadedmetadata="_updateMetrics" muted="[[muted]]" autoplay="[[autoplay]]" loop="[[loop]]" webkit-playsinline playsinline>
</video>
`,
is: 'app-media-video',
behaviors: [IronResizableBehavior],
properties: {
/**
* The input source for the element. This can be a Media Stream, a
* Blob or a string URL.
*
* @type {MediaStream|Blob|string}
*/
source: {
type: Object,
notify: true,
observer: '_sourceChanged',
},
/**
* If true, the video will be scaled so that the source video is
* flush with the edge of the element, but fully contained by it. If
* false (the default), the video will be scaled to the smallest size
* that is at full-bleed with respect to the element's bounding box.
* Both settings preserve the aspect ratio of the source video.
*/
contain: {
type: Boolean,
value: false,
},
/**
* If true, the video will be inverted along the x-axis so that it is
* effectively mirrored.
*/
mirror: {
type: Boolean,
value: false,
},
/**
* If true, the video is muted.
*/
muted: {
type: Boolean,
value: false,
},
/**
* If true, the video will automatically play when it has a source.
*/
autoplay: {
type: Boolean,
value: false,
},
/**
* If true, the video will loop when it reaches the end of the source.
*/
loop: {
type: Boolean,
value: false,
},
/**
* A bindable reference to the video element that actually plays the
* source. This is sometimes useful in conjunction with
* `app-media-audio`, which can accept an HTMLVideoElement as its
* source.
*
* @type {HTMLVideoElement}
*/
videoElement: {
type: Object,
notify: true,
readOnly: true,
},
/**
* A rect-like object that describes the projection of the source
* video element to the viewport (the bounding box of the
* app-media-video element).
*
* This rectangle is useful to understand the space within the
* source video element that is visible to the user at any given
* time. Depending on the value of contain, and also any transforms
* that affect the scale of the video element, this rectangle could
* have a range of values that are opportunistically calculated
* and made available through this property.
*/
sourceRect: {
type: Object,
notify: true,
readOnly: true,
value: function() {
return {top: 0, left: 0, width: 0, height: 0};
},
},
/**
* This is the bounding ClientRect of the app-media-video element.
* This is kept here for easy future access by users of the element
* and is useful in conjunction with sourceRect.
*
* @type {ClientRect}
*/
viewportRect: {
type: Object,
notify: true,
readOnly: true,
}
},
listeners: {
'videoElement.loadedmetadata': '_updateMetrics',
'iron-resize': '_updateMetrics'
},
observers: ['_updateMetrics(contain, mirror)'],
/** @override */
attached: function() {
this._setVideoElement(
/** @type {HTMLVideoElement} */ (this.$.videoElement));
this._updateMetrics();
},
/**
* Play the video.
*/
play: function() {
this.$.videoElement.play();
},
/**
* Pause the video.
*/
pause: function() {
this.$.videoElement.pause();
},
_sourceChanged: function() {
const videoElement = /** @type {HTMLVideoElement} */ (this.$.videoElement);
var oldSrc = videoElement.src;
var oldPaused = videoElement.paused;
if (typeof this.source === 'string') {
videoElement.src = this.source;
} else {
try {
// NOTE(cdata): Chrome as of 55 does not support anything other than
// MediaStream as the value of srcObject (even though the value is
// allowed to be Blob, per the spec). We try the standardized way,
// and then fall back to URL.createObjectURL if necessary.
videoElement.srcObject = /** @type {MediaStream} */ (this.source);
} catch (e) {
if (this.source instanceof Blob) {
videoElement.src = URL.createObjectURL(this.source);
} else {
this._error(this._logf(e));
}
}
}
if (typeof oldSrc === 'string') {
try {
URL.revokeObjectURL(oldSrc);
} catch (e) {
}
}
// Ensure that the video keeps playing if it was playing before we
// changed the source:
if (!oldPaused && videoElement.paused) {
this.play();
}
// NOTE(cdata): No need to manually update metrics here, since the
// new source will cause the video element to fire a loadmetadata
// event when it is ready.
},
_updateMetrics: function() {
this.debounce('_updateMetrics', function() {
// These values are the width and the height of the video source
// being displayed by the video element. They stay the same
// regardless of the dimensions of the video element itself:
var videoWidth = this.$.videoElement.videoWidth;
var videoHeight = this.$.videoElement.videoHeight;
// If the source video dimensions describe an effectively invisible
// or unavailable video, we can just hide the element and exit early.
if (!videoWidth || !videoHeight) {
this.$.videoElement.style.visibility = 'hidden';
return;
} else {
this.$.videoElement.style.visibility = '';
}
// Clear the current transform so that we can get the natural
// bounding rect of the video element:
this.$.videoElement.style.transform = '';
var videoRect = this.$.videoElement.getBoundingClientRect();
var selfRect = this.getBoundingClientRect();
var selfRatio = selfRect.width / selfRect.height;
var videoRatio = videoWidth / videoHeight;
var scaleByHeight =
this.contain ? videoRatio < selfRatio : videoRatio > selfRatio;
// This is the scale of the source video's width compared to the
// width of the video tag's bounding box.
var sourceScale = videoWidth / videoRect.width;
// This is the scale applied to the video to transform it based
// on whether it should be contained within the viewport or not.
var videoScale;
if (scaleByHeight) {
videoScale = selfRect.height / videoRect.height;
} else {
videoScale = selfRect.width / videoRect.width;
}
// Since we already grabbed this element's bounding rect, cache it
// for future comparisons with the source rect:
this._setViewportRect(selfRect);
// The source rect needs to account for:
// - The (eventual) scale of the video element within this element
// - Any transforms on this or ancestor elements that may change
// the effective scale of the video element.
// - Vertical and horizontal centering of the video element within
// this element due to flex layout rules.
var downScaledSelfWidth = selfRect.width / videoScale;
var downScaledSelfHeight = selfRect.height / videoScale;
var realHorizontalOverlap =
(videoRect.width - downScaledSelfWidth) * sourceScale;
var realVerticalOverlap =
(videoRect.height - downScaledSelfHeight) * sourceScale;
this._setSourceRect({
left: realHorizontalOverlap / 2,
top: realVerticalOverlap / 2,
width: downScaledSelfWidth * sourceScale,
height: downScaledSelfHeight * sourceScale
});
this.$.videoElement.style.transform = 'scale(' +
(this.mirror ? -videoScale : videoScale) + ',' + videoScale + ')';
});
}
});