Search in sources :

Example 1 with M3u8

use of com.acgist.snail.pojo.bean.M3u8 in project snail by acgist.

the class HlsContextTest method testHlsContext.

@Test
void testHlsContext() throws DownloadException {
    final String id = UUID.randomUUID().toString();
    final TaskEntity task = new TaskEntity();
    task.setId(id);
    task.setName("acgist");
    final ITaskSession session = TaskSession.newInstance(task);
    assertDoesNotThrow(() -> {
        HlsContext.getInstance().m3u8(id, new M3u8(Type.FILE, null, List.of()));
        HlsContext.getInstance().hlsSession(session);
        HlsContext.getInstance().remove(session);
    });
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) ITaskSession(com.acgist.snail.pojo.ITaskSession) M3u8(com.acgist.snail.pojo.bean.M3u8) Test(org.junit.jupiter.api.Test)

Example 2 with M3u8

use of com.acgist.snail.pojo.bean.M3u8 in project snail by acgist.

the class M3u8Builder method buildCipher.

/**
 * <p>新建加密套件</p>
 *
 * @return 加密套件
 *
 * @throws NetException 网络异常
 */
private Cipher buildCipher() throws NetException {
    final var optional = this.labels.stream().filter(label -> LABEL_EXT_X_KEY.equalsIgnoreCase(label.getName())).findFirst();
    if (optional.isEmpty()) {
        // 没有加密标签:不用解密
        return null;
    }
    final KeyValueWrapper wrapper = optional.get().attrs();
    final String method = wrapper.getIgnoreCase(ATTR_METHOD);
    final M3u8.Protocol protocol = M3u8.Protocol.of(method);
    LOGGER.debug("HLS加密算法:{}", method);
    if (protocol == null || protocol == M3u8.Protocol.NONE) {
        throw new NetException("不支持的HLS加密算法:" + method);
    }
    if (protocol == M3u8.Protocol.AES_128) {
        return this.buildCipherAes128(wrapper.getIgnoreCase(ATTR_IV), wrapper.getIgnoreCase(ATTR_URI));
    } else {
        throw new NetException("不支持的HLS加密算法:" + method);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) SymbolConfig(com.acgist.snail.config.SymbolConfig) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ArrayList(java.util.ArrayList) UrlUtils(com.acgist.snail.utils.UrlUtils) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NetException(com.acgist.snail.context.exception.NetException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) LoggerFactory(com.acgist.snail.logger.LoggerFactory) M3u8(com.acgist.snail.pojo.bean.M3u8) StringUtils(com.acgist.snail.utils.StringUtils) Type(com.acgist.snail.pojo.bean.M3u8.Type) Files(java.nio.file.Files) Logger(com.acgist.snail.logger.Logger) IOException(java.io.IOException) CollectionUtils(com.acgist.snail.utils.CollectionUtils) Collectors(java.util.stream.Collectors) DownloadException(com.acgist.snail.context.exception.DownloadException) File(java.io.File) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyValueWrapper(com.acgist.snail.pojo.wrapper.KeyValueWrapper) InvalidKeyException(java.security.InvalidKeyException) HttpClient(com.acgist.snail.net.http.HttpClient) KeyValueWrapper(com.acgist.snail.pojo.wrapper.KeyValueWrapper) NetException(com.acgist.snail.context.exception.NetException) M3u8(com.acgist.snail.pojo.bean.M3u8)

Example 3 with M3u8

use of com.acgist.snail.pojo.bean.M3u8 in project snail by acgist.

the class HlsContext method hlsSession.

/**
 * <p>获取HLS任务信息</p>
 *
 * @param taskSession 任务信息
 *
 * @return HLS任务信息
 */
public HlsSession hlsSession(ITaskSession taskSession) {
    final String id = taskSession.getId();
    final M3u8 m3u8 = this.m3u8s.get(id);
    return this.sessions.computeIfAbsent(id, key -> HlsSession.newInstance(m3u8, taskSession));
}
Also used : M3u8(com.acgist.snail.pojo.bean.M3u8)

Example 4 with M3u8

use of com.acgist.snail.pojo.bean.M3u8 in project snail by acgist.

the class M3u8Builder method buildM3u8.

/**
 * <p>新建M3U8信息</p>
 *
 * @return {@link M3u8}
 *
 * @throws NetException 网络异常
 */
private M3u8 buildM3u8() throws NetException {
    final M3u8.Type type = this.buildType();
    final Cipher cipher = this.buildCipher();
    List<String> links;
    if (type == Type.M3U8) {
        links = this.buildM3u8Links();
    } else {
        // 获取LABEL_EXTINF标签数据
        links = this.buildFileLinks(LABEL_EXTINF);
        // 没有LABEL_EXTINF标签数据:获取LABEL_EXT_X_BITRATE标签数据
        if (CollectionUtils.isEmpty(links)) {
            links = this.buildFileLinks(LABEL_EXT_X_BITRATE);
        }
        if (CollectionUtils.isEmpty(links)) {
            throw new NetException("没有下载文件");
        }
    }
    return new M3u8(type, cipher, links);
}
Also used : Type(com.acgist.snail.pojo.bean.M3u8.Type) NetException(com.acgist.snail.context.exception.NetException) M3u8(com.acgist.snail.pojo.bean.M3u8) Cipher(javax.crypto.Cipher)

Aggregations

M3u8 (com.acgist.snail.pojo.bean.M3u8)4 NetException (com.acgist.snail.context.exception.NetException)2 Type (com.acgist.snail.pojo.bean.M3u8.Type)2 Cipher (javax.crypto.Cipher)2 SymbolConfig (com.acgist.snail.config.SymbolConfig)1 DownloadException (com.acgist.snail.context.exception.DownloadException)1 Logger (com.acgist.snail.logger.Logger)1 LoggerFactory (com.acgist.snail.logger.LoggerFactory)1 HttpClient (com.acgist.snail.net.http.HttpClient)1 ITaskSession (com.acgist.snail.pojo.ITaskSession)1 TaskEntity (com.acgist.snail.pojo.entity.TaskEntity)1 KeyValueWrapper (com.acgist.snail.pojo.wrapper.KeyValueWrapper)1 CollectionUtils (com.acgist.snail.utils.CollectionUtils)1 StringUtils (com.acgist.snail.utils.StringUtils)1 UrlUtils (com.acgist.snail.utils.UrlUtils)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1