diff --git a/src/cpp/type-conversion.cpp b/src/cpp/type-conversion.cpp index 165031e..093a2ab 100644 --- a/src/cpp/type-conversion.cpp +++ b/src/cpp/type-conversion.cpp @@ -410,6 +410,28 @@ namespace TypeConversion { // Add flags from buffer GstBufferFlags flags = gst_buffer_get_flags(buf); result.Set("flags", Napi::Number::New(env, static_cast(flags))); + + // Add timing and offset information from buffer (pts/dts/duration in nanoseconds). + // Each field is omitted when the buffer holds no valid value for it. + if (GST_BUFFER_PTS_IS_VALID(buf)) { + result.Set("pts", Napi::Number::New(env, static_cast(GST_BUFFER_PTS(buf)))); + } + if (GST_BUFFER_DTS_IS_VALID(buf)) { + result.Set("dts", Napi::Number::New(env, static_cast(GST_BUFFER_DTS(buf)))); + } + if (GST_BUFFER_DURATION_IS_VALID(buf)) { + result.Set( + "duration", Napi::Number::New(env, static_cast(GST_BUFFER_DURATION(buf))) + ); + } + if (GST_BUFFER_OFFSET_IS_VALID(buf)) { + result.Set("offset", Napi::Number::New(env, static_cast(GST_BUFFER_OFFSET(buf)))); + } + if (GST_BUFFER_OFFSET_END_IS_VALID(buf)) { + result.Set( + "offsetEnd", Napi::Number::New(env, static_cast(GST_BUFFER_OFFSET_END(buf))) + ); + } } // Add caps from sample diff --git a/src/ts/appsink.test.ts b/src/ts/appsink.test.ts index 1ca8f88..cc54f23 100644 --- a/src/ts/appsink.test.ts +++ b/src/ts/appsink.test.ts @@ -107,4 +107,29 @@ describe("AppSink", () => { expect(samples).toHaveLength(frames); expect(samples[0].buffer).toBeDefined(); }); + + it("should expose the buffer timing metadata on pulled samples", async () => { + const pipeline = new Pipeline( + "videotestsrc num-buffers=5 is-live=false ! video/x-raw,framerate=30/1 " + + "! videoconvert ! appsink name=sink" + ); + const sink = pipeline.getElementByName("sink"); + + if (sink?.type !== "app-sink-element") throw new Error("Expected app sink element"); + + await pipeline.play(); + + const first = await sink.getSample(); + const second = await sink.getSample(); + + await pipeline.stop(); + + expect(typeof first?.pts).toBe("number"); + expect(typeof second?.pts).toBe("number"); + // videotestsrc timestamps buffers by running time: at 30fps consecutive PTS differ by 1/30s. + expect((second!.pts as number) - (first!.pts as number)).toBe(33_333_333); + // Same source stamps each buffer with the frame duration and its frame offsets. + expect(first?.duration).toBe(33_333_333); + expect(second!.offset as number).toBe((first!.offset as number) + 1); + }); }); diff --git a/src/ts/index.ts b/src/ts/index.ts index 5a1ab33..c9c594b 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -17,6 +17,15 @@ export type GStreamerPropertyValue = // Sample object returned for GST_VALUE_HOLDS_SAMPLE properties export type GStreamerSample = { buffer?: Buffer; + + // Timing and offset information from the sample's buffer (omitted when the buffer has no + // valid value for that field) + pts?: number; // Presentation timestamp (nanoseconds) + dts?: number; // Decode timestamp (nanoseconds) + duration?: number; // Buffer duration (nanoseconds) + offset?: number; // Media-specific start offset + offsetEnd?: number; // Media-specific end offset + flags?: number; caps?: { name?: string;