Skip to content

Commit a8af9c0

Browse files
add videolayer ut review by luox
1 parent 6895c3f commit a8af9c0

File tree

6 files changed

+146
-9
lines changed

6 files changed

+146
-9
lines changed

src/common/overlay/video/VideoLayerRenderer.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'videojs-flvjs-es6';
1313
* @version 11.2.0
1414
* @param {Object} options - 参数。
1515
* @param {string} [options.id] - 图层 ID。
16-
* @param {Array} [options.extent] - 视频范围
16+
* @param {string} [options.url] - 视频地址
1717
* @usage
1818
*/
1919

@@ -33,7 +33,10 @@ export class VideoLayerRenderer {
3333
video.setAttribute('crossorigin', 'anonymous');
3434
document.body.appendChild(video);
3535
}
36-
36+
/**
37+
* @function VideoLayerRenderer.prototype.createVideo
38+
* @description 创建videojs 实例。
39+
*/
3740
createVideo() {
3841
this._createVideoElement();
3942
let options = this._getVideoOptions();
@@ -85,7 +88,11 @@ export class VideoLayerRenderer {
8588
return options;
8689
}
8790

88-
getVideoDom() {
91+
/**
92+
* @function VideoLayerRenderer.prototype.getVideoDomId
93+
* @description 获取创建的 video 元素的 DOM id。
94+
*/
95+
getVideoDomId() {
8996
if (!this.url) {
9097
return;
9198
}

src/mapboxgl/overlay/VideoLayer.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { bbox, polygon } from '@turf/turf';
1111
* @category Visualization Video
1212
* @modulecategory Overlay
1313
* @param {Object} options - 构造参数。
14-
* @param {string} options.url - 视频 或 流链接。支持 flv, m3u8 流格式
14+
* @param {string} options.url - 视频 或 流链接。支持 flv, m3u8, map4 格式
1515
* @param {Array} options.extent - 视频范围。
16-
* @param {Object} [options.opencv] - opencv.js 实例。
16+
* @param {Object} [options.opencv] - opencv.js 实例, 未传入时将去 window.cv 获取
1717
* @param {string} [options.id] - 视频图层 ID。默认使用 CommonUtil.createUniqueID("VideoLayer_") 创建专题图层 ID。
1818
* @extends {mapboxgl.Evented}
1919
* @usage
@@ -28,7 +28,7 @@ export class VideoLayer extends mapboxgl.Evented {
2828
this.extent = this.options.extent;
2929
this.cv = this.options.opencv || window.cv;
3030
if (!this.cv) {
31-
throw new Error('opencv.js is not existed!');
31+
throw new Error('opencv.js instance is not existed!');
3232
}
3333
this.id = _options.id ? _options.id : CommonUtil.createUniqueID("VideoLayer_");
3434
this.layerId = this.id + '_outer';
@@ -45,7 +45,7 @@ export class VideoLayer extends mapboxgl.Evented {
4545
this.map = map;
4646
this.renderer = new VideoLayerRenderer({ url: this.url, id: this.layerId });
4747
this.video = this.renderer.createVideo();
48-
this.videoDomId = this.renderer.getVideoDom();
48+
this.videoDomId = this.renderer.getVideoDomId();
4949
this.video.one('firstplay', () => {
5050
this.video.play();
5151
});
@@ -65,7 +65,7 @@ export class VideoLayer extends mapboxgl.Evented {
6565

6666
render() {}
6767

68-
getPixelBbox(map) {
68+
_getPixelBbox(map) {
6969
let res = [];
7070
let minX = 0;
7171
let minY = 0;
@@ -92,7 +92,7 @@ export class VideoLayer extends mapboxgl.Evented {
9292

9393
_addVideoLayer(map) {
9494
let url = this.videoDomId || this.url;
95-
this.pixelBBox = this.getPixelBbox(map);
95+
this.pixelBBox = this._getPixelBbox(map);
9696
const result = bbox(polygon([
9797
this.extent.concat(this.extent[0])
9898
]));

src/mapboxgl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"author": "SuperMap",
1616
"license": "Apache-2.0",
1717
"dependencies": {
18+
"@turf/turf": "6.5.0",
1819
"mapv": "2.0.62",
1920
"mapbox-gl": "1.13.2",
2021
"three": "0.150.1",
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { VideoLayer } from '../../../src/mapboxgl/overlay/VideoLayer';
2+
import mapboxgl from 'mapbox-gl';
3+
import mbglmap from '../../tool/mock_mapboxgl_map';
4+
var url = GlobeParameter.ChinaURL + '/zxyTileImage.png?z={z}&x={x}&y={y}';
5+
describe('mapboxgl_VideoLayer', () => {
6+
var originalTimeout;
7+
var testDiv, map;
8+
beforeAll((done) => {
9+
testDiv = window.document.createElement('div');
10+
testDiv.setAttribute('id', 'map');
11+
testDiv.style.styleFloat = 'left';
12+
testDiv.style.marginLeft = '8px';
13+
testDiv.style.marginTop = '50px';
14+
testDiv.style.width = '500px';
15+
testDiv.style.height = '500px';
16+
window.document.body.appendChild(testDiv);
17+
spyOn(mapboxgl, 'Map').and.callFake(mbglmap);
18+
map = new mapboxgl.Map({
19+
container: 'map',
20+
style: {
21+
version: 8,
22+
sources: {
23+
'raster-tiles': {
24+
type: 'raster',
25+
tiles: [url],
26+
tileSize: 256
27+
}
28+
},
29+
layers: [
30+
{
31+
id: 'simple-tiles',
32+
type: 'raster',
33+
source: 'raster-tiles',
34+
minzoom: 0,
35+
maxzoom: 22
36+
}
37+
]
38+
},
39+
center: [0, 0],
40+
zoom: 3
41+
});
42+
map.on('load', function () {
43+
done();
44+
});
45+
});
46+
beforeEach(() => {
47+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
48+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
49+
});
50+
51+
afterEach(() => {
52+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
53+
});
54+
55+
afterAll(() => {
56+
document.body.removeChild(testDiv);
57+
map = null;
58+
});
59+
60+
it('init videoLayer', (done) => {
61+
var url = 'http://172.16.15.84:8000/live/test.flv';
62+
var cv = {
63+
CV_32FC2: 'CV_32FC2',
64+
matFromImageData: function () {
65+
return {
66+
delete: function () {
67+
68+
}
69+
}
70+
},
71+
Size: function () {
72+
return {
73+
width: 770,
74+
height: 690
75+
}
76+
},
77+
matFromArray: function () {
78+
79+
},
80+
Mat: function () {
81+
return {
82+
delete: function () { },
83+
cols: 2,
84+
rows: 2,
85+
data: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
86+
}
87+
},
88+
findHomography: function () {
89+
90+
},
91+
warpPerspective: function () {
92+
93+
}
94+
}
95+
spyOn(cv, 'Size');
96+
spyOn(cv, 'findHomography');
97+
spyOn(cv, 'warpPerspective');
98+
var videoLayer = new VideoLayer({
99+
url: url,
100+
opencv: cv,
101+
extent: [
102+
[116.14394400766855, 28.249134537249257],
103+
[116.143464581289, 28.252977295834056],
104+
[116.14734767029731, 28.251762901914655],
105+
[116.14737169684759, 28.25095489453961]
106+
]
107+
});
108+
videoLayer.onAdd(map);
109+
setTimeout(() => {
110+
expect(cv.Size).toHaveBeenCalled();
111+
expect(cv.findHomography).toHaveBeenCalled();
112+
expect(cv.warpPerspective).toHaveBeenCalled();
113+
expect(videoLayer.url).toBe(url);
114+
expect(videoLayer.videoDomId).not.toBeNull();
115+
expect(videoLayer.video).not.toBeNull();
116+
expect(videoLayer.pixelBBox).not.toBeNull();
117+
done();
118+
}, 3000);
119+
});
120+
});

test/test-main-mapboxgl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import './mapboxgl/services/GeoRelationAnalysisSpec.js';
3838
import './mapboxgl/services/GeometryBatchAnalysisSpec.js';
3939
import './mapboxgl/services/GeoprocessingServiceSpec.js';
4040
import './mapboxgl/overlay/FGBLayerSpec.js';
41+
import './mapboxgl/overlay/VideoLayerSpec.js';
4142
import './mapboxgl/services/GetFeaturesByBoundsSpec.js';
4243
import './mapboxgl/services/GetFeaturesByBufferSpec.js';
4344
import './mapboxgl/services/GetFeaturesByGeometrySpec.js';

test/tool/mock_mapboxgl_map.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ const Map = function (options) {
128128

129129
this.getSource = function (name) {
130130
this._sources[name];
131+
if (this._sources[name].type === 'video') {
132+
return {
133+
play: function() {}
134+
}
135+
}
131136
};
132137

133138
this.loaded = function () {
@@ -141,6 +146,9 @@ const Map = function (options) {
141146
this.overlayLayersManager = {};
142147

143148
this.addSource = function (name, source) {
149+
if (source && source.drawImageCallback) {
150+
source.drawImageCallback();
151+
}
144152
this._sources[name] = source;
145153
};
146154
this.removeSource = function (name) {

0 commit comments

Comments
 (0)