Search in sources :

Example 1 with AacH264MixedTsSegmenter

use of com.feeyo.hls.ts.segmenter.AacH264MixedTsSegmenter 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)

Example 2 with AacH264MixedTsSegmenter

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

the class AacH264MixedTsEncoderTest method main.

public static void main(String[] args) {
    byte[] videoData = TestDataUtil.getVideoData();
    ArrayDeque<AvFrame> videoDeque = new ArrayDeque<AvFrame>();
    // 丢弃最后不足2048的语音部分
    long avcCounter = 0;
    for (int i = 0; i <= videoData.length - 2048; i = i + 2048) {
        byte[] dest = new byte[2048];
        System.arraycopy(videoData, i, dest, 0, 2048);
        videoDeque.offer(new AvFrame(dest, 0, avcCounter++));
    }
    byte[] audioData = TestDataUtil.getAudioData();
    long aacCounter = 0;
    ArrayDeque<AvFrame> audioDeque = new ArrayDeque<AvFrame>();
    // 丢弃最后不足2048的语音部分
    for (int k = 0; k <= audioData.length - 2048; k = k + 2048) {
        byte[] dest = new byte[2048];
        System.arraycopy(audioData, k, dest, 0, 2048);
        audioDeque.offer(new AvFrame(dest, 1, aacCounter++));
    }
    List<AvFrame> avList = new ArrayList<AvFrame>();
    // Random
    Random rand = new Random();
    while (!audioDeque.isEmpty() || !videoDeque.isEmpty()) {
        int randNum = rand.nextInt(100) + 1;
        if (randNum > 50) {
            if (!audioDeque.isEmpty()) {
                avList.add(audioDeque.pop());
            } else {
                while (!videoDeque.isEmpty()) avList.add(videoDeque.pop());
            }
        } else {
            if (!videoDeque.isEmpty()) {
                avList.add(videoDeque.pop());
            } else {
                while (!audioDeque.isEmpty()) avList.add(audioDeque.pop());
            }
        }
    }
    AbstractTsSegmenter tsSegmenter = new AacH264MixedTsSegmenter();
    tsSegmenter.initialize(8000, 16, 1, 25);
    for (AvFrame avFrame : avList) {
        if (avFrame.type == 0) {
            tsSegmenter.getTsBuf(V5PacketType.H264_STREAM, avFrame.payload, ByteUtil.longToBytes(avFrame.index));
        } else if (avFrame.type == 1) {
            tsSegmenter.getTsBuf(V5PacketType.AAC_STREAM, avFrame.payload, ByteUtil.longToBytes(avFrame.index));
        } else {
            System.out.println("## ERROR!");
        }
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) AacH264MixedTsSegmenter(com.feeyo.hls.ts.segmenter.AacH264MixedTsSegmenter) ArrayDeque(java.util.ArrayDeque) AbstractTsSegmenter(com.feeyo.hls.ts.segmenter.AbstractTsSegmenter)

Aggregations

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