mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-28 05:29:24 +08:00
可以自定义选择编译某特性
This commit is contained in:
@@ -52,12 +52,14 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp,
|
||||
strVhost = DEFAULT_VHOST;
|
||||
}
|
||||
|
||||
#if defined(ENABLE_HLS)
|
||||
if(enableHls) {
|
||||
auto m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
|
||||
_hlsMaker.reset(new HlsRecorder(m3u8FilePath,string(VHOST_KEY) + "=" + strVhost ,hlsBufSize, hlsDuration, hlsNum));
|
||||
}
|
||||
#endif //defined(ENABLE_HLS)
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
#if defined(ENABLE_MP4V2)
|
||||
GET_CONFIG_AND_REGISTER(string,recordPath,Record::kFilePath);
|
||||
GET_CONFIG_AND_REGISTER(string,recordAppName,Record::kAppName);
|
||||
|
||||
@@ -65,32 +67,38 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp,
|
||||
auto mp4FilePath = recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/" + strId + "/";
|
||||
_mp4Maker.reset(new Mp4Maker(mp4FilePath,strVhost,strApp,strId));
|
||||
}
|
||||
#endif //ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_MP4V2)
|
||||
}
|
||||
|
||||
MediaRecorder::~MediaRecorder() {
|
||||
}
|
||||
|
||||
void MediaRecorder::inputFrame(const Frame::Ptr &frame) {
|
||||
#if defined(ENABLE_HLS)
|
||||
if (_hlsMaker) {
|
||||
_hlsMaker->inputFrame(frame);
|
||||
}
|
||||
#ifdef ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_HLS)
|
||||
|
||||
#if defined(ENABLE_MP4V2)
|
||||
if (_mp4Maker) {
|
||||
_mp4Maker->inputFrame(frame);
|
||||
}
|
||||
#endif //ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_MP4V2)
|
||||
}
|
||||
|
||||
void MediaRecorder::addTrack(const Track::Ptr &track) {
|
||||
#if defined(ENABLE_HLS)
|
||||
if (_hlsMaker) {
|
||||
_hlsMaker->addTrack(track);
|
||||
}
|
||||
#ifdef ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_HLS)
|
||||
|
||||
#if defined(ENABLE_MP4V2)
|
||||
if (_mp4Maker) {
|
||||
_mp4Maker->addTrack(track);
|
||||
}
|
||||
#endif //ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_MP4V2)
|
||||
}
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -30,12 +30,9 @@
|
||||
#include <memory>
|
||||
#include "Player/PlayerBase.h"
|
||||
#include "Common/MediaSink.h"
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
#include "Mp4Maker.h"
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
#include "HlsRecorder.h"
|
||||
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
@@ -63,11 +60,13 @@ public:
|
||||
*/
|
||||
void addTrack(const Track::Ptr & track) override;
|
||||
private:
|
||||
#if defined(ENABLE_HLS)
|
||||
std::shared_ptr<HlsRecorder> _hlsMaker;
|
||||
#ifdef ENABLE_MP4V2
|
||||
std::shared_ptr<Mp4Maker> _mp4Maker;
|
||||
#endif //ENABLE_MP4V2
|
||||
#endif //defined(ENABLE_HLS)
|
||||
|
||||
#if defined(ENABLE_MP4V2)
|
||||
std::shared_ptr<Mp4Maker> _mp4Maker;
|
||||
#endif //defined(ENABLE_MP4V2)
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -25,8 +25,9 @@
|
||||
*/
|
||||
|
||||
#include "TsMuxer.h"
|
||||
#include <assert.h>
|
||||
#if defined(ENABLE_HLS)
|
||||
#include "mpeg-ts-proto.h"
|
||||
#include "mpeg-ts.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
@@ -121,4 +122,6 @@ void TsMuxer::uninit() {
|
||||
_codecid_to_stream_id.clear();
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
}//namespace mediakit
|
||||
|
||||
#endif// defined(ENABLE_HLS)
|
||||
@@ -28,7 +28,6 @@
|
||||
#define TSMUXER_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include "mpeg-ts.h"
|
||||
#include "Extension/Frame.h"
|
||||
#include "Extension/Track.h"
|
||||
#include "Util/File.h"
|
||||
@@ -56,136 +55,5 @@ private:
|
||||
List<Frame::Ptr> _frameCached;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class TsMuxer2{
|
||||
public:
|
||||
typedef function<void(const void *packet, int bytes)> onTsCallback;
|
||||
TsMuxer2(){
|
||||
init();
|
||||
}
|
||||
~TsMuxer2(){
|
||||
uninit();
|
||||
}
|
||||
|
||||
bool addTrack(int track,int codec_id){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
auto it = _allTrackMap.find(track);
|
||||
if(it != _allTrackMap.end()){
|
||||
// WarnL << "Track:" << track << "已经存在!";
|
||||
return false;
|
||||
}
|
||||
_allTrackMap[track] = codec_id;
|
||||
resetAllTracks();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool removeTrack(int track){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
auto it = _allTrackMap.find(track);
|
||||
if(it == _allTrackMap.end()){
|
||||
// WarnL << "Track:" << track << "不存在!";
|
||||
return false;
|
||||
}
|
||||
DebugL << "删除Track:" << track;
|
||||
_allTrackMap.erase(it);
|
||||
resetAllTracks();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool inputTrackData(int track, const char *data, int length, int64_t pts, int64_t dts, int flags){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
auto it = _track_id_to_stream_id.find(track);
|
||||
if(it == _track_id_to_stream_id.end()){
|
||||
WarnL << "Track:" << track << "不存在!";
|
||||
return false;
|
||||
}
|
||||
mpeg_ts_write(_context,it->second,flags,pts,dts,data,length);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setOnTsCallback(const onTsCallback &cb) {
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
_onts = cb;
|
||||
}
|
||||
|
||||
bool saveToFile(const string &file){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
FILE *fp = File::createfile_file(file.data(),"ab");
|
||||
if(!fp){
|
||||
WarnL << "打开文件失败:" << file << " " << get_uv_errmsg();
|
||||
return false;
|
||||
}
|
||||
setvbuf(fp, _file_buf, _IOFBF, sizeof(_file_buf));
|
||||
_file.reset(fp,[](FILE *fp){
|
||||
fclose(fp);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
private:
|
||||
void init() {
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
static mpeg_ts_func_t s_func= {
|
||||
[](void* param, size_t bytes){
|
||||
TsMuxer2 *muxer = (TsMuxer2 *)param;
|
||||
assert(sizeof(TsMuxer2::_tsbuf) >= bytes);
|
||||
return (void *)muxer->_tsbuf;
|
||||
},
|
||||
[](void* param, void* packet){
|
||||
//do nothing
|
||||
},
|
||||
[](void* param, const void* packet, size_t bytes){
|
||||
TsMuxer2 *muxer = (TsMuxer2 *)param;
|
||||
muxer->onTs(packet, bytes);
|
||||
}
|
||||
};
|
||||
if(_context == nullptr){
|
||||
_context = mpeg_ts_create(&s_func,this);
|
||||
}
|
||||
}
|
||||
|
||||
void uninit() {
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
if(_context){
|
||||
mpeg_ts_destroy(_context);
|
||||
_context = nullptr;
|
||||
}
|
||||
_track_id_to_stream_id.clear();
|
||||
}
|
||||
|
||||
void resetAllTracks(){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
uninit();
|
||||
init();
|
||||
|
||||
//添加Track
|
||||
for (auto &pr : _allTrackMap){
|
||||
InfoL << "添加Track:" << pr.first << " " << pr.second;
|
||||
_track_id_to_stream_id[pr.first] = mpeg_ts_add_stream(_context,pr.second, nullptr,0);
|
||||
}
|
||||
}
|
||||
|
||||
void onTs(const void *packet, int bytes) {
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
if(_onts){
|
||||
_onts(packet,bytes);
|
||||
}
|
||||
|
||||
if(_file){
|
||||
fwrite(packet,bytes,1,_file.get());
|
||||
}
|
||||
}
|
||||
private:
|
||||
void *_context = nullptr;
|
||||
char *_tsbuf[188];
|
||||
unordered_map<int,int > _track_id_to_stream_id;
|
||||
unordered_map<int,int > _allTrackMap;
|
||||
recursive_mutex _mtx;
|
||||
onTsCallback _onts;
|
||||
|
||||
char _file_buf[64 * 1024];
|
||||
std::shared_ptr<FILE> _file;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
#endif //TSMUXER_H
|
||||
|
||||
Reference in New Issue
Block a user