2019-12-28 18:50:56 +08:00
|
|
|
/*
|
2019-12-28 16:48:42 +08:00
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
|
|
|
*
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
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){
|
2019-12-28 18:50:56 +08:00
|
|
|
_readerCount = 0;
|
2019-12-29 17:55:02 +08:00
|
|
|
_ring = std::make_shared<RingType>();
|
2019-12-28 18:50:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~HlsMediaSource() = default;
|
|
|
|
|
2019-12-29 17:55:02 +08:00
|
|
|
/**
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
*/
|
|
|
|
const RingType::Ptr &getRing() const {
|
|
|
|
return _ring;
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
/**
|
|
|
|
* 获取播放器个数
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int readerCount() override {
|
|
|
|
return _readerCount.load();
|
|
|
|
}
|
2019-12-28 16:48:42 +08:00
|
|
|
|
|
|
|
/**
|
2019-12-28 18:50:56 +08:00
|
|
|
* 注册hls
|
2019-12-28 16:48:42 +08:00
|
|
|
*/
|
2019-12-28 18:50:56 +08:00
|
|
|
void registHls(){
|
|
|
|
if(!_registed){
|
|
|
|
regist();
|
|
|
|
_registed = true;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
++_readerCount;
|
|
|
|
return;
|
|
|
|
}
|
2019-12-28 16:48:42 +08:00
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
if (--_readerCount == 0 && totalReaderCount() == 0) {
|
|
|
|
onNoneReader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
atomic_int _readerCount;
|
|
|
|
bool _registed = false;
|
2019-12-29 17:55:02 +08:00
|
|
|
RingType::Ptr _ring;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HlsCookieData{
|
|
|
|
public:
|
2020-01-02 17:46:20 +08:00
|
|
|
typedef std::shared_ptr<HlsCookieData> Ptr;
|
2020-02-13 11:33:59 +08:00
|
|
|
HlsCookieData(const MediaInfo &info, const string &peer_ip, uint16_t peer_port);
|
2019-12-29 17:55:02 +08:00
|
|
|
~HlsCookieData();
|
|
|
|
void addByteUsage(uint64_t bytes);
|
|
|
|
private:
|
|
|
|
void addReaderCount();
|
|
|
|
private:
|
|
|
|
uint64_t _bytes = 0;
|
|
|
|
MediaInfo _info;
|
2020-02-13 11:33:59 +08:00
|
|
|
string _peer_ip;
|
|
|
|
uint16_t _peer_port;
|
2019-12-29 17:55:02 +08:00
|
|
|
std::shared_ptr<bool> _added;
|
|
|
|
weak_ptr<HlsMediaSource> _src;
|
|
|
|
Ticker _ticker;
|
|
|
|
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
|