ZLMediaKit/src/Record/MP4Muxer.h

153 lines
3.1 KiB
C++
Raw Normal View History

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
*
* 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
*/
#ifndef ZLMEDIAKIT_MP4MUXER_H
#define ZLMEDIAKIT_MP4MUXER_H
2020-04-03 20:45:58 +08:00
#ifdef ENABLE_MP4
2019-08-01 18:49:04 +08:00
#include "Common/MediaSink.h"
#include "Extension/AAC.h"
#include "Extension/G711.h"
2019-08-01 18:49:04 +08:00
#include "Extension/H264.h"
#include "Extension/H265.h"
2019-12-04 10:45:38 +08:00
#include "Common/Stamp.h"
2020-04-03 20:45:58 +08:00
#include "MP4.h"
2019-08-01 18:49:04 +08:00
namespace mediakit{
class MP4MuxerInterface : public MediaSinkInterface {
2019-08-01 18:49:04 +08:00
public:
MP4MuxerInterface() = default;
~MP4MuxerInterface() override = default;
2019-12-03 12:32:57 +08:00
/**
* ready状态的track
*/
bool addTrack(const Track::Ptr &track) override;
2019-08-01 18:49:04 +08:00
/**
2019-12-03 12:32:57 +08:00
*
2019-08-01 18:49:04 +08:00
*/
bool inputFrame(const Frame::Ptr &frame) override;
2019-08-01 18:49:04 +08:00
/**
2019-12-03 12:32:57 +08:00
* track
2019-08-01 18:49:04 +08:00
*/
void resetTracks() override;
2020-04-03 20:45:58 +08:00
2020-09-20 19:45:04 +08:00
/**
*
2020-09-20 19:45:04 +08:00
*/
bool haveVideo() const;
2020-09-20 19:45:04 +08:00
2020-07-16 10:40:30 +08:00
/**
* fmp4分片
2020-07-16 10:40:30 +08:00
*/
void saveSegment();
/**
*
*/
void initSegment();
protected:
virtual MP4FileIO::Writer createWriter() = 0;
2020-07-16 10:40:30 +08:00
2020-04-03 20:45:58 +08:00
private:
2020-05-15 18:08:54 +08:00
void stampSync();
2020-04-03 20:45:58 +08:00
2019-08-01 18:49:04 +08:00
private:
bool _started = false;
bool _have_video = false;
MP4FileIO::Writer _mov_writter;
2020-05-15 18:08:54 +08:00
struct track_info {
2019-08-01 21:59:26 +08:00
int track_id = -1;
Stamp stamp;
2019-08-01 21:59:26 +08:00
};
unordered_map<int, track_info> _codec_to_trackid;
2021-04-26 18:26:07 +08:00
FrameMerger _frame_merger{FrameMerger::mp4_nal_size};
};
class MP4Muxer : public MP4MuxerInterface{
public:
typedef std::shared_ptr<MP4Muxer> Ptr;
MP4Muxer();
~MP4Muxer() override;
/**
* track
*/
void resetTracks() override;
/**
* mp4
* @param file
*/
void openMP4(const string &file);
/**
* ()
*/
void closeMP4();
protected:
MP4FileIO::Writer createWriter() override;
private:
2019-12-03 12:32:57 +08:00
string _file_name;
MP4FileDisk::Ptr _mp4_file;
2019-08-01 18:49:04 +08:00
};
class MP4MuxerMemory : public MP4MuxerInterface{
public:
MP4MuxerMemory();
~MP4MuxerMemory() override = default;
/**
* track
*/
void resetTracks() override;
/**
*
*/
bool inputFrame(const Frame::Ptr &frame) override;
/**
* fmp4 init segment
*/
const string &getInitSegment();
protected:
/**
* fmp4切片回调函数
* @param string
* @param stamp
* @param key_frame
*/
virtual void onSegmentData(const string &string, uint32_t stamp, bool key_frame) = 0;
protected:
MP4FileIO::Writer createWriter() override;
private:
bool _key_frame = false;
Ticker _ticker;
string _init_segment;
MP4FileMemory::Ptr _memory_file;
};
2019-08-01 18:49:04 +08:00
}//namespace mediakit
2020-04-03 20:45:58 +08:00
#endif//#ifdef ENABLE_MP4
2019-08-01 18:49:04 +08:00
#endif //ZLMEDIAKIT_MP4MUXER_H