Search in sources :

Example 6 with TsSegment

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

the class M3u8Builder method generateM3u8.

public M3U8 generateM3u8(long m3u8Seq, List<TsSegment> tsSegments) {
    int maxTsDuration = TS_DURATION;
    int disCountinuityEndPos = -1;
    for (int i = 0; i < tsSegments.size(); i++) {
        TsSegment ts = tsSegments.get(i);
        if (ts.isDiscontinue())
            disCountinuityEndPos = i;
        maxTsDuration = (int) Math.ceil(ts.getDuration()) > maxTsDuration ? (int) Math.ceil(ts.getDuration()) : maxTsDuration;
    }
    m3u8Builder.setLength(0);
    // EXTM3U 必须在第一行, 标识是一个 Extended M3U Playlist 文件
    m3u8Builder.append("#EXTM3U").append("\n");
    // 表示 Playlist 兼容的版本
    m3u8Builder.append("#EXT-X-VERSION:3").append("\n");
    // 用于指定最大的 Media Segment duration
    m3u8Builder.append("#EXT-X-TARGETDURATION:").append(maxTsDuration).append("\n");
    // 用于指定第一个 Media Segment 的 Media Sequence Number
    m3u8Builder.append("#EXT-X-MEDIA-SEQUENCE:").append(m3u8Seq);
    for (int i = 0; i < tsSegments.size(); i++) {
        TsSegment ts = tsSegments.get(i);
        m3u8Builder.append("\n");
        // EXTINF, 用于指定 Media Segment 的 duration
        m3u8Builder.append("#EXTINF:").append(ts.getDuration()).append(",").append("\n");
        m3u8Builder.append(ts.getName());
        if (i == disCountinuityEndPos && disCountinuityEndPos != tsSegments.size() - 1) {
            m3u8Builder.append("\n");
            // EXT-X-DISCONTINUITY, 表示不连续
            m3u8Builder.append("#EXT-X-DISCONTINUITY");
        }
    }
    byte[] buf = m3u8Builder.toString().getBytes();
    M3U8 m3u8 = new M3U8(m3u8Seq, buf, System.currentTimeMillis());
    return m3u8;
}
Also used : TsSegment(com.feeyo.hls.ts.TsSegment)

Aggregations

TsSegment (com.feeyo.hls.ts.TsSegment)6 AdsCfg (com.feeyo.cfg.AdsCfg)1 HlsClientSession (com.feeyo.hls.HlsClientSession)1 HlsLiveStream (com.feeyo.hls.HlsLiveStream)1 M3U8 (com.feeyo.hls.m3u8.M3U8)1 AacH264MixedTsSegmenter (com.feeyo.hls.ts.segmenter.AacH264MixedTsSegmenter)1 AacTsSegmenter (com.feeyo.hls.ts.segmenter.AacTsSegmenter)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 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1