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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2021-12-31 11:01:56 +08:00
|
|
|
|
#include <assert.h>
|
2021-12-28 21:04:53 +08:00
|
|
|
|
#include "MPEG.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
#include "mpeg-ts-proto.h"
|
2021-12-29 14:18:52 +08:00
|
|
|
|
#include "mpeg-muxer.h"
|
2021-12-28 21:04:53 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
2021-12-28 21:04:53 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
|
|
|
|
|
|
MpegMuxer::MpegMuxer(bool is_ps) {
|
|
|
|
|
|
_is_ps = is_ps;
|
|
|
|
|
|
createContext();
|
2021-12-31 11:01:56 +08:00
|
|
|
|
_buffer_pool.setSize(64);
|
2021-12-28 21:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MpegMuxer::~MpegMuxer() {
|
|
|
|
|
|
releaseContext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define XX(name, type, value, str, mpeg_id) \
|
|
|
|
|
|
case name : { \
|
|
|
|
|
|
if (mpeg_id == PSI_STREAM_RESERVED) { \
|
|
|
|
|
|
break; \
|
|
|
|
|
|
} \
|
2022-02-02 20:34:50 +08:00
|
|
|
|
_codec_to_trackid[track->getCodecId()] = mpeg_muxer_add_stream((::mpeg_muxer_t *)_context, mpeg_id, nullptr, 0); \
|
2021-12-28 21:04:53 +08:00
|
|
|
|
return true; \
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MpegMuxer::addTrack(const Track::Ptr &track) {
|
|
|
|
|
|
if (track->getTrackType() == TrackVideo) {
|
|
|
|
|
|
_have_video = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
switch (track->getCodecId()) {
|
|
|
|
|
|
CODEC_MAP(XX)
|
|
|
|
|
|
default: break;
|
|
|
|
|
|
}
|
|
|
|
|
|
WarnL << "不支持该编码格式,已忽略:" << track->getCodecName();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef XX
|
|
|
|
|
|
|
|
|
|
|
|
bool MpegMuxer::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
|
|
auto it = _codec_to_trackid.find(frame->getCodecId());
|
|
|
|
|
|
if (it == _codec_to_trackid.end()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
auto track_id = it->second;
|
|
|
|
|
|
_key_pos = !_have_video;
|
|
|
|
|
|
switch (frame->getCodecId()) {
|
|
|
|
|
|
case CodecH264:
|
|
|
|
|
|
case CodecH265: {
|
|
|
|
|
|
//这里的代码逻辑是让SPS、PPS、IDR这些时间戳相同的帧打包到一起当做一个帧处理,
|
2022-10-16 19:49:56 +08:00
|
|
|
|
return _frame_merger.inputFrame(frame,[this, track_id](uint64_t dts, uint64_t pts, const Buffer::Ptr &buffer, bool have_idr) {
|
2021-12-28 21:04:53 +08:00
|
|
|
|
_key_pos = have_idr;
|
|
|
|
|
|
//取视频时间戳为TS的时间戳
|
2022-08-08 17:13:39 +08:00
|
|
|
|
_timestamp = dts;
|
2022-01-06 15:30:09 +08:00
|
|
|
|
_max_cache_size = 512 + 1.2 * buffer->size();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
mpeg_muxer_input((::mpeg_muxer_t *)_context, track_id, have_idr ? 0x0001 : 0, pts * 90LL,dts * 90LL, buffer->data(), buffer->size());
|
2022-01-06 15:30:09 +08:00
|
|
|
|
flushCache();
|
2021-12-28 21:04:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case CodecAAC: {
|
|
|
|
|
|
if (frame->prefixSize() == 0) {
|
|
|
|
|
|
WarnL << "必须提供adts头才能mpeg-ts打包";
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
|
if (!_have_video) {
|
|
|
|
|
|
//没有视频时,才以音频时间戳为TS的时间戳
|
2022-08-08 17:13:39 +08:00
|
|
|
|
_timestamp = frame->dts();
|
2021-12-28 21:04:53 +08:00
|
|
|
|
}
|
2022-01-06 15:30:09 +08:00
|
|
|
|
_max_cache_size = 512 + 1.2 * frame->size();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
mpeg_muxer_input((::mpeg_muxer_t *)_context, track_id, frame->keyFrame() ? 0x0001 : 0, frame->pts() * 90LL, frame->dts() * 90LL, frame->data(), frame->size());
|
2022-01-06 15:30:09 +08:00
|
|
|
|
flushCache();
|
2021-12-28 21:04:53 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MpegMuxer::resetTracks() {
|
|
|
|
|
|
_have_video = false;
|
|
|
|
|
|
//通知片段中断
|
|
|
|
|
|
onWrite(nullptr, _timestamp, false);
|
|
|
|
|
|
releaseContext();
|
|
|
|
|
|
createContext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MpegMuxer::createContext() {
|
|
|
|
|
|
static mpeg_muxer_func_t func = {
|
|
|
|
|
|
/*alloc*/
|
|
|
|
|
|
[](void *param, size_t bytes) {
|
2022-01-06 15:30:09 +08:00
|
|
|
|
MpegMuxer *thiz = (MpegMuxer *)param;
|
|
|
|
|
|
if (!thiz->_current_buffer
|
|
|
|
|
|
|| thiz->_current_buffer->size() + bytes > thiz->_current_buffer->getCapacity()) {
|
|
|
|
|
|
if (thiz->_current_buffer) {
|
|
|
|
|
|
//WarnL << "need realloc mpeg buffer" << thiz->_current_buffer->size() + bytes << " > " << thiz->_current_buffer->getCapacity();
|
|
|
|
|
|
thiz->flushCache();
|
|
|
|
|
|
}
|
|
|
|
|
|
thiz->_current_buffer = thiz->_buffer_pool.obtain2();
|
|
|
|
|
|
thiz->_current_buffer->setSize(0);
|
|
|
|
|
|
thiz->_current_buffer->setCapacity(MAX(thiz->_max_cache_size, bytes));
|
|
|
|
|
|
}
|
|
|
|
|
|
return (void *)(thiz->_current_buffer->data() + thiz->_current_buffer->size());
|
2021-12-28 21:04:53 +08:00
|
|
|
|
},
|
|
|
|
|
|
/*free*/
|
|
|
|
|
|
[](void *param, void *packet) {
|
|
|
|
|
|
//什么也不做
|
|
|
|
|
|
},
|
|
|
|
|
|
/*wtite*/
|
|
|
|
|
|
[](void *param, int stream, void *packet, size_t bytes) {
|
|
|
|
|
|
MpegMuxer *thiz = (MpegMuxer *) param;
|
|
|
|
|
|
thiz->onWrite_l(packet, bytes);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
if (_context == nullptr) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
_context = (struct mpeg_muxer_t *)mpeg_muxer_create(_is_ps, &func, this);
|
2021-12-28 21:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MpegMuxer::onWrite_l(const void *packet, size_t bytes) {
|
2022-01-06 15:30:09 +08:00
|
|
|
|
assert(_current_buffer && _current_buffer->data() + _current_buffer->size() == packet);
|
|
|
|
|
|
_current_buffer->setSize(_current_buffer->size() + bytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MpegMuxer::flushCache() {
|
2021-12-31 11:01:56 +08:00
|
|
|
|
onWrite(std::move(_current_buffer), _timestamp, _key_pos);
|
2021-12-28 21:04:53 +08:00
|
|
|
|
_key_pos = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MpegMuxer::releaseContext() {
|
|
|
|
|
|
if (_context) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
mpeg_muxer_destroy((::mpeg_muxer_t *)_context);
|
2021-12-28 21:04:53 +08:00
|
|
|
|
_context = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
_codec_to_trackid.clear();
|
|
|
|
|
|
_frame_merger.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
void MpegMuxer::flush() {
|
|
|
|
|
|
_frame_merger.flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-28 21:04:53 +08:00
|
|
|
|
}//mediakit
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|