mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +08:00
操作MediaSource对象时确保线程安全
This commit is contained in:
parent
97116e1208
commit
09af12a183
@ -699,22 +699,23 @@ void installWebApi() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs
|
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs
|
||||||
api_regist("/index/api/getMediaInfo",[](API_ARGS_MAP){
|
api_regist("/index/api/getMediaInfo",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("schema","vhost","app","stream");
|
CHECK_ARGS("schema","vhost","app","stream");
|
||||||
auto src = MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]);
|
auto src = MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]);
|
||||||
if(!src){
|
if(!src){
|
||||||
val["online"] = false;
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
val = makeMediaSourceJson(*src);
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
val["online"] = true;
|
auto val = makeMediaSourceJson(*src);
|
||||||
val["code"] = API::Success;
|
val["code"] = API::Success;
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//主动关断流,包括关断拉流、推流
|
//主动关断流,包括关断拉流、推流
|
||||||
//测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1
|
//测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1
|
||||||
api_regist("/index/api/close_stream",[](API_ARGS_MAP){
|
api_regist("/index/api/close_stream",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("schema","vhost","app","stream");
|
CHECK_ARGS("schema","vhost","app","stream");
|
||||||
//踢掉推流器
|
//踢掉推流器
|
||||||
@ -722,16 +723,17 @@ void installWebApi() {
|
|||||||
allArgs["vhost"],
|
allArgs["vhost"],
|
||||||
allArgs["app"],
|
allArgs["app"],
|
||||||
allArgs["stream"]);
|
allArgs["stream"]);
|
||||||
if (src) {
|
if (!src) {
|
||||||
bool flag = src->close(allArgs["force"].as<bool>());
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool force = allArgs["force"].as<bool>();
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
bool flag = src->close(force);
|
||||||
val["result"] = flag ? 0 : -1;
|
val["result"] = flag ? 0 : -1;
|
||||||
val["msg"] = flag ? "success" : "close failed";
|
val["msg"] = flag ? "success" : "close failed";
|
||||||
val["code"] = flag ? API::Success : API::OtherFailed;
|
val["code"] = flag ? API::Success : API::OtherFailed;
|
||||||
} else {
|
});
|
||||||
val["result"] = -2;
|
|
||||||
val["msg"] = "can not find the stream";
|
|
||||||
val["code"] = API::OtherFailed;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//批量主动关断流,包括关断拉流、推流
|
//批量主动关断流,包括关断拉流、推流
|
||||||
@ -1105,7 +1107,7 @@ void installWebApi() {
|
|||||||
|
|
||||||
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["from_mp4"].as<int>());
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["from_mp4"].as<int>());
|
||||||
if (!src) {
|
if (!src) {
|
||||||
throw ApiRetException("该媒体流不存在", API::OtherFailed);
|
throw ApiRetException("can not find the source stream", API::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaSourceEvent::SendRtpArgs args;
|
MediaSourceEvent::SendRtpArgs args;
|
||||||
@ -1120,6 +1122,7 @@ void installWebApi() {
|
|||||||
args.only_audio = allArgs["only_audio"].empty() ? false : allArgs["only_audio"].as<bool>();
|
args.only_audio = allArgs["only_audio"].empty() ? false : allArgs["only_audio"].as<bool>();
|
||||||
TraceL << "startSendRtp, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
TraceL << "startSendRtp, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
||||||
|
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
||||||
if (ex) {
|
if (ex) {
|
||||||
val["code"] = API::OtherFailed;
|
val["code"] = API::OtherFailed;
|
||||||
@ -1129,6 +1132,7 @@ void installWebApi() {
|
|||||||
invoker(200, headerOut, val.toStyledString());
|
invoker(200, headerOut, val.toStyledString());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
api_regist("/index/api/startSendRtpPassive",[](API_ARGS_MAP_ASYNC){
|
api_regist("/index/api/startSendRtpPassive",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
@ -1136,7 +1140,7 @@ void installWebApi() {
|
|||||||
|
|
||||||
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["from_mp4"].as<int>());
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["from_mp4"].as<int>());
|
||||||
if (!src) {
|
if (!src) {
|
||||||
throw ApiRetException("该媒体流不存在", API::OtherFailed);
|
throw ApiRetException("can not find the source stream", API::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaSourceEvent::SendRtpArgs args;
|
MediaSourceEvent::SendRtpArgs args;
|
||||||
@ -1148,6 +1152,8 @@ void installWebApi() {
|
|||||||
args.use_ps = allArgs["use_ps"].empty() ? true : allArgs["use_ps"].as<bool>();
|
args.use_ps = allArgs["use_ps"].empty() ? true : allArgs["use_ps"].as<bool>();
|
||||||
args.only_audio = allArgs["only_audio"].empty() ? false : allArgs["only_audio"].as<bool>();
|
args.only_audio = allArgs["only_audio"].empty() ? false : allArgs["only_audio"].as<bool>();
|
||||||
TraceL << "startSendRtpPassive, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
TraceL << "startSendRtpPassive, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
||||||
|
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
||||||
if (ex) {
|
if (ex) {
|
||||||
val["code"] = API::OtherFailed;
|
val["code"] = API::OtherFailed;
|
||||||
@ -1157,20 +1163,27 @@ void installWebApi() {
|
|||||||
invoker(200, headerOut, val.toStyledString());
|
invoker(200, headerOut, val.toStyledString());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
api_regist("/index/api/stopSendRtp",[](API_ARGS_MAP){
|
api_regist("/index/api/stopSendRtp",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("vhost", "app", "stream");
|
CHECK_ARGS("vhost", "app", "stream");
|
||||||
|
|
||||||
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"]);
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"]);
|
||||||
if (!src) {
|
if (!src) {
|
||||||
throw ApiRetException("该媒体流不存在", API::OtherFailed);
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
// ssrc如果为空,关闭全部
|
// ssrc如果为空,关闭全部
|
||||||
if (!src->stopSendRtp(allArgs["ssrc"])) {
|
if (!src->stopSendRtp(allArgs["ssrc"])) {
|
||||||
throw ApiRetException("尚未开始推流,停止失败", API::OtherFailed);
|
val["code"] = API::OtherFailed;
|
||||||
|
val["msg"] = "stopSendRtp failed";
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
api_regist("/index/api/pauseRtpCheck", [](API_ARGS_MAP) {
|
api_regist("/index/api/pauseRtpCheck", [](API_ARGS_MAP) {
|
||||||
@ -1199,80 +1212,102 @@ void installWebApi() {
|
|||||||
#endif//ENABLE_RTPPROXY
|
#endif//ENABLE_RTPPROXY
|
||||||
|
|
||||||
// 开始录制hls或MP4
|
// 开始录制hls或MP4
|
||||||
api_regist("/index/api/startRecord",[](API_ARGS_MAP){
|
api_regist("/index/api/startRecord",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("type","vhost","app","stream");
|
CHECK_ARGS("type","vhost","app","stream");
|
||||||
auto result = Recorder::startRecord((Recorder::type) allArgs["type"].as<int>(),
|
|
||||||
allArgs["vhost"],
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"] );
|
||||||
allArgs["app"],
|
if (!src) {
|
||||||
allArgs["stream"],
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
allArgs["customized_path"],
|
}
|
||||||
allArgs["max_second"].as<size_t>());
|
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
auto result = src->setupRecord((Recorder::type)allArgs["type"].as<int>(), true, allArgs["customized_path"], allArgs["max_second"].as<size_t>());
|
||||||
val["result"] = result;
|
val["result"] = result;
|
||||||
val["code"] = result ? API::Success : API::OtherFailed;
|
val["code"] = result ? API::Success : API::OtherFailed;
|
||||||
val["msg"] = result ? "success" : "start record failed";
|
val["msg"] = result ? "success" : "start record failed";
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//设置录像流播放速度
|
//设置录像流播放速度
|
||||||
api_regist("/index/api/setRecordSpeed", [](API_ARGS_MAP) {
|
api_regist("/index/api/setRecordSpeed", [](API_ARGS_MAP_ASYNC) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("schema", "vhost", "app", "stream", "speed");
|
CHECK_ARGS("schema", "vhost", "app", "stream", "speed");
|
||||||
auto src = MediaSource::find(allArgs["schema"],
|
auto src = MediaSource::find(allArgs["schema"],
|
||||||
allArgs["vhost"],
|
allArgs["vhost"],
|
||||||
allArgs["app"],
|
allArgs["app"],
|
||||||
allArgs["stream"]);
|
allArgs["stream"]);
|
||||||
if (src) {
|
if (!src) {
|
||||||
bool flag = src->speed(allArgs["speed"].as<float>());
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto speed = allArgs["speed"].as<float>();
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
bool flag = src->speed(speed);
|
||||||
val["result"] = flag ? 0 : -1;
|
val["result"] = flag ? 0 : -1;
|
||||||
val["msg"] = flag ? "success" : "set failed";
|
val["msg"] = flag ? "success" : "set failed";
|
||||||
val["code"] = flag ? API::Success : API::OtherFailed;
|
val["code"] = flag ? API::Success : API::OtherFailed;
|
||||||
} else {
|
invoker(200, headerOut, val.toStyledString());
|
||||||
val["result"] = -2;
|
});
|
||||||
val["msg"] = "can not find the stream";
|
|
||||||
val["code"] = API::OtherFailed;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
api_regist("/index/api/seekRecordStamp", [](API_ARGS_MAP) {
|
api_regist("/index/api/seekRecordStamp", [](API_ARGS_MAP_ASYNC) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("schema", "vhost", "app", "stream", "stamp");
|
CHECK_ARGS("schema", "vhost", "app", "stream", "stamp");
|
||||||
auto src = MediaSource::find(allArgs["schema"],
|
auto src = MediaSource::find(allArgs["schema"],
|
||||||
allArgs["vhost"],
|
allArgs["vhost"],
|
||||||
allArgs["app"],
|
allArgs["app"],
|
||||||
allArgs["stream"]);
|
allArgs["stream"]);
|
||||||
if (src) {
|
if (!src) {
|
||||||
bool flag = src->seekTo(allArgs["stamp"].as<size_t>());
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto stamp = allArgs["stamp"].as<size_t>();
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
bool flag = src->seekTo(stamp);
|
||||||
val["result"] = flag ? 0 : -1;
|
val["result"] = flag ? 0 : -1;
|
||||||
val["msg"] = flag ? "success" : "seek failed";
|
val["msg"] = flag ? "success" : "seek failed";
|
||||||
val["code"] = flag ? API::Success : API::OtherFailed;
|
val["code"] = flag ? API::Success : API::OtherFailed;
|
||||||
} else {
|
invoker(200, headerOut, val.toStyledString());
|
||||||
val["result"] = -2;
|
});
|
||||||
val["msg"] = "can not find the stream";
|
|
||||||
val["code"] = API::OtherFailed;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 停止录制hls或MP4
|
// 停止录制hls或MP4
|
||||||
api_regist("/index/api/stopRecord",[](API_ARGS_MAP){
|
api_regist("/index/api/stopRecord",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("type","vhost","app","stream");
|
CHECK_ARGS("type","vhost","app","stream");
|
||||||
auto result = Recorder::stopRecord((Recorder::type) allArgs["type"].as<int>(),
|
|
||||||
allArgs["vhost"],
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"] );
|
||||||
allArgs["app"],
|
if (!src) {
|
||||||
allArgs["stream"]);
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto type = (Recorder::type)allArgs["type"].as<int>();
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
auto result = src->setupRecord(type, false, "", 0);
|
||||||
val["result"] = result;
|
val["result"] = result;
|
||||||
val["code"] = result ? API::Success : API::OtherFailed;
|
val["code"] = result ? API::Success : API::OtherFailed;
|
||||||
val["msg"] = result ? "success" : "stop record failed";
|
val["msg"] = result ? "success" : "stop record failed";
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 获取hls或MP4录制状态
|
// 获取hls或MP4录制状态
|
||||||
api_regist("/index/api/isRecording",[](API_ARGS_MAP){
|
api_regist("/index/api/isRecording",[](API_ARGS_MAP_ASYNC){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("type","vhost","app","stream");
|
CHECK_ARGS("type","vhost","app","stream");
|
||||||
val["status"] = Recorder::isRecording((Recorder::type) allArgs["type"].as<int>(),
|
|
||||||
allArgs["vhost"],
|
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"]);
|
||||||
allArgs["app"],
|
if (!src) {
|
||||||
allArgs["stream"]);
|
throw ApiRetException("can not find the stream", API::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto type = (Recorder::type)allArgs["type"].as<int>();
|
||||||
|
src->getOwnerPoller()->async([=]() mutable {
|
||||||
|
val["status"] = src->isRecording(type);
|
||||||
|
invoker(200, headerOut, val.toStyledString());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//获取录像文件夹列表或mp4文件列表
|
//获取录像文件夹列表或mp4文件列表
|
||||||
|
Loading…
Reference in New Issue
Block a user