diff --git a/android/src/main/cpp/VideoDecoder.hpp b/android/src/main/cpp/VideoDecoder.hpp index f81df04..ba9cc61 100644 --- a/android/src/main/cpp/VideoDecoder.hpp +++ b/android/src/main/cpp/VideoDecoder.hpp @@ -172,11 +172,27 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { i += length; } int64_t ts = static_cast(timestamp); - { - std::lock_guard lock(_mutex); - _durations[ts] = duration; - } - _input.post([this, annexB = std::move(annexB), ts, key] { + _input.post([this, annexB = std::move(annexB), ts, duration, key] { + // A key frame earlier than a chunk before it (a GOP that goes back in + // time, without a flush): a new stream, as on iOS. The frames still + // held for reordering go out first, as a flush puts them out, and the + // codec starts over. Without this, c2.goldfish.h264.decoder (the + // emulator's) held the previous GOP's last frames until the end of + // the stream and put out the earlier GOP in decoding order (right + // pictures, wrong order). + if (key && ts < _latestQueued) { + if (!drainCodec()) { + return; + } + } + { + // Here, after any drain or earlier flush, not on the JS thread: + // takeDuration() drops every entry up to the picture put out, which + // would take those of a GOP going back in time while the GOP before + // it still comes out. + std::lock_guard lock(_mutex); + _durations[ts] = duration; + } queueInput(annexB.data(), annexB.size(), ts, key ? kBufferFlagKeyFrame : 0); }); } @@ -204,58 +220,14 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { } return; } - if (!_failed) { - queueInput(nullptr, 0, 0, kBufferFlagEndOfStream); - } - // Every rendered picture is taken before the next one (waitAcquired), - // so all of them come; a second without any is a lost one. - // Stuck once nothing came for 10 s (from the flush or the last picture). - std::unique_lock lock(_mutex); - auto flushStart = std::chrono::steady_clock::now(); - bool ended = false; - while (!_failed) { - auto now = std::chrono::steady_clock::now(); - if (_eos && (_delivered >= _rendered || now - _lastImage > std::chrono::seconds(1))) { - ended = true; - break; - } - if (_heldByJs) { - // Not stuck: waiting for JS to close a frame. - flushStart = now; - } - if (now - std::max(flushStart, _lastImage) > std::chrono::seconds(10)) { - break; - } - _cv.wait_for(lock, std::chrono::milliseconds(20)); - } - lock.unlock(); - if (!ended && !_failed) { - fail(errorMessage("EncodingError", "decoder-failed", "timed out waiting for the decoder to flush")); - } - if (_failed) { + if (!drainCodec()) { promise->reject(std::make_exception_ptr(std::runtime_error(_failure))); return; } { - // Stops the drain thread if the stream did not end in time. std::lock_guard l(_mutex); - _draining = false; - } - if (_drain.joinable()) { - _drain.join(); - } - // Ready for more input (from a key frame). - AMediaCodec_flush(_codec); - { - std::lock_guard l(_mutex); - _eos = false; - _rendered = 0; - _delivered = 0; - _acquired = 0; _durations.clear(); } - _queuedSinceFlush = 0; - startDrain(); promise->resolve(); }); return promise; @@ -271,6 +243,64 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { return static_cast(std::min(pixels * 3 / 4, INT32_MAX)); } + // On the input thread: ends the stream, waits for every picture to be + // delivered, then flushes the codec, ready for input from a key frame. + // False (and failed) when the decoder failed or did not end in time. + bool drainCodec() { + if (!_failed) { + queueInput(nullptr, 0, 0, kBufferFlagEndOfStream); + } + // Every rendered picture is taken before the next one (waitAcquired), + // so all of them come; a second without any is a lost one. + // Stuck once nothing came for 10 s (from the flush or the last picture). + std::unique_lock lock(_mutex); + auto flushStart = std::chrono::steady_clock::now(); + bool ended = false; + while (!_failed) { + auto now = std::chrono::steady_clock::now(); + if (_eos && (_delivered >= _rendered || now - _lastImage > std::chrono::seconds(1))) { + ended = true; + break; + } + if (_heldByJs) { + // Not stuck: waiting for JS to close a frame. + flushStart = now; + } + if (now - std::max(flushStart, _lastImage) > std::chrono::seconds(10)) { + break; + } + _cv.wait_for(lock, std::chrono::milliseconds(20)); + } + lock.unlock(); + if (!ended && !_failed) { + fail(errorMessage("EncodingError", "decoder-failed", "timed out waiting for the decoder to flush")); + } + if (_failed) { + return false; + } + { + // Stops the drain thread if the stream did not end in time. + std::lock_guard l(_mutex); + _draining = false; + } + if (_drain.joinable()) { + _drain.join(); + } + // Ready for more input (from a key frame). + AMediaCodec_flush(_codec); + { + std::lock_guard l(_mutex); + _eos = false; + _rendered = 0; + _delivered = 0; + _acquired = 0; + } + _queuedSinceFlush = 0; + _latestQueued = INT64_MIN; + startDrain(); + return true; + } + void startDrain() { { std::lock_guard lock(_mutex); @@ -317,6 +347,7 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { fail(describeCodecStatus(status, "decoder", "decoder")); } else if ((flags & kBufferFlagEndOfStream) == 0) { _queuedSinceFlush++; + _latestQueued = std::max(_latestQueued, ts); } } @@ -562,6 +593,9 @@ class HybridNativeVideoDecoder : public HybridNativeVideoDecoderSpec { std::thread _drain; // Chunks queued since configure() or the last flush (the input thread's). int64_t _queuedSinceFlush = 0; + // The latest timestamp queued since then, INT64_MIN when none (the input + // thread's). + int64_t _latestQueued = INT64_MIN; SerialQueue _input{"wc-vdec-input"}; SerialQueue _deliver{"wc-vdec-out"}; diff --git a/src/__tests__/WebCodecs.spec.ts b/src/__tests__/WebCodecs.spec.ts index cc2beb3..33647ae 100644 --- a/src/__tests__/WebCodecs.spec.ts +++ b/src/__tests__/WebCodecs.spec.ts @@ -519,6 +519,7 @@ const runOnDevice = async ( example: any; mediabunny: any; webcodecs: any; + device: GPUDevice; ctx: any; }) => Promise, ctx: object, @@ -526,12 +527,14 @@ const runOnDevice = async ( ): Promise => { const key = `__e2e_${Math.random().toString(36).slice(2)}`; await client.eval( - ({ example, mediabunny, webcodecs, ctx: c }) => { + ({ example, mediabunny, webcodecs, device, ctx: c }) => { const g = globalThis as any; const run = eval(`(${c.fn})`); g[c.key] = null; Promise.resolve() - .then(() => run({ example, mediabunny, webcodecs, ctx: c.args })) + .then(() => + run({ example, mediabunny, webcodecs, device, ctx: c.args }), + ) .then( (value: any) => (g[c.key] = { value }), (e: any) => (g[c.key] = { error: String((e && e.stack) || e) }), @@ -3878,6 +3881,9 @@ describe("react-native-webcodecs + Mediabunny", () => { packets: { timestamp: number; key: boolean }[]; // Each run's output timestamps, in output order. runs: number[][]; + // With `grids`: each output frame's mean luma (0-255) over a 16x16 + // grid, by timestamp, per run. + grids: Record[]; errors: string[]; } @@ -3919,9 +3925,9 @@ describe("react-native-webcodecs + Mediabunny", () => { // Decodes `runs` of GOPs (each a list of [first, end) GOP ranges, in // that order, without a flush between them), each run followed by a // flush, with one decoder. - const decodeRuns = (file: string, runs: number[][][]) => + const decodeRuns = (file: string, runs: number[][][], grids = false) => runOnDevice( - ({ mediabunny: mb, webcodecs: wc, ctx }) => { + ({ mediabunny: mb, webcodecs: wc, device, ctx }) => { const input = new mb.Input({ source: new mb.CustomSource(wc.fileSourceOptions(ctx.file)), formats: mb.ALL_FORMATS, @@ -3930,8 +3936,68 @@ describe("react-native-webcodecs + Mediabunny", () => { supported: false, packets: [], runs: [], + grids: [], errors: [], }; + // The frame (through WebGPU, as an app draws it), as the mean + // luma of each cell of a 16x16 grid. + const grid = (frame: any): Promise => { + const width = frame.displayWidth; + const height = frame.displayHeight; + const buffer = RNWebGPU.createNativeBufferFromPointer( + frame.pixelBuffer, + { colorSpace: frame.colorSpace.toJSON() }, + ); + const texture = device.createTexture({ + size: [width, height], + format: "rgba8unorm", + usage: + GPUTextureUsage.RENDER_ATTACHMENT | + GPUTextureUsage.COPY_SRC | + GPUTextureUsage.TEXTURE_BINDING, + }); + device.queue.copyExternalImageToTexture( + { source: buffer }, + { texture }, + [width, height], + ); + const bytesPerRow = Math.ceil((width * 4) / 256) * 256; + const readback = device.createBuffer({ + size: bytesPerRow * height, + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, + }); + const encoder = device.createCommandEncoder(); + encoder.copyTextureToBuffer( + { texture }, + { buffer: readback, bytesPerRow }, + [width, height], + ); + device.queue.submit([encoder.finish()]); + return readback.mapAsync(GPUMapMode.READ).then(() => { + buffer.release(); + frame.close(); + const data = new Uint8Array(readback.getMappedRange()); + const sums = new Array(256).fill(0); + const counts = new Array(256).fill(0); + for (let y = 0; y < height; y += 2) { + for (let x = 0; x < width; x += 2) { + const o = y * bytesPerRow + x * 4; + const cell = + Math.floor((y * 16) / height) * 16 + + Math.floor((x * 16) / width); + sums[cell] += + 0.299 * data[o]! + + 0.587 * data[o + 1]! + + 0.114 * data[o + 2]!; + counts[cell]++; + } + } + readback.unmap(); + readback.destroy(); + texture.destroy(); + return sums.map((v, i) => Math.round(v / counts[i])); + }); + }; return input.getPrimaryVideoTrack().then((track: any) => track.getDecoderConfig().then((config: any) => wc.VideoDecoder.isConfigSupported(config).then((s: any) => { @@ -3960,10 +4026,29 @@ describe("react-native-webcodecs + Mediabunny", () => { return read(); }); let current: number[] = []; + let currentGrids: Record = {}; + let sampling: Promise = Promise.resolve(); const decoder = new wc.VideoDecoder({ output: (frame: any) => { current.push(frame.timestamp); - frame.close(); + if (!ctx.grids) { + frame.close(); + return; + } + if (!(frame.duration > 0)) { + result.errors.push(`no duration at ${frame.timestamp}`); + } + const into = currentGrids; + sampling = sampling + .then(() => grid(frame)) + .then( + (g) => { + into[String(frame.timestamp)] = g; + }, + (e: any) => { + result.errors.push(`grid: ${e}`); + }, + ); }, error: (e: any) => result.errors.push(String(e)), }); @@ -3984,6 +4069,8 @@ describe("react-native-webcodecs + Mediabunny", () => { } current = []; result.runs.push(current); + currentGrids = {}; + result.grids.push(currentGrids); order.length = 0; ctx.runs[n].forEach((range: number[]) => { const end = @@ -3999,6 +4086,7 @@ describe("react-native-webcodecs + Mediabunny", () => { .catch((e: any) => { result.errors.push(`flush: ${e}`); }) + .then(() => sampling) .then(() => run(n + 1)); }; return read() @@ -4014,7 +4102,7 @@ describe("react-native-webcodecs + Mediabunny", () => { ), ); }, - { file, runs }, + { file, runs, grids }, 120000, ); @@ -4088,32 +4176,48 @@ describe("react-native-webcodecs + Mediabunny", () => { "-sc_threshold", "0", ]); - const decoded = await decodeRuns(file, [ + // The second GOP then the first without a flush; then each GOP on + // its own (from a flushed decoder), for what each frame looks like. + const decoded = await decodeRuns( + file, [ - [1, 2], - [0, 1], + [ + [1, 2], + [0, 1], + ], + [[0, 1]], + [[1, 2]], ], - ]); - console.log(`back in time: ${JSON.stringify(decoded)}`); + true, + ); + console.log( + `back in time: ${JSON.stringify({ ...decoded, grids: undefined })}`, + ); if (!decoded.supported) { return; } const times = (gop: number) => sorted(gopOf(decoded.packets, gop).map((p) => p.timestamp)); - if (!ios()) { - // Android (c2 decoders on the emulator) puts out the first GOP in - // decode order and holds the second GOP's last frames until the - // end of the stream; every frame still comes out, once. - expect(decoded.errors).toEqual([]); - expect(sorted(decoded.runs[0])).toEqual( - sorted(times(1).concat(times(0))), - ); - return; - } expect({ errors: decoded.errors, frames: decoded.runs[0] }).toEqual({ errors: [], frames: times(1).concat(times(0)), }); + // Each picture is the one its timestamp says: closest to the frame of + // that timestamp decoded on its own. + const reference = { ...decoded.grids[1], ...decoded.grids[2] }; + const distance = (a: number[], b: number[]) => + a.reduce((sum, v, i) => sum + Math.abs(v - b[i]!), 0); + const pictures = decoded.runs[0]!.map((t) => { + const got = decoded.grids[0]![String(t)]!; + let best = ""; + Object.entries(reference).forEach(([key, want]) => { + if (!best || distance(got, want) < distance(got, reference[best]!)) { + best = key; + } + }); + return best; + }); + expect(pictures).toEqual(decoded.runs[0]!.map(String)); }, 300000); });