2018-03-20 11:28:13 +08:00
|
|
|
|
/*
|
2018-02-02 18:19:35 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2018-02-02 18:19:35 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_MEDIASOURCE_H
|
|
|
|
|
#define ZLMEDIAKIT_MEDIASOURCE_H
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include "Util/NoticeCenter.h"
|
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
2019-05-27 22:32:07 +08:00
|
|
|
|
namespace toolkit{
|
|
|
|
|
class TcpSession;
|
|
|
|
|
}//namespace toolkit
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
2019-05-27 18:39:43 +08:00
|
|
|
|
class MediaSource;
|
|
|
|
|
class MediaSourceEvent{
|
2018-02-02 18:19:35 +08:00
|
|
|
|
public:
|
|
|
|
|
MediaSourceEvent(){};
|
|
|
|
|
virtual ~MediaSourceEvent(){};
|
|
|
|
|
public:
|
2019-05-29 18:08:50 +08:00
|
|
|
|
virtual bool seekTo(MediaSource &sender,uint32_t ui32Stamp){
|
2018-02-06 10:56:58 +08:00
|
|
|
|
//拖动进度条
|
2018-02-02 18:19:35 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-27 16:09:29 +08:00
|
|
|
|
|
2019-05-29 18:08:50 +08:00
|
|
|
|
virtual bool close(MediaSource &sender,bool force) {
|
2018-02-06 10:56:58 +08:00
|
|
|
|
//通知其停止推流
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-27 16:09:29 +08:00
|
|
|
|
|
2019-05-29 18:08:50 +08:00
|
|
|
|
virtual void onNoneReader(MediaSource &sender);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
};
|
2019-05-27 18:39:43 +08:00
|
|
|
|
|
|
|
|
|
class MediaInfo{
|
2018-02-02 18:19:35 +08:00
|
|
|
|
public:
|
|
|
|
|
MediaInfo(){}
|
|
|
|
|
MediaInfo(const string &url){
|
|
|
|
|
parse(url);
|
|
|
|
|
}
|
|
|
|
|
~MediaInfo(){}
|
|
|
|
|
|
|
|
|
|
void parse(const string &url);
|
|
|
|
|
|
|
|
|
|
string &operator[](const string &key){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _params[key];
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
public:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _schema;
|
|
|
|
|
string _host;
|
|
|
|
|
string _port;
|
|
|
|
|
string _vhost;
|
|
|
|
|
string _app;
|
|
|
|
|
string _streamid;
|
|
|
|
|
StrCaseMap _params;
|
2018-10-25 17:39:19 +08:00
|
|
|
|
string _param_strs;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MediaSource: public enable_shared_from_this<MediaSource> {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<MediaSource> Ptr;
|
|
|
|
|
typedef unordered_map<string, weak_ptr<MediaSource> > StreamMap;
|
|
|
|
|
typedef unordered_map<string, StreamMap > AppStreamMap;
|
|
|
|
|
typedef unordered_map<string, AppStreamMap > VhostAppStreamMap;
|
|
|
|
|
typedef unordered_map<string, VhostAppStreamMap > SchemaVhostAppStreamMap;
|
|
|
|
|
|
|
|
|
|
MediaSource(const string &strSchema,
|
|
|
|
|
const string &strVhost,
|
|
|
|
|
const string &strApp,
|
|
|
|
|
const string &strId) :
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strSchema(strSchema),
|
|
|
|
|
_strApp(strApp),
|
|
|
|
|
_strId(strId) {
|
2018-02-02 18:19:35 +08:00
|
|
|
|
if(strVhost.empty()){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strVhost = DEFAULT_VHOST;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}else{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strVhost = strVhost;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual ~MediaSource() {
|
|
|
|
|
unregist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Ptr find(const string &schema,
|
|
|
|
|
const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &id,
|
|
|
|
|
bool bMake = true) ;
|
|
|
|
|
|
2019-05-27 22:32:07 +08:00
|
|
|
|
static void findAsync(const MediaInfo &info,
|
|
|
|
|
const std::shared_ptr<TcpSession> &session,
|
|
|
|
|
bool retry,
|
|
|
|
|
const function<void(const MediaSource::Ptr &src)> &cb);
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
const string& getSchema() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strSchema;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
const string& getVhost() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strVhost;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
const string& getApp() const {
|
|
|
|
|
//获取该源的id
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strApp;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
const string& getId() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strId;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool seekTo(uint32_t ui32Stamp) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto listener = _listener.lock();
|
2018-02-02 18:19:35 +08:00
|
|
|
|
if(!listener){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-29 18:08:50 +08:00
|
|
|
|
return listener->seekTo(*this,ui32Stamp);
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 15:09:08 +08:00
|
|
|
|
virtual uint32_t getTimeStamp(TrackType trackType) = 0;
|
|
|
|
|
|
2019-05-27 18:39:43 +08:00
|
|
|
|
bool close(bool force) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto listener = _listener.lock();
|
2018-02-06 10:56:58 +08:00
|
|
|
|
if(!listener){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-29 18:08:50 +08:00
|
|
|
|
return listener->close(*this,force);
|
2018-02-06 10:56:58 +08:00
|
|
|
|
}
|
2019-06-10 12:33:45 +08:00
|
|
|
|
|
|
|
|
|
void onNoneReader(){
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
listener->onNoneReader(*this);
|
|
|
|
|
}
|
2019-05-27 14:14:42 +08:00
|
|
|
|
virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_listener = listener;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
}
|
2018-02-06 10:56:58 +08:00
|
|
|
|
|
|
|
|
|
template <typename FUN>
|
|
|
|
|
static void for_each_media(FUN && fun){
|
|
|
|
|
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
|
|
|
|
|
for (auto &pr0 : g_mapMediaSrc){
|
|
|
|
|
for(auto &pr1 : pr0.second){
|
|
|
|
|
for(auto &pr2 : pr1.second){
|
|
|
|
|
for(auto &pr3 : pr2.second){
|
|
|
|
|
fun(pr0.first,pr1.first,pr2.first,pr3.first,pr3.second.lock());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-31 14:13:00 +08:00
|
|
|
|
|
2019-05-27 16:09:29 +08:00
|
|
|
|
virtual int readerCount() = 0;
|
2018-08-31 14:13:00 +08:00
|
|
|
|
protected:
|
2019-03-13 20:12:34 +08:00
|
|
|
|
void regist() ;
|
2018-08-31 14:13:00 +08:00
|
|
|
|
bool unregist() ;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
private:
|
|
|
|
|
template <typename FUN>
|
|
|
|
|
static bool searchMedia(const string &schema,
|
|
|
|
|
const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &id,
|
|
|
|
|
FUN &&fun){
|
|
|
|
|
auto it0 = g_mapMediaSrc.find(schema);
|
|
|
|
|
if (it0 == g_mapMediaSrc.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 fun(it0,it1,it2,it3);
|
|
|
|
|
}
|
|
|
|
|
template <typename IT0,typename IT1,typename IT2>
|
|
|
|
|
static void eraseIfEmpty(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()){
|
|
|
|
|
g_mapMediaSrc.erase(it0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-02-06 10:56:58 +08:00
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
void unregisted();
|
|
|
|
|
protected:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
std::weak_ptr<MediaSourceEvent> _listener;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
private:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strSchema;//协议类型
|
|
|
|
|
string _strVhost; //vhost
|
|
|
|
|
string _strApp; //媒体app
|
|
|
|
|
string _strId; //媒体id
|
2018-02-02 18:19:35 +08:00
|
|
|
|
static SchemaVhostAppStreamMap g_mapMediaSrc; //静态的媒体源表
|
|
|
|
|
static recursive_mutex g_mtxMediaSrc; //访问静态的媒体源表的互斥锁
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_MEDIASOURCE_H
|