HlsRecorder.h 3.15 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT 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.
9 10 11 12 13 14 15 16 17
 */

#ifndef HLSRECORDER_H
#define HLSRECORDER_H

#include "HlsMakerImp.h"
#include "TsMuxer.h"
namespace mediakit {

18
class HlsRecorder : public MediaSourceEventInterceptor, public TsMuxer, public std::enable_shared_from_this<HlsRecorder> {
19
public:
20
    typedef std::shared_ptr<HlsRecorder> Ptr;
xiongziliang committed
21
    HlsRecorder(const string &m3u8_file, const string &params){
22 23
        GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum);
        GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize);
24
        GET_CONFIG(float, hlsDuration, Hls::kSegmentDuration);
25 26 27
        _hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum);
        //清空上次的残余文件
        _hls->clearCache();
xiongziliang committed
28
    }
29 30 31 32

    ~HlsRecorder(){}

    void setMediaSource(const string &vhost, const string &app, const string &stream_id) {
33 34 35
        _hls->setMediaSource(vhost, app, stream_id);
    }

36
    void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {
37
        setDelegate(listener);
38 39
        _hls->getMediaSource()->setListener(shared_from_this());
        //先注册媒体流,后续可以按需生成
40
        _hls->getMediaSource()->registHls(false);
41 42 43 44 45 46 47
    }

    int readerCount() {
        return _hls->getMediaSource()->readerCount();
    }

    void onReaderChanged(MediaSource &sender, int size) override {
48
        GET_CONFIG(bool, hls_demand, General::kHlsDemand);
49
        //hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
50 51
        _enabled = hls_demand ? (_hls->isLive() ? size : true) : true;
        if (!size && _hls->isLive() && hls_demand) {
52 53 54 55
            //hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
            _clear_cache = true;
        }
        MediaSourceEventInterceptor::onReaderChanged(sender, size);
56
    }
57 58

    bool isEnabled() {
59
        GET_CONFIG(bool, hls_demand, General::kHlsDemand);
60
        //缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
61
        return hls_demand ? (_clear_cache ? true : _enabled) : true;
62 63
    }

64 65 66
    void inputFrame(const Frame::Ptr &frame) override {
        GET_CONFIG(bool, hls_demand, General::kHlsDemand);
        if (_clear_cache && hls_demand) {
67 68 69
            _clear_cache = false;
            _hls->clearCache();
        }
70
        if (_enabled || !hls_demand) {
71 72 73 74 75
            TsMuxer::inputFrame(frame);
        }
    }

private:
76
    void onTs(const void *packet, size_t bytes, uint32_t timestamp, bool is_idr_fast_packet) override {
77 78 79
        _hls->inputData((char *) packet, bytes, timestamp, is_idr_fast_packet);
    }

xiongziliang committed
80
private:
81 82 83 84
    //默认不生成hls文件,有播放器时再生成
    bool _enabled = false;
    bool _clear_cache = false;
    std::shared_ptr<HlsMakerImp> _hls;
85 86 87
};
}//namespace mediakit
#endif //HLSRECORDER_H