Skip to content

Commit 6921703

Browse files
committed
Add methods to read DTS and PTS timestamps
1 parent e56d0af commit 6921703

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/org/freedesktop/gstreamer/Buffer.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
package org.freedesktop.gstreamer;
2424

25-
import com.sun.jna.Pointer;
25+
import static org.freedesktop.gstreamer.lowlevel.GstBufferAPI.GSTBUFFER_API;
26+
2627
import java.nio.ByteBuffer;
2728

2829
import org.freedesktop.gstreamer.lowlevel.GstBufferAPI;
30+
import org.freedesktop.gstreamer.lowlevel.GstBufferAPI.BufferStruct;
2931
import org.freedesktop.gstreamer.lowlevel.GstBufferAPI.MapInfoStruct;
3032

31-
import static org.freedesktop.gstreamer.lowlevel.GstBufferAPI.GSTBUFFER_API;
33+
import com.sun.jna.Pointer;
3234

3335
/**
3436
* Data-passing buffer type, supporting sub-buffers.
@@ -91,10 +93,13 @@ public class Buffer extends MiniObject {
9193
public static final String GTYPE_NAME = "GstBuffer";
9294

9395
private final MapInfoStruct mapInfo;
96+
private final BufferStruct struct;
97+
9498

9599
public Buffer(Initializer init) {
96100
super(init);
97101
mapInfo = new MapInfoStruct();
102+
struct = new BufferStruct(handle());
98103
}
99104

100105
/**
@@ -141,17 +146,36 @@ private static Pointer allocBuffer(int size) {
141146
* @return A {@link java.nio.ByteBuffer} that can access this Buffer's data.
142147
*/
143148
public ByteBuffer map(boolean writeable) {
144-
boolean ok = GSTBUFFER_API.gst_buffer_map(this, mapInfo,
149+
final boolean ok = GSTBUFFER_API.gst_buffer_map(this, mapInfo,
145150
writeable ? GstBufferAPI.GST_MAP_WRITE : GstBufferAPI.GST_MAP_READ);
146-
if (ok) {
151+
if (ok && mapInfo.data != null) {
147152
return mapInfo.data.getByteBuffer(0, mapInfo.size.intValue());
148-
} else {
149-
return null;
150153
}
154+
return null;
151155
}
152156

153157
public void unmap() {
154158
GSTBUFFER_API.gst_buffer_unmap(this, mapInfo);
155159
}
156160

161+
/**
162+
* Gets the timestamps of this buffer.
163+
* The buffer DTS refers to the timestamp when the buffer should be decoded and is usually monotonically increasing.
164+
*
165+
* @return a ClockTime representing the timestamp or {@link ClockTime#NONE} when the timestamp is not known or relevant.
166+
*/
167+
public ClockTime getDecodeTimestamp() {
168+
return (ClockTime)this.struct.readField("dts");
169+
}
170+
171+
/**
172+
* Gets the timestamps of this buffer.
173+
* The buffer PTS refers to the timestamp when the buffer content should be presented to the user and is not always monotonically increasing.
174+
*
175+
* @return a ClockTime representing the timestamp or {@link ClockTime#NONE} when the timestamp is not known or relevant.
176+
*/
177+
public ClockTime getPresentationTimestamp() {
178+
return (ClockTime)this.struct.readField("pts");
179+
}
180+
157181
}

src/org/freedesktop/gstreamer/lowlevel/GstBufferAPI.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020

2121
package org.freedesktop.gstreamer.lowlevel;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
import org.freedesktop.gstreamer.Buffer;
2427
import org.freedesktop.gstreamer.ClockTime;
2528
import org.freedesktop.gstreamer.lowlevel.GstMiniObjectAPI.MiniObjectStruct;
2629
import org.freedesktop.gstreamer.lowlevel.annotations.CallerOwnsReturn;
2730

2831
import com.sun.jna.NativeLong;
2932
import com.sun.jna.Pointer;
30-
import java.util.Arrays;
31-
import java.util.List;
3233

3334
/**
3435
* GstBuffer functions
@@ -53,7 +54,7 @@ public static final class MapInfoStruct extends com.sun.jna.Structure {
5354
public MapInfoStruct() {
5455
}
5556
public MapInfoStruct(Pointer ptr) {
56-
useMemory(ptr);
57+
super(ptr);
5758
}
5859

5960
@Override
@@ -98,8 +99,7 @@ public static final class BufferStruct extends com.sun.jna.Structure {
9899
public long offset_end;
99100

100101
public BufferStruct(Pointer ptr) {
101-
useMemory(ptr);
102-
read();
102+
super(ptr);
103103
}
104104

105105
@Override

0 commit comments

Comments
 (0)