addFFmpegSource接口支持enable_hls、enable_mp4参数

This commit is contained in:
xiongziliang 2020-12-20 20:25:44 +08:00
parent 3cdc893856
commit bc125e65be
4 changed files with 63 additions and 16 deletions

View File

@ -428,7 +428,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/addStreamProxy?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=test&url=rtmp://live.hkstv.hk.lxdns.com/live/hks2&enable_rtsp=1&enable_rtmp=1",
"raw": "{{ZLMediaKit_URL}}/index/api/addStreamProxy?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=test&url=rtmp://live.hkstv.hk.lxdns.com/live/hks2",
"host": [
"{{ZLMediaKit_URL}}"
],
@ -523,7 +523,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/addFFmpegSource?secret={{ZLMediaKit_secret}}&src_url=http://live.hkstv.hk.lxdns.com/live/hks2/playlist.m3u8&dst_url=rtmp://10.8.9.115:8554/live/hks2&timeout_ms=10000",
"raw": "{{ZLMediaKit_URL}}/index/api/addFFmpegSource?secret={{ZLMediaKit_secret}}&src_url=http://hefeng.live.tempsource.cjyun.org/videotmp/s10100-hftv.m3u8&dst_url=rtmp://127.0.0.1/live/hks2&timeout_ms=10000&enable_hls=0&enable_mp4=0",
"host": [
"{{ZLMediaKit_URL}}"
],
@ -540,7 +540,7 @@
},
{
"key": "src_url",
"value": "http://live.hkstv.hk.lxdns.com/live/hks2/playlist.m3u8",
"value": "http://hefeng.live.tempsource.cjyun.org/videotmp/s10100-hftv.m3u8",
"description": "FFmpeg拉流地址,支持任意协议或格式(只要FFmpeg支持即可)"
},
{
@ -552,6 +552,16 @@
"key": "timeout_ms",
"value": "10000",
"description": "FFmpeg推流成功超时时间,单位毫秒"
},
{
"key": "enable_hls",
"value": "0",
"description": "是否开启hls录制"
},
{
"key": "enable_mp4",
"value": "0",
"description": "是否开启mp4录制"
}
]
}

View File

@ -59,6 +59,11 @@ static bool is_local_ip(const string &ip){
return false;
}
void FFmpegSource::setupRecord(bool enable_hls, bool enable_mp4){
_enable_hls = enable_hls;
_enable_mp4 = enable_mp4;
}
void FFmpegSource::play(const string &src_url,const string &dst_url,int timeout_ms,const onPlay &cb) {
GET_CONFIG(string,ffmpeg_bin,FFmpeg::kBin);
GET_CONFIG(string,ffmpeg_cmd,FFmpeg::kCmd);
@ -263,6 +268,12 @@ void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) {
//防止多次进入onGetMediaSource函数导致无限递归调用的bug
setDelegate(listener);
src->setListener(shared_from_this());
if (_enable_hls) {
src->setupRecord(Recorder::type_hls, true, "");
}
if (_enable_mp4) {
src->setupRecord(Recorder::type_mp4, true, "");
}
}
}

View File

@ -46,13 +46,29 @@ public:
typedef function<void(const SockException &ex)> onPlay;
FFmpegSource();
virtual ~FFmpegSource();
~FFmpegSource();
/**
*
* @param cb
*/
void setOnClose(const function<void()> &cb);
/**
* url
* @param src_url FFmpeg拉流地址
* @param dst_url FFmpeg推流地址
* @param timeout_ms
* @param cb
*/
void play(const string &src_url, const string &dst_url, int timeout_ms, const onPlay &cb);
/**
*
* @param enable_hls hls直播或录制
* @param enable_mp4 mp4
*/
void setupRecord(bool enable_hls, bool enable_mp4);
private:
void findAsync(int maxWaitMS ,const function<void(const MediaSource::Ptr &src)> &cb);
void startTimer(int timeout_ms);
@ -69,6 +85,8 @@ private:
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
private:
bool _enable_hls = false;
bool _enable_mp4 = false;
Process _process;
Timer::Ptr _timer;
EventPoller::Ptr _poller;

View File

@ -694,6 +694,8 @@ void installWebApi() {
static auto addFFmpegSource = [](const string &src_url,
const string &dst_url,
int timeout_ms,
bool enable_hls,
bool enable_mp4,
const function<void(const SockException &ex, const string &key)> &cb) {
auto key = MD5(dst_url).hexdigest();
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
@ -710,6 +712,7 @@ void installWebApi() {
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
s_ffmpegMap.erase(key);
});
ffmpeg->setupRecord(enable_hls, enable_mp4);
ffmpeg->play(src_url, dst_url, timeout_ms, [cb, key](const SockException &ex) {
if (ex) {
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
@ -727,8 +730,11 @@ void installWebApi() {
auto src_url = allArgs["src_url"];
auto dst_url = allArgs["dst_url"];
int timeout_ms = allArgs["timeout_ms"];
auto enable_hls = allArgs["enable_hls"].as<int>();
auto enable_mp4 = allArgs["enable_mp4"].as<int>();
addFFmpegSource(src_url,dst_url,timeout_ms,[invoker,val,headerOut](const SockException &ex,const string &key){
addFFmpegSource(src_url, dst_url, timeout_ms, enable_hls, enable_mp4,
[invoker, val, headerOut](const SockException &ex, const string &key) {
if (ex) {
const_cast<Value &>(val)["code"] = API::OtherFailed;
const_cast<Value &>(val)["msg"] = ex.what();
@ -1084,6 +1090,8 @@ void installWebApi() {
addFFmpegSource("http://hls-ott-zhibo.wasu.tv/live/272/index.m3u8",/** ffmpeg拉流支持任意编码格式任意协议 **/
dst_url,
(1000 * timeout_sec) - 500,
false,
false,
[invoker,val,headerOut](const SockException &ex,const string &key){
if(ex){
const_cast<Value &>(val)["code"] = API::OtherFailed;