ZLMediaKit/src/TS/TSMediaSource.h

121 lines
3.3 KiB
C++
Raw Normal View History

2020-09-20 19:50:08 +08:00
/*
2020-09-20 00:21:46 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* 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.
*/
#ifndef ZLMEDIAKIT_TSMEDIASOURCE_H
#define ZLMEDIAKIT_TSMEDIASOURCE_H
#include "Common/MediaSource.h"
using namespace toolkit;
#define TS_GOP_SIZE 512
namespace mediakit {
//TS直播数据包
class TSPacket : public BufferRaw{
public:
using Ptr = std::shared_ptr<TSPacket>;
template<typename ...ARGS>
TSPacket(ARGS && ...args) : BufferRaw(std::forward<ARGS>(args)...) {};
~TSPacket() override = default;
public:
uint32_t time_stamp = 0;
};
//TS直播源
2020-10-24 23:28:25 +08:00
class TSMediaSource : public MediaSource, public RingDelegate<TSPacket::Ptr>, public PacketCache<TSPacket>{
2020-09-20 00:21:46 +08:00
public:
using PoolType = ResourcePool<TSPacket>;
using Ptr = std::shared_ptr<TSMediaSource>;
using RingDataType = std::shared_ptr<List<TSPacket::Ptr> >;
using RingType = RingBuffer<RingDataType>;
TSMediaSource(const string &vhost,
const string &app,
const string &stream_id,
int ring_size = TS_GOP_SIZE) : MediaSource(TS_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {}
~TSMediaSource() override = default;
/**
*
*/
const RingType::Ptr &getRing() const {
return _ring;
}
/**
*
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
/**
* TS包
* @param packet TS包
* @param key
*/
void onWrite(TSPacket::Ptr packet, bool key) override {
2020-12-05 12:22:17 +08:00
_speed[TrackVideo] += packet->size();
2020-09-20 00:21:46 +08:00
if (!_ring) {
createRing();
}
if (key) {
_have_video = true;
}
2020-10-24 23:28:25 +08:00
auto stamp = packet->time_stamp;
PacketCache<TSPacket>::inputPacket(stamp, true, std::move(packet), key);
2020-09-20 00:21:46 +08:00
}
/**
* GOP缓存
*/
void clearCache() override {
2020-10-24 23:28:25 +08:00
PacketCache<TSPacket>::clearCache();
2020-09-20 00:21:46 +08:00
_ring->clearCache();
}
private:
void createRing(){
weak_ptr<TSMediaSource> weak_self = dynamic_pointer_cast<TSMediaSource>(shared_from_this());
_ring = std::make_shared<RingType>(_ring_size, [weak_self](int size) {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
strong_self->onReaderChanged(size);
});
onReaderChanged(0);
//注册媒体源
regist();
}
/**
*
* @param packet_list
* @param key_pos
*/
void onFlush(std::shared_ptr<List<TSPacket::Ptr> > packet_list, bool key_pos) override {
2020-09-20 00:21:46 +08:00
//如果不存在视频那么就没有存在GOP缓存的意义所以确保一直清空GOP缓存
_ring->write(std::move(packet_list), _have_video ? key_pos : true);
2020-09-20 00:21:46 +08:00
}
private:
bool _have_video = false;
int _ring_size;
RingType::Ptr _ring;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_TSMEDIASOURCE_H