public void readTsFile(SeekableByteChannel ch) throws IOException { ch.position(0); ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE); for (long pos = ch.position(); ch.read(buf) != -1; pos = ch.position()) { buf.flip(); while (buf.hasRemaining()) { ByteBuffer tsBuf = NIOUtils.read(buf, 188); pos += 188; Assert.assertEquals(0x47, tsBuf.get() & 0xff); int guidFlags = ((tsBuf.get() & 0xff) << 8) | (tsBuf.get() & 0xff); int guid = (int) guidFlags & 0x1fff; int payloadStart = (guidFlags >> 14) & 0x1; int b0 = tsBuf.get() & 0xff; int counter = b0 & 0xf; if ((b0 & 0x20) != 0) { NIOUtils.skip(tsBuf, tsBuf.get() & 0xff); } boolean sectionSyntax = payloadStart == 1 && (getRel(tsBuf, getRel(tsBuf, 0) + 2) & 0x80) == 0x80; if (sectionSyntax) { NIOUtils.skip(tsBuf, tsBuf.get() & 0xff); } if (!onPkt(guid, payloadStart == 1, tsBuf, pos - tsBuf.remaining())) return; } buf.flip(); } }
@Override protected ByteBuffer readPes( SeekableByteChannel ch, long pesPosition, int pesSize, int payloadSize, int pesAbsIdx) throws IOException { ch.position(pesPosition * 188); ByteBuffer buf = NIOUtils.fetchFrom(ch, pesSize * 188); // NOW REMOVE THE TS CRAP ByteBuffer dst = buf.duplicate(); while (buf.hasRemaining()) { ByteBuffer tsBuf = NIOUtils.read(buf, 188); Assert.assertEquals(0x47, tsBuf.get() & 0xff); int guidFlags = ((tsBuf.get() & 0xff) << 8) | (tsBuf.get() & 0xff); int guid = (int) guidFlags & 0x1fff; if (guid == targetGuid) { int b0 = tsBuf.get() & 0xff; int counter = b0 & 0xf; if ((b0 & 0x20) != 0) { NIOUtils.skip(tsBuf, tsBuf.get() & 0xff); } dst.put(tsBuf); } } dst.flip(); readPESHeader(dst, 0); dst.limit(dst.position() + payloadSize); return dst; }
private static void dumpTSPackets(ReadableByteChannel _in) throws IOException { ByteBuffer buf = ByteBuffer.allocate(188 * 1024); while (_in.read(buf) != -1) { buf.flip(); buf.limit((buf.limit() / 188) * 188); int pmtPid = -1; for (int pkt = 0; buf.hasRemaining(); ++pkt) { ByteBuffer tsBuf = NIOUtils.read(buf, 188); Assert.assertEquals(0x47, tsBuf.get() & 0xff); int guidFlags = ((tsBuf.get() & 0xff) << 8) | (tsBuf.get() & 0xff); int guid = (int) guidFlags & 0x1fff; int payloadStart = (guidFlags >> 14) & 0x1; int b0 = tsBuf.get() & 0xff; int counter = b0 & 0xf; if ((b0 & 0x20) != 0) { NIOUtils.skip(tsBuf, (tsBuf.get() & 0xff)); } System.out.print( "#" + pkt + "[guid: " + guid + ", cnt: " + counter + ", start: " + (payloadStart == 1 ? "y" : "-")); if (guid == 0 || guid == pmtPid) { System.out.print(", PSI]: "); if (payloadStart == 1) { NIOUtils.skip(tsBuf, (tsBuf.get() & 0xff)); } if (guid == 0) { PATSection pat = PATSection.parsePAT(tsBuf); IntIntMap programs = pat.getPrograms(); pmtPid = programs.values()[0]; printPat(pat); } else if (guid == pmtPid) { PMTSection pmt = PMTSection.parsePMT(tsBuf); printPmt(pmt); } } else { System.out.print("]: " + tsBuf.remaining()); } System.out.println(); } buf.clear(); } }
protected void analyseBuffer(ByteBuffer buf, long pos) { while (buf.hasRemaining()) { ByteBuffer tsBuf = NIOUtils.read(buf, 188); pos += 188; Assert.assertEquals(0x47, tsBuf.get() & 0xff); int guidFlags = ((tsBuf.get() & 0xff) << 8) | (tsBuf.get() & 0xff); int guid = (int) guidFlags & 0x1fff; for (int i = 0; i < indexer.indexers.length; i++) { if (guid == indexer.indexers[i].targetGuid) { int payloadStart = (guidFlags >> 14) & 0x1; int b0 = tsBuf.get() & 0xff; int counter = b0 & 0xf; if ((b0 & 0x20) != 0) { NIOUtils.skip(tsBuf, tsBuf.get() & 0xff); } indexer.indexers[i].analyseBuffer(tsBuf, pos - tsBuf.remaining()); } } } }