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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_MEDIASOURCE_H
|
|
|
|
|
#define ZLMEDIAKIT_MEDIASOURCE_H
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "Common/config.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Common/Parser.h"
|
2018-02-02 18:19:35 +08:00
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include "Util/NoticeCenter.h"
|
2020-04-09 16:19:03 +08:00
|
|
|
|
#include "Util/List.h"
|
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Rtmp/Rtmp.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Extension/Track.h"
|
2020-04-05 09:26:29 +08:00
|
|
|
|
#include "Record/Recorder.h"
|
2018-02-02 18:19:35 +08:00
|
|
|
|
|
|
|
|
|
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;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
}// namespace toolkit
|
2019-05-27 22:32:07 +08:00
|
|
|
|
|
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:
|
2020-03-23 10:21:17 +08:00
|
|
|
|
friend class MediaSource;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
MediaSourceEvent(){};
|
|
|
|
|
virtual ~MediaSourceEvent(){};
|
2019-12-03 16:10:02 +08:00
|
|
|
|
|
|
|
|
|
// 通知拖动进度条
|
2020-09-06 17:52:07 +08:00
|
|
|
|
virtual bool seekTo(MediaSource &sender, uint32_t stamp) { return false; }
|
|
|
|
|
// 通知其停止产生流
|
|
|
|
|
virtual bool close(MediaSource &sender, bool force) { return false; }
|
|
|
|
|
// 获取观看总人数
|
2019-12-28 16:48:11 +08:00
|
|
|
|
virtual int totalReaderCount(MediaSource &sender) = 0;
|
2020-03-23 10:21:17 +08:00
|
|
|
|
// 通知无人观看
|
2020-06-19 10:19:42 +08:00
|
|
|
|
virtual void onNoneReader(MediaSource &sender);
|
2020-07-02 18:14:39 +08:00
|
|
|
|
//流注册或注销事件
|
|
|
|
|
virtual void onRegist(MediaSource &sender, bool regist) {};
|
2020-06-19 10:19:42 +08:00
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
////////////////////////仅供MultiMediaSourceMuxer对象继承////////////////////////
|
|
|
|
|
// 开启或关闭录制
|
|
|
|
|
virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { return false; };
|
|
|
|
|
// 获取录制状态
|
|
|
|
|
virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; };
|
|
|
|
|
|
2020-03-23 10:21:17 +08:00
|
|
|
|
private:
|
|
|
|
|
Timer::Ptr _async_close_timer;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
};
|
2019-05-27 18:39:43 +08:00
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
//该对象用于拦截感兴趣的MediaSourceEvent事件
|
|
|
|
|
class MediaSourceEventInterceptor : public MediaSourceEvent{
|
|
|
|
|
public:
|
|
|
|
|
MediaSourceEventInterceptor(){}
|
|
|
|
|
~MediaSourceEventInterceptor() override {}
|
|
|
|
|
|
|
|
|
|
bool seekTo(MediaSource &sender, uint32_t stamp) override;
|
|
|
|
|
bool close(MediaSource &sender, bool force) override;
|
|
|
|
|
int totalReaderCount(MediaSource &sender) override;
|
|
|
|
|
void onNoneReader(MediaSource &sender) override;
|
|
|
|
|
void onRegist(MediaSource &sender, bool regist) override;;
|
|
|
|
|
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override;
|
|
|
|
|
bool isRecording(MediaSource &sender, Recorder::type type) override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::weak_ptr<MediaSourceEvent> _listener;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-29 10:49:04 +08:00
|
|
|
|
/**
|
|
|
|
|
* 解析url获取媒体相关信息
|
|
|
|
|
*/
|
2019-05-27 18:39:43 +08:00
|
|
|
|
class MediaInfo{
|
2018-02-02 18:19:35 +08:00
|
|
|
|
public:
|
2020-09-06 17:52:07 +08:00
|
|
|
|
~MediaInfo() {}
|
|
|
|
|
MediaInfo() {}
|
|
|
|
|
MediaInfo(const string &url) { parse(url); }
|
2018-02-02 18:19:35 +08:00
|
|
|
|
void parse(const string &url);
|
2020-09-06 17:52:07 +08:00
|
|
|
|
|
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;
|
2018-10-25 17:39:19 +08:00
|
|
|
|
string _param_strs;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* 媒体源,任何rtsp/rtmp的直播流都源自该对象
|
|
|
|
|
*/
|
2019-12-03 12:32:57 +08:00
|
|
|
|
class MediaSource: public TrackSource, public enable_shared_from_this<MediaSource> {
|
2018-02-02 18:19:35 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id) ;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
virtual ~MediaSource() ;
|
|
|
|
|
|
|
|
|
|
// 获取协议类型
|
|
|
|
|
const string& getSchema() const;
|
|
|
|
|
// 虚拟主机
|
|
|
|
|
const string& getVhost() const;
|
|
|
|
|
// 应用名
|
|
|
|
|
const string& getApp() const;
|
|
|
|
|
// 流id
|
|
|
|
|
const string& getId() const;
|
|
|
|
|
|
|
|
|
|
// 设置TrackSource
|
|
|
|
|
void setTrackSource(const std::weak_ptr<TrackSource> &track_src);
|
2019-12-29 10:49:04 +08:00
|
|
|
|
// 获取所有Track
|
2020-09-06 17:52:07 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override;
|
2019-12-29 10:49:04 +08:00
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 设置监听者
|
2020-09-06 17:52:33 +08:00
|
|
|
|
void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
|
2019-12-29 10:49:04 +08:00
|
|
|
|
// 获取监听者
|
|
|
|
|
const std::weak_ptr<MediaSourceEvent>& getListener() const;
|
|
|
|
|
|
2019-12-28 16:48:11 +08:00
|
|
|
|
// 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数
|
2019-12-03 16:10:02 +08:00
|
|
|
|
virtual int readerCount() = 0;
|
2019-12-28 16:48:11 +08:00
|
|
|
|
// 观看者个数,包括(hls/rtsp/rtmp)
|
|
|
|
|
virtual int totalReaderCount();
|
2019-12-29 10:49:04 +08:00
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 获取流当前时间戳
|
2020-09-06 17:52:07 +08:00
|
|
|
|
virtual uint32_t getTimeStamp(TrackType type) { return 0; };
|
2019-12-29 10:49:04 +08:00
|
|
|
|
// 设置时间戳
|
2020-09-06 17:52:07 +08:00
|
|
|
|
virtual void setTimeStamp(uint32_t stamp) {};
|
2018-10-26 15:09:08 +08:00
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 拖动进度条
|
2020-09-06 17:52:07 +08:00
|
|
|
|
bool seekTo(uint32_t stamp);
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 关闭该流
|
|
|
|
|
bool close(bool force);
|
|
|
|
|
// 该流无人观看
|
|
|
|
|
void onNoneReader();
|
2020-04-05 09:26:29 +08:00
|
|
|
|
// 开启或关闭录制
|
2020-09-06 17:52:33 +08:00
|
|
|
|
bool setupRecord(Recorder::type type, bool start, const string &custom_path);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
// 获取录制状态
|
2020-09-06 17:52:33 +08:00
|
|
|
|
bool isRecording(Recorder::type type);
|
2019-11-18 12:07:11 +08:00
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 同步查找流
|
2020-05-26 10:11:58 +08:00
|
|
|
|
static Ptr find(const string &schema, const string &vhost, const string &app, const string &id);
|
2019-12-03 16:10:02 +08:00
|
|
|
|
// 异步查找流
|
|
|
|
|
static void findAsync(const MediaInfo &info, const std::shared_ptr<TcpSession> &session, const function<void(const Ptr &src)> &cb);
|
|
|
|
|
// 遍历所有流
|
|
|
|
|
static void for_each_media(const function<void(const Ptr &src)> &cb);
|
2020-04-03 23:27:16 +08:00
|
|
|
|
// 从mp4文件生成MediaSource
|
2020-09-06 17:52:07 +08:00
|
|
|
|
static MediaSource::Ptr createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &file_path = "", bool check_app = true);
|
2020-05-26 10:11:58 +08:00
|
|
|
|
|
2018-08-31 14:13:00 +08:00
|
|
|
|
protected:
|
2020-09-06 17:52:07 +08:00
|
|
|
|
//媒体注册
|
|
|
|
|
void regist();
|
2020-05-26 10:11:58 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2020-09-06 17:52:07 +08:00
|
|
|
|
//媒体注销
|
|
|
|
|
bool unregist();
|
|
|
|
|
//触发媒体事件
|
|
|
|
|
void emitEvent(bool regist);
|
|
|
|
|
|
2018-02-02 18:19:35 +08:00
|
|
|
|
private:
|
2020-09-06 17:52:07 +08:00
|
|
|
|
string _schema;
|
|
|
|
|
string _vhost;
|
|
|
|
|
string _app;
|
|
|
|
|
string _stream_id;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
weak_ptr<TrackSource> _track_source;
|
2020-09-06 17:52:07 +08:00
|
|
|
|
std::weak_ptr<MediaSourceEvent> _listener;
|
2018-02-02 18:19:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-09 16:19:03 +08:00
|
|
|
|
///缓存刷新策略类
|
|
|
|
|
class FlushPolicy {
|
|
|
|
|
public:
|
2020-05-27 11:09:02 +08:00
|
|
|
|
FlushPolicy() = default;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
~FlushPolicy() = default;
|
|
|
|
|
|
|
|
|
|
uint32_t getStamp(const RtpPacket::Ptr &packet) {
|
|
|
|
|
return packet->timeStamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t getStamp(const RtmpPacket::Ptr &packet) {
|
2020-08-30 10:48:34 +08:00
|
|
|
|
return packet->time_stamp;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
bool isFlushAble(bool is_video, bool is_key, uint32_t new_stamp, int cache_size);
|
|
|
|
|
|
2020-04-09 16:19:03 +08:00
|
|
|
|
private:
|
2020-05-27 11:09:02 +08:00
|
|
|
|
uint32_t _last_stamp[2] = {0, 0};
|
2020-04-09 16:19:03 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
/// 合并写缓存模板
|
2020-04-09 16:19:03 +08:00
|
|
|
|
/// \tparam packet 包类型
|
|
|
|
|
/// \tparam policy 刷新缓存策略
|
|
|
|
|
/// \tparam packet_list 包缓存类型
|
|
|
|
|
template<typename packet, typename policy = FlushPolicy, typename packet_list = List<std::shared_ptr<packet> > >
|
2020-05-27 11:09:02 +08:00
|
|
|
|
class PacketCache {
|
2020-04-09 16:19:03 +08:00
|
|
|
|
public:
|
2020-05-27 11:09:02 +08:00
|
|
|
|
PacketCache(){
|
2020-04-09 16:19:03 +08:00
|
|
|
|
_cache = std::make_shared<packet_list>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
virtual ~PacketCache() = default;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
void inputPacket(bool is_video, const std::shared_ptr<packet> &pkt, bool key_pos) {
|
|
|
|
|
if (_policy.isFlushAble(is_video, key_pos, _policy.getStamp(pkt), _cache->size())) {
|
2020-04-09 16:19:03 +08:00
|
|
|
|
flushAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//追加数据到最后
|
2020-05-27 11:09:02 +08:00
|
|
|
|
_cache->emplace_back(pkt);
|
2020-04-09 16:19:03 +08:00
|
|
|
|
if (key_pos) {
|
|
|
|
|
_key_pos = key_pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
virtual void onFlush(std::shared_ptr<packet_list> &, bool key_pos) = 0;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void flushAll() {
|
|
|
|
|
if (_cache->empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-27 11:09:02 +08:00
|
|
|
|
onFlush(_cache, _key_pos);
|
2020-04-09 16:19:03 +08:00
|
|
|
|
_cache = std::make_shared<packet_list>();
|
|
|
|
|
_key_pos = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-06 17:52:07 +08:00
|
|
|
|
bool _key_pos = false;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
policy _policy;
|
|
|
|
|
std::shared_ptr<packet_list> _cache;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2020-05-27 11:09:02 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_MEDIASOURCE_H
|