use of org.taktik.mpegts.sources.ResettableMTSSource in project ddf by codice.
the class MpegTransportStreamMetadataExtractor method extractTransportStreamMetadata.
private void extractTransportStreamMetadata(final BiConsumer<Integer, byte[]> callback) throws Exception {
final ResettableMTSSource source = MTSSources.from(byteSource);
getProgramSpecificInformation(source);
source.reset();
MTSValidPacketIterator packetIterator = new MTSValidPacketIterator(source);
MTSPacket transportStreamPacket = packetIterator.getNextValidPacket();
try {
if (transportStreamPacket != null) {
LOGGER.debug("First valid packet found after {} failures", packetIterator.getPacketsFailed());
}
while (transportStreamPacket != null) {
final int packetId = transportStreamPacket.getPid();
if (isElementaryStreamPacket(packetId)) {
handleElementaryStreamPacket(transportStreamPacket, packetId, callback);
}
transportStreamPacket = packetIterator.getNextValidPacket();
}
} finally {
LOGGER.debug("Mpegts Packet Processing Complete: Total Processed {}, Total Failed: {}", packetIterator.getPacketsProcessed(), packetIterator.getPacketsFailed());
handleLastPacketOfEachStream(callback);
}
}
use of org.taktik.mpegts.sources.ResettableMTSSource in project alliance by codice.
the class RawUdpDataToMTSPacketDecoderTest method testCorruptedData.
@SuppressWarnings("unchecked")
@Test
public void testCorruptedData() throws Exception {
RawUdpDataToMTSPacketDecoder.MTSParser mtsParser = mock(RawUdpDataToMTSPacketDecoder.MTSParser.class);
ResettableMTSSource resettable = mock(ResettableMTSSource.class);
Mockito.when(mtsParser.parse(Mockito.any())).thenReturn(resettable);
MTSPacket mtsPacket = mock(MTSPacket.class);
Mockito.when(resettable.nextPacket()).thenThrow(RuntimeException.class).thenReturn(mtsPacket);
int packetCount = 2;
List<DatagramPacket> datagramPackets = toDatagrams(flatten(createTsPackets(packetCount)));
PacketBuffer packetBuffer = mock(PacketBuffer.class);
RawUdpDataToMTSPacketDecoder rawUdpDataToMTSPacketDecoder = new RawUdpDataToMTSPacketDecoder(packetBuffer, mock(UdpStreamProcessor.class));
rawUdpDataToMTSPacketDecoder.setMtsParser(mtsParser);
EmbeddedChannel channel = new EmbeddedChannel(rawUdpDataToMTSPacketDecoder);
datagramPackets.forEach(channel::writeInbound);
List<Object> outputList = NettyUtility.read(channel);
assertThat(outputList, hasSize(packetCount - 1));
}
use of org.taktik.mpegts.sources.ResettableMTSSource in project alliance by codice.
the class RawUdpDataToMTSPacketDecoder method parseMpegTsPacket.
/**
* Attempt to parse the first {@link #TS_PACKET_SIZE} bytes from the ByteBuf. If the parsing
* succeeds, then add the new mpeg-ts packet to the output list and add the raw bytes to the
* packet buffer. If parsing fails, then rewind the read operation on the ByteBuf and discard the
* first byte of the ByteBuf, which was a potential sync byte. In either case, skip to the next
* sync byte.
*
* <p>Note: {@link ResettableMTSSource#nextPacket()} can throw unchecked exceptions when parsing
* fails.
*
* @param outputList write parsed mpeg-ts packets to this list
*/
private void parseMpegTsPacket(List<Object> outputList) {
byte[] payload = new byte[TS_PACKET_SIZE];
byteBuf.markReaderIndex();
byteBuf.readBytes(payload);
MTSPacket packet = null;
try {
ResettableMTSSource src = mtsParser.parse(ByteSource.wrap(payload));
packet = src.nextPacket();
} catch (Exception e) {
LOGGER.debug("unable to parse mpeg-ts packet", e);
byteBuf.resetReaderIndex();
byteBuf.skipBytes(1);
}
if (packet != null) {
packetBuffer.write(payload);
outputList.add(packet);
}
skipToSyncByte();
}
use of org.taktik.mpegts.sources.ResettableMTSSource in project alliance by codice.
the class H262Test method testIDRFrameCount.
@Test
public void testIDRFrameCount() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new MTSPacketToPESPacketDecoder(), new PESPacketToApplicationDataDecoder(), new DecodedStreamDataHandler(packetBuffer));
InputStream inputStream = getInputStream("/Closed_Caption_EIA_MPEG2.ts");
byte[] buffer = new byte[TS_SIZE];
int c;
while ((c = inputStream.read(buffer)) != -1) {
if (c == TS_SIZE) {
ResettableMTSSource src = MTSSources.from(ByteSource.wrap(buffer));
MTSPacket packet = null;
try {
packet = src.nextPacket();
} catch (IOException e) {
LOGGER.debug("unable to parse mpegst packet", e);
}
if (packet != null) {
channel.writeInbound(packet);
}
}
}
verify(packetBuffer, times(37)).frameComplete(PacketBuffer.FrameType.IDR);
}
Aggregations