2021-12-28 21:04:53 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/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_MPEG_H
|
|
|
|
|
#define ZLMEDIAKIT_MPEG_H
|
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "Extension/Frame.h"
|
|
|
|
|
#include "Extension/Track.h"
|
|
|
|
|
#include "Common/MediaSink.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Util/ResourcePool.h"
|
2021-12-28 21:04:53 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
//该类用于产生MPEG-TS/MPEG-PS
|
|
|
|
|
class MpegMuxer : public MediaSinkInterface {
|
|
|
|
|
public:
|
|
|
|
|
MpegMuxer(bool is_ps);
|
|
|
|
|
~MpegMuxer() override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加音视频轨道
|
|
|
|
|
*/
|
|
|
|
|
bool addTrack(const Track::Ptr &track) override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置音视频轨道
|
|
|
|
|
*/
|
|
|
|
|
void resetTracks() override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入帧数据
|
|
|
|
|
*/
|
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override;
|
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 刷新输出所有frame缓存
|
|
|
|
|
*/
|
|
|
|
|
void flush() override;
|
|
|
|
|
|
2021-12-28 21:04:53 +08:00
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* 输出ts/ps数据回调
|
|
|
|
|
* @param buffer ts/ps数据包
|
|
|
|
|
* @param timestamp 时间戳,单位毫秒
|
|
|
|
|
* @param key_pos 是否为关键帧的第一个ts/ps包,用于确保ts切片第一帧为关键帧
|
|
|
|
|
*/
|
2022-08-08 17:13:39 +08:00
|
|
|
|
virtual void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint64_t timestamp, bool key_pos) = 0;
|
2021-12-28 21:04:53 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void createContext();
|
|
|
|
|
void releaseContext();
|
|
|
|
|
void onWrite_l(const void *packet, size_t bytes);
|
2022-01-06 15:30:09 +08:00
|
|
|
|
void flushCache();
|
2021-12-28 21:04:53 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool _is_ps = false;
|
|
|
|
|
bool _have_video = false;
|
|
|
|
|
bool _key_pos = false;
|
2022-01-06 15:30:09 +08:00
|
|
|
|
uint32_t _max_cache_size = 0;
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t _timestamp = 0;
|
2021-12-29 14:18:52 +08:00
|
|
|
|
struct mpeg_muxer_t *_context = nullptr;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::unordered_map<int, int/*track_id*/> _codec_to_trackid;
|
2021-12-28 21:04:53 +08:00
|
|
|
|
FrameMerger _frame_merger{FrameMerger::h264_prefix};
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::BufferRaw::Ptr _current_buffer;
|
|
|
|
|
toolkit::ResourcePool<toolkit::BufferRaw> _buffer_pool;
|
2021-12-28 21:04:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//mediakit
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#include "Common/MediaSink.h"
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
class MpegMuxer : public MediaSinkInterface {
|
|
|
|
|
public:
|
|
|
|
|
MpegMuxer(bool is_ps) {};
|
|
|
|
|
~MpegMuxer() override = default;
|
|
|
|
|
bool addTrack(const Track::Ptr &track) override { return false; }
|
|
|
|
|
void resetTracks() override {}
|
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override { return false; }
|
|
|
|
|
|
|
|
|
|
protected:
|
2022-08-08 17:13:39 +08:00
|
|
|
|
virtual void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint64_t timestamp, bool key_pos) = 0;
|
2021-12-28 21:04:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_MPEG_H
|