Search in sources :

Example 1 with Pes

use of com.feeyo.mpeg2ts.Pes in project feeyo-hlsserver by variflight.

the class TsFileAnalyzer method initLayout.

public void initLayout() {
    final JTextField tsFileNameTextField = new JTextField();
    tsFileNameTextField.setPreferredSize(new Dimension(400, 35));
    tsFileNameTextField.setEnabled(false);
    Button tsLoadButton = new Button();
    tsLoadButton.setLabel("Load MPEG-TS files");
    tsLoadButton.addActionListener(new ActionListener() {

        String lastPath = prefs.get("last_path", "");

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser jfc = new JFileChooser(lastPath);
            jfc.setFileFilter(new FileFilter() {

                public boolean accept(File f) {
                    if (f.isDirectory())
                        return true;
                    else if (f.getName().endsWith(".ts"))
                        return true;
                    else
                        return false;
                }

                public String getDescription() {
                    return "ts files";
                }
            });
            if (jfc.showOpenDialog(TsFileAnalyzer.this) == JFileChooser.APPROVE_OPTION) {
                File f = jfc.getSelectedFile();
                prefs.put("last_path", f.getPath());
                try {
                    tsFileNameTextField.setText(f.getPath());
                    byte[] buff = Files.readAllBytes(f.toPath());
                    tsArray = tsReader.parseTsPacket(buff);
                    // 
                    rowData = new Object[tsArray.length][10];
                    for (int i = 0; i < tsArray.length; i++) {
                        Ts ts = tsArray[i];
                        rowData[i][0] = i;
                        if (ts instanceof Pes) {
                            Pes pes = (Pes) ts;
                            if (pes.stream_id == Ts.AUDIO_STREAM_ID) {
                                rowData[i][1] = "PES/(Audio)";
                            } else if (pes.stream_id == Ts.VIDEO_STREAM_ID) {
                                rowData[i][1] = "PES/(Video)";
                            } else {
                                rowData[i][1] = "PES";
                            }
                            rowData[i][7] = pes.adaptation_filed == null ? 0 : pes.adaptation_filed.getPCR();
                            rowData[i][8] = pes.pts;
                            rowData[i][9] = pes.dts;
                        } else {
                            if (ts instanceof Pat) {
                                rowData[i][1] = "PAT";
                            } else if (ts instanceof Pmt) {
                                rowData[i][1] = "PMT";
                            } else {
                                rowData[i][1] = "UNKNOW";
                            }
                            rowData[i][7] = 0;
                            rowData[i][8] = 0;
                            rowData[i][9] = 0;
                        }
                        rowData[i][2] = ts.PID;
                        rowData[i][3] = ts.payload_unit_start_indicator;
                        rowData[i][4] = ts.transport_scrambling_control;
                        rowData[i][5] = ts.adaptation_field_control;
                        rowData[i][6] = ts.continuity_counter;
                    }
                    TableModel dataModel = new DefaultTableModel(rowData, columnNames) {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public boolean isCellEditable(int row, int column) {
                            return false;
                        }
                    };
                    tsTable.setModel(dataModel);
                    for (int i = 0; i < colunmWidths.length; i++) {
                        tsTable.getColumnModel().getColumn(i).setPreferredWidth(colunmWidths[i]);
                    }
                    tsTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });
    tsTable = new JTable(rowData, columnNames);
    tsTable.setRowSelectionAllowed(true);
    tsTable.setColumnSelectionAllowed(false);
    tsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    tsTable.setBorder(BorderFactory.createLineBorder(Color.BLACK, 0));
    tsTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

        private static final long serialVersionUID = 2842108295158264818L;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            if (tsArray != null && row < tsArray.length) {
                Ts ts = tsArray[row];
                final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                c.setBackground(ts != null && ts instanceof Pes && ts.payload_unit_start_indicator == 1 ? new Color(255, 255, 200) : Color.WHITE);
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        }
    });
    tsTable.addMouseListener(new MouseAdapter() {

        private String toHex(int v) {
            String hex = "0x" + String.format("%2s", Integer.toHexString(v)).replace(' ', '0');
            return hex;
        }

        private String getTsString(Ts ts) {
            StringBuffer fieldBuffer = new StringBuffer(500);
            Class<?> clazz = ts.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                try {
                    Field field = fields[i];
                    field.setAccessible(true);
                    String name = field.getName();
                    Object value = field.get(ts);
                    // skip
                    if ("adaptation_filed".equals(name)) {
                        continue;
                    }
                    if ("program_map_PID".equals(name) && value != null) {
                        int[] pmtIds = (int[]) value;
                        for (int j = 0; j < pmtIds.length; j++) {
                            fieldBuffer.append("program_map_PID=").append(pmtIds[j]).append("\r\n");
                        }
                    } else if ("descriptors".equals(name) && value != null) {
                        fieldBuffer.append("\r\n");
                        fieldBuffer.append("descriptors\r\n");
                        @SuppressWarnings("unchecked") List<Pmt.Descriptor> descriptors = (List<Pmt.Descriptor>) value;
                        for (Pmt.Descriptor descriptor : descriptors) {
                            fieldBuffer.append("tag=").append(descriptor.tag).append("\r\n");
                            fieldBuffer.append("content=").append(new String(descriptor.rawData)).append("\r\n");
                            fieldBuffer.append("\r\n");
                        }
                    } else if ("streams".equals(name) && value != null) {
                        fieldBuffer.append("\r\n");
                        fieldBuffer.append("streams\r\n");
                        @SuppressWarnings("unchecked") List<Pmt.Stream> streams = (List<Pmt.Stream>) value;
                        for (Pmt.Stream stream : streams) {
                            fieldBuffer.append("stream_type=").append(stream.stream_type).append("\r\n");
                            fieldBuffer.append("elementary_PID=").append(stream.elementary_PID).append("\r\n");
                            fieldBuffer.append("descriptor=").append(stream.descriptor).append("\r\n");
                            fieldBuffer.append("ES_info_length=").append(stream.ES_info_length).append("\r\n");
                            fieldBuffer.append("reserved5=").append(stream.reserved5).append("\r\n");
                            fieldBuffer.append("reserved6=").append(stream.reserved6).append("\r\n");
                            fieldBuffer.append("\r\n");
                        }
                    } else if ("es_data".equals(name) && value != null) {
                        byte[] bb = (byte[]) value;
                        String esHexString = TsUtil.hexString(bb, 0, bb.length);
                        fieldBuffer.append("\r\n");
                        fieldBuffer.append("ES_data \r\n");
                        fieldBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                        fieldBuffer.append(esHexString);
                        fieldBuffer.append("\r\n");
                    } else {
                        if (value != null) {
                            fieldBuffer.append(name).append("=").append(value).append("\r\n");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return fieldBuffer.toString();
        }

        public void mousePressed(MouseEvent me) {
            if (me.getClickCount() == 1) {
                JTable table = (JTable) me.getSource();
                int rowIndex = table.getSelectedRow();
                int columnIndex = 0;
                Object obj = table.getModel().getValueAt(rowIndex, columnIndex);
                if (obj != null && tsArray != null) {
                    int idx = (int) obj;
                    Ts ts = tsArray[idx];
                    String hexFullStr = TsUtil.hexString(ts.full_data, 0, ts.full_data.length);
                    // 
                    StringBuffer tsStrBuffer = new StringBuffer(500);
                    // ts header info
                    tsStrBuffer.append("\r\n");
                    tsStrBuffer.append("TS_header\r\n");
                    tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                    tsStrBuffer.append("sync_byte=").append(toHex(ts.sync_byte)).append("\r\n");
                    tsStrBuffer.append("transport_error_indicator=").append(ts.transport_error_indicator).append("\r\n");
                    tsStrBuffer.append("payload_unit_start_indicator=").append(ts.payload_unit_start_indicator).append("\r\n");
                    tsStrBuffer.append("transport_priority=").append(ts.transport_priority).append("\r\n");
                    tsStrBuffer.append("PID=").append(ts.PID).append("\r\n");
                    tsStrBuffer.append("transport_scrambling_control=").append(ts.transport_scrambling_control).append("\r\n");
                    tsStrBuffer.append("adaptation_field_control=").append(ts.adaptation_field_control).append("\r\n");
                    tsStrBuffer.append("continuity_counter=").append(ts.continuity_counter).append("\r\n");
                    tsStrBuffer.append("\r\n");
                    if (ts instanceof Pat) {
                        tsStrBuffer.append("PAT_header\r\n");
                        tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                        tsStrBuffer.append(getTsString(ts));
                    } else if (ts instanceof Pmt) {
                        tsStrBuffer.append("PMT_header\r\n");
                        tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                        tsStrBuffer.append(getTsString(ts));
                    } else if (ts instanceof Pes) {
                        Pes pes = (Pes) ts;
                        if (pes.adaptation_filed != null) {
                            tsStrBuffer.append("Adaptation_filed\r\n");
                            tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                            tsStrBuffer.append("discontinuity_indicator=").append(pes.adaptation_filed.discontinuity_indicator).append("\r\n");
                            tsStrBuffer.append("random_access_indicator=").append(pes.adaptation_filed.random_access_indicator).append("\r\n");
                            tsStrBuffer.append("elementary_stream_priority_indicator=").append(pes.adaptation_filed.elementary_stream_priority_indicator).append("\r\n");
                            tsStrBuffer.append("pcr_flag=").append(pes.adaptation_filed.pcr_flag).append("\r\n");
                            tsStrBuffer.append("opcr_flag=").append(pes.adaptation_filed.opcr_flag).append("\r\n");
                            tsStrBuffer.append("splicing_point_flag=").append(pes.adaptation_filed.splicing_point_flag).append("\r\n");
                            tsStrBuffer.append("transport_private_data_flag=").append(pes.adaptation_filed.transport_private_data_flag).append("\r\n");
                            tsStrBuffer.append("adaptation_field_extension_flag=").append(pes.adaptation_filed.adaptation_field_extension_flag).append("\r\n");
                            tsStrBuffer.append("program_clock_reference_base=").append(pes.adaptation_filed.program_clock_reference_base).append("\r\n");
                            tsStrBuffer.append("program_clock_reference_extension=").append(pes.adaptation_filed.program_clock_reference_extension).append("\r\n");
                            tsStrBuffer.append("PCR=").append(pes.adaptation_filed.getPCR()).append("\r\n");
                            tsStrBuffer.append("PCR_TIME=").append(pes.adaptation_filed.getPCR_TIME()).append("\r\n");
                            tsStrBuffer.append("\r\n");
                        }
                        if (pes.payload_unit_start_indicator == 0) {
                            byte[] bb = pes.es_data;
                            String esHexString = TsUtil.hexString(bb, 0, bb.length);
                            tsStrBuffer.append("\r\n");
                            tsStrBuffer.append("ES_data \r\n");
                            tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                            tsStrBuffer.append(esHexString);
                            tsStrBuffer.append("\r\n");
                        } else {
                            tsStrBuffer.append("PES_header\r\n");
                            tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                            tsStrBuffer.append(getTsString(ts));
                        }
                    }
                    tsStrBuffer.append("\r\n");
                    tsStrBuffer.append("HEX \r\n");
                    tsStrBuffer.append("+++++++++++++++++++++++++++++++++++++\r\n");
                    tsStrBuffer.append(hexFullStr);
                    tsStrBuffer.append("\r\n");
                    mpegTsTextArea.setText(tsStrBuffer.toString());
                }
            }
        }
    });
    // 主布局
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    // 顶部布局
    JPanel topPanel = new JPanel();
    // new GridLayout(0, 2)
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
    topPanel.add(tsFileNameTextField);
    topPanel.add(tsLoadButton);
    mpegTsPanel = new JPanel();
    mpegTsPanel.setLayout(new BorderLayout(2, 1));
    mpegTsPanel.setBackground(Color.WHITE);
    mpegTsPanel.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    mpegTsTextArea = new JTextArea();
    mpegTsTextArea.setFont(new Font("monospaced", Font.PLAIN, 12));
    mpegTsTextArea.setDoubleBuffered(true);
    mpegTsPanel.add(mpegTsTextArea);
    // split panel
    // +++++++++++++++++++++++++++++++++++++++++
    JScrollPane topJScrollPane = new JScrollPane(tsTable);
    topJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JScrollPane bottomPane = new JScrollPane(mpegTsPanel);
    bottomPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JSplitPane upDownSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createPanelForComponent(topJScrollPane), createPanelForComponent(bottomPane));
    upDownSplitPane.setOneTouchExpandable(true);
    upDownSplitPane.setDividerLocation(300);
    mainPanel.add(upDownSplitPane);
    // +++++++++++++++++++++++++++++++++++++++++
    this.add(topPanel, BorderLayout.NORTH);
    this.add(mainPanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("MPEG-TS Analyzer 0.1alpha");
    this.setResizable(false);
    this.setPreferredSize(new Dimension(800, 680));
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
        }
    });
}
Also used : JPanel(javax.swing.JPanel) Pat(com.feeyo.mpeg2ts.Pat) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) DefaultTableModel(javax.swing.table.DefaultTableModel) BoxLayout(javax.swing.BoxLayout) WindowAdapter(java.awt.event.WindowAdapter) JTextField(javax.swing.JTextField) Font(java.awt.Font) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTextField(javax.swing.JTextField) Field(java.lang.reflect.Field) BorderLayout(java.awt.BorderLayout) Button(java.awt.Button) List(java.util.List) FileFilter(javax.swing.filechooser.FileFilter) JComponent(javax.swing.JComponent) Component(java.awt.Component) Ts(com.feeyo.mpeg2ts.Ts) JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) Color(java.awt.Color) MouseAdapter(java.awt.event.MouseAdapter) Dimension(java.awt.Dimension) IOException(java.io.IOException) IOException(java.io.IOException) ActionListener(java.awt.event.ActionListener) JFileChooser(javax.swing.JFileChooser) Pmt(com.feeyo.mpeg2ts.Pmt) JTable(javax.swing.JTable) WindowEvent(java.awt.event.WindowEvent) JSplitPane(javax.swing.JSplitPane) File(java.io.File) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) Pes(com.feeyo.mpeg2ts.Pes)

Example 2 with Pes

use of com.feeyo.mpeg2ts.Pes in project feeyo-hlsserver by variflight.

the class TsWriterTest method main.

public static void main(String[] args) {
    byte[] buf1 = readTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/ec6d6ac2-283c-4f86-bbb3-ea4e03a214bf.ts");
    // 
    TsReader tsReader = new TsReader();
    int frameLength = 0;
    int audioFrameLength = 0;
    int videoFrameLength = 0;
    Ts[] tsPackets = tsReader.parseTsPacket(buf1);
    for (int i = 0; i < tsPackets.length; i++) {
        Ts ts = tsPackets[i];
        if (ts instanceof Pes) {
            Pes pes = (Pes) ts;
            if (pes.payload_unit_start_indicator == 1) {
                frameLength++;
                if (pes.stream_id == Ts.AUDIO_STREAM_ID) {
                    audioFrameLength++;
                } else {
                    videoFrameLength++;
                }
            }
        }
    }
    // 
    FrameData[] frames = new FrameData[frameLength];
    int frameIdx = 0;
    int currentFrameIdx = 0;
    for (int i = 0; i < tsPackets.length; i++) {
        Ts ts = tsPackets[i];
        if (ts instanceof Pes) {
            Pes pes = (Pes) ts;
            if (pes.payload_unit_start_indicator == 1) {
                currentFrameIdx = frameIdx;
                frames[currentFrameIdx] = new FrameData();
                frames[currentFrameIdx].buf = append(frames[currentFrameIdx].buf, pes.es_data, 0, pes.es_data.length);
                frames[currentFrameIdx].dts = pes.dts;
                frames[currentFrameIdx].pts = pes.pts;
                frames[currentFrameIdx].isAudio = (pes.stream_id == Ts.AUDIO_STREAM_ID);
                frameIdx++;
            } else {
                frames[currentFrameIdx].buf = append(frames[currentFrameIdx].buf, pes.es_data, 0, pes.es_data.length);
            }
        }
    }
    TsWriter tsWriter1 = new TsWriter();
    byte[] tsFileBuf = tsWriter1.write(true, FrameDataType.MIXED, frames);
    // System.out.println( bytesToHexString( tsFileBuf, 0, tsFileBuf.length));
    // mix
    writeTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/test1.ts", tsFileBuf);
    int audioFrameIndex = 0;
    FrameData[] audioFrames = new FrameData[audioFrameLength];
    int videoFrameIndex = 0;
    FrameData[] videoFrames = new FrameData[videoFrameLength];
    for (int i = 0; i < frames.length; i++) {
        FrameData frame = frames[i];
        if (frame.isAudio) {
            audioFrames[audioFrameIndex] = frame;
            audioFrameIndex++;
        } else {
            videoFrames[videoFrameIndex] = frame;
            videoFrameIndex++;
        }
    }
    TsWriter tsWriter2 = new TsWriter();
    // long lastPts = 90000;
    // for(int i = 0; i < audioFrames.length; i++) {
    // 
    // long pts =  (90000L * 1024) / 8000;
    // AvStreamFrame frame = audioFrames[i];
    // lastPts += pts;
    // frame.pts = lastPts;
    // }
    byte[] tsFileBuf2 = tsWriter2.write(true, FrameDataType.AUDIO, audioFrames);
    writeTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/test2.ts", tsFileBuf2);
    TsWriter tsWriter3 = new TsWriter();
    byte[] tsFileBuf3 = tsWriter3.write(true, FrameDataType.VIDEO, videoFrames);
    writeTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/test3.ts", tsFileBuf3);
}
Also used : TsReader(com.feeyo.mpeg2ts.TsReader) TsWriter(com.feeyo.mpeg2ts.TsWriter) Ts(com.feeyo.mpeg2ts.Ts) Pes(com.feeyo.mpeg2ts.Pes) FrameData(com.feeyo.mpeg2ts.TsWriter.FrameData)

Example 3 with Pes

use of com.feeyo.mpeg2ts.Pes in project feeyo-hlsserver by variflight.

the class TsWriterTest2 method main.

public static void main(String[] args) {
    byte[] buf1 = readTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/10844.ts");
    // 
    TsReader tsReader = new TsReader();
    int frameLength = 0;
    Ts[] tsPackets = tsReader.parseTsPacket(buf1);
    for (int i = 0; i < tsPackets.length; i++) {
        Ts ts = tsPackets[i];
        if (ts instanceof Pes) {
            Pes pes = (Pes) ts;
            if (pes.payload_unit_start_indicator == 1) {
                frameLength++;
            }
        }
    }
    // 
    FrameData[] frames = new FrameData[frameLength];
    int frameIdx = 0;
    int currentFrameIdx = 0;
    for (int i = 0; i < tsPackets.length; i++) {
        Ts ts = tsPackets[i];
        if (ts instanceof Pes) {
            Pes pes = (Pes) ts;
            if (pes.payload_unit_start_indicator == 1) {
                currentFrameIdx = frameIdx;
                frames[currentFrameIdx] = new FrameData();
                frames[currentFrameIdx].buf = append(frames[currentFrameIdx].buf, pes.es_data, 0, pes.es_data.length);
                frames[currentFrameIdx].dts = pes.dts;
                frames[currentFrameIdx].pts = pes.pts;
                frames[currentFrameIdx].isAudio = (pes.stream_id == Ts.AUDIO_STREAM_ID);
                frameIdx++;
            } else {
                frames[currentFrameIdx].buf = append(frames[currentFrameIdx].buf, pes.es_data, 0, pes.es_data.length);
            }
        }
    }
    TsWriter tsWriter1 = new TsWriter();
    byte[] tsFileBuf = tsWriter1.write(true, FrameDataType.AUDIO, frames);
    // mix
    writeTsFile("/Users/zhuam/git/feeyo/feeyostreamhls/testdata/test222.ts", tsFileBuf);
}
Also used : TsReader(com.feeyo.mpeg2ts.TsReader) TsWriter(com.feeyo.mpeg2ts.TsWriter) Ts(com.feeyo.mpeg2ts.Ts) Pes(com.feeyo.mpeg2ts.Pes) FrameData(com.feeyo.mpeg2ts.TsWriter.FrameData)

Aggregations

Pes (com.feeyo.mpeg2ts.Pes)3 Ts (com.feeyo.mpeg2ts.Ts)3 TsReader (com.feeyo.mpeg2ts.TsReader)2 TsWriter (com.feeyo.mpeg2ts.TsWriter)2 FrameData (com.feeyo.mpeg2ts.TsWriter.FrameData)2 Pat (com.feeyo.mpeg2ts.Pat)1 Pmt (com.feeyo.mpeg2ts.Pmt)1 BorderLayout (java.awt.BorderLayout)1 Button (java.awt.Button)1 Color (java.awt.Color)1 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 File (java.io.File)1