|
| 1 | +/* c8 ignore start */ |
| 2 | +import type { WebAVPacket, WebAVStream } from './types'; |
| 3 | + |
| 4 | +let Module: any; // TODO: rm any |
| 5 | + |
| 6 | +self.postMessage({ |
| 7 | + type: 'FFmpegWorkerLoaded', |
| 8 | +}); |
| 9 | + |
| 10 | +self.addEventListener('message', async function (e) { |
| 11 | + const { type, data = {}, msgId } = e.data; |
| 12 | + |
| 13 | + try { |
| 14 | + if (type === 'LoadWASM') { |
| 15 | + const { wasmLoaderPath } = data || {}; |
| 16 | + |
| 17 | + const ModuleLoader = await import(/* @vite-ignore */ wasmLoaderPath); |
| 18 | + Module = await ModuleLoader.default(); |
| 19 | + } else if (type === 'GetAVStream') { |
| 20 | + const { file, streamType, streamIndex } = data; |
| 21 | + const result = Module.getAVStream(file, streamType, streamIndex); |
| 22 | + |
| 23 | + self.postMessage( |
| 24 | + { |
| 25 | + type, |
| 26 | + msgId, |
| 27 | + result, |
| 28 | + }, |
| 29 | + [result.codecpar.extradata.buffer], |
| 30 | + ); |
| 31 | + } else if (type === 'GetAVStreams') { |
| 32 | + const { file } = data; |
| 33 | + const result = Module.getAVStreams(file); |
| 34 | + |
| 35 | + self.postMessage( |
| 36 | + { |
| 37 | + type, |
| 38 | + msgId, |
| 39 | + result, |
| 40 | + }, |
| 41 | + result.map((stream: WebAVStream) => stream.codecpar.extradata.buffer), |
| 42 | + ); |
| 43 | + } else if (type === 'GetAVPacket') { |
| 44 | + const { file, time, streamType, streamIndex } = data; |
| 45 | + const result = Module.getAVPacket(file, time, streamType, streamIndex); |
| 46 | + |
| 47 | + self.postMessage( |
| 48 | + { |
| 49 | + type, |
| 50 | + msgId, |
| 51 | + result, |
| 52 | + }, |
| 53 | + [result.data.buffer], |
| 54 | + ); |
| 55 | + } else if (type === 'GetAVPackets') { |
| 56 | + const { file, time } = data; |
| 57 | + const result = Module.getAVPackets(file, time); |
| 58 | + |
| 59 | + self.postMessage( |
| 60 | + { |
| 61 | + type, |
| 62 | + msgId, |
| 63 | + result, |
| 64 | + }, |
| 65 | + result.map((packet: WebAVPacket) => packet.data.buffer), |
| 66 | + ); |
| 67 | + } else if (type === 'ReadAVPacket') { |
| 68 | + const { file, start, end, streamType, streamIndex } = data; |
| 69 | + const result = Module.readAVPacket(msgId, file, start, end, streamType, streamIndex); |
| 70 | + |
| 71 | + self.postMessage({ |
| 72 | + type, |
| 73 | + msgId, |
| 74 | + result, |
| 75 | + }); |
| 76 | + } |
| 77 | + } catch (e) { |
| 78 | + self.postMessage({ |
| 79 | + type, |
| 80 | + msgId, |
| 81 | + errMsg: e instanceof Error ? e.message : 'Unknown Error', |
| 82 | + }); |
| 83 | + } |
| 84 | +}); |
0 commit comments