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-08-01 18:49:04 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-08-01 18:49:04 +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-08-01 18:49:04 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2023-07-02 12:02:33 +08:00
|
|
|
|
#if defined(ENABLE_MP4) || defined(ENABLE_HLS_FMP4)
|
2021-12-16 17:46:03 +08:00
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
#include "MP4Muxer.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Extension/AAC.h"
|
|
|
|
|
#include "Extension/G711.h"
|
2020-07-16 16:26:13 +08:00
|
|
|
|
#include "Extension/H264.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Extension/H265.h"
|
2022-12-30 13:56:57 +08:00
|
|
|
|
#include "Extension/JPEG.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.h"
|
2021-12-16 17:46:03 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
namespace mediakit {
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
MP4Muxer::~MP4Muxer() {
|
|
|
|
|
closeMP4();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4Muxer::openMP4(const string &file) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
closeMP4();
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_file_name = file;
|
|
|
|
|
_mp4_file = std::make_shared<MP4FileDisk>();
|
|
|
|
|
_mp4_file->openFile(_file_name.data(), "wb+");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
MP4FileIO::Writer MP4Muxer::createWriter() {
|
2020-09-20 19:44:20 +08:00
|
|
|
|
GET_CONFIG(bool, mp4FastStart, Record::kFastStart);
|
2020-09-20 19:45:37 +08:00
|
|
|
|
return _mp4_file->createWriter(mp4FastStart ? MOV_FLAG_FASTSTART : 0, false);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}
|
2020-09-20 19:45:04 +08:00
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4Muxer::closeMP4() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
MP4MuxerInterface::resetTracks();
|
|
|
|
|
_mp4_file = nullptr;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 12:32:57 +08:00
|
|
|
|
void MP4Muxer::resetTracks() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
MP4MuxerInterface::resetTracks();
|
|
|
|
|
openMP4(_file_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////// MP4MuxerInterface /////////////////////////////////////////////
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4MuxerInterface::saveSegment() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
mp4_writer_save_segment(_mov_writter.get());
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4MuxerInterface::initSegment() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
mp4_writer_init_segment(_mov_writter.get());
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
bool MP4MuxerInterface::haveVideo() const {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
return _have_video;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 16:40:21 +08:00
|
|
|
|
uint64_t MP4MuxerInterface::getDuration() const {
|
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
for (auto &pr : _codec_to_trackid) {
|
2022-11-12 21:59:48 +08:00
|
|
|
|
if (pr.second.stamp.getRelativeStamp() > (int64_t)ret) {
|
2022-10-16 16:40:21 +08:00
|
|
|
|
ret = pr.second.stamp.getRelativeStamp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:37 +08:00
|
|
|
|
void MP4MuxerInterface::resetTracks() {
|
2019-12-03 12:32:57 +08:00
|
|
|
|
_started = false;
|
2020-01-24 21:09:47 +08:00
|
|
|
|
_have_video = false;
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_mov_writter = nullptr;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
_frame_merger.clear();
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_codec_to_trackid.clear();
|
2019-12-03 12:32:57 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
void MP4MuxerInterface::flush() {
|
|
|
|
|
_frame_merger.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool MP4MuxerInterface::inputFrame(const Frame::Ptr &frame) {
|
2019-08-01 18:49:04 +08:00
|
|
|
|
auto it = _codec_to_trackid.find(frame->getCodecId());
|
2021-12-16 17:46:03 +08:00
|
|
|
|
if (it == _codec_to_trackid.end()) {
|
2019-08-01 21:59:26 +08:00
|
|
|
|
//该Track不存在或初始化失败
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 21:09:47 +08:00
|
|
|
|
if (!_started) {
|
2020-08-30 09:15:02 +08:00
|
|
|
|
//该逻辑确保含有视频时,第一帧为关键帧
|
|
|
|
|
if (_have_video && !frame->keyFrame()) {
|
|
|
|
|
//含有视频,但是不是关键帧,那么前面的帧丢弃
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-02 10:53:00 +08:00
|
|
|
|
}
|
2020-08-30 09:15:02 +08:00
|
|
|
|
//开始写文件
|
|
|
|
|
_started = true;
|
2019-08-02 10:53:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 21:59:26 +08:00
|
|
|
|
//mp4文件时间戳需要从0开始
|
|
|
|
|
auto &track_info = it->second;
|
2020-01-03 14:27:26 +08:00
|
|
|
|
switch (frame->getCodecId()) {
|
2021-07-07 11:37:22 +08:00
|
|
|
|
case CodecH264:
|
2020-01-03 14:27:26 +08:00
|
|
|
|
case CodecH265: {
|
|
|
|
|
//这里的代码逻辑是让SPS、PPS、IDR这些时间戳相同的帧打包到一起当做一个帧处理,
|
2022-10-16 19:49:56 +08:00
|
|
|
|
_frame_merger.inputFrame(frame, [this, &track_info](uint64_t dts, uint64_t pts, const Buffer::Ptr &buffer, bool have_idr) {
|
|
|
|
|
int64_t dts_out, pts_out;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
track_info.stamp.revise(dts, pts, dts_out, pts_out);
|
|
|
|
|
mp4_writer_write(_mov_writter.get(),
|
|
|
|
|
track_info.track_id,
|
|
|
|
|
buffer->data(),
|
|
|
|
|
buffer->size(),
|
|
|
|
|
pts_out,
|
|
|
|
|
dts_out,
|
|
|
|
|
have_idr ? MOV_AV_FLAG_KEYFREAME : 0);
|
|
|
|
|
});
|
2020-01-03 14:27:26 +08:00
|
|
|
|
break;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
}
|
2023-04-19 19:40:58 +08:00
|
|
|
|
case CodecJPEG:{
|
|
|
|
|
int64_t dts_out, pts_out;
|
|
|
|
|
track_info.stamp.revise(frame->dts(), frame->pts(), dts_out, pts_out);
|
|
|
|
|
mp4_writer_write(_mov_writter.get(),
|
|
|
|
|
track_info.track_id,
|
|
|
|
|
frame->data(),
|
|
|
|
|
frame->size(),
|
|
|
|
|
pts_out,
|
|
|
|
|
dts_out,
|
|
|
|
|
frame->keyFrame() ? MOV_AV_FLAG_KEYFREAME : 0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-26 18:26:07 +08:00
|
|
|
|
|
2020-01-03 14:27:26 +08:00
|
|
|
|
default: {
|
2022-10-16 19:49:56 +08:00
|
|
|
|
int64_t dts_out, pts_out;
|
2020-01-03 14:27:26 +08:00
|
|
|
|
track_info.stamp.revise(frame->dts(), frame->pts(), dts_out, pts_out);
|
2020-09-20 19:44:20 +08:00
|
|
|
|
mp4_writer_write(_mov_writter.get(),
|
|
|
|
|
track_info.track_id,
|
|
|
|
|
frame->data() + frame->prefixSize(),
|
|
|
|
|
frame->size() - frame->prefixSize(),
|
|
|
|
|
pts_out,
|
|
|
|
|
dts_out,
|
|
|
|
|
frame->keyFrame() ? MOV_AV_FLAG_KEYFREAME : 0);
|
2020-01-03 14:27:26 +08:00
|
|
|
|
break;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
}
|
2020-01-03 14:27:26 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return true;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
static uint8_t getObject(CodecId codecId) {
|
2020-05-12 11:48:15 +08:00
|
|
|
|
switch (codecId){
|
|
|
|
|
case CodecG711A : return MOV_OBJECT_G711a;
|
|
|
|
|
case CodecG711U : return MOV_OBJECT_G711u;
|
|
|
|
|
case CodecOpus : return MOV_OBJECT_OPUS;
|
|
|
|
|
case CodecAAC : return MOV_OBJECT_AAC;
|
|
|
|
|
case CodecH264 : return MOV_OBJECT_H264;
|
|
|
|
|
case CodecH265 : return MOV_OBJECT_HEVC;
|
2022-12-30 13:56:57 +08:00
|
|
|
|
case CodecJPEG : return MOV_OBJECT_JPEG;
|
2020-05-12 11:48:15 +08:00
|
|
|
|
default : return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4MuxerInterface::stampSync() {
|
|
|
|
|
if (_codec_to_trackid.size() < 2) {
|
2020-05-15 18:08:54 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stamp *audio = nullptr, *video = nullptr;
|
|
|
|
|
for(auto &pr : _codec_to_trackid){
|
|
|
|
|
switch (getTrackType((CodecId) pr.first)){
|
|
|
|
|
case TrackAudio : audio = &pr.second.stamp; break;
|
|
|
|
|
case TrackVideo : video = &pr.second.stamp; break;
|
|
|
|
|
default : break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
if (audio && video) {
|
2020-05-15 18:08:54 +08:00
|
|
|
|
//音频时间戳同步于视频,因为音频时间戳被修改后不影响播放
|
|
|
|
|
audio->syncTo(*video);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool MP4MuxerInterface::addTrack(const Track::Ptr &track) {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (!_mov_writter) {
|
|
|
|
|
_mov_writter = createWriter();
|
|
|
|
|
}
|
2020-05-12 11:48:15 +08:00
|
|
|
|
auto mp4_object = getObject(track->getCodecId());
|
|
|
|
|
if (!mp4_object) {
|
|
|
|
|
WarnL << "MP4录制不支持该编码格式:" << track->getCodecName();
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-05-12 11:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!track->ready()) {
|
|
|
|
|
WarnL << "Track[" << track->getCodecName() << "]未就绪";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-05-12 11:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
switch (track->getCodecId()) {
|
2020-04-23 15:19:20 +08:00
|
|
|
|
case CodecG711A:
|
2020-05-12 11:48:15 +08:00
|
|
|
|
case CodecG711U:
|
|
|
|
|
case CodecOpus: {
|
|
|
|
|
auto audio_track = dynamic_pointer_cast<AudioTrack>(track);
|
2020-04-23 15:19:20 +08:00
|
|
|
|
if (!audio_track) {
|
2020-05-12 11:48:15 +08:00
|
|
|
|
WarnL << "不是音频Track:" << track->getCodecName();
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-04-23 15:19:20 +08:00
|
|
|
|
}
|
2020-05-12 11:48:15 +08:00
|
|
|
|
|
2020-09-20 19:44:20 +08:00
|
|
|
|
auto track_id = mp4_writer_add_audio(_mov_writter.get(),
|
2020-05-12 11:48:15 +08:00
|
|
|
|
mp4_object,
|
2020-04-23 15:19:20 +08:00
|
|
|
|
audio_track->getAudioChannel(),
|
|
|
|
|
audio_track->getAudioSampleBit() * audio_track->getAudioChannel(),
|
|
|
|
|
audio_track->getAudioSampleRate(),
|
|
|
|
|
nullptr, 0);
|
|
|
|
|
if (track_id < 0) {
|
2020-05-12 11:48:15 +08:00
|
|
|
|
WarnL << "添加Track[" << track->getCodecName() << "]失败:" << track_id;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-04-23 15:19:20 +08:00
|
|
|
|
}
|
|
|
|
|
_codec_to_trackid[track->getCodecId()].track_id = track_id;
|
|
|
|
|
break;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
}
|
2020-04-23 15:19:20 +08:00
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
case CodecAAC: {
|
2020-04-23 15:19:20 +08:00
|
|
|
|
auto audio_track = dynamic_pointer_cast<AACTrack>(track);
|
|
|
|
|
if (!audio_track) {
|
2019-08-01 18:49:04 +08:00
|
|
|
|
WarnL << "不是AAC Track";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
2020-05-12 11:48:15 +08:00
|
|
|
|
|
2020-09-20 19:44:20 +08:00
|
|
|
|
auto track_id = mp4_writer_add_audio(_mov_writter.get(),
|
2020-05-12 11:48:15 +08:00
|
|
|
|
mp4_object,
|
2020-04-23 15:19:20 +08:00
|
|
|
|
audio_track->getAudioChannel(),
|
|
|
|
|
audio_track->getAudioSampleBit() * audio_track->getAudioChannel(),
|
|
|
|
|
audio_track->getAudioSampleRate(),
|
2020-06-11 17:19:10 +08:00
|
|
|
|
audio_track->getAacCfg().data(),
|
|
|
|
|
audio_track->getAacCfg().size());
|
2021-12-16 17:46:03 +08:00
|
|
|
|
if (track_id < 0) {
|
2019-08-01 21:59:26 +08:00
|
|
|
|
WarnL << "添加AAC Track失败:" << track_id;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 21:59:26 +08:00
|
|
|
|
}
|
2019-08-22 16:54:30 +08:00
|
|
|
|
_codec_to_trackid[track->getCodecId()].track_id = track_id;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
break;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
case CodecH264: {
|
|
|
|
|
auto h264_track = dynamic_pointer_cast<H264Track>(track);
|
|
|
|
|
if (!h264_track) {
|
|
|
|
|
WarnL << "不是H264 Track";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:36:07 +08:00
|
|
|
|
struct mpeg4_avc_t avc;
|
|
|
|
|
memset(&avc, 0, sizeof(avc));
|
2019-08-01 18:49:04 +08:00
|
|
|
|
string sps_pps = string("\x00\x00\x00\x01", 4) + h264_track->getSps() +
|
|
|
|
|
string("\x00\x00\x00\x01", 4) + h264_track->getPps();
|
2021-12-16 17:46:03 +08:00
|
|
|
|
h264_annexbtomp4(&avc, sps_pps.data(), (int) sps_pps.size(), NULL, 0, NULL, NULL);
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
uint8_t extra_data[1024];
|
|
|
|
|
int extra_data_size = mpeg4_avc_decoder_configuration_record_save(&avc, extra_data, sizeof(extra_data));
|
|
|
|
|
if (extra_data_size == -1) {
|
|
|
|
|
WarnL << "生成H264 extra_data 失败";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:44:20 +08:00
|
|
|
|
auto track_id = mp4_writer_add_video(_mov_writter.get(),
|
2020-05-12 11:48:15 +08:00
|
|
|
|
mp4_object,
|
2019-08-01 18:49:04 +08:00
|
|
|
|
h264_track->getVideoWidth(),
|
|
|
|
|
h264_track->getVideoHeight(),
|
|
|
|
|
extra_data,
|
|
|
|
|
extra_data_size);
|
2019-08-01 21:59:26 +08:00
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
if (track_id < 0) {
|
2019-08-01 21:59:26 +08:00
|
|
|
|
WarnL << "添加H264 Track失败:" << track_id;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 21:59:26 +08:00
|
|
|
|
}
|
2019-08-22 16:54:30 +08:00
|
|
|
|
_codec_to_trackid[track->getCodecId()].track_id = track_id;
|
2020-01-24 21:09:47 +08:00
|
|
|
|
_have_video = true;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
break;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
case CodecH265: {
|
|
|
|
|
auto h265_track = dynamic_pointer_cast<H265Track>(track);
|
|
|
|
|
if (!h265_track) {
|
|
|
|
|
WarnL << "不是H265 Track";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-12-18 15:02:55 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
2022-08-08 17:36:07 +08:00
|
|
|
|
struct mpeg4_hevc_t hevc;
|
|
|
|
|
memset(&hevc, 0, sizeof(hevc));
|
2019-08-01 18:49:04 +08:00
|
|
|
|
string vps_sps_pps = string("\x00\x00\x00\x01", 4) + h265_track->getVps() +
|
|
|
|
|
string("\x00\x00\x00\x01", 4) + h265_track->getSps() +
|
|
|
|
|
string("\x00\x00\x00\x01", 4) + h265_track->getPps();
|
2021-12-16 17:46:03 +08:00
|
|
|
|
h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int) vps_sps_pps.size(), NULL, 0, NULL, NULL);
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
uint8_t extra_data[1024];
|
|
|
|
|
int extra_data_size = mpeg4_hevc_decoder_configuration_record_save(&hevc, extra_data, sizeof(extra_data));
|
|
|
|
|
if (extra_data_size == -1) {
|
|
|
|
|
WarnL << "生成H265 extra_data 失败";
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:44:20 +08:00
|
|
|
|
auto track_id = mp4_writer_add_video(_mov_writter.get(),
|
2020-05-12 11:48:15 +08:00
|
|
|
|
mp4_object,
|
2019-08-01 18:49:04 +08:00
|
|
|
|
h265_track->getVideoWidth(),
|
|
|
|
|
h265_track->getVideoHeight(),
|
|
|
|
|
extra_data,
|
|
|
|
|
extra_data_size);
|
2021-12-16 17:46:03 +08:00
|
|
|
|
if (track_id < 0) {
|
2019-08-01 21:59:26 +08:00
|
|
|
|
WarnL << "添加H265 Track失败:" << track_id;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2019-08-01 21:59:26 +08:00
|
|
|
|
}
|
2019-08-22 16:54:30 +08:00
|
|
|
|
_codec_to_trackid[track->getCodecId()].track_id = track_id;
|
2020-01-24 21:09:47 +08:00
|
|
|
|
_have_video = true;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
break;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
}
|
2020-05-12 11:48:15 +08:00
|
|
|
|
|
2022-12-30 13:56:57 +08:00
|
|
|
|
case CodecJPEG: {
|
|
|
|
|
auto jpeg_track = dynamic_pointer_cast<JPEGTrack>(track);
|
|
|
|
|
if (!jpeg_track) {
|
|
|
|
|
WarnL << "不是JPEG Track";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto track_id = mp4_writer_add_video(_mov_writter.get(),
|
|
|
|
|
mp4_object,
|
|
|
|
|
jpeg_track->getVideoWidth(),
|
|
|
|
|
jpeg_track->getVideoHeight(),
|
|
|
|
|
nullptr,
|
|
|
|
|
0);
|
|
|
|
|
if (track_id < 0) {
|
|
|
|
|
WarnL << "添加JPEG Track失败:" << track_id;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_codec_to_trackid[track->getCodecId()].track_id = track_id;
|
|
|
|
|
_have_video = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
default: WarnL << "MP4录制不支持该编码格式:" << track->getCodecName(); return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
2020-05-15 18:08:54 +08:00
|
|
|
|
|
|
|
|
|
//尝试音视频同步
|
|
|
|
|
stampSync();
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return true;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:37 +08:00
|
|
|
|
/////////////////////////////////////////// MP4MuxerMemory /////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
MP4MuxerMemory::MP4MuxerMemory() {
|
|
|
|
|
_memory_file = std::make_shared<MP4FileMemory>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MP4FileIO::Writer MP4MuxerMemory::createWriter() {
|
|
|
|
|
return _memory_file->createWriter(MOV_FLAG_SEGMENT, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
const string &MP4MuxerMemory::getInitSegment() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_init_segment.empty()) {
|
|
|
|
|
initSegment();
|
|
|
|
|
saveSegment();
|
|
|
|
|
_init_segment = _memory_file->getAndClearMemory();
|
|
|
|
|
}
|
|
|
|
|
return _init_segment;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
void MP4MuxerMemory::resetTracks() {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
MP4MuxerInterface::resetTracks();
|
|
|
|
|
_memory_file = std::make_shared<MP4FileMemory>();
|
|
|
|
|
_init_segment.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 17:46:03 +08:00
|
|
|
|
bool MP4MuxerMemory::inputFrame(const Frame::Ptr &frame) {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_init_segment.empty()) {
|
|
|
|
|
//尚未生成init segment
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-09-20 19:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 12:02:33 +08:00
|
|
|
|
// flush切片
|
2021-12-16 17:46:03 +08:00
|
|
|
|
saveSegment();
|
|
|
|
|
|
2021-12-16 12:09:49 +08:00
|
|
|
|
auto data = _memory_file->getAndClearMemory();
|
|
|
|
|
if (!data.empty()) {
|
2023-07-02 12:02:33 +08:00
|
|
|
|
// 输出切片数据
|
|
|
|
|
onSegmentData(std::move(data), _last_dst, _key_frame);
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_key_frame = false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 12:02:33 +08:00
|
|
|
|
if (frame->keyFrame()) {
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_key_frame = true;
|
|
|
|
|
}
|
2023-07-02 12:02:33 +08:00
|
|
|
|
if (frame->getTrackType() == TrackVideo || !haveVideo()) {
|
|
|
|
|
_last_dst = frame->dts();
|
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return MP4MuxerInterface::inputFrame(frame);
|
2020-09-20 19:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}//namespace mediakit
|
2023-07-02 12:02:33 +08:00
|
|
|
|
#endif //defined(ENABLE_MP4) || defined(ENABLE_HLS_FMP4)
|