ZLMediaKit/src/Record/HlsMakerImp.h
Caner Ateş 64ae6e43c5
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
DockerPy / build (push) Has been cancelled
Linux / build (push) Has been cancelled
Linux_Python / build (push) Has been cancelled
macOS / build (push) Has been cancelled
macOS_Python / build (push) Has been cancelled
Windows / build (push) Has been cancelled
Windows_Python / build (push) Has been cancelled
支持配置 fmp4 HLS 切片文件扩展名 (.mp4/.m4s) (#4746)
## 背景 / 问题
fmp4 模式下,HLS 切片文件名被硬编码为 `.mp4`(见 `HlsMakerImp::onOpenSegment`),无法改用 fmp4
媒体段更规范、更常见的 `.m4s` 扩展名(`init` 段用 `init.mp4`、媒体段用 `.m4s` 是 CMAF/DASH
的通行约定)。

## 改动
- 新增配置项 `hls.fmp4SegExt`,默认 `.mp4`,**完全向后兼容**;用户可设为 `.m4s`。
- `init` 段仍固定为 `init.mp4`,mpegts 切片仍为 `.ts`,相关行为不变。
- 切片文件名同时用作 m3u8 中的分片 URI,因此扩展名变更后 playlist 自动保持一致,无需额外同步。
- 在 HTTP MIME 表中注册 `.m4s` -> `video/mp4`;否则会以 `text/plain` 返回,导致 Safari
等播放器无法播放 HLS-fmp4。
- 配置值不带前导点(如 `m4s`)时自动规范化为 `.m4s`。

## 涉及文件
`src/Common/config.h`、`src/Common/config.cpp`、`src/Record/HlsRecorder.h`、`src/Record/HlsMakerImp.h`、`src/Record/HlsMakerImp.cpp`、`src/Http/HttpConst.cpp`、`conf/config.ini`

## 测试
- **默认(未配置)**:切片仍为 `*.mp4`,`init` 为 `init.mp4`,行为与改动前一致(回归正常)。
- **`hls.fmp4SegExt=.m4s`**:切片文件为 `*_N.m4s`,m3u8 中 `#EXTINF` 的 URI 指向
`.m4s`,`#EXT-X-MAP` 仍为 `init.mp4`;HTTP 拉取切片返回 `Content-Type:
video/mp4`;hls.js 与 Safari 播放正常。
- **`hls.fmp4SegExt=m4s`(无前导点)**:自动规范化,仍生成 `*.m4s`。

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 21:25:25 +08:00

90 lines
2.5 KiB
C++

/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef HLSMAKERIMP_H
#define HLSMAKERIMP_H
#include <memory>
#include <string>
#include <stdlib.h>
#include "HlsMaker.h"
#include "HlsMediaSource.h"
namespace mediakit {
class HlsMakerImp : public HlsMaker {
public:
HlsMakerImp(bool is_fmp4, const std::string &m3u8_file, const std::string &params, uint32_t bufSize = 64 * 1024,
float seg_duration = 5, uint32_t seg_number = 3, bool seg_keep = false,
const std::string &fmp4_seg_ext = ".mp4");
~HlsMakerImp() override;
/**
* 设置媒体信息
* Set media information
* [AUTO-TRANSLATED:d205db9f]
*/
void setMediaSource(const MediaTuple& tuple);
/**
* 获取MediaSource
* @return
* Get MediaSource
* @return
* [AUTO-TRANSLATED:af916433]
*/
HlsMediaSource::Ptr getMediaSource() const;
/**
* 清空缓存
* Clear cache
* [AUTO-TRANSLATED:f872d7e2]
*/
void clearCache();
protected:
std::string onOpenSegment(uint64_t index) override ;
void onDelSegment(uint64_t index) override;
void onWriteInitSegment(const char *data, size_t len) override;
void onWriteSegment(const char *data, size_t len) override;
void onWriteHls(const std::string &data, bool include_delay) override;
void onFlushLastSegment(uint64_t duration_ms) override;
private:
std::shared_ptr<FILE> makeFile(const std::string &file,bool setbuf = false);
void clearCache(bool immediately, bool eof);
void saveCurrentDir();
private:
int _buf_size;
std::string _params;
std::string _fmp4_seg_ext;
std::string _path_hls;
std::string _path_hls_delay;
std::string _path_init;
std::string _path_prefix;
std::string _current_dir;
std::string _current_dir_init_file;
RecordInfo _info;
std::shared_ptr<FILE> _file;
std::shared_ptr<char> _file_buf;
HlsMediaSource::Ptr _media_src;
toolkit::EventPoller::Ptr _poller;
std::map<uint64_t/*index*/,std::string/*file_path*/> _segment_file_paths;
std::deque<std::tuple<int,std::string> > _current_dir_seg_list;
};
}//namespace mediakit
#endif //HLSMAKERIMP_H