2018-03-20 11:28:13 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-02-02 18:19:35 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
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.
|
2018-02-02 18:19:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2020-04-24 15:28:27 +08:00
|
|
|
|
#include <math.h>
|
2018-02-02 18:19:35 +08:00
|
|
|
|
#include "MediaSource.h"
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "Record/MP4Reader.h"
|
2018-02-02 18:19:35 +08:00
|
|
|
|
#include "Util/util.h"
|
2018-03-05 10:41:15 +08:00
|
|
|
|
#include "Network/sockutil.h"
|
2019-05-27 22:32:07 +08:00
|
|
|
|
#include "Network/TcpSession.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
namespace mediakit {
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
|
|
|
|
recursive_mutex MediaSource::g_mtxMediaSrc;
|
|
|
|
|
MediaSource::SchemaVhostAppStreamMap MediaSource::g_mapMediaSrc;
|
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
MediaSource::MediaSource(const string &strSchema, const string &strVhost, const string &strApp, const string &strId) :
|
2019-12-28 18:50:56 +08:00
|
|
|
|
_strSchema(strSchema), _strApp(strApp), _strId(strId) {
|
2019-12-03 16:10:02 +08:00
|
|
|
|
if (strVhost.empty()) {
|
|
|
|
|
_strVhost = DEFAULT_VHOST;
|
|
|
|
|
} else {
|
|
|
|
|
_strVhost = strVhost;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MediaSource::~MediaSource() {
|
|
|
|
|
unregist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string& MediaSource::getSchema() const {
|
|
|
|
|
return _strSchema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string& MediaSource::getVhost() const {
|
|
|
|
|
return _strVhost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string& MediaSource::getApp() const {
|
|
|
|
|
//获取该源的id
|
|
|
|
|
return _strApp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string& MediaSource::getId() const {
|
|
|
|
|
return _strId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<Track::Ptr> MediaSource::getTracks(bool trackReady) const {
|
|
|
|
|
auto strongPtr = _track_source.lock();
|
|
|
|
|
if(strongPtr){
|
|
|
|
|
return strongPtr->getTracks(trackReady);
|
|
|
|
|
}
|
|
|
|
|
return vector<Track::Ptr>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MediaSource::setTrackSource(const std::weak_ptr<TrackSource> &track_src) {
|
|
|
|
|
_track_source = track_src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
|
|
|
|
|
_listener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::weak_ptr<MediaSourceEvent>& MediaSource::getListener() const{
|
|
|
|
|
return _listener;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 16:48:11 +08:00
|
|
|
|
int MediaSource::totalReaderCount(){
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return readerCount();
|
|
|
|
|
}
|
|
|
|
|
return listener->totalReaderCount(*this);
|
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
bool MediaSource::seekTo(uint32_t ui32Stamp) {
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return listener->seekTo(*this,ui32Stamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MediaSource::close(bool force) {
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return listener->close(*this,force);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MediaSource::onNoneReader(){
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-23 10:21:17 +08:00
|
|
|
|
if (listener->totalReaderCount(*this) == 0) {
|
|
|
|
|
listener->onNoneReader(*this);
|
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path){
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if (!listener) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return listener->setupRecord(*this, type, start, custom_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MediaSource::isRecording(Recorder::type type){
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return listener->isRecording(*this, type);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src)> &cb) {
|
2020-04-06 21:23:35 +08:00
|
|
|
|
decltype(g_mapMediaSrc) copy;
|
|
|
|
|
{
|
|
|
|
|
//拷贝g_mapMediaSrc后再遍历,考虑到是高频使用的全局单例锁,并且在上锁时会执行回调代码
|
|
|
|
|
//很容易导致多个锁交叉死锁的情况,而且该函数使用频率不高,拷贝开销相对来说是可以接受的
|
|
|
|
|
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
|
|
|
|
|
copy = g_mapMediaSrc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto &pr0 : copy) {
|
2019-12-03 16:10:02 +08:00
|
|
|
|
for (auto &pr1 : pr0.second) {
|
|
|
|
|
for (auto &pr2 : pr1.second) {
|
|
|
|
|
for (auto &pr3 : pr2.second) {
|
|
|
|
|
auto src = pr3.second.lock();
|
|
|
|
|
if(src){
|
|
|
|
|
cb(src);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename MAP, typename FUNC>
|
2019-12-28 18:50:56 +08:00
|
|
|
|
static bool searchMedia(MAP &map, const string &schema, const string &vhost, const string &app, const string &id, FUNC &&func) {
|
2019-12-03 16:10:02 +08:00
|
|
|
|
auto it0 = map.find(schema);
|
|
|
|
|
if (it0 == map.end()) {
|
|
|
|
|
//未找到协议
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
auto it1 = it0->second.find(vhost);
|
|
|
|
|
if (it1 == it0->second.end()) {
|
|
|
|
|
//未找到vhost
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
auto it2 = it1->second.find(app);
|
|
|
|
|
if (it2 == it1->second.end()) {
|
|
|
|
|
//未找到app
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
auto it3 = it2->second.find(id);
|
|
|
|
|
if (it3 == it2->second.end()) {
|
|
|
|
|
//未找到streamId
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return func(it0, it1, it2, it3);
|
|
|
|
|
}
|
2019-05-27 22:32:07 +08:00
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
template<typename MAP, typename IT0, typename IT1, typename IT2>
|
|
|
|
|
static void eraseIfEmpty(MAP &map, IT0 it0, IT1 it1, IT2 it2) {
|
|
|
|
|
if (it2->second.empty()) {
|
|
|
|
|
it1->second.erase(it2);
|
|
|
|
|
if (it1->second.empty()) {
|
|
|
|
|
it0->second.erase(it1);
|
|
|
|
|
if (it0->second.empty()) {
|
|
|
|
|
map.erase(it0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
void findAsync_l(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, bool retry,
|
|
|
|
|
const function<void(const MediaSource::Ptr &src)> &cb){
|
|
|
|
|
auto src = MediaSource::find(info._schema, info._vhost, info._app, info._streamid, true);
|
2019-05-27 22:32:07 +08:00
|
|
|
|
if(src || !retry){
|
|
|
|
|
cb(src);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *listener_tag = session.get();
|
|
|
|
|
weak_ptr<TcpSession> weakSession = session;
|
|
|
|
|
//广播未找到流,此时可以立即去拉流,这样还来得及
|
2020-04-23 21:38:44 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastNotFoundStream,info, static_cast<SockInfo &>(*session));
|
2019-05-27 22:32:07 +08:00
|
|
|
|
|
2019-05-28 09:25:41 +08:00
|
|
|
|
//最多等待一定时间,如果这个时间内,流未注册上,那么返回未找到流
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(int,maxWaitMS,General::kMaxStreamWaitTimeMS);
|
2019-05-28 09:25:41 +08:00
|
|
|
|
|
2019-05-27 22:32:07 +08:00
|
|
|
|
//若干秒后执行等待媒体注册超时回调
|
2019-05-28 09:25:41 +08:00
|
|
|
|
auto onRegistTimeout = session->getPoller()->doDelayTask(maxWaitMS,[cb,listener_tag](){
|
2019-05-27 22:32:07 +08:00
|
|
|
|
//取消监听该事件
|
|
|
|
|
NoticeCenter::Instance().delListener(listener_tag,Broadcast::kBroadcastMediaChanged);
|
|
|
|
|
cb(nullptr);
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-28 09:25:41 +08:00
|
|
|
|
auto onRegist = [listener_tag,weakSession,info,cb,onRegistTimeout](BroadcastMediaChangedArgs) {
|
2019-06-06 15:29:36 +08:00
|
|
|
|
auto strongSession = weakSession.lock();
|
|
|
|
|
if(!strongSession) {
|
|
|
|
|
//自己已经销毁
|
|
|
|
|
//取消延时任务,防止多次回调
|
|
|
|
|
onRegistTimeout->cancel();
|
|
|
|
|
//取消事件监听
|
|
|
|
|
NoticeCenter::Instance().delListener(listener_tag,Broadcast::kBroadcastMediaChanged);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 14:08:16 +08:00
|
|
|
|
if (!bRegist ||
|
|
|
|
|
sender.getSchema() != info._schema ||
|
|
|
|
|
sender.getVhost() != info._vhost ||
|
|
|
|
|
sender.getApp() != info._app ||
|
|
|
|
|
sender.getId() != info._streamid) {
|
2019-05-27 22:32:07 +08:00
|
|
|
|
//不是自己感兴趣的事件,忽略之
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//取消延时任务,防止多次回调
|
|
|
|
|
onRegistTimeout->cancel();
|
2019-06-06 15:29:36 +08:00
|
|
|
|
//取消事件监听
|
|
|
|
|
NoticeCenter::Instance().delListener(listener_tag,Broadcast::kBroadcastMediaChanged);
|
2019-05-27 22:32:07 +08:00
|
|
|
|
|
2019-06-06 15:29:36 +08:00
|
|
|
|
//播发器请求的流终于注册上了,切换到自己的线程再回复
|
|
|
|
|
strongSession->async([weakSession,info,cb](){
|
2019-05-27 22:32:07 +08:00
|
|
|
|
auto strongSession = weakSession.lock();
|
|
|
|
|
if(!strongSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DebugL << "收到媒体注册事件,回复播放器:" << info._schema << "/" << info._vhost << "/" << info._app << "/" << info._streamid;
|
|
|
|
|
//再找一遍媒体源,一般能找到
|
2019-12-03 16:10:02 +08:00
|
|
|
|
findAsync_l(info,strongSession,false,cb);
|
2019-05-27 22:32:07 +08:00
|
|
|
|
}, false);
|
|
|
|
|
};
|
|
|
|
|
//监听媒体注册事件
|
|
|
|
|
NoticeCenter::Instance().addListener(listener_tag, Broadcast::kBroadcastMediaChanged, onRegist);
|
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
|
|
|
|
|
void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr<TcpSession> &session,const function<void(const Ptr &src)> &cb){
|
|
|
|
|
return findAsync_l(info, session, true, cb);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 18:50:56 +08:00
|
|
|
|
MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost_tmp, const string &app, const string &id, bool bMake) {
|
2018-02-02 18:19:35 +08:00
|
|
|
|
string vhost = vhost_tmp;
|
|
|
|
|
if(vhost.empty()){
|
|
|
|
|
vhost = DEFAULT_VHOST;
|
|
|
|
|
}
|
2019-05-28 17:14:36 +08:00
|
|
|
|
|
|
|
|
|
GET_CONFIG(bool,enableVhost,General::kEnableVhost);
|
|
|
|
|
if(!enableVhost){
|
|
|
|
|
vhost = DEFAULT_VHOST;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
MediaSource::Ptr ret;
|
2020-03-04 21:57:31 +08:00
|
|
|
|
{
|
|
|
|
|
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
|
|
|
|
|
//查找某一媒体源,找到后返回
|
|
|
|
|
searchMedia(g_mapMediaSrc, schema, vhost, app, id, [&](SchemaVhostAppStreamMap::iterator &it0,
|
|
|
|
|
VhostAppStreamMap::iterator &it1,
|
|
|
|
|
AppStreamMap::iterator &it2,
|
|
|
|
|
StreamMap::iterator &it3) {
|
|
|
|
|
ret = it3->second.lock();
|
|
|
|
|
if (!ret) {
|
|
|
|
|
//该对象已经销毁
|
|
|
|
|
it2->second.erase(it3);
|
|
|
|
|
eraseIfEmpty(g_mapMediaSrc, it0, it1, it2);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
if(!ret && bMake){
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//未查找媒体源,则创建一个
|
2020-04-03 23:27:16 +08:00
|
|
|
|
ret = createFromMP4(schema, vhost, app, id);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-03-13 20:12:34 +08:00
|
|
|
|
void MediaSource::regist() {
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(bool,enableVhost,General::kEnableVhost);
|
|
|
|
|
if(!enableVhost){
|
|
|
|
|
_strVhost = DEFAULT_VHOST;
|
|
|
|
|
}
|
2018-02-02 18:19:35 +08:00
|
|
|
|
//注册该源,注册后服务器才能找到该源
|
2018-11-07 16:59:42 +08:00
|
|
|
|
{
|
|
|
|
|
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
|
2019-03-13 20:12:34 +08:00
|
|
|
|
g_mapMediaSrc[_strSchema][_strVhost][_strApp][_strId] = shared_from_this();
|
2018-11-07 16:59:42 +08:00
|
|
|
|
}
|
2020-04-24 15:08:28 +08:00
|
|
|
|
_StrPrinter codec_info;
|
|
|
|
|
auto tracks = getTracks(true);
|
|
|
|
|
for(auto &track : tracks) {
|
|
|
|
|
auto codec_type = track->getTrackType();
|
|
|
|
|
codec_info << track->getCodecName();
|
|
|
|
|
switch (codec_type) {
|
|
|
|
|
case TrackAudio : {
|
|
|
|
|
auto audio_track = dynamic_pointer_cast<AudioTrack>(track);
|
|
|
|
|
codec_info << "["
|
|
|
|
|
<< audio_track->getAudioSampleRate() << "/"
|
|
|
|
|
<< audio_track->getAudioChannel() << "/"
|
|
|
|
|
<< audio_track->getAudioSampleBit() << "] ";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case TrackVideo : {
|
|
|
|
|
auto video_track = dynamic_pointer_cast<VideoTrack>(track);
|
|
|
|
|
codec_info << "["
|
|
|
|
|
<< video_track->getVideoWidth() << "/"
|
|
|
|
|
<< video_track->getVideoHeight() << "/"
|
2020-04-24 15:28:27 +08:00
|
|
|
|
<< round(video_track->getVideoFps()) << "] ";
|
2020-04-24 15:08:28 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId << " " << codec_info;
|
2020-02-01 22:57:48 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, true, *this);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
2020-03-04 21:57:31 +08:00
|
|
|
|
|
|
|
|
|
//反注册该源
|
2018-02-02 18:19:35 +08:00
|
|
|
|
bool MediaSource::unregist() {
|
2020-03-04 21:57:31 +08:00
|
|
|
|
bool ret;
|
|
|
|
|
{
|
|
|
|
|
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
|
|
|
|
|
ret = searchMedia(g_mapMediaSrc, _strSchema, _strVhost, _strApp, _strId,
|
|
|
|
|
[&](SchemaVhostAppStreamMap::iterator &it0,
|
|
|
|
|
VhostAppStreamMap::iterator &it1,
|
|
|
|
|
AppStreamMap::iterator &it2,
|
|
|
|
|
StreamMap::iterator &it3) {
|
|
|
|
|
auto strongMedia = it3->second.lock();
|
|
|
|
|
if (strongMedia && this != strongMedia.get()) {
|
|
|
|
|
//不是自己,不允许反注册
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
it2->second.erase(it3);
|
|
|
|
|
eraseIfEmpty(g_mapMediaSrc, it0, it1, it2);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
2020-03-04 21:57:31 +08:00
|
|
|
|
if(ret){
|
2020-04-24 15:08:28 +08:00
|
|
|
|
InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId;
|
2020-03-04 21:57:31 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, false, *this);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
|
|
|
|
|
/////////////////////////////////////MediaInfo//////////////////////////////////////
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
void MediaInfo::parse(const string &url){
|
|
|
|
|
//string url = "rtsp://127.0.0.1:8554/live/id?key=val&a=1&&b=2&vhost=vhost.com";
|
|
|
|
|
auto schema_pos = url.find("://");
|
|
|
|
|
if(schema_pos != string::npos){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_schema = url.substr(0,schema_pos);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}else{
|
|
|
|
|
schema_pos = -3;
|
|
|
|
|
}
|
|
|
|
|
auto split_vec = split(url.substr(schema_pos + 3),"/");
|
|
|
|
|
if(split_vec.size() > 0){
|
|
|
|
|
auto vhost = split_vec[0];
|
|
|
|
|
auto pos = vhost.find(":");
|
|
|
|
|
if(pos != string::npos){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_host = _vhost = vhost.substr(0,pos);
|
|
|
|
|
_port = vhost.substr(pos + 1);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
} else{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_host = _vhost = vhost;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
2020-03-12 12:47:15 +08:00
|
|
|
|
|
|
|
|
|
if(_vhost == "localhost" || INADDR_NONE != inet_addr(_vhost.data())){
|
|
|
|
|
//如果访问的是localhost或ip,那么则为默认虚拟主机
|
|
|
|
|
_vhost = DEFAULT_VHOST;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
if(split_vec.size() > 1){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_app = split_vec[1];
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
if(split_vec.size() > 2){
|
|
|
|
|
string steamid;
|
|
|
|
|
for(int i = 2 ; i < split_vec.size() ; ++i){
|
|
|
|
|
steamid.append(split_vec[i] + "/");
|
|
|
|
|
}
|
|
|
|
|
if(steamid.back() == '/'){
|
|
|
|
|
steamid.pop_back();
|
|
|
|
|
}
|
|
|
|
|
auto pos = steamid.find("?");
|
|
|
|
|
if(pos != string::npos){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_streamid = steamid.substr(0,pos);
|
2018-10-25 17:39:19 +08:00
|
|
|
|
_param_strs = steamid.substr(pos + 1);
|
2019-12-29 10:49:04 +08:00
|
|
|
|
auto params = Parser::parseArgs(_param_strs);
|
|
|
|
|
if(params.find(VHOST_KEY) != params.end()){
|
|
|
|
|
_vhost = params[VHOST_KEY];
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
} else{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_streamid = steamid;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-29 09:26:16 +08:00
|
|
|
|
|
|
|
|
|
GET_CONFIG(bool,enableVhost,General::kEnableVhost);
|
2020-03-12 12:47:15 +08:00
|
|
|
|
if(!enableVhost || _vhost.empty()){
|
|
|
|
|
//如果关闭虚拟主机或者虚拟主机为空,则设置虚拟主机为默认
|
2019-05-29 09:28:35 +08:00
|
|
|
|
_vhost = DEFAULT_VHOST;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
/////////////////////////////////////MediaSourceEvent//////////////////////////////////////
|
|
|
|
|
|
2019-05-29 18:08:50 +08:00
|
|
|
|
void MediaSourceEvent::onNoneReader(MediaSource &sender){
|
2020-04-03 22:39:44 +08:00
|
|
|
|
GET_CONFIG(string, recordApp, Record::kAppName);
|
2020-03-23 10:21:17 +08:00
|
|
|
|
GET_CONFIG(int, stream_none_reader_delay, General::kStreamNoneReaderDelayMS);
|
|
|
|
|
|
2020-04-03 22:39:44 +08:00
|
|
|
|
//如果mp4点播, 无人观看时我们强制关闭点播
|
|
|
|
|
bool is_mp4_vod = sender.getApp() == recordApp;
|
|
|
|
|
//无人观看mp4点播时,3秒后自动关闭
|
|
|
|
|
auto close_delay = is_mp4_vod ? 3.0 : stream_none_reader_delay / 1000.0;
|
|
|
|
|
|
|
|
|
|
//没有任何人观看该视频源,表明该源可以关闭了
|
2020-03-23 10:21:17 +08:00
|
|
|
|
weak_ptr<MediaSource> weakSender = sender.shared_from_this();
|
2020-04-03 22:39:44 +08:00
|
|
|
|
_async_close_timer = std::make_shared<Timer>(close_delay, [weakSender,is_mp4_vod]() {
|
2020-03-23 10:21:17 +08:00
|
|
|
|
auto strongSender = weakSender.lock();
|
|
|
|
|
if (!strongSender) {
|
|
|
|
|
//对象已经销毁
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strongSender->totalReaderCount() != 0) {
|
|
|
|
|
//还有人消费
|
|
|
|
|
return false;
|
2019-07-11 14:38:54 +08:00
|
|
|
|
}
|
2020-03-23 10:21:17 +08:00
|
|
|
|
|
2020-04-03 22:39:44 +08:00
|
|
|
|
if(!is_mp4_vod){
|
|
|
|
|
//直播时触发无人观看事件,让开发者自行选择是否关闭
|
|
|
|
|
WarnL << "无人观看事件:"
|
|
|
|
|
<< strongSender->getSchema() << "/"
|
|
|
|
|
<< strongSender->getVhost() << "/"
|
|
|
|
|
<< strongSender->getApp() << "/"
|
|
|
|
|
<< strongSender->getId();
|
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastStreamNoneReader, *strongSender);
|
|
|
|
|
}else{
|
|
|
|
|
//这个是mp4点播,我们自动关闭
|
|
|
|
|
WarnL << "MP4点播无人观看,自动关闭:"
|
|
|
|
|
<< strongSender->getSchema() << "/"
|
|
|
|
|
<< strongSender->getVhost() << "/"
|
|
|
|
|
<< strongSender->getApp() << "/"
|
|
|
|
|
<< strongSender->getId();
|
|
|
|
|
strongSender->close(false);
|
|
|
|
|
}
|
2020-03-23 10:21:17 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}, nullptr);
|
2019-05-29 18:08:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:27:16 +08:00
|
|
|
|
MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &filePath , bool checkApp){
|
|
|
|
|
GET_CONFIG(string, appName, Record::kAppName);
|
|
|
|
|
if (checkApp && app != appName) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-04-04 08:30:20 +08:00
|
|
|
|
#ifdef ENABLE_MP4
|
2020-04-03 23:27:16 +08:00
|
|
|
|
try {
|
|
|
|
|
MP4Reader::Ptr pReader(new MP4Reader(vhost, app, stream, filePath));
|
|
|
|
|
pReader->startReadMP4();
|
|
|
|
|
return MediaSource::find(schema, vhost, app, stream, false);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << ex.what();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
WarnL << "创建MP4点播失败,请编译时打开\"ENABLE_MP4\"选项";
|
|
|
|
|
return nullptr;
|
|
|
|
|
#endif //ENABLE_MP4
|
|
|
|
|
}
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
2020-04-09 16:19:03 +08:00
|
|
|
|
static bool isFlushAble_default(bool is_audio, uint32_t last_stamp, uint32_t new_stamp, int cache_size) {
|
|
|
|
|
if (new_stamp < last_stamp) {
|
|
|
|
|
//时间戳回退(可能seek中)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_audio) {
|
|
|
|
|
//这是视频,时间戳发送变化或者缓存超过1024个
|
|
|
|
|
return last_stamp != new_stamp || cache_size >= 1024;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这是音频,缓存超过100ms或者缓存个数超过10个
|
|
|
|
|
return new_stamp > last_stamp + 100 || cache_size > 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isFlushAble_merge(bool is_audio, uint32_t last_stamp, uint32_t new_stamp, int cache_size, int merge_ms) {
|
|
|
|
|
if (new_stamp < last_stamp) {
|
|
|
|
|
//时间戳回退(可能seek中)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(new_stamp > last_stamp + merge_ms){
|
|
|
|
|
//时间戳增量超过合并写阈值
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_audio) {
|
|
|
|
|
//这是视频,缓存数超过1024个,这个逻辑用于避免时间戳异常的流导致的内存暴增问题
|
|
|
|
|
//而且sendmsg接口一般最多只能发送1024个数据包
|
|
|
|
|
return cache_size >= 1024;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这是音频,音频缓存超过20个
|
|
|
|
|
return cache_size > 20;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:51:04 +08:00
|
|
|
|
bool FlushPolicy::isFlushAble(uint32_t new_stamp, int cache_size) {
|
|
|
|
|
bool ret = false;
|
|
|
|
|
GET_CONFIG(bool, ultraLowDelay, General::kUltraLowDelay);
|
|
|
|
|
GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS);
|
|
|
|
|
if (ultraLowDelay || mergeWriteMS <= 0) {
|
2020-04-09 16:19:03 +08:00
|
|
|
|
//关闭了合并写或者合并写阈值小于等于0
|
2020-04-24 13:51:04 +08:00
|
|
|
|
ret = isFlushAble_default(_is_audio, _last_stamp, new_stamp, cache_size);
|
|
|
|
|
} else {
|
|
|
|
|
ret = isFlushAble_merge(_is_audio, _last_stamp, new_stamp, cache_size, mergeWriteMS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
// DebugL << _is_audio << " " << _last_stamp << " " << new_stamp;
|
|
|
|
|
_last_stamp = new_stamp;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
}
|
2020-04-24 13:51:04 +08:00
|
|
|
|
return ret;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|