HlsMediaSource.h 2.99 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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
 */
xiongziliang committed
10

11 12
#ifndef ZLMEDIAKIT_HLSMEDIASOURCE_H
#define ZLMEDIAKIT_HLSMEDIASOURCE_H
13

14
#include <atomic>
15
#include "Util/TimeTicker.h"
16 17 18
#include "Common/MediaSource.h"
namespace mediakit{

19
class HlsMediaSource : public MediaSource {
20 21
public:
    friend class HlsCookieData;
22
    typedef RingBuffer<string> RingType;
23
    typedef std::shared_ptr<HlsMediaSource> Ptr;
24
    HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){}
25
    ~HlsMediaSource() override = default;
26 27

    /**
28 29
     * 	获取媒体源的环形缓冲
     */
30 31 32 33 34
    const RingType::Ptr &getRing() const {
        return _ring;
    }

    /**
35 36
     * 获取播放器个数
     */
37
    int readerCount() override {
38
        return _ring ? _ring->readerCount() : 0;
39
    }
40 41

    /**
42
     * 生成m3u8文件时触发
43
     * @param file_created 是否产生了hls文件
44
     */
45
    void registHls(bool file_created){
46 47
        if (!_is_regist) {
            _is_regist = true;
48 49 50 51 52 53 54 55 56
            weak_ptr<HlsMediaSource> weakSelf = dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
            auto lam = [weakSelf](int size) {
                auto strongSelf = weakSelf.lock();
                if (!strongSelf) {
                    return;
                }
                strongSelf->onReaderChanged(size);
            };
            _ring = std::make_shared<RingType>(0, std::move(lam));
57 58
            onReaderChanged(0);
            regist();
59
        }
60

61 62 63 64
        if (!file_created) {
            //没产生文件
            return;
        }
65 66 67 68 69 70 71 72 73 74 75
        //m3u8文件生成,发送给播放器
        decltype(_list_cb) copy;
        {
            lock_guard<mutex> lck(_mtx_cb);
            copy.swap(_list_cb);
        }
        copy.for_each([](const function<void()> &cb) {
            cb();
        });
    }

76
    void waitForFile(function<void()> cb) {
77 78 79
        //等待生成m3u8文件
        lock_guard<mutex> lck(_mtx_cb);
        _list_cb.emplace_back(std::move(cb));
80
    }
81

82
private:
83
    bool _is_regist = false;
84
    RingType::Ptr _ring;
85 86
    mutex _mtx_cb;
    List<function<void()> > _list_cb;
87 88 89 90
};

class HlsCookieData{
public:
91
    typedef std::shared_ptr<HlsCookieData> Ptr;
92
    HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockInfo> &sock_info);
93 94
    ~HlsCookieData();
    void addByteUsage(uint64_t bytes);
95

96 97
private:
    void addReaderCount();
98

99
private:
100
    atomic<uint64_t> _bytes {0};
101 102 103
    MediaInfo _info;
    std::shared_ptr<bool> _added;
    Ticker _ticker;
104
    std::shared_ptr<SockInfo> _sock_info;
105
    HlsMediaSource::RingType::RingReader::Ptr _ring_reader;
106 107
};

108

109
}//namespace mediakit
110
#endif //ZLMEDIAKIT_HLSMEDIASOURCE_H