(bytes: Uint8Array)
| 220 | const NV12_FULL_MAGIC = 0x4e563146; |
| 221 | |
| 222 | function parseFrameMetadata(bytes: Uint8Array): FrameMetadata | null { |
| 223 | if (bytes.byteLength < 24) return null; |
| 224 | |
| 225 | if (bytes.byteLength >= 28) { |
| 226 | const formatOffset = bytes.byteOffset + bytes.byteLength - 4; |
| 227 | const formatView = new DataView(bytes.buffer, formatOffset, 4); |
| 228 | const formatFlag = formatView.getUint32(0, true); |
| 229 | |
| 230 | if (formatFlag === NV12_VIDEO_MAGIC || formatFlag === NV12_FULL_MAGIC) { |
| 231 | const metadataOffset = bytes.byteOffset + bytes.byteLength - 28; |
| 232 | const meta = new DataView(bytes.buffer, metadataOffset, 28); |
| 233 | const yStride = meta.getUint32(0, true); |
| 234 | const height = meta.getUint32(4, true); |
| 235 | const width = meta.getUint32(8, true); |
| 236 | const frameNumber = meta.getUint32(12, true); |
| 237 | const targetTimeNs = meta.getBigUint64(16, true); |
| 238 | |
| 239 | if (!width || !height || !yStride) return null; |
| 240 | |
| 241 | const ySize = yStride * height; |
| 242 | const uvSize = yStride * Math.floor(height / 2); |
| 243 | const totalSize = ySize + uvSize; |
| 244 | |
| 245 | if (bytes.byteLength - 28 < totalSize) { |
| 246 | return null; |
| 247 | } |
| 248 | |
| 249 | return { |
| 250 | format: "nv12", |
| 251 | width, |
| 252 | height, |
| 253 | yStride, |
| 254 | fullRange: formatFlag === NV12_FULL_MAGIC, |
| 255 | frameNumber, |
| 256 | targetTimeNs, |
| 257 | ySize, |
| 258 | uvSize, |
| 259 | totalSize, |
| 260 | }; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | const metadataOffset = bytes.byteOffset + bytes.byteLength - 24; |
| 265 | const meta = new DataView(bytes.buffer, metadataOffset, 24); |
| 266 | const strideBytes = meta.getUint32(0, true); |
| 267 | const height = meta.getUint32(4, true); |
| 268 | const width = meta.getUint32(8, true); |
| 269 | const frameNumber = meta.getUint32(12, true); |
| 270 | const targetTimeNs = meta.getBigUint64(16, true); |
| 271 | |
| 272 | if (!width || !height) return null; |
| 273 | |
| 274 | const expectedRowBytes = width * 4; |
| 275 | const availableLength = strideBytes * height; |
| 276 | |
| 277 | if ( |
| 278 | strideBytes === 0 || |
| 279 | strideBytes < expectedRowBytes || |
no outgoing calls
no test coverage detected