Skip to content

Commit 6561136

Browse files
committed
Allow updating of the granule position while writing packets
This allows you to have the granule position updated as you go, helpful when transcoding/repacking files. Based on bb78fc8 from @andrm
1 parent 9c79c25 commit 6561136

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

core/src/main/java/org/gagravarr/ogg/OggPacketWriter.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,22 @@ private OggPage getCurrentPage(boolean forceNew) {
7171
/**
7272
* Buffers the given packet up ready for
7373
* writing to the stream, but doesn't
74-
* write it to disk yet.
74+
* write it to disk yet. The granule
75+
* position is unchanged.
7576
*/
7677
public void bufferPacket(OggPacket packet) {
78+
bufferPacket(packet, currentGranulePosition);
79+
}
80+
/**
81+
* Buffers the given packet up ready for
82+
* writing to the stream, but doesn't
83+
* write it to disk yet. The granule position
84+
* is updated on the page.
85+
* If writing the packet requires a new page,
86+
* then the updated granule position only
87+
* applies to the new page
88+
*/
89+
public void bufferPacket(OggPacket packet, long granulePosition) {
7790
if(closed) {
7891
throw new IllegalStateException("Can't buffer packets on a closed stream!");
7992
}
@@ -93,9 +106,11 @@ public void bufferPacket(OggPacket packet) {
93106
if(pos < size) {
94107
page = getCurrentPage(true);
95108
page.setIsContinuation();
109+
page.setGranulePosition(granulePosition);
96110
}
97111
emptyPacket = false;
98112
}
113+
page.setGranulePosition(granulePosition);
99114
packet.setParent(page);
100115
}
101116

core/src/test/java/org/gagravarr/ogg/TestBasicWrite.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/**
2424
* Test that we can do basic writing without error
2525
*/
26+
@SuppressWarnings("resource")
2627
public class TestBasicWrite extends TestCase {
2728
public void testOpen() throws IOException {
2829
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -92,9 +93,10 @@ public void testEmptyPages() throws IOException {
9293
w.bufferPacket(p);
9394
w.flush();
9495

95-
// And a packet with something in it
96+
// And a packet with something in it,
97+
// and with a granule position
9698
p = new OggPacket(new byte[] {22});
97-
w.bufferPacket(p);
99+
w.bufferPacket(p, 54321l);
98100
w.close();
99101

100102
// Check again
@@ -106,6 +108,7 @@ public void testEmptyPages() throws IOException {
106108
assertEquals(true, p.isBeginningOfStream());
107109
assertEquals(true, p.isEndOfStream());
108110
assertEquals(1234, p.getSid());
111+
assertEquals(0, p.getGranulePosition());
109112
assertEquals(0, p.getSequenceNumber());
110113
assertEquals(0, p.getData().length);
111114

@@ -114,6 +117,7 @@ public void testEmptyPages() throws IOException {
114117
assertEquals(true, p.isBeginningOfStream());
115118
assertEquals(false, p.isEndOfStream());
116119
assertEquals(54321, p.getSid());
120+
assertEquals(0, p.getGranulePosition());
117121
assertEquals(0, p.getSequenceNumber());
118122
assertEquals(0, p.getData().length);
119123

@@ -122,6 +126,7 @@ public void testEmptyPages() throws IOException {
122126
assertEquals(false, p.isBeginningOfStream());
123127
assertEquals(true, p.isEndOfStream());
124128
assertEquals(54321, p.getSid());
129+
assertEquals(54321l, p.getGranulePosition());
125130
assertEquals(1, p.getSequenceNumber());
126131
assertEquals(1, p.getData().length);
127132

0 commit comments

Comments
 (0)