Search in sources :

Example 1 with MTSPacket

use of org.taktik.mpegts.MTSPacket 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);
    }
}
Also used : MTSPacket(org.taktik.mpegts.MTSPacket) ResettableMTSSource(org.taktik.mpegts.sources.ResettableMTSSource)

Example 2 with MTSPacket

use of org.taktik.mpegts.MTSPacket 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));
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) MTSPacket(org.taktik.mpegts.MTSPacket) DatagramPacket(io.netty.channel.socket.DatagramPacket) ResettableMTSSource(org.taktik.mpegts.sources.ResettableMTSSource) Test(org.junit.Test)

Example 3 with MTSPacket

use of org.taktik.mpegts.MTSPacket 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();
}
Also used : MTSPacket(org.taktik.mpegts.MTSPacket) ResettableMTSSource(org.taktik.mpegts.sources.ResettableMTSSource) SecurityServiceException(ddf.security.service.SecurityServiceException) IOException(java.io.IOException)

Example 4 with MTSPacket

use of org.taktik.mpegts.MTSPacket in project alliance by codice.

the class MpegTsDecoderTest method testRead.

@Test
public void testRead() {
    MTSUtils.StreamType streamType = MTSUtils.StreamType.VIDEO_H264;
    byte expectedByte1 = 0x01;
    byte expectedByte2 = 0x02;
    byte expectedByte3 = 0x03;
    byte expectedByte4 = 0x04;
    int programMapTableId = 1;
    int videoPacketId = 2;
    MpegTsDecoderImpl decoder = new MpegTsDecoderImpl();
    PATSection patSection = mock(PATSection.class);
    when(patSection.getPrograms()).thenReturn(Collections.singletonMap(1, programMapTableId));
    MpegTsDecoderImpl.PATSectionParser patSectionParser = mock(MpegTsDecoderImpl.PATSectionParser.class);
    when(patSectionParser.parse(any())).thenReturn(patSection);
    decoder.setPatSectionParser(patSectionParser);
    MTSPacket programAssociationTablePacket = mock(MTSPacket.class);
    when(programAssociationTablePacket.getPid()).thenReturn(Constants.PROGRAM_ASSOCIATION_TABLE_PID);
    when(programAssociationTablePacket.isPayloadUnitStartIndicator()).thenReturn(true);
    when(programAssociationTablePacket.getPayload()).thenReturn(ByteBuffer.wrap(new byte[] { 0x00 }));
    PMTSection.PMTStream pmtStream = mock(PMTSection.PMTStream.class);
    when(pmtStream.getStreamType()).thenReturn(streamType);
    when(pmtStream.getPid()).thenReturn(videoPacketId);
    PMTSection pmtSection = mock(PMTSection.class);
    when(pmtSection.getStreams()).thenReturn(new PMTSection.PMTStream[] { pmtStream });
    MpegTsDecoderImpl.PMTSectionParser pmtSectionParser = mock(MpegTsDecoderImpl.PMTSectionParser.class);
    when(pmtSectionParser.parse(any())).thenReturn(pmtSection);
    decoder.setPmtSectionParser(pmtSectionParser);
    MTSPacket programMapTablePacket = mock(MTSPacket.class);
    when(programMapTablePacket.getPid()).thenReturn(programMapTableId);
    when(programMapTablePacket.isPayloadUnitStartIndicator()).thenReturn(true);
    when(programMapTablePacket.getPayload()).thenReturn(ByteBuffer.wrap(new byte[] { 0x00 }));
    MTSPacket elementaryStreamPacket1 = createElementary(true, videoPacketId, expectedByte1);
    MTSPacket elementaryStreamPacket2 = createElementary(false, videoPacketId, expectedByte2);
    MTSPacket elementaryStreamPacket3 = createElementary(false, videoPacketId, expectedByte3);
    MTSPacket elementaryStreamPacket4 = createElementary(false, videoPacketId, expectedByte4);
    MTSPacket elementaryStreamPacket5 = createElementary(true, videoPacketId, (byte) 0x00);
    List<Object> outputList = new LinkedList<>();
    Stream.of(programAssociationTablePacket, programMapTablePacket, elementaryStreamPacket1, elementaryStreamPacket2, elementaryStreamPacket3, elementaryStreamPacket4, elementaryStreamPacket5).forEach(mtsPacket -> {
        try {
            decoder.read(mtsPacket, outputList::add);
        } catch (IOException e) {
            fail();
        }
    });
    assertThat(outputList, hasSize(1));
    assertThat(outputList.get(0), is(instanceOf(PESPacket.class)));
    PESPacket pesPacket = (PESPacket) outputList.get(0);
    assertThat(pesPacket.getPacketId(), is(videoPacketId));
    assertThat(pesPacket.getStreamType(), is(MpegStreamType.lookup(streamType)));
    assertThat(pesPacket.getPayload(), is(new byte[] { expectedByte1, expectedByte2, expectedByte3, expectedByte4 }));
}
Also used : MTSUtils(org.jcodec.containers.mps.MTSUtils) IOException(java.io.IOException) LinkedList(java.util.LinkedList) MTSPacket(org.taktik.mpegts.MTSPacket) PATSection(org.taktik.mpegts.PATSection) PMTSection(org.jcodec.containers.mps.psi.PMTSection) Test(org.junit.Test)

Example 5 with MTSPacket

use of org.taktik.mpegts.MTSPacket in project alliance by codice.

the class MpegTsDecoderTest method createElementary.

private MTSPacket createElementary(boolean isStart, int pid, byte data) {
    MTSPacket elementaryStreamPacket = mock(MTSPacket.class);
    when(elementaryStreamPacket.getPid()).thenReturn(pid);
    when(elementaryStreamPacket.isPayloadUnitStartIndicator()).thenReturn(isStart);
    when(elementaryStreamPacket.getPayload()).thenReturn(ByteBuffer.wrap(new byte[] { data }));
    when(elementaryStreamPacket.isContainsPayload()).thenReturn(true);
    return elementaryStreamPacket;
}
Also used : MTSPacket(org.taktik.mpegts.MTSPacket)

Aggregations

MTSPacket (org.taktik.mpegts.MTSPacket)7 Test (org.junit.Test)4 ResettableMTSSource (org.taktik.mpegts.sources.ResettableMTSSource)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 IOException (java.io.IOException)3 SecurityServiceException (ddf.security.service.SecurityServiceException)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1 InputStream (java.io.InputStream)1 LinkedList (java.util.LinkedList)1 MpegTsDecoder (org.codice.alliance.libs.mpegts.MpegTsDecoder)1 MTSUtils (org.jcodec.containers.mps.MTSUtils)1 PMTSection (org.jcodec.containers.mps.psi.PMTSection)1 PATSection (org.taktik.mpegts.PATSection)1