MediaSource.h 5.03 KB
Newer Older
1
/*
2 3
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * 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"
xiongziliang committed
37
#include "Common/Parser.h"
38 39 40
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/NoticeCenter.h"
xiongziliang committed
41
#include "Extension/Track.h"
42 43

using namespace std;
xiongziliang committed
44
using namespace toolkit;
45

46 47
namespace toolkit{
    class TcpSession;
xiongziliang committed
48
}// namespace toolkit
49

xiongziliang committed
50
namespace mediakit {
51

52 53
class MediaSource;
class MediaSourceEvent{
54 55 56
public:
    MediaSourceEvent(){};
    virtual ~MediaSourceEvent(){};
xiongziliang committed
57 58

    // 通知拖动进度条
xiongziliang committed
59
    virtual bool seekTo(MediaSource &sender,uint32_t ui32Stamp){
60 61
        return false;
    }
62

xiongziliang committed
63
    // 通知其停止推流
xiongziliang committed
64
    virtual bool close(MediaSource &sender,bool force) {
65 66
        return false;
    }
67

xiongziliang committed
68
    // 通知无人观看
xiongziliang committed
69
    virtual void onNoneReader(MediaSource &sender);
70 71 72

    // 观看总人数
    virtual int totalReaderCount(MediaSource &sender) = 0;
73
};
74

75 76 77
/**
 * 解析url获取媒体相关信息
 */
78
class MediaInfo{
79 80 81
public:
    MediaInfo(){}
    ~MediaInfo(){}
82
    MediaInfo(const string &url){ parse(url); }
83 84
    void parse(const string &url);
public:
85 86 87 88 89 90
    string _schema;
    string _host;
    string _port;
    string _vhost;
    string _app;
    string _streamid;
91
    string _param_strs;
92 93
};

xiongziliang committed
94 95 96
/**
 * 媒体源,任何rtsp/rtmp的直播流都源自该对象
 */
xiongziliang committed
97
class MediaSource: public TrackSource, public enable_shared_from_this<MediaSource> {
98 99 100 101 102 103 104
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;

xiongziliang committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    MediaSource(const string &strSchema, const string &strVhost, const string &strApp, const string &strId) ;
    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);
119 120 121
    // 获取所有Track
    vector<Track::Ptr> getTracks(bool trackReady = true) const override;

xiongziliang committed
122 123
    // 设置监听者
    virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
124 125 126 127
    // 获取监听者
    const std::weak_ptr<MediaSourceEvent>& getListener() const;


128
    // 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数
xiongziliang committed
129
    virtual int readerCount() = 0;
130 131
    // 观看者个数,包括(hls/rtsp/rtmp)
    virtual int totalReaderCount();
132

xiongziliang committed
133
    // 获取流当前时间戳
134
    virtual uint32_t getTimeStamp(TrackType trackType) { return 0; };
135 136
    // 设置时间戳
    virtual void setTimeStamp(uint32_t uiStamp) {};
137

xiongziliang committed
138 139 140 141 142 143
    // 拖动进度条
    bool seekTo(uint32_t ui32Stamp);
    // 关闭该流
    bool close(bool force);
    // 该流无人观看
    void onNoneReader();
144

xiongziliang committed
145 146 147 148 149 150
    // 同步查找流
    static Ptr find(const string &schema, const string &vhost, const string &app, const string &id, bool bMake = true) ;
    // 异步查找流
    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);
151
protected:
152
    void regist() ;
153
    bool unregist() ;
154
private:
xiongziliang committed
155 156 157 158 159 160 161 162
    string _strSchema;
    string _strVhost;
    string _strApp;
    string _strId;
    std::weak_ptr<MediaSourceEvent> _listener;
    weak_ptr<TrackSource> _track_source;
    static SchemaVhostAppStreamMap g_mapMediaSrc;
    static recursive_mutex g_mtxMediaSrc;
163 164
};

xiongziliang committed
165
} /* namespace mediakit */
166 167 168


#endif //ZLMEDIAKIT_MEDIASOURCE_H