Search in sources :

Example 1 with H264TsSegmenter

use of com.feeyo.hls.ts.segmenter.H264TsSegmenter in project feeyo-hlsserver by variflight.

the class TsEncoderTest method main.

public static void main(String[] args) {
    // AacTsSegmenter tsSegmenter = new AacTsSegmenter();
    // byte[] data = TestDataUtil.getAudioData();
    H264TsSegmenter tsSegmenter = new H264TsSegmenter();
    byte[] data = TestDataUtil.getVideoData();
    List<byte[]> dataList = new ArrayList<byte[]>();
    // 丢弃最后不足2048的语音部分
    for (int i = 0; i <= data.length - 2048; i = i + 2048) {
        byte[] dest = new byte[2048];
        System.arraycopy(data, i, dest, 0, 2048);
        dataList.add(dest);
    }
    while (true) {
        for (byte[] media : dataList) {
            tsSegmenter.getTsBuf(V5PacketType.H264_STREAM, media, null);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) H264TsSegmenter(com.feeyo.hls.ts.segmenter.H264TsSegmenter)

Example 2 with H264TsSegmenter

use of com.feeyo.hls.ts.segmenter.H264TsSegmenter in project feeyo-hlsserver by variflight.

the class AdsMagr method initialize.

// 
public static void initialize() {
    String adsPath = HlsCtx.INSTANCE().getHomePath() + File.separator + "ads";
    File adsDirectory = new File(adsPath);
    if (adsDirectory.exists() && adsDirectory.isDirectory()) {
        List<AdsCfg> adsCfgs = HlsCtx.INSTANCE().getAdsCfgs();
        for (AdsCfg adsCfg : adsCfgs) {
            String md5 = Md5.md5_32(adsCfg.getType() + adsCfg.getSampleRate() + adsCfg.getSampleSizeInBits() + adsCfg.getChannels() + adsCfg.getFps());
            String filePath = adsPath + File.separator + adsCfg.getName();
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStream in = null;
                try {
                    in = new FileInputStream(file);
                    byte[] adRawData = new byte[(int) file.length()];
                    in.read(adRawData, 0, adRawData.length);
                    switch(adsCfg.getType()) {
                        case "audio":
                            AacTsSegmenter aacTsSegmenter = new AacTsSegmenter();
                            aacTsSegmenter.initialize(adsCfg.getSampleRate(), adsCfg.getSampleSizeInBits(), adsCfg.getChannels(), adsCfg.getFps());
                            // cache aac ts
                            if (adRawData != null) {
                                List<TsSegment> aacTsSegs = new ArrayList<TsSegment>();
                                aacTsSegmenter = new AacTsSegmenter();
                                // 3 : TS_PES_AU_NUM
                                aacTsSegmenter.setPts(aacTsSegmenter.getPtsIncPerFrame() * 3);
                                int rawDataPtr = 0;
                                int tsNum = aacTsSegmenter.calcTsNum(adRawData.length);
                                byte[] frameBuf = new byte[1024];
                                for (int i = 0; i < tsNum; i++) {
                                    if (rawDataPtr + frameBuf.length < adRawData.length) {
                                        System.arraycopy(adRawData, rawDataPtr, frameBuf, 0, frameBuf.length);
                                        rawDataPtr += frameBuf.length;
                                    } else if (rawDataPtr < adRawData.length) {
                                        Arrays.fill(frameBuf, (byte) 0);
                                        System.arraycopy(adRawData, rawDataPtr, frameBuf, 0, adRawData.length - rawDataPtr);
                                        rawDataPtr += frameBuf.length;
                                    } else {
                                        frameBuf = FaacUtils.ZERO_PCM_DATA;
                                    }
                                    byte[] tsSegment = aacTsSegmenter.getTsBuf(V5PacketType.AAC_STREAM, frameBuf, null);
                                    if (tsSegment != null) {
                                        aacTsSegs.add(new TsSegment((i + 1) + ".ts", tsSegment, aacTsSegmenter.getTsSegTime(), true));
                                    }
                                }
                                adsSegs.put(md5, aacTsSegs);
                            }
                            break;
                        case "video":
                            H264TsSegmenter h264TsSegmenter = new H264TsSegmenter();
                            h264TsSegmenter.initialize(adsCfg.getSampleRate(), adsCfg.getSampleSizeInBits(), adsCfg.getChannels(), adsCfg.getFps());
                            // cache h264 ts
                            if (adRawData != null) {
                                List<TsSegment> h264TsSegs = new ArrayList<TsSegment>();
                                h264TsSegmenter = new H264TsSegmenter();
                                int index = 0;
                                int ptr = 0;
                                while (ptr < adRawData.length) {
                                    int len = ptr + 2048 < adRawData.length ? 2048 : adRawData.length - ptr;
                                    byte[] dest = new byte[len];
                                    System.arraycopy(adRawData, ptr, dest, 0, len);
                                    byte[] tsSegment = h264TsSegmenter.getTsBuf(V5PacketType.H264_STREAM, dest, null);
                                    if (tsSegment != null)
                                        h264TsSegs.add(new TsSegment((++index) + ".ts", tsSegment, h264TsSegmenter.getTsSegTime(), true));
                                    ptr += 2048;
                                }
                                adsSegs.put(md5, h264TsSegs);
                            }
                            break;
                        case "mixed":
                            AacH264MixedTsSegmenter mixedTsSegmenter = new AacH264MixedTsSegmenter();
                            mixedTsSegmenter.initialize(adsCfg.getSampleRate(), adsCfg.getSampleSizeInBits(), adsCfg.getChannels(), adsCfg.getFps());
                            if (adRawData != null) {
                                List<TsSegment> mixedTsSegs = new ArrayList<TsSegment>();
                                // TODO segment
                                adsSegs.put(md5, mixedTsSegs);
                            }
                            break;
                        default:
                            continue;
                    }
                } catch (Exception e) {
                    LOGGER.error(e.getMessage());
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
        }
    }
}
Also used : AacTsSegmenter(com.feeyo.hls.ts.segmenter.AacTsSegmenter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AacH264MixedTsSegmenter(com.feeyo.hls.ts.segmenter.AacH264MixedTsSegmenter) IOException(java.io.IOException) TsSegment(com.feeyo.hls.ts.TsSegment) H264TsSegmenter(com.feeyo.hls.ts.segmenter.H264TsSegmenter) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) AdsCfg(com.feeyo.cfg.AdsCfg) File(java.io.File)

Aggregations

H264TsSegmenter (com.feeyo.hls.ts.segmenter.H264TsSegmenter)2 ArrayList (java.util.ArrayList)2 AdsCfg (com.feeyo.cfg.AdsCfg)1 TsSegment (com.feeyo.hls.ts.TsSegment)1 AacH264MixedTsSegmenter (com.feeyo.hls.ts.segmenter.AacH264MixedTsSegmenter)1 AacTsSegmenter (com.feeyo.hls.ts.segmenter.AacTsSegmenter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1