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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-12-28 16:48:42 +08:00
|
|
|
|
*
|
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
|
|
|
|
|
|
|
|
|
#include "Common/MediaSource.h"
|
2022-02-11 16:21:19 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include <atomic>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
namespace mediakit {
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
class HlsMediaSource : public MediaSource {
|
2019-12-28 16:48:42 +08:00
|
|
|
|
public:
|
|
|
|
|
friend class HlsCookieData;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
|
|
|
|
using RingType = toolkit::RingBuffer<std::string>;
|
|
|
|
|
using Ptr = std::shared_ptr<HlsMediaSource>;
|
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
HlsMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id)
|
|
|
|
|
: MediaSource(HLS_SCHEMA, vhost, app, stream_id) {}
|
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
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
|
*/
|
2022-02-11 16:21:19 +08:00
|
|
|
|
const RingType::Ptr &getRing() const { return _ring; }
|
2019-12-29 17:55:02 +08:00
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
/**
|
2020-03-20 11:51:24 +08:00
|
|
|
|
* 获取播放器个数
|
|
|
|
|
*/
|
2022-02-11 16:21:19 +08:00
|
|
|
|
int readerCount() override { return _ring ? _ring->readerCount() : 0; }
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2022-02-12 16:24:55 +08:00
|
|
|
|
* 设置或清空m3u8索引文件内容
|
2019-12-28 16:48:42 +08:00
|
|
|
|
*/
|
2022-02-12 16:24:55 +08:00
|
|
|
|
void setIndexFile(std::string index_file) {
|
|
|
|
|
if (!_ring) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::weak_ptr<HlsMediaSource> weakSelf = std::dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
|
2020-09-12 21:27:34 +08:00
|
|
|
|
auto lam = [weakSelf](int size) {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->onReaderChanged(size);
|
|
|
|
|
};
|
|
|
|
|
_ring = std::make_shared<RingType>(0, std::move(lam));
|
2020-09-12 19:09:56 +08:00
|
|
|
|
regist();
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
//赋值m3u8索引文件内容
|
|
|
|
|
std::lock_guard<std::mutex> lck(_mtx_index);
|
|
|
|
|
_index_file = std::move(index_file);
|
|
|
|
|
|
2022-02-12 16:24:55 +08:00
|
|
|
|
if (!_index_file.empty()) {
|
|
|
|
|
_list_cb.for_each([&](const std::function<void(const std::string &str)> &cb) { cb(_index_file); });
|
|
|
|
|
_list_cb.clear();
|
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 16:24:55 +08:00
|
|
|
|
/**
|
|
|
|
|
* 异步获取m3u8文件
|
|
|
|
|
*/
|
2022-02-11 16:21:19 +08:00
|
|
|
|
void getIndexFile(std::function<void(const std::string &str)> cb) {
|
|
|
|
|
std::lock_guard<std::mutex> lck(_mtx_index);
|
|
|
|
|
if (!_index_file.empty()) {
|
|
|
|
|
cb(_index_file);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
//等待生成m3u8文件
|
|
|
|
|
_list_cb.emplace_back(std::move(cb));
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
2022-02-12 16:24:55 +08:00
|
|
|
|
/**
|
|
|
|
|
* 同步获取m3u8文件
|
|
|
|
|
*/
|
2022-02-11 16:21:19 +08:00
|
|
|
|
std::string getIndexFile() const {
|
|
|
|
|
std::lock_guard<std::mutex> lck(_mtx_index);
|
|
|
|
|
return _index_file;
|
2020-10-01 11:02:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
void onSegmentSize(size_t bytes) { _speed[TrackVideo] += bytes; }
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
private:
|
2019-12-29 17:55:02 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2022-02-11 16:21:19 +08:00
|
|
|
|
std::string _index_file;
|
|
|
|
|
mutable std::mutex _mtx_index;
|
2022-02-12 16:24:55 +08:00
|
|
|
|
toolkit::List<std::function<void(const std::string &)>> _list_cb;
|
2019-12-29 17:55:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
class HlsCookieData {
|
2019-12-29 17:55:02 +08:00
|
|
|
|
public:
|
2022-02-11 16:21:19 +08:00
|
|
|
|
using Ptr = std::shared_ptr<HlsCookieData>;
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HlsCookieData(const MediaInfo &info, const std::shared_ptr<toolkit::SockInfo> &sock_info);
|
2019-12-29 17:55:02 +08:00
|
|
|
|
~HlsCookieData();
|
2022-02-11 16:21:19 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void addByteUsage(size_t bytes);
|
2022-02-11 16:21:19 +08:00
|
|
|
|
void setMediaSource(const HlsMediaSource::Ptr &src);
|
|
|
|
|
HlsMediaSource::Ptr getMediaSource() const;
|
2020-09-12 20:52:53 +08:00
|
|
|
|
|
2019-12-29 17:55:02 +08:00
|
|
|
|
private:
|
|
|
|
|
void addReaderCount();
|
2020-09-12 20:52:53 +08:00
|
|
|
|
|
2019-12-29 17:55:02 +08:00
|
|
|
|
private:
|
2022-02-11 16:21:19 +08:00
|
|
|
|
std::atomic<uint64_t> _bytes { 0 };
|
2019-12-29 17:55:02 +08:00
|
|
|
|
MediaInfo _info;
|
|
|
|
|
std::shared_ptr<bool> _added;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _ticker;
|
2022-02-11 16:21:19 +08:00
|
|
|
|
std::weak_ptr<HlsMediaSource> _src;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::shared_ptr<toolkit::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
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 16:21:19 +08:00
|
|
|
|
} // namespace mediakit
|
|
|
|
|
#endif // ZLMEDIAKIT_HLSMEDIASOURCE_H
|