修改MediaSource相关接口

This commit is contained in:
xiongziliang 2020-05-26 10:11:58 +08:00
parent eddb5adcb2
commit ff7914e441
9 changed files with 25 additions and 21 deletions

View File

@ -118,8 +118,7 @@ void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSourc
auto src = MediaSource::find(_media_info._schema, auto src = MediaSource::find(_media_info._schema,
_media_info._vhost, _media_info._vhost,
_media_info._app, _media_info._app,
_media_info._streamid, _media_info._streamid);
false);
if(src || !maxWaitMS){ if(src || !maxWaitMS){
cb(src); cb(src);
return; return;

View File

@ -439,14 +439,14 @@ void installWebApi() {
api_regist1("/index/api/isMediaOnline",[](API_ARGS1){ api_regist1("/index/api/isMediaOnline",[](API_ARGS1){
CHECK_SECRET(); CHECK_SECRET();
CHECK_ARGS("schema","vhost","app","stream"); CHECK_ARGS("schema","vhost","app","stream");
val["online"] = (bool) (MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"],false)); val["online"] = (bool) (MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]));
}); });
//测试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_regist1("/index/api/getMediaInfo",[](API_ARGS1){ api_regist1("/index/api/getMediaInfo",[](API_ARGS1){
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"],false); auto src = MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]);
if(!src){ if(!src){
val["online"] = false; val["online"] = false;
return; return;

View File

@ -180,9 +180,8 @@ static void eraseIfEmpty(MAP &map, IT0 it0, IT1 it1, IT2 it2) {
} }
}; };
void findAsync_l(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, bool retry, void MediaSource::findAsync_l(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, bool retry, const function<void(const MediaSource::Ptr &src)> &cb){
const function<void(const MediaSource::Ptr &src)> &cb){ auto src = MediaSource::find_l(info._schema, info._vhost, info._app, info._streamid, true);
auto src = MediaSource::find(info._schema, info._vhost, info._app, info._streamid, true);
if(src || !retry){ if(src || !retry){
cb(src); cb(src);
return; return;
@ -248,7 +247,11 @@ void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr<TcpSess
return findAsync_l(info, session, true, cb); return findAsync_l(info, session, true, cb);
} }
MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost_tmp, const string &app, const string &id, bool bMake) { MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost, const string &app, const string &id) {
return find_l(schema, vhost, app, id, false);
}
MediaSource::Ptr MediaSource::find_l(const string &schema, const string &vhost_tmp, const string &app, const string &id, bool bMake) {
string vhost = vhost_tmp; string vhost = vhost_tmp;
if(vhost.empty()){ if(vhost.empty()){
vhost = DEFAULT_VHOST; vhost = DEFAULT_VHOST;
@ -419,12 +422,10 @@ void MediaSourceEvent::onNoneReader(MediaSource &sender){
//如果mp4点播, 无人观看时我们强制关闭点播 //如果mp4点播, 无人观看时我们强制关闭点播
bool is_mp4_vod = sender.getApp() == recordApp; bool is_mp4_vod = sender.getApp() == recordApp;
//无人观看mp4点播时3秒后自动关闭
auto close_delay = is_mp4_vod ? 3.0 : stream_none_reader_delay / 1000.0;
//没有任何人观看该视频源,表明该源可以关闭了 //没有任何人观看该视频源,表明该源可以关闭了
weak_ptr<MediaSource> weakSender = sender.shared_from_this(); weak_ptr<MediaSource> weakSender = sender.shared_from_this();
_async_close_timer = std::make_shared<Timer>(close_delay, [weakSender,is_mp4_vod]() { _async_close_timer = std::make_shared<Timer>(stream_none_reader_delay / 1000.0, [weakSender,is_mp4_vod]() {
auto strongSender = weakSender.lock(); auto strongSender = weakSender.lock();
if (!strongSender) { if (!strongSender) {
//对象已经销毁 //对象已经销毁
@ -467,7 +468,7 @@ MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string &
try { try {
MP4Reader::Ptr pReader(new MP4Reader(vhost, app, stream, filePath)); MP4Reader::Ptr pReader(new MP4Reader(vhost, app, stream, filePath));
pReader->startReadMP4(); pReader->startReadMP4();
return MediaSource::find(schema, vhost, app, stream, false); return MediaSource::find(schema, vhost, app, stream);
} catch (std::exception &ex) { } catch (std::exception &ex) {
WarnL << ex.what(); WarnL << ex.what();
return nullptr; return nullptr;

View File

@ -134,7 +134,7 @@ public:
virtual bool isRecording(Recorder::type type); virtual bool isRecording(Recorder::type type);
// 同步查找流 // 同步查找流
static Ptr find(const string &schema, const string &vhost, const string &app, const string &id, bool bMake = true) ; static Ptr find(const string &schema, const string &vhost, const string &app, const string &id);
// 异步查找流 // 异步查找流
static void findAsync(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, const function<void(const Ptr &src)> &cb); static void findAsync(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, const function<void(const Ptr &src)> &cb);
// 遍历所有流 // 遍历所有流
@ -142,9 +142,14 @@ public:
// 从mp4文件生成MediaSource // 从mp4文件生成MediaSource
static MediaSource::Ptr createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &filePath = "", bool checkApp = true); static MediaSource::Ptr createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &filePath = "", bool checkApp = true);
protected: protected:
void regist() ; void regist() ;
bool unregist() ; bool unregist();
private:
static Ptr find_l(const string &schema, const string &vhost, const string &app, const string &id, bool bMake);
static void findAsync_l(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, bool retry, const function<void(const MediaSource::Ptr &src)> &cb);
private: private:
string _strSchema; string _strSchema;
string _strVhost; string _strVhost;

View File

@ -80,11 +80,11 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
} }
static MediaSource::Ptr getMediaSource(const string &vhost, const string &app, const string &stream_id){ static MediaSource::Ptr getMediaSource(const string &vhost, const string &app, const string &stream_id){
auto src = MediaSource::find(RTMP_SCHEMA, vhost, app, stream_id, false); auto src = MediaSource::find(RTMP_SCHEMA, vhost, app, stream_id);
if(src){ if(src){
return src; return src;
} }
return MediaSource::find(RTSP_SCHEMA, vhost, app, stream_id, false); return MediaSource::find(RTSP_SCHEMA, vhost, app, stream_id);
} }
bool Recorder::isRecording(type type, const string &vhost, const string &app, const string &stream_id){ bool Recorder::isRecording(type type, const string &vhost, const string &app, const string &stream_id){

View File

@ -167,7 +167,7 @@ void FlvMuxer::stop() {
///////////////////////////////////////////////////////FlvRecorder///////////////////////////////////////////////////// ///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const string &vhost, const string &app, const string &stream,const string &file_path) { void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const string &vhost, const string &app, const string &stream,const string &file_path) {
startRecord(poller,dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream,false)),file_path); startRecord(poller,dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream)),file_path);
} }
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media, const string &file_path) { void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media, const string &file_path) {

View File

@ -130,8 +130,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) {
auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA, auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,
_mediaInfo._vhost, _mediaInfo._vhost,
_mediaInfo._app, _mediaInfo._app,
_mediaInfo._streamid, _mediaInfo._streamid));
false));
bool authSuccess = err.empty(); bool authSuccess = err.empty();
bool ok = (!src && !_pPublisherSrc && authSuccess); bool ok = (!src && !_pPublisherSrc && authSuccess);
AMFValue status(AMF_OBJECT); AMFValue status(AMF_OBJECT);

View File

@ -81,6 +81,7 @@ RtpMultiCaster::~RtpMultiCaster() {
_pReader->setDetachCB(nullptr); _pReader->setDetachCB(nullptr);
DebugL; DebugL;
} }
RtpMultiCaster::RtpMultiCaster(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) { RtpMultiCaster::RtpMultiCaster(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream) {
auto src = dynamic_pointer_cast<RtspMediaSource>(MediaSource::find(RTSP_SCHEMA,strVhost,strApp, strStream)); auto src = dynamic_pointer_cast<RtspMediaSource>(MediaSource::find(RTSP_SCHEMA,strVhost,strApp, strStream));
if(!src){ if(!src){

View File

@ -221,8 +221,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTSP_SCHEMA, auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTSP_SCHEMA,
_mediaInfo._vhost, _mediaInfo._vhost,
_mediaInfo._app, _mediaInfo._app,
_mediaInfo._streamid, _mediaInfo._streamid));
false));
if(src){ if(src){
sendRtspResponse("406 Not Acceptable", {"Content-Type", "text/plain"}, "Already publishing."); sendRtspResponse("406 Not Acceptable", {"Content-Type", "text/plain"}, "Already publishing.");
string err = StrPrinter << "ANNOUNCE:" string err = StrPrinter << "ANNOUNCE:"