@oim-media/player-ovm 0.0.0-stage → 1.27.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +55 -0
- package/NOTICE +6 -0
- package/README.md +74 -3
- package/package.json +31 -3
- package/src/index.js +11 -0
- package/src/ovm-player.js +444 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
+
|
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
+
|
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
+
|
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
+
|
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
+
|
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
+
|
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
+
|
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
+
|
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
+
|
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
+
|
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
+
|
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
+
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
|
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @oim-media/player-ovm
|
|
2
|
+
|
|
3
|
+
`<ovm-player>`: a web component for `.ovm` interactive video, the video
|
|
4
|
+
counterpart of [`@oim-media/player-oim`](../player-oim/README.md). It plays the
|
|
5
|
+
video with the browser's own `<video>` element and draws text layers on top of it,
|
|
6
|
+
positioned by the tracking data stored in the file.
|
|
7
|
+
|
|
8
|
+
**Current state: reserved / pending specification.** The `.ovm` interactive video format is reserved for a future specification. This package currently ships a minimal vanilla-JS prototype for simple text overlays, with no signature verification, no events and no playback API of its own. For the production specification and verified reference player, see [`@oim-media/player-oim`](../player-oim/README.md) and [`@oim-media/spec`](../spec/README.md).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @oim-media/player-ovm
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The package is published to GitHub Packages. See the
|
|
17
|
+
[install section](../../README.md#install) for the `.npmrc` lines.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import "@oim-media/player-ovm"; // registers <ovm-player>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<ovm-player src="/video/intro.ovm" lang="en" autoplay></ovm-player>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The element sizes itself to a 16:9 box with a black background.
|
|
30
|
+
|
|
31
|
+
## Attributes and properties
|
|
32
|
+
|
|
33
|
+
| Name | Kind | Behaviour |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| `src` | attribute and property | URL of the `.ovm` file. Setting it loads the file. Read back as a string, empty if unset. |
|
|
36
|
+
| `lang` | attribute and property | Language for the text. Default `es`. Changing it redraws the text. Falls back to `en`, then `es`, then empty. |
|
|
37
|
+
| `autoplay` | attribute | If present on connect, the video starts automatically, subject to the browser's autoplay rules. |
|
|
38
|
+
|
|
39
|
+
## How a file is read
|
|
40
|
+
|
|
41
|
+
The metadata is looked for in three places, in order:
|
|
42
|
+
|
|
43
|
+
1. The video header, where the `OVM_Metadata` block is written.
|
|
44
|
+
2. The last 64 KiB of the file, fetched with a range request.
|
|
45
|
+
3. A sidecar file at `<src>.json`, fetched with a plain request.
|
|
46
|
+
|
|
47
|
+
If none of them yields metadata, the element shows "No OVM metadata found".
|
|
48
|
+
|
|
49
|
+
The metadata is checked for shape only. A file that fails the check shows
|
|
50
|
+
"Corrupted or Tampered OVM file" and is not played.
|
|
51
|
+
|
|
52
|
+
## What is drawn
|
|
53
|
+
|
|
54
|
+
For each slot in the metadata that has text in the current language, the element
|
|
55
|
+
creates a `div` over the video. Its position comes from `tracking_data` keyframes
|
|
56
|
+
at the current time, or from the static `position` when there is no tracking.
|
|
57
|
+
Colour, font size and font family come from the slot's `style`. Only
|
|
58
|
+
`fillColor`, `fontSize` and `fontFamily` are read.
|
|
59
|
+
|
|
60
|
+
Slot text is inserted as text, not as HTML.
|
|
61
|
+
|
|
62
|
+
## Not included yet
|
|
63
|
+
|
|
64
|
+
- **No signature verification.** The metadata is trusted as read. Do not use this
|
|
65
|
+
player for files whose authorship or integrity matters until verification is added.
|
|
66
|
+
The `oim-player` verification path is the reference.
|
|
67
|
+
- **No events.** Nothing is dispatched.
|
|
68
|
+
- **No playback API.** There are no `play()`, `pause()` or `seek()` methods on the
|
|
69
|
+
element. Control the video through the browser's native `<video>` element.
|
|
70
|
+
- **Only text layers are drawn.** Image, vector and other layers are not rendered.
|
|
71
|
+
|
|
72
|
+
## Licence
|
|
73
|
+
|
|
74
|
+
Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oim-media/player-ovm",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.27.8",
|
|
4
|
+
"description": "Web player for OVM interactive video: a framework-free web component.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"oim",
|
|
7
|
+
"ovm",
|
|
8
|
+
"interactive-video",
|
|
9
|
+
"web-component",
|
|
10
|
+
"player"
|
|
11
|
+
],
|
|
12
|
+
"author": "Xue Studios LLC",
|
|
13
|
+
"homepage": "https://oim.media",
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./src/index.js",
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"NOTICE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"provenance": true
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/oim-media/oim.git",
|
|
29
|
+
"directory": "packages/player-ovm"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "echo \"No build needed for vanilla OVM player\""
|
|
33
|
+
}
|
|
6
34
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2026 Xue Studios LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* <ovm-player> — Experimental / Reserved Web Component for OVM video playback
|
|
9
|
+
* (Pending canonical specification)
|
|
10
|
+
*
|
|
11
|
+
* Framework-agnostic placeholder reader. Uses HTTP Range requests to extract
|
|
12
|
+
* metadata from trailing bytes, delegates video buffering to native <video>.
|
|
13
|
+
*
|
|
14
|
+
* Attributes:
|
|
15
|
+
* src — URL of the .ovm file
|
|
16
|
+
* lang — language code (es, en, zh, etc.)
|
|
17
|
+
* autoplay — if present, video plays automatically
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* <ovm-player src="video.ovm" lang="es"></ovm-player>
|
|
21
|
+
*/
|
|
22
|
+
(function () {
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
var METADATA_TAIL_BYTES = 65536;
|
|
26
|
+
var MAX_DECOMPRESSED_BYTES = 10 * 1024 * 1024;
|
|
27
|
+
var STYLES = [
|
|
28
|
+
':host { display:block; position:relative; width:100%; aspect-ratio:16/9;',
|
|
29
|
+
'background:#000; border-radius:8px; overflow:hidden; contain:layout style paint; }',
|
|
30
|
+
'video { display:block; width:100%; height:100%; object-fit:contain; background:#000; }',
|
|
31
|
+
'.overlay { position:absolute; inset:0; pointer-events:none; z-index:2; }',
|
|
32
|
+
'.slot { position:absolute; transform:translate(-50%,-50%); white-space:nowrap;',
|
|
33
|
+
'font-family:var(--ovm-font,"Inter",system-ui,sans-serif);',
|
|
34
|
+
'text-shadow:0 1px 4px rgba(0,0,0,0.8),0 0 8px rgba(0,0,0,0.5); }',
|
|
35
|
+
'.loader { position:absolute; inset:0; display:flex; align-items:center; justify-content:center;',
|
|
36
|
+
'color:rgba(255,255,255,0.4); font-family:var(--ovm-mono,"JetBrains Mono",monospace);',
|
|
37
|
+
'font-size:12px; z-index:3; }',
|
|
38
|
+
'.loader.error { color:#ef4444; }'
|
|
39
|
+
].join('\n');
|
|
40
|
+
|
|
41
|
+
async function decompressBytes(bytes, format) {
|
|
42
|
+
var ds = new DecompressionStream(format);
|
|
43
|
+
var writer = ds.writable.getWriter();
|
|
44
|
+
writer.write(bytes).catch(function () {});
|
|
45
|
+
writer.close().catch(function () {});
|
|
46
|
+
|
|
47
|
+
var reader = ds.readable.getReader();
|
|
48
|
+
var chunks = [];
|
|
49
|
+
var total = 0;
|
|
50
|
+
|
|
51
|
+
while (true) {
|
|
52
|
+
var step = await reader.read();
|
|
53
|
+
if (step.done) break;
|
|
54
|
+
total += step.value.byteLength;
|
|
55
|
+
if (total > MAX_DECOMPRESSED_BYTES) {
|
|
56
|
+
await reader.cancel('OVM metadata exceeds maximum decompressed size');
|
|
57
|
+
throw new Error('OVM metadata exceeds maximum decompressed size (10 MB)');
|
|
58
|
+
}
|
|
59
|
+
chunks.push(step.value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var out = new Uint8Array(total);
|
|
63
|
+
var offset = 0;
|
|
64
|
+
for (var i = 0; i < chunks.length; i++) {
|
|
65
|
+
out.set(chunks[i], offset);
|
|
66
|
+
offset += chunks[i].byteLength;
|
|
67
|
+
}
|
|
68
|
+
return new TextDecoder().decode(out);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function decodeAndDecompressJSON(base64Str) {
|
|
72
|
+
var cleanB64 = base64Str.replace(/\s+/g, '');
|
|
73
|
+
var binary = atob(cleanB64);
|
|
74
|
+
var bytes = new Uint8Array(binary.length);
|
|
75
|
+
for (var i = 0; i < binary.length; i++) {
|
|
76
|
+
bytes[i] = binary.charCodeAt(i);
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
var decompressed = await decompressBytes(bytes, 'deflate');
|
|
80
|
+
return JSON.parse(decompressed);
|
|
81
|
+
} catch (e1) {
|
|
82
|
+
try {
|
|
83
|
+
var decompressedRaw = await decompressBytes(bytes, 'deflate-raw');
|
|
84
|
+
return JSON.parse(decompressedRaw);
|
|
85
|
+
} catch (e2) {
|
|
86
|
+
console.error('[OvmPlayer] Decompression failed for both deflate and deflate-raw:', e1, e2);
|
|
87
|
+
throw e2;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function extractOvmMetadataFromVideo(src) {
|
|
93
|
+
try {
|
|
94
|
+
var resp = await fetch(src, {
|
|
95
|
+
headers: { Range: 'bytes=0-262143' }
|
|
96
|
+
});
|
|
97
|
+
if (!resp.ok && resp.status !== 206) {
|
|
98
|
+
throw new Error('HTTP ' + resp.status);
|
|
99
|
+
}
|
|
100
|
+
var buffer = await resp.arrayBuffer();
|
|
101
|
+
var bytes = new Uint8Array(buffer);
|
|
102
|
+
var text = new TextDecoder().decode(bytes);
|
|
103
|
+
|
|
104
|
+
var idx = text.indexOf('OVM_Metadata');
|
|
105
|
+
if (idx === -1) {
|
|
106
|
+
idx = text.indexOf('metadata.ovmdata');
|
|
107
|
+
}
|
|
108
|
+
if (idx === -1) {
|
|
109
|
+
throw new Error('OVM_Metadata signature not found in video header');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
var slice = text.substring(idx);
|
|
113
|
+
var match = slice.match(/[A-Za-z0-9+/=]{100,}/);
|
|
114
|
+
if (!match) {
|
|
115
|
+
throw new Error('Base64 metadata block not found after signature');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var b64 = match[0];
|
|
119
|
+
for (var i = b64.length; i >= 10; i--) {
|
|
120
|
+
var cand = b64.substring(0, i);
|
|
121
|
+
if (cand.length % 4 !== 0) {
|
|
122
|
+
cand += '='.repeat(4 - (cand.length % 4));
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
var parsed = await decodeAndDecompressJSON(cand);
|
|
126
|
+
if (parsed && (parsed.slots || parsed.elements || parsed.layers)) {
|
|
127
|
+
return parsed;
|
|
128
|
+
}
|
|
129
|
+
} catch (_) {
|
|
130
|
+
// Keep trying smaller prefix to discard trailing garbage bytes
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
throw new Error('Failed to decompress base64 metadata block');
|
|
134
|
+
} catch (e) {
|
|
135
|
+
console.warn('[OvmPlayer] Error extracting embedded metadata:', e);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
var OvmPlayer = class extends HTMLElement {
|
|
141
|
+
constructor() {
|
|
142
|
+
super();
|
|
143
|
+
var self = this;
|
|
144
|
+
self._metadata = null;
|
|
145
|
+
self._currentTime = 0;
|
|
146
|
+
self._shadow = self.attachShadow({ mode: 'open' });
|
|
147
|
+
var style = document.createElement('style');
|
|
148
|
+
style.textContent = STYLES;
|
|
149
|
+
self._shadow.appendChild(style);
|
|
150
|
+
self._loader = document.createElement('div');
|
|
151
|
+
self._loader.className = 'loader';
|
|
152
|
+
self._loader.textContent = 'Extracting metadata\u2026';
|
|
153
|
+
self._shadow.appendChild(self._loader);
|
|
154
|
+
self._video = document.createElement('video');
|
|
155
|
+
self._video.preload = 'metadata';
|
|
156
|
+
self._video.playsInline = true;
|
|
157
|
+
self._video.style.display = 'none';
|
|
158
|
+
self._shadow.appendChild(self._video);
|
|
159
|
+
self._overlay = document.createElement('div');
|
|
160
|
+
self._overlay.className = 'overlay';
|
|
161
|
+
self._shadow.appendChild(self._overlay);
|
|
162
|
+
self._onTimeUpdate = function () {
|
|
163
|
+
self._currentTime = self._video.currentTime;
|
|
164
|
+
self._renderSlots();
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
OvmPlayer.observedAttributes = ['src', 'lang'];
|
|
170
|
+
|
|
171
|
+
OvmPlayer.prototype.connectedCallback = function () {
|
|
172
|
+
this._video.addEventListener('timeupdate', this._onTimeUpdate);
|
|
173
|
+
if (this.hasAttribute('autoplay')) {
|
|
174
|
+
this._video.autoplay = true;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
OvmPlayer.prototype.disconnectedCallback = function () {
|
|
179
|
+
this._video.removeEventListener('timeupdate', this._onTimeUpdate);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
OvmPlayer.prototype.attributeChangedCallback = function (name, oldVal, newVal) {
|
|
183
|
+
if (oldVal === newVal) return;
|
|
184
|
+
if (name === 'src' && newVal) {
|
|
185
|
+
this._load(newVal);
|
|
186
|
+
} else if (name === 'lang') {
|
|
187
|
+
this._renderSlots();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
Object.defineProperties(OvmPlayer.prototype, {
|
|
192
|
+
src: {
|
|
193
|
+
get: function () { return this.getAttribute('src') || ''; },
|
|
194
|
+
set: function (v) { this.setAttribute('src', v); }
|
|
195
|
+
},
|
|
196
|
+
lang: {
|
|
197
|
+
get: function () { return this.getAttribute('lang') || 'es'; },
|
|
198
|
+
set: function (v) { this.setAttribute('lang', v); }
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
// ===== SCHEMA VALIDATOR (inline Zod-equivalent) =====
|
|
204
|
+
function validateOvmMetadata(json) {
|
|
205
|
+
var errors = [];
|
|
206
|
+
function fail(msg) { errors.push(msg); }
|
|
207
|
+
function okStr(v, max) { return typeof v === "string" && v.length <= (max || 2000); }
|
|
208
|
+
function okNum(v) { return typeof v === "number" && isFinite(v); }
|
|
209
|
+
|
|
210
|
+
if (typeof json !== "object" || json === null) { fail("metadata must be object"); return errors; }
|
|
211
|
+
|
|
212
|
+
var slots = json.slots || json.elements || [];
|
|
213
|
+
if (!Array.isArray(slots)) { fail("slots must be array"); return errors; }
|
|
214
|
+
if (slots.length > 50) { fail("max 50 slots"); }
|
|
215
|
+
|
|
216
|
+
for (var i = 0; i < slots.length; i++) {
|
|
217
|
+
var s = slots[i];
|
|
218
|
+
if (typeof s !== "object" || s === null) { fail("slot[" + i + "] must be object"); continue; }
|
|
219
|
+
if (!s.id || typeof s.id !== "string" || s.id.length > 128) fail("slot[" + i + "].id invalid");
|
|
220
|
+
|
|
221
|
+
var content = s.content;
|
|
222
|
+
if (typeof content !== "object" || content === null) { fail("slot[" + i + "].content must be object"); }
|
|
223
|
+
else {
|
|
224
|
+
var keys = Object.keys(content);
|
|
225
|
+
for (var k = 0; k < keys.length; k++) {
|
|
226
|
+
if (!okStr(content[keys[k]])) fail("slot[" + i + "].content." + keys[k] + " invalid");
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// `fillColor` and not `color`: contract 1.5.0 removed the aliases of
|
|
231
|
+
// `style`, and the compiler now only writes the canonical ones.
|
|
232
|
+
if (s.style && s.style.fillColor && !/^#[0-9a-fA-F]{3,8}$/.test(s.style.fillColor)) {
|
|
233
|
+
fail("slot[" + i + "].style.fillColor invalid");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (s.position && (!Array.isArray(s.position) || s.position.length < 2 || !okNum(s.position[0]) || !okNum(s.position[1]))) {
|
|
237
|
+
fail("slot[" + i + "].position invalid");
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return errors;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
OvmPlayer.prototype._load = async function (src) {
|
|
245
|
+
var self = this;
|
|
246
|
+
self._showLoader('Extracting metadata\u2026', false);
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
// 1. Try extracting metadata from the video header
|
|
250
|
+
var metadata = await extractOvmMetadataFromVideo(src);
|
|
251
|
+
|
|
252
|
+
// 2. Try extracting from trailing bytes (tail range requests)
|
|
253
|
+
if (!metadata) {
|
|
254
|
+
try {
|
|
255
|
+
metadata = await self._fetchMetadataTail(src);
|
|
256
|
+
} catch (_) {}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// 3. Try fallback sidecar JSON file
|
|
260
|
+
if (!metadata) {
|
|
261
|
+
try {
|
|
262
|
+
var res = await fetch(src + '.json');
|
|
263
|
+
if (res.ok) {
|
|
264
|
+
metadata = await res.json();
|
|
265
|
+
}
|
|
266
|
+
} catch (_) {}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (!metadata) {
|
|
270
|
+
throw new Error('No OVM metadata found');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
var errs = validateOvmMetadata(metadata);
|
|
274
|
+
if (errs.length > 0) {
|
|
275
|
+
console.error('[ovm-player] Invalid metadata:', errs);
|
|
276
|
+
throw new Error('Corrupted or Tampered OVM file: ' + errs[0]);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
self._metadata = metadata;
|
|
280
|
+
self._video.src = src;
|
|
281
|
+
self._video.style.display = 'block';
|
|
282
|
+
self._hideLoader();
|
|
283
|
+
self._renderSlots();
|
|
284
|
+
} catch (err) {
|
|
285
|
+
console.error('[ovm-player]', err);
|
|
286
|
+
self._showLoader(err.message, true);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
OvmPlayer.prototype._fetchMetadataTail = function (src) {
|
|
291
|
+
return fetch(src, { headers: { Range: 'bytes=-' + METADATA_TAIL_BYTES } })
|
|
292
|
+
.then(function (res) {
|
|
293
|
+
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
294
|
+
return res.arrayBuffer();
|
|
295
|
+
})
|
|
296
|
+
.then(function (buffer) {
|
|
297
|
+
return new Uint8Array(buffer);
|
|
298
|
+
})
|
|
299
|
+
.then(this._extractJSONFromTail);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
OvmPlayer.prototype._extractJSONFromTail = function (bytes) {
|
|
303
|
+
var text = new TextDecoder().decode(bytes);
|
|
304
|
+
var len = text.length;
|
|
305
|
+
|
|
306
|
+
// Find JSON with OVM metadata signature near the opening brace
|
|
307
|
+
function hasOvmSig(t, start) {
|
|
308
|
+
var head = t.substring(start, Math.min(start + 80, t.length));
|
|
309
|
+
return head.indexOf('ovm_version') !== -1 || head.indexOf('slots') !== -1;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function findClosing(t, start) {
|
|
313
|
+
var depth = 0, inStr = false, esc = false;
|
|
314
|
+
for (var i = start; i < t.length; i++) {
|
|
315
|
+
var ch = t[i];
|
|
316
|
+
if (esc) { esc = false; continue; }
|
|
317
|
+
if (ch === '\\') { esc = true; continue; }
|
|
318
|
+
if (ch === '"') { inStr = !inStr; continue; }
|
|
319
|
+
if (inStr) continue;
|
|
320
|
+
if (ch === '{') { depth++; }
|
|
321
|
+
else if (ch === '}') { depth--; if (depth === 0) return t.substring(start, i + 1); }
|
|
322
|
+
}
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Priority: match by signature first
|
|
327
|
+
for (var i = 0; i < len; i++) {
|
|
328
|
+
if (text[i] !== '{') continue;
|
|
329
|
+
if (!hasOvmSig(text, i)) continue;
|
|
330
|
+
var json = findClosing(text, i);
|
|
331
|
+
if (!json) continue;
|
|
332
|
+
try { return JSON.parse(json); } catch (_) {}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Fallback: any JSON with slots/layers
|
|
336
|
+
for (var j = 0; j < len; j++) {
|
|
337
|
+
if (text[j] !== '{') continue;
|
|
338
|
+
var jjson = findClosing(text, j);
|
|
339
|
+
if (!jjson) continue;
|
|
340
|
+
try {
|
|
341
|
+
var parsed = JSON.parse(jjson);
|
|
342
|
+
if (parsed.slots || parsed.elements || parsed.layers) return parsed;
|
|
343
|
+
} catch (_) {}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return null;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
OvmPlayer.prototype._showLoader = function (msg, isError) {
|
|
350
|
+
this._loader.textContent = msg;
|
|
351
|
+
this._loader.className = isError ? 'loader error' : 'loader';
|
|
352
|
+
this._loader.style.display = 'flex';
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
OvmPlayer.prototype._hideLoader = function () {
|
|
356
|
+
this._loader.style.display = 'none';
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
OvmPlayer.prototype._renderSlots = function () {
|
|
360
|
+
if (!this._metadata) return;
|
|
361
|
+
var slots = this._metadata.slots || this._metadata.elements || [];
|
|
362
|
+
var lang = this.lang;
|
|
363
|
+
var t = this._currentTime;
|
|
364
|
+
this._overlay.innerHTML = '';
|
|
365
|
+
for (var i = 0; i < slots.length; i++) {
|
|
366
|
+
var slot = slots[i];
|
|
367
|
+
var content = slot.content || {};
|
|
368
|
+
var text = content[lang] || content.en || content.es || '';
|
|
369
|
+
if (!text) continue;
|
|
370
|
+
|
|
371
|
+
var left, top;
|
|
372
|
+
var tracking = slot.tracking_data;
|
|
373
|
+
if (tracking && tracking.length > 0) {
|
|
374
|
+
var box = this._interpolateTracking(tracking, t);
|
|
375
|
+
if (box) {
|
|
376
|
+
left = ((box.x1 + box.x3) / 2) * 100;
|
|
377
|
+
top = ((box.y1 + box.y3) / 2) * 100;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (left === undefined) {
|
|
382
|
+
var pos = this._normPos(slot.position || [0.5, 0.5]);
|
|
383
|
+
left = pos[0] * 100;
|
|
384
|
+
top = pos[1] * 100;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
var st = slot.style || {};
|
|
388
|
+
var slotEl = document.createElement('div');
|
|
389
|
+
slotEl.className = 'slot';
|
|
390
|
+
slotEl.style.left = left.toFixed(2) + '%';
|
|
391
|
+
slotEl.style.top = top.toFixed(2) + '%';
|
|
392
|
+
// Canonical keys of contract 1.5.0. It used to read `color`, `size` and
|
|
393
|
+
// `font`, which the compiler no longer writes: the text of a signed .ovm
|
|
394
|
+
// came out without color, without size and without font.
|
|
395
|
+
if (st.fillColor) slotEl.style.color = st.fillColor;
|
|
396
|
+
if (st.fontSize) slotEl.style.fontSize = st.fontSize;
|
|
397
|
+
if (st.fontFamily) slotEl.style.fontFamily = st.fontFamily + ',sans-serif';
|
|
398
|
+
slotEl.textContent = text;
|
|
399
|
+
this._overlay.appendChild(slotEl);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
OvmPlayer.prototype._interpolateTracking = function (keyframes, t) {
|
|
404
|
+
if (keyframes.length === 1) {
|
|
405
|
+
var c0 = keyframes[0].slice(1);
|
|
406
|
+
return boxFrom(c0);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
var before = null, after = null;
|
|
410
|
+
for (var i = 0; i < keyframes.length; i++) {
|
|
411
|
+
if (keyframes[i][0] <= t) before = keyframes[i];
|
|
412
|
+
else { after = keyframes[i]; break; }
|
|
413
|
+
}
|
|
414
|
+
if (!before && after) return boxFrom(after.slice(1));
|
|
415
|
+
if (before && !after) return boxFrom(before.slice(1));
|
|
416
|
+
if (!before && !after) return null;
|
|
417
|
+
|
|
418
|
+
var t0 = before[0], t1 = after[0];
|
|
419
|
+
var p = t1 > t0 ? (t - t0) / (t1 - t0) : 0;
|
|
420
|
+
p = Math.max(0, Math.min(1, p));
|
|
421
|
+
|
|
422
|
+
var b1 = before.slice(1), b2 = after.slice(1);
|
|
423
|
+
var interp = b1.map(function (v, idx) { return v + (b2[idx] - v) * p; });
|
|
424
|
+
return boxFrom(interp);
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
function boxFrom(arr) {
|
|
428
|
+
return { x1: arr[0], y1: arr[1], x2: arr[2], y2: arr[3], x3: arr[4], y3: arr[5], x4: arr[6], y4: arr[7] };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
OvmPlayer.prototype._normPos = function (pos) {
|
|
432
|
+
var x = pos[0], y = pos[1];
|
|
433
|
+
if (x > 1 || y > 1) { x /= 100; y /= 100; }
|
|
434
|
+
return [x, y];
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
OvmPlayer.prototype._esc = function (text) {
|
|
438
|
+
return String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
if (typeof customElements !== 'undefined' && !customElements.get('ovm-player')) {
|
|
442
|
+
customElements.define('ovm-player', OvmPlayer);
|
|
443
|
+
}
|
|
444
|
+
})();
|