/
video-listener-mixin.js
177 lines (154 loc) · 5.04 KB
/
video-listener-mixin.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 { STATE_IDLE, STATE_COMPLETE, STATE_STALLED, STATE_LOADING, STATE_PLAYING, STATE_PAUSED,
PROVIDER_FIRST_FRAME, CLICK, MEDIA_BUFFER_FULL, MEDIA_RATE_CHANGE,
MEDIA_BUFFER, MEDIA_META, MEDIA_TIME, MEDIA_SEEKED, MEDIA_VOLUME, MEDIA_MUTE, MEDIA_COMPLETE
} from 'events/events';
import { between } from 'utils/math';
// This will trigger the events required by jwplayer model to
// properly follow the state of the video tag
//
// Assumptions
// 1. All functions are bound to the "this" of the provider
// 2. The provider has an attribute "video" which is the video tag
const VideoListenerMixin = {
canplay() {
this.trigger(MEDIA_BUFFER_FULL);
},
play() {
this.stallTime = -1;
if (!this.video.paused && this.state !== STATE_PLAYING) {
this.setState(STATE_LOADING);
}
},
loadedmetadata() {
const metadata = {
metadataType: 'media',
duration: this.getDuration(),
height: this.video.videoHeight,
width: this.video.videoWidth,
seekRange: this.getSeekRange()
};
const drmUsed = this.drmUsed;
if (drmUsed) {
metadata.drm = drmUsed;
}
this.trigger(MEDIA_META, metadata);
},
timeupdate() {
const currentTime = this.video.currentTime;
const position = this.getCurrentTime();
const duration = this.getDuration();
if (isNaN(duration)) {
return;
}
if (!this.seeking && !this.video.paused &&
(this.state === STATE_STALLED || this.state === STATE_LOADING) &&
this.stallTime !== currentTime) {
this.stallTime = -1;
this.setState(STATE_PLAYING);
this.trigger(PROVIDER_FIRST_FRAME);
}
const timeEventObject = {
position,
duration,
currentTime: currentTime,
seekRange: this.getSeekRange(),
metadata: {
currentTime: currentTime
}
};
if (this.getPtsOffset) {
const ptsOffset = this.getPtsOffset();
if (ptsOffset >= 0) {
timeEventObject.metadata.mpegts = ptsOffset + position;
}
}
const latency = this.getLiveLatency();
if (latency !== null) {
timeEventObject.latency = latency;
}
// only emit time events when playing or seeking
if (this.state === STATE_PLAYING || this.seeking) {
this.trigger(MEDIA_TIME, timeEventObject);
}
},
click(evt) {
this.trigger(CLICK, evt);
},
volumechange() {
const video = this.video;
this.trigger(MEDIA_VOLUME, {
volume: Math.round(video.volume * 100)
});
this.trigger(MEDIA_MUTE, {
mute: video.muted
});
},
seeked() {
if (!this.seeking) {
return;
}
this.seeking = false;
this.trigger(MEDIA_SEEKED);
},
playing() {
// When stalling, STATE_PLAYING is only set on timeupdate
// because Safari and Firefox will fire "playing" before playback recovers from stalling
if (this.stallTime === -1) {
// Here setting STATE_PLAYING ensures a quick recovery from STATE_LOADING after seeking
this.setState(STATE_PLAYING);
}
this.trigger(PROVIDER_FIRST_FRAME);
},
pause() {
// Sometimes the browser will fire "complete" and then a "pause" event
if (this.state === STATE_COMPLETE) {
return;
}
if (this.video.ended) {
return;
}
if (this.video.error) {
return;
}
// If "pause" fires before "complete", we still don't want to propagate it
if (this.video.currentTime === this.video.duration) {
return;
}
this.setState(STATE_PAUSED);
},
progress() {
const dur = this.getDuration();
if (dur <= 0 || dur === Infinity) {
return;
}
const buf = this.video.buffered;
if (!buf || buf.length === 0) {
return;
}
const buffered = between(buf.end(buf.length - 1) / dur, 0, 1);
this.trigger(MEDIA_BUFFER, {
bufferPercent: buffered * 100,
position: this.getCurrentTime(),
duration: dur,
currentTime: this.video.currentTime,
seekRange: this.getSeekRange()
});
},
ratechange() {
this.trigger(MEDIA_RATE_CHANGE, { playbackRate: this.video.playbackRate });
},
ended() {
this.videoHeight = 0;
this.streamBitrate = 0;
if (this.state !== STATE_IDLE && this.state !== STATE_COMPLETE) {
this.trigger(MEDIA_COMPLETE);
}
},
loadeddata () {
// If we're not rendering natively text tracks will be provided from another source - don't duplicate them here
if (this.renderNatively) {
this.setTextTracks(this.video.textTracks);
}
}
};
export default VideoListenerMixin;