|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+<template>
|
|
|
2
|
+ <div :id="containerId" ref="containerRef" class="ezviz-hls-player" />
|
|
|
3
|
+</template>
|
|
|
4
|
+
|
|
|
5
|
+<script>
|
|
|
6
|
+import HlsPlayer from "@ezuikit/player-hls"
|
|
|
7
|
+
|
|
|
8
|
+let containerSeq = 0
|
|
|
9
|
+
|
|
|
10
|
+export default {
|
|
|
11
|
+ name: "EzvizHlsPlayer",
|
|
|
12
|
+ props: {
|
|
|
13
|
+ url: {
|
|
|
14
|
+ type: String,
|
|
|
15
|
+ default: ""
|
|
|
16
|
+ }
|
|
|
17
|
+ },
|
|
|
18
|
+ data() {
|
|
|
19
|
+ containerSeq += 1
|
|
|
20
|
+ return {
|
|
|
21
|
+ containerId: `ezviz-hls-player-${containerSeq}`,
|
|
|
22
|
+ player: null,
|
|
|
23
|
+ resizeObserver: null
|
|
|
24
|
+ }
|
|
|
25
|
+ },
|
|
|
26
|
+ computed: {
|
|
|
27
|
+ decoderStaticPath() {
|
|
|
28
|
+ const base = process.env.BASE_URL || "/"
|
|
|
29
|
+ return `${base}ezuikit-player-hls/`
|
|
|
30
|
+ }
|
|
|
31
|
+ },
|
|
|
32
|
+ watch: {
|
|
|
33
|
+ url(val) {
|
|
|
34
|
+ this.rebuildPlayer(val)
|
|
|
35
|
+ }
|
|
|
36
|
+ },
|
|
|
37
|
+ mounted() {
|
|
|
38
|
+ this.bindResizeObserver()
|
|
|
39
|
+ if (this.url) {
|
|
|
40
|
+ this.$nextTick(() => {
|
|
|
41
|
+ this.initPlayer(this.url)
|
|
|
42
|
+ })
|
|
|
43
|
+ }
|
|
|
44
|
+ },
|
|
|
45
|
+ beforeDestroy() {
|
|
|
46
|
+ this.unbindResizeObserver()
|
|
|
47
|
+ this.destroyPlayer()
|
|
|
48
|
+ },
|
|
|
49
|
+ methods: {
|
|
|
50
|
+ getContainerSize() {
|
|
|
51
|
+ const el = this.$refs.containerRef
|
|
|
52
|
+ if (!el) {
|
|
|
53
|
+ return { width: 0, height: 0 }
|
|
|
54
|
+ }
|
|
|
55
|
+ const parent = el.parentElement
|
|
|
56
|
+ const width = el.clientWidth || (parent && parent.clientWidth) || el.offsetWidth || 0
|
|
|
57
|
+ const height = el.clientHeight || (parent && parent.clientHeight) || el.offsetHeight || 0
|
|
|
58
|
+ return { width, height }
|
|
|
59
|
+ },
|
|
|
60
|
+ bindResizeObserver() {
|
|
|
61
|
+ if (typeof ResizeObserver === "undefined") {
|
|
|
62
|
+ return
|
|
|
63
|
+ }
|
|
|
64
|
+ this.resizeObserver = new ResizeObserver(() => {
|
|
|
65
|
+ this.syncPlayerSize()
|
|
|
66
|
+ })
|
|
|
67
|
+ this.$nextTick(() => {
|
|
|
68
|
+ const el = this.$refs.containerRef
|
|
|
69
|
+ if (el && el.parentElement) {
|
|
|
70
|
+ this.resizeObserver.observe(el.parentElement)
|
|
|
71
|
+ }
|
|
|
72
|
+ })
|
|
|
73
|
+ },
|
|
|
74
|
+ unbindResizeObserver() {
|
|
|
75
|
+ if (this.resizeObserver) {
|
|
|
76
|
+ this.resizeObserver.disconnect()
|
|
|
77
|
+ this.resizeObserver = null
|
|
|
78
|
+ }
|
|
|
79
|
+ },
|
|
|
80
|
+ syncPlayerSize() {
|
|
|
81
|
+ if (!this.player || typeof this.player.resize !== "function") {
|
|
|
82
|
+ return
|
|
|
83
|
+ }
|
|
|
84
|
+ const { width, height } = this.getContainerSize()
|
|
|
85
|
+ if (width > 0 && height > 0) {
|
|
|
86
|
+ this.player.resize(width, height)
|
|
|
87
|
+ }
|
|
|
88
|
+ },
|
|
|
89
|
+ destroyPlayer() {
|
|
|
90
|
+ if (this.player) {
|
|
|
91
|
+ try {
|
|
|
92
|
+ this.player.destroy()
|
|
|
93
|
+ } catch (e) {
|
|
|
94
|
+ // ignore teardown errors
|
|
|
95
|
+ }
|
|
|
96
|
+ this.player = null
|
|
|
97
|
+ }
|
|
|
98
|
+ const el = this.$refs.containerRef
|
|
|
99
|
+ if (el) {
|
|
|
100
|
+ el.innerHTML = ""
|
|
|
101
|
+ }
|
|
|
102
|
+ },
|
|
|
103
|
+ rebuildPlayer(url) {
|
|
|
104
|
+ this.destroyPlayer()
|
|
|
105
|
+ if (url) {
|
|
|
106
|
+ this.$nextTick(() => {
|
|
|
107
|
+ this.initPlayer(url)
|
|
|
108
|
+ })
|
|
|
109
|
+ }
|
|
|
110
|
+ },
|
|
|
111
|
+ initPlayer(url, retryCount = 0) {
|
|
|
112
|
+ const text = url != null ? String(url).trim() : ""
|
|
|
113
|
+ if (!text) {
|
|
|
114
|
+ return
|
|
|
115
|
+ }
|
|
|
116
|
+ const el = document.getElementById(this.containerId)
|
|
|
117
|
+ if (!el) {
|
|
|
118
|
+ return
|
|
|
119
|
+ }
|
|
|
120
|
+ el.innerHTML = ""
|
|
|
121
|
+ const { width, height } = this.getContainerSize()
|
|
|
122
|
+ if (width <= 0 || height <= 0) {
|
|
|
123
|
+ if (retryCount < 8) {
|
|
|
124
|
+ requestAnimationFrame(() => {
|
|
|
125
|
+ this.initPlayer(url, retryCount + 1)
|
|
|
126
|
+ })
|
|
|
127
|
+ }
|
|
|
128
|
+ return
|
|
|
129
|
+ }
|
|
|
130
|
+ this.player = new HlsPlayer({
|
|
|
131
|
+ id: this.containerId,
|
|
|
132
|
+ url: text,
|
|
|
133
|
+ staticPath: '/',
|
|
|
134
|
+ isLive: true,
|
|
|
135
|
+ width,
|
|
|
136
|
+ height
|
|
|
137
|
+ })
|
|
|
138
|
+ this.player.play().catch(() => {
|
|
|
139
|
+ this.$emit("error")
|
|
|
140
|
+ })
|
|
|
141
|
+ }
|
|
|
142
|
+ }
|
|
|
143
|
+}
|
|
|
144
|
+</script>
|
|
|
145
|
+
|
|
|
146
|
+<style scoped>
|
|
|
147
|
+.ezviz-hls-player {
|
|
|
148
|
+ width: 100%;
|
|
|
149
|
+ height: 100%;
|
|
|
150
|
+ overflow: hidden;
|
|
|
151
|
+ background: #000;
|
|
|
152
|
+}
|
|
|
153
|
+
|
|
|
154
|
+.ezviz-hls-player ::v-deep .ezui-player-container,
|
|
|
155
|
+.ezviz-hls-player ::v-deep .ezui-hls-container {
|
|
|
156
|
+ width: 100% !important;
|
|
|
157
|
+ height: 100% !important;
|
|
|
158
|
+}
|
|
|
159
|
+
|
|
|
160
|
+.ezviz-hls-player ::v-deep canvas,
|
|
|
161
|
+.ezviz-hls-player ::v-deep video {
|
|
|
162
|
+ width: 100% !important;
|
|
|
163
|
+ height: 100% !important;
|
|
|
164
|
+ object-fit: contain;
|
|
|
165
|
+}
|
|
|
166
|
+</style>
|