Skip to content

Commit 10e3696

Browse files
mapboxgl 新增 videoLayer
1 parent 1accf41 commit 10e3696

File tree

7 files changed

+210
-1
lines changed

7 files changed

+210
-1
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
"three": "0.150.1",
165165
"urijs": "^1.19.11",
166166
"util": "^0.12.4",
167+
"flv.js": "^1.6.2",
168+
"video.js": "^7.10.2",
169+
"videojs-flvjs-es6": "^1.0.1",
167170
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz"
168171
},
169172
"pre-commit": [
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright© 2000 - 2023 SuperMap Software Co.Ltd. All rights reserved.
2+
* This program are made available under the terms of the Apache License, Version 2.0
3+
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4+
5+
import videojs from 'video.js';
6+
import 'flv.js';
7+
import 'videojs-flvjs-es6';
8+
9+
/**
10+
* @private
11+
* @class VideoLayerRenderer
12+
* @classdesc Video。
13+
* @version 11.2.0
14+
* @param {Object} options - 参数。
15+
* @param {string} [options.id] - 图层 ID。
16+
* @param {Array} [options.extent] - 视频范围。
17+
* @usage
18+
*/
19+
20+
export class VideoLayerRenderer {
21+
constructor(options = {}) {
22+
this.options = options;
23+
this.url = options.url;
24+
this.layerId = options.id;
25+
}
26+
27+
_createVideoElement() {
28+
let video = document.createElement('video');
29+
video.id = this.layerId;
30+
video.className = 'video-js';
31+
video.style.position = 'fixed';
32+
video.style.left = '-200%';
33+
video.setAttribute('crossorigin', 'anonymous');
34+
document.body.appendChild(video);
35+
}
36+
37+
createVideo() {
38+
this._createVideoElement();
39+
let options = this._getVideoOptions();
40+
this.video = videojs(this.layerId, options, () => { });
41+
return this.video;
42+
}
43+
44+
_getVideoOptions() {
45+
if (!this.url) {
46+
return;
47+
}
48+
let options = {
49+
autoplay: true,
50+
muted: true
51+
};
52+
options.loop = this.loop !== false;
53+
if (this.url.includes('mp4')) {
54+
options['sources'] = [
55+
{
56+
src: this.url,
57+
type: 'video/mp4'
58+
}
59+
];
60+
} else if (this.url.includes('flv')) {
61+
options = {
62+
autoplay: this.autoplay,
63+
techOrder: ['html5', 'flvjs'],
64+
flvjs: {
65+
mediaDataSource: {
66+
isLive: true,
67+
cors: true,
68+
withCredentials: false
69+
}
70+
},
71+
sources: [
72+
{
73+
src: this.url,
74+
type: 'video/x-flv'
75+
}
76+
]
77+
};
78+
} else if (this.url.includes('m3u8')) {
79+
options['sources'] = [
80+
{
81+
src: this.url
82+
}
83+
];
84+
}
85+
return options;
86+
}
87+
88+
getVideoDom() {
89+
if (!this.url) {
90+
return;
91+
}
92+
let videoDomId;
93+
if (this.url.includes('flv')) {
94+
let videoWrap = document.querySelector(`#${this.layerId}`);
95+
videoDomId = `${this.layerId}_flvjs_api`;
96+
videoWrap.style.position = 'fixed';
97+
videoWrap.style.left = '-200%';
98+
} else {
99+
videoDomId = this.video.el_.firstChild.id;
100+
}
101+
return videoDomId;
102+
}
103+
}

src/common/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"node-forge": "^1.3.1",
2929
"rbush": "^2.0.2",
3030
"urijs": "^1.19.11",
31+
"flv.js": "^1.6.2",
32+
"video.js": "^7.10.2",
33+
"videojs-flvjs-es6": "^1.0.1",
3134
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz"
3235
}
3336
}

src/mapboxgl/namespace.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
LabelThemeLayer,
99
MapvLayer,
1010
FGBLayer,
11+
VideoLayer,
1112
RangeTheme3DLayer,
1213
RangeThemeLayer,
1314
RankSymbolThemeLayer,
@@ -95,6 +96,7 @@ mapboxgl.supermap.HeatMapLayer = HeatMapLayer;
9596
mapboxgl.supermap.LabelThemeLayer = LabelThemeLayer;
9697
mapboxgl.supermap.MapvLayer = MapvLayer;
9798
mapboxgl.supermap.FGBLayer = FGBLayer;
99+
mapboxgl.supermap.VideoLayer = VideoLayer;
98100
mapboxgl.supermap.RangeTheme3DLayer = RangeTheme3DLayer;
99101
mapboxgl.supermap.RangeThemeLayer = RangeThemeLayer;
100102
mapboxgl.supermap.RankSymbolThemeLayer = RankSymbolThemeLayer;

src/mapboxgl/overlay/VideoLayer.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Copyright© 2000 - 2023 SuperMap Software Co.Ltd. All rights reserved.
2+
* This program are made available under the terms of the Apache License, Version 2.0
3+
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4+
import mapboxgl from 'mapbox-gl';
5+
import { Util as CommonUtil } from '@supermap/iclient-common/commontypes/Util';
6+
import { VideoLayerRenderer } from '@supermap/iclient-common/overlay/video/VideoLayerRenderer';
7+
8+
/**
9+
* @class VideoLayer
10+
* @category Visualization Video
11+
* @modulecategory Overlay
12+
* @param {string} name - 图层名称。
13+
* @param {Object} options - 构造参数。
14+
* @param {string} [options.id] - 专题图层 ID。默认使用 CommonUtil.createUniqueID("VideoLayer_") 创建专题图层 ID。
15+
* @param {string} [options.url] - 视频 或 流链接。支持 flv, m3u8 流格式。
16+
* @param {string} [options.extent] - 视频范围。
17+
* @extends {mapboxgl.Evented}
18+
* @usage
19+
*/
20+
export class VideoLayer extends mapboxgl.Evented {
21+
22+
constructor(name, options) {
23+
super();
24+
var _options = options ? options : {};
25+
this.options = _options;
26+
this.name = name;
27+
this.url = this.options.url;
28+
this.extent = this.options.extent;
29+
this.id = _options.id ? _options.id : CommonUtil.createUniqueID("VideoLayer_");
30+
this.layerId = this.id + 'outer';
31+
this.type = 'custom';
32+
this.renderingMode = '3d';
33+
this.overlay = true;
34+
}
35+
36+
/**
37+
* @function VideoLayer.prototype.onAdd
38+
* @description 添加该图层。
39+
*/
40+
onAdd(map) {
41+
this.map = map;
42+
this.renderer = new VideoLayerRenderer({ url: this.url, id: this.layerId });
43+
this.video = this.renderer.createVideo();
44+
this.videoDomId = this.renderer.getVideoDom();
45+
this.video.one('firstplay', () => {
46+
this.video.play();
47+
});
48+
this.video.one('ready', () => {
49+
this._addVideoLayer(this.map);
50+
});
51+
this.video.one('canplay', () => {
52+
setTimeout(() => {
53+
map.getSource(this.layerId).play();
54+
}, 1000);
55+
});
56+
}
57+
58+
render() { }
59+
60+
_addVideoLayer(map) {
61+
let url = this.videoDomId || this.url;
62+
map.addSource(this.layerId, {
63+
type: 'video',
64+
urls: [url],
65+
coordinates: this.extent
66+
});
67+
68+
map.addLayer(
69+
{
70+
id: this.layerId,
71+
type: 'raster',
72+
source: this.layerId
73+
}
74+
);
75+
}
76+
77+
/**
78+
* @function VideoLayer.prototype.moveLayer
79+
* @description 移动图层。
80+
*/
81+
moveLayer(beforeId) {
82+
this.map.moveLayer(this.layerId, beforeId);
83+
}
84+
85+
/**
86+
* @function VideoLayer.prototype.setVisibility
87+
* @description 设置图层可见性。
88+
* @param {boolean} [visibility] - 是否显示图层。
89+
*/
90+
setVisibility(visibility) {
91+
const visible = visibility ? 'visible' : 'none';
92+
this.map.setLayoutProperty(this.layerId, 'visibility', visible);
93+
}
94+
}

src/mapboxgl/overlay/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export { HeatMapLayer } from './HeatMapLayer';
1616
export { DeckglLayer } from './DeckglLayer';
1717
export { GraticuleLayer } from './GraticuleLayer';
1818
export { FGBLayer } from './FGBLayer';
19+
export { VideoLayer } from './VideoLayer';
1920
export { GraphMap } from './GraphMap';
2021

2122
export * from './graphic';

src/mapboxgl/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"fast-xml-parser": "^4.2.7",
2424
"flatgeobuf": "3.23.1",
2525
"rbush": "^2.0.2",
26-
"proj4": "2.9.0"
26+
"proj4": "2.9.0",
27+
"flv.js": "^1.6.2",
28+
"video.js": "^7.10.2",
29+
"videojs-flvjs-es6": "^1.0.1"
2730
}
2831
}

0 commit comments

Comments
 (0)