Files
ZLMediaKit/src/TS/TSMediaSource.h

151 lines
4.5 KiB
C++
Raw Normal View History

2020-09-20 19:50:08 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2020-09-20 00:21:46 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2020-09-20 00:21:46 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-09-20 00:21:46 +08:00
* 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"
#include "Common/PacketCache.h"
#include "Util/RingBuffer.h"
2020-09-20 00:21:46 +08:00
#define TS_GOP_SIZE 512
namespace mediakit {
// TS直播数据包 [AUTO-TRANSLATED:02fb2e8e]
// TS Live Data Packet
class TSPacket : public toolkit::BufferOffset<toolkit::Buffer::Ptr>{
2020-09-20 00:21:46 +08:00
public:
using Ptr = std::shared_ptr<TSPacket>;
template<typename ...ARGS>
2021-07-07 16:15:42 +08:00
TSPacket(ARGS && ...args) : BufferOffset<Buffer::Ptr>(std::forward<ARGS>(args)...) {};
2020-09-20 00:21:46 +08:00
public:
2022-08-08 17:13:39 +08:00
uint64_t time_stamp = 0;
2020-09-20 00:21:46 +08:00
};
// TS直播源 [AUTO-TRANSLATED:0d25ead6]
// TS Live Source
2022-10-16 19:49:56 +08:00
class TSMediaSource final : public MediaSource, public toolkit::RingDelegate<TSPacket::Ptr>, private PacketCache<TSPacket>{
2020-09-20 00:21:46 +08:00
public:
using Ptr = std::shared_ptr<TSMediaSource>;
using RingDataType = std::shared_ptr<toolkit::List<TSPacket::Ptr> >;
using RingType = toolkit::RingBuffer<RingDataType>;
2020-09-20 00:21:46 +08:00
2023-05-25 16:23:24 +08:00
TSMediaSource(const MediaTuple& tuple, int ring_size = TS_GOP_SIZE): MediaSource(TS_SCHEMA, tuple), _ring_size(ring_size) {}
2020-09-20 00:21:46 +08:00
~TSMediaSource() override {
try {
flush();
} catch (std::exception &ex) {
WarnL << ex.what();
}
}
2020-09-20 00:21:46 +08:00
/**
* 获取媒体源的环形缓冲
* Get the circular buffer of the media source
* [AUTO-TRANSLATED:91a762bc]
2020-09-20 00:21:46 +08:00
*/
const RingType::Ptr &getRing() const {
return _ring;
}
void getPlayerList(const std::function<void(const std::list<toolkit::Any> &info_list)> &cb,
const std::function<toolkit::Any(toolkit::Any &&info)> &on_change) override {
_ring->getInfoList(cb, on_change);
}
2020-09-20 00:21:46 +08:00
/**
* 获取播放器个数
* Get the number of players
* [AUTO-TRANSLATED:a451c846]
2020-09-20 00:21:46 +08:00
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
/**
* 输入TS包
* @param packet TS包
* @param key 是否为关键帧第一个包
* Input TS packet
* @param packet TS packet
* @param key Whether it is the first packet of the key frame
* [AUTO-TRANSLATED:cd773549]
2020-09-20 00:21:46 +08:00
*/
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缓存
* Clear GOP cache
* [AUTO-TRANSLATED:d863f8c9]
2020-09-20 00:21:46 +08:00
*/
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(){
2023-04-28 22:04:38 +08:00
std::weak_ptr<TSMediaSource> weak_self = std::static_pointer_cast<TSMediaSource>(shared_from_this());
2020-09-20 00:21:46 +08:00
_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);
});
// 注册媒体源 [AUTO-TRANSLATED:b87b5ac4]
// Register media source
2020-09-20 00:21:46 +08:00
regist();
}
/**
* 合并写回调
* @param packet_list 合并写缓存列队
* @param key_pos 是否包含关键帧
* Merge write callback
* @param packet_list Merge write cache queue
* @param key_pos Whether it contains a key frame
* [AUTO-TRANSLATED:6e93913e]
2020-09-20 00:21:46 +08:00
*/
void onFlush(std::shared_ptr<toolkit::List<TSPacket::Ptr> > packet_list, bool key_pos) override {
// 如果不存在视频,那么就没有存在GOP缓存的意义,所以确保一直清空GOP缓存 [AUTO-TRANSLATED:66208f94]
// If there is no video, then there is no meaning to the existence of GOP cache, so make sure to clear the GOP cache all the time
_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