添加更多录制相关方法

This commit is contained in:
xiongziliang 2020-02-01 22:58:58 +08:00
parent e8ba94ba09
commit 51e0c71007
2 changed files with 80 additions and 76 deletions

View File

@ -34,67 +34,42 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
MediaSinkInterface *createHlsRecorder(const string &strVhost_tmp, const string &strApp, const string &strId, const string &customized_path) { string Recorder::getRecordPath(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path) {
#if defined(ENABLE_HLS)
GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(bool, enableVhost, General::kEnableVhost);
GET_CONFIG(string, hlsPath, Hls::kFilePath); switch (type) {
case Recorder::type_hls: {
string strVhost = strVhost_tmp; GET_CONFIG(string, hlsPath, Hls::kFilePath);
if (trim(strVhost).empty()) { string m3u8FilePath;
//如果strVhost为空则强制为默认虚拟主机 if (enableVhost) {
strVhost = DEFAULT_VHOST; m3u8FilePath = vhost + "/" + app + "/" + stream_id + "/hls.m3u8";
} else {
m3u8FilePath = app + "/" + stream_id + "/hls.m3u8";
}
//Here we use the customized file path.
if (!customized_path.empty()) {
m3u8FilePath = customized_path + "/hls.m3u8";
}
return File::absolutePath(m3u8FilePath, hlsPath);
}
case Recorder::type_mp4: {
GET_CONFIG(string, recordPath, Record::kFilePath);
GET_CONFIG(string, recordAppName, Record::kAppName);
string mp4FilePath;
if (enableVhost) {
mp4FilePath = vhost + "/" + recordAppName + "/" + app + "/" + stream_id + "/";
} else {
mp4FilePath = recordAppName + "/" + app + "/" + stream_id + "/";
}
//Here we use the customized file path.
if (!customized_path.empty()) {
mp4FilePath = customized_path + "/";
}
return File::absolutePath(mp4FilePath, recordPath);
}
default:
return "";
} }
string m3u8FilePath;
string params;
if (enableVhost) {
m3u8FilePath = strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
params = string(VHOST_KEY) + "=" + strVhost;
} else {
m3u8FilePath = strApp + "/" + strId + "/hls.m3u8";
}
//Here we use the customized file path.
if(!customized_path.empty()){
m3u8FilePath = customized_path + "/hls.m3u8";
}
m3u8FilePath = File::absolutePath(m3u8FilePath, hlsPath);
auto ret = new HlsRecorder(m3u8FilePath, params);
ret->setMediaSource(strVhost, strApp, strId);
return ret;
#else
return nullptr;
#endif //defined(ENABLE_HLS)
} }
MediaSinkInterface *createMP4Recorder(const string &strVhost_tmp, const string &strApp, const string &strId, const string &customized_path) {
#if defined(ENABLE_MP4RECORD)
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
GET_CONFIG(string, recordPath, Record::kFilePath);
GET_CONFIG(string, recordAppName, Record::kAppName);
string strVhost = strVhost_tmp;
if (trim(strVhost).empty()) {
//如果strVhost为空则强制为默认虚拟主机
strVhost = DEFAULT_VHOST;
}
string mp4FilePath;
if (enableVhost) {
mp4FilePath = strVhost + "/" + recordAppName + "/" + strApp + "/" + strId + "/";
} else {
mp4FilePath = recordAppName + "/" + strApp + "/" + strId + "/";
}
//Here we use the customized file path.
if(!customized_path.empty()){
mp4FilePath = customized_path + "/";
}
mp4FilePath = File::absolutePath(mp4FilePath, recordPath);
return new MP4Recorder(mp4FilePath, strVhost, strApp, strId);
#else
return nullptr;
#endif //defined(ENABLE_MP4RECORD)
}
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
class RecorderHelper { class RecorderHelper {
@ -215,9 +190,10 @@ public:
return -1; return -1;
} }
auto recorder = MediaSinkInterface::Ptr(createRecorder(vhost, app, stream_id, customized_path)); auto recorder = Recorder::createRecorder(type, vhost, app, stream_id, customized_path);
if (!recorder) { if (!recorder) {
// 创建录制器失败 // 创建录制器失败
WarnL << "不支持该录制类型:" << type;
return -2; return -2;
} }
auto helper = std::make_shared<RecorderHelper>(recorder, continueRecord); auto helper = std::make_shared<RecorderHelper>(recorder, continueRecord);
@ -334,23 +310,6 @@ private:
return vhost + "/" + app + "/" + stream_id; return vhost + "/" + app + "/" + stream_id;
} }
MediaSinkInterface *createRecorder(const string &vhost, const string &app, const string &stream_id, const string &customized_path) {
MediaSinkInterface *ret = nullptr;
switch (type) {
case Recorder::type_hls:
ret = createHlsRecorder(vhost, app, stream_id,customized_path);
break;
case Recorder::type_mp4:
ret = createMP4Recorder(vhost, app, stream_id,customized_path);
break;
default:
break;
}
if(!ret){
WarnL << "can not create recorder of type: " << type;
}
return ret;
}
/** /**
* track就绪即可录制 * track就绪即可录制
@ -392,6 +351,29 @@ std::shared_ptr<MediaSinkInterface> Recorder::getRecorder(type type, const strin
return nullptr; return nullptr;
} }
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path){
auto path = Recorder::getRecordPath(type, vhost, app, stream_id);
switch (type) {
case Recorder::type_hls: {
#if defined(ENABLE_HLS)
auto ret = std::make_shared<HlsRecorder>(path, string(VHOST_KEY) + "=" + vhost);
ret->setMediaSource(vhost, app, stream_id);
return ret;
#endif
return nullptr;
}
case Recorder::type_mp4: {
#if defined(ENABLE_MP4RECORD)
return std::make_shared<MP4Recorder>(path, vhost, app, stream_id);
#endif
return nullptr;
}
default:
return nullptr;
}
}
int Recorder::startRecord(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path, bool waitForRecord, bool continueRecord) { int Recorder::startRecord(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path, bool waitForRecord, bool continueRecord) {
switch (type){ switch (type){

View File

@ -53,6 +53,17 @@ public:
type_mp4 = 1 type_mp4 = 1
} type; } type;
/**
*
* @param type hls还是MP4录制
* @param vhost
* @param app
* @param stream_id id
* @param customized_path
* @return
*/
static string getRecordPath(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path = "");
/** /**
* *
* @param type hls还是MP4录制 * @param type hls还是MP4录制
@ -98,6 +109,17 @@ public:
* @param stream_id id * @param stream_id id
*/ */
static std::shared_ptr<MediaSinkInterface> getRecorder(type type, const string &vhost, const string &app, const string &stream_id); static std::shared_ptr<MediaSinkInterface> getRecorder(type type, const string &vhost, const string &app, const string &stream_id);
/**
*
* @param type hls还是MP4录制
* @param vhost
* @param app
* @param stream_id id
* @param customized_path
* @return nullptr
*/
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path);
private: private:
Recorder() = delete; Recorder() = delete;
~Recorder() = delete; ~Recorder() = delete;