2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-04-01 12:57:33 +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-04-01 12:57:33 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TSMUXER_H
|
|
|
|
|
|
#define TSMUXER_H
|
|
|
|
|
|
|
2020-09-12 19:46:58 +08:00
|
|
|
|
#if defined(ENABLE_HLS)
|
2019-04-01 12:57:33 +08:00
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
#include "Extension/Frame.h"
|
|
|
|
|
|
#include "Extension/Track.h"
|
|
|
|
|
|
#include "Util/File.h"
|
2019-08-01 18:49:04 +08:00
|
|
|
|
#include "Common/MediaSink.h"
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "Common/Stamp.h"
|
2019-04-01 12:57:33 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
//该类用于产生MPEG-TS
|
2019-12-03 12:32:57 +08:00
|
|
|
|
class TsMuxer : public MediaSinkInterface {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
public:
|
|
|
|
|
|
TsMuxer();
|
|
|
|
|
|
virtual ~TsMuxer();
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加音视频轨道
|
|
|
|
|
|
*/
|
2019-08-27 11:34:20 +08:00
|
|
|
|
void addTrack(const Track::Ptr &track) override;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重置音视频轨道
|
|
|
|
|
|
*/
|
2019-10-11 16:51:10 +08:00
|
|
|
|
void resetTracks() override;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 输入帧数据
|
|
|
|
|
|
*/
|
2019-10-11 16:51:10 +08:00
|
|
|
|
void inputFrame(const Frame::Ptr &frame) override;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
protected:
|
2020-05-15 18:08:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 输出mpegts数据回调
|
|
|
|
|
|
* @param packet mpegts数据
|
|
|
|
|
|
* @param bytes mpegts数据长度
|
|
|
|
|
|
* @param timestamp 时间戳,单位毫秒
|
|
|
|
|
|
* @param is_idr_fast_packet 是否为关键帧的第一个TS包,用于确保ts切片第一帧为关键帧
|
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual void onTs(const void *packet, size_t bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
private:
|
|
|
|
|
|
void init();
|
|
|
|
|
|
void uninit();
|
2020-05-15 18:08:54 +08:00
|
|
|
|
//音视频时间戳同步用
|
|
|
|
|
|
void stampSync();
|
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
private:
|
2020-05-15 18:08:54 +08:00
|
|
|
|
void *_context = nullptr;
|
|
|
|
|
|
char _tsbuf[188];
|
2019-04-01 12:57:33 +08:00
|
|
|
|
uint32_t _timestamp = 0;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
struct track_info {
|
2019-08-02 18:06:37 +08:00
|
|
|
|
int track_id = -1;
|
|
|
|
|
|
Stamp stamp;
|
|
|
|
|
|
};
|
2020-05-15 18:08:54 +08:00
|
|
|
|
unordered_map<int, track_info> _codec_to_trackid;
|
2019-08-27 11:34:20 +08:00
|
|
|
|
List<Frame::Ptr> _frameCached;
|
2020-01-24 22:00:55 +08:00
|
|
|
|
bool _is_idr_fast_packet = false;
|
|
|
|
|
|
bool _have_video = false;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
2020-09-12 19:46:58 +08:00
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
|
|
#include "Common/MediaSink.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
class TsMuxer : public MediaSinkInterface {
|
|
|
|
|
|
public:
|
|
|
|
|
|
TsMuxer() {}
|
|
|
|
|
|
~TsMuxer() override {}
|
|
|
|
|
|
void addTrack(const Track::Ptr &track) override {}
|
|
|
|
|
|
void resetTracks() override {}
|
|
|
|
|
|
void inputFrame(const Frame::Ptr &frame) override {}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual void onTs(const void *packet, size_t bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0;
|
2020-09-12 19:46:58 +08:00
|
|
|
|
};
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
|
|
#endif// defined(ENABLE_HLS)
|
|
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
#endif //TSMUXER_H
|