2020-03-06 13:00:06 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-12-28 16:48:42 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2019-12-28 16:48:42 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
#ifndef ZLMEDIAKIT_HLSMEDIASOURCE_H
|
|
|
|
|
#define ZLMEDIAKIT_HLSMEDIASOURCE_H
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
#include <atomic>
|
2019-12-29 15:38:29 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
2019-12-28 16:48:42 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
class HlsMediaSource : public MediaSource {
|
2019-12-28 16:48:42 +08:00
|
|
|
|
public:
|
|
|
|
|
friend class HlsCookieData;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
typedef RingBuffer<string> RingType;
|
2019-12-28 18:50:56 +08:00
|
|
|
|
typedef std::shared_ptr<HlsMediaSource> Ptr;
|
2019-12-29 11:52:02 +08:00
|
|
|
|
HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_reader_count = 0;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
_ring = std::make_shared<RingType>();
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:09:56 +08:00
|
|
|
|
~HlsMediaSource() override = default;
|
2019-12-28 18:50:56 +08:00
|
|
|
|
|
2019-12-29 17:55:02 +08:00
|
|
|
|
/**
|
2020-03-20 11:51:24 +08:00
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
|
*/
|
2019-12-29 17:55:02 +08:00
|
|
|
|
const RingType::Ptr &getRing() const {
|
|
|
|
|
return _ring;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
/**
|
2020-03-20 11:51:24 +08:00
|
|
|
|
* 获取播放器个数
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2019-12-28 18:50:56 +08:00
|
|
|
|
int readerCount() override {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
return _reader_count.load();
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2020-09-12 19:20:18 +08:00
|
|
|
|
* 生成m3u8文件时触发
|
2020-09-12 20:42:58 +08:00
|
|
|
|
* @param file_created 是否产生了hls文件
|
2019-12-28 16:48:42 +08:00
|
|
|
|
*/
|
2020-09-12 20:42:58 +08:00
|
|
|
|
void registHls(bool file_created){
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (!_is_regist) {
|
|
|
|
|
_is_regist = true;
|
2020-09-12 19:09:56 +08:00
|
|
|
|
onReaderChanged(0);
|
|
|
|
|
regist();
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2020-09-12 20:42:58 +08:00
|
|
|
|
if (!file_created) {
|
|
|
|
|
//没产生文件
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
//m3u8文件生成,发送给播放器
|
|
|
|
|
decltype(_list_cb) copy;
|
|
|
|
|
{
|
|
|
|
|
lock_guard<mutex> lck(_mtx_cb);
|
|
|
|
|
copy.swap(_list_cb);
|
|
|
|
|
}
|
|
|
|
|
copy.for_each([](const function<void()> &cb) {
|
|
|
|
|
cb();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 20:42:58 +08:00
|
|
|
|
void waitForFile(function<void()> cb){
|
2020-09-12 19:20:18 +08:00
|
|
|
|
//等待生成m3u8文件
|
|
|
|
|
lock_guard<mutex> lck(_mtx_cb);
|
|
|
|
|
_list_cb.emplace_back(std::move(cb));
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* 修改观看者个数
|
|
|
|
|
* @param add 添加海思删除
|
|
|
|
|
*/
|
2019-12-29 12:10:31 +08:00
|
|
|
|
void modifyReaderCount(bool add) {
|
2019-12-28 18:50:56 +08:00
|
|
|
|
if (add) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
++_reader_count;
|
2020-09-12 19:09:56 +08:00
|
|
|
|
} else {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
--_reader_count;
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
onReaderChanged(_reader_count);
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
private:
|
2020-09-12 19:20:18 +08:00
|
|
|
|
bool _is_regist = false;
|
|
|
|
|
atomic_int _reader_count;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
mutex _mtx_cb;
|
|
|
|
|
List<function<void()> > _list_cb;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HlsCookieData{
|
|
|
|
|
public:
|
2020-01-02 17:46:20 +08:00
|
|
|
|
typedef std::shared_ptr<HlsCookieData> Ptr;
|
2020-04-23 22:04:59 +08:00
|
|
|
|
HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockInfo> &sock_info);
|
2019-12-29 17:55:02 +08:00
|
|
|
|
~HlsCookieData();
|
|
|
|
|
void addByteUsage(uint64_t bytes);
|
|
|
|
|
private:
|
|
|
|
|
void addReaderCount();
|
|
|
|
|
private:
|
|
|
|
|
uint64_t _bytes = 0;
|
|
|
|
|
MediaInfo _info;
|
|
|
|
|
std::shared_ptr<bool> _added;
|
|
|
|
|
weak_ptr<HlsMediaSource> _src;
|
|
|
|
|
Ticker _ticker;
|
2020-04-23 22:04:59 +08:00
|
|
|
|
std::shared_ptr<SockInfo> _sock_info;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
HlsMediaSource::RingType::RingReader::Ptr _ring_reader;
|
2019-12-28 16:48:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
|
2019-12-28 16:48:42 +08:00
|
|
|
|
}//namespace mediakit
|
2019-12-28 18:50:56 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_HLSMEDIASOURCE_H
|