MediaSource.h 12.1 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * 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.
9 10 11 12 13 14 15 16 17 18 19
 */

#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
20
#include "Common/Parser.h"
21 22 23
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/NoticeCenter.h"
xiongziliang committed
24
#include "Util/List.h"
25
#include "Network/Socket.h"
xiongziliang committed
26 27
#include "Rtsp/Rtsp.h"
#include "Rtmp/Rtmp.h"
xiongziliang committed
28
#include "Extension/Track.h"
29
#include "Record/Recorder.h"
30 31

using namespace std;
xiongziliang committed
32
using namespace toolkit;
33

34
namespace toolkit{
35
    class Session;
xiongziliang committed
36
}// namespace toolkit
37

xiongziliang committed
38
namespace mediakit {
39

40 41 42 43 44 45 46 47
enum class MediaOriginType : uint8_t {
    unknown = 0,
    rtmp_push ,
    rtsp_push,
    rtp_push,
    pull,
    ffmpeg_pull,
    mp4_vod,
xia-chu committed
48 49
    device_chn,
    rtc_push,
50 51 52 53
};

string getOriginTypeString(MediaOriginType type);

54 55
class MediaSource;
class MediaSourceEvent{
56
public:
57
    friend class MediaSource;
58 59
    MediaSourceEvent(){};
    virtual ~MediaSourceEvent(){};
xiongziliang committed
60

61 62 63 64 65 66 67
    // 获取媒体源类型
    virtual MediaOriginType getOriginType(MediaSource &sender) const { return MediaOriginType::unknown; }
    // 获取媒体源url或者文件路径
    virtual string getOriginUrl(MediaSource &sender) const { return ""; }
    // 获取媒体源客户端相关信息
    virtual std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const { return nullptr; }

xiongziliang committed
68
    // 通知拖动进度条
xiongziliang committed
69
    virtual bool seekTo(MediaSource &sender, uint32_t stamp) { return false; }
ziyue committed
70 71
    // 通知暂停或恢复
    virtual bool pause(MediaSource &sender, bool pause) { return false; }
72
    // 通知倍数
ziyue committed
73
    virtual bool speed(MediaSource &sender, float speed) { return false; }
xiongziliang committed
74 75 76
    // 通知其停止产生流
    virtual bool close(MediaSource &sender, bool force) { return false; }
    // 获取观看总人数
77
    virtual int totalReaderCount(MediaSource &sender) = 0;
78 79
    // 通知观看人数变化
    virtual void onReaderChanged(MediaSource &sender, int size);
80 81
    //流注册或注销事件
    virtual void onRegist(MediaSource &sender, bool regist) {};
82

xiongziliang committed
83 84
    ////////////////////////仅供MultiMediaSourceMuxer对象继承////////////////////////
    // 开启或关闭录制
85
    virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { return false; };
xiongziliang committed
86 87
    // 获取录制状态
    virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; };
88
    // 获取所有track相关信息
89
    virtual vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const { return vector<Track::Ptr>(); };
90
    // 开始发送ps-rtp
91
    virtual void startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb) { cb(0, SockException(Err_other, "not implemented"));};
92
    // 停止发送ps-rtp
93
    virtual bool stopSendRtp(MediaSource &sender, const string &ssrc) {return false; }
xiongziliang committed
94

95 96
private:
    Timer::Ptr _async_close_timer;
97
};
98

xiongziliang committed
99 100 101 102 103 104
//该对象用于拦截感兴趣的MediaSourceEvent事件
class MediaSourceEventInterceptor : public MediaSourceEvent{
public:
    MediaSourceEventInterceptor(){}
    ~MediaSourceEventInterceptor() override {}

105 106 107
    void setDelegate(const std::weak_ptr<MediaSourceEvent> &listener);
    std::shared_ptr<MediaSourceEvent> getDelegate() const;

108 109 110 111
    MediaOriginType getOriginType(MediaSource &sender) const override;
    string getOriginUrl(MediaSource &sender) const override;
    std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;

xiongziliang committed
112
    bool seekTo(MediaSource &sender, uint32_t stamp) override;
ziyue committed
113 114
    bool pause(MediaSource &sender,  bool pause) override;
    bool speed(MediaSource &sender, float speed) override;
xiongziliang committed
115 116
    bool close(MediaSource &sender, bool force) override;
    int totalReaderCount(MediaSource &sender) override;
117
    void onReaderChanged(MediaSource &sender, int size) override;
118
    void onRegist(MediaSource &sender, bool regist) override;
119
    bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) override;
xiongziliang committed
120
    bool isRecording(MediaSource &sender, Recorder::type type) override;
121
    vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const override;
122
    void startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb) override;
123
    bool stopSendRtp(MediaSource &sender, const string &ssrc) override;
xiongziliang committed
124

125
private:
xiongziliang committed
126 127 128
    std::weak_ptr<MediaSourceEvent> _listener;
};

129 130 131
/**
 * 解析url获取媒体相关信息
 */
132
class MediaInfo{
133
public:
xiongziliang committed
134 135 136
    ~MediaInfo() {}
    MediaInfo() {}
    MediaInfo(const string &url) { parse(url); }
137
    void parse(const string &url);
xiongziliang committed
138

139
public:
140
    string _full_url;
141 142 143 144 145 146
    string _schema;
    string _host;
    string _port;
    string _vhost;
    string _app;
    string _streamid;
147
    string _param_strs;
148 149
};

150 151 152 153 154 155 156 157
class BytesSpeed {
public:
    BytesSpeed() = default;
    ~BytesSpeed() = default;

    /**
     * 添加统计字节
     */
158
    BytesSpeed& operator += (size_t bytes) {
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        _bytes += bytes;
        if (_bytes > 1024 * 1024) {
            //数据大于1MB就计算一次网速
            computeSpeed();
        }
        return *this;
    }

    /**
     * 获取速度,单位bytes/s
     */
    int getSpeed() {
        if (_ticker.elapsedTime() < 1000) {
            //获取频率小于1秒,那么返回上次计算结果
            return _speed;
        }
        return computeSpeed();
    }

private:
179
    int computeSpeed() {
180 181 182 183
        auto elapsed = _ticker.elapsedTime();
        if (!elapsed) {
            return _speed;
        }
184
        _speed = (int)(_bytes * 1000 / elapsed);
185 186 187 188 189 190 191
        _ticker.resetTime();
        _bytes = 0;
        return _speed;
    }

private:
    int _speed = 0;
192
    size_t _bytes = 0;
193 194 195
    Ticker _ticker;
};

xiongziliang committed
196 197 198
/**
 * 媒体源,任何rtsp/rtmp的直播流都源自该对象
 */
xiongziliang committed
199
class MediaSource: public TrackSource, public enable_shared_from_this<MediaSource> {
200
public:
201 202 203 204 205 206
    static constexpr MediaSource *NullMediaSource = nullptr;
    using Ptr = std::shared_ptr<MediaSource>;
    using StreamMap = unordered_map<string, weak_ptr<MediaSource> >;
    using AppStreamMap = unordered_map<string, StreamMap>;
    using VhostAppStreamMap = unordered_map<string, AppStreamMap>;
    using SchemaVhostAppStreamMap = unordered_map<string, VhostAppStreamMap>;
207

xiongziliang committed
208
    MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id) ;
209
    virtual ~MediaSource();
xiongziliang committed
210

211 212
    ////////////////获取MediaSource相关信息////////////////

xiongziliang committed
213 214 215 216 217 218 219 220 221
    // 获取协议类型
    const string& getSchema() const;
    // 虚拟主机
    const string& getVhost() const;
    // 应用名
    const string& getApp() const;
    // 流id
    const string& getId() const;

222
    // 获取所有Track
xiongziliang committed
223
    vector<Track::Ptr> getTracks(bool ready = true) const override;
224

225 226 227 228 229
    // 获取流当前时间戳
    virtual uint32_t getTimeStamp(TrackType type) { return 0; };
    // 设置时间戳
    virtual void setTimeStamp(uint32_t stamp) {};

230
    // 获取数据速率,单位bytes/s
xiongziliang committed
231
    int getBytesSpeed(TrackType type = TrackInvalid);
232 233 234 235
    // 获取流创建GMT unix时间戳,单位秒
    uint64_t getCreateStamp() const;
    // 获取流上线时间,单位秒
    uint64_t getAliveSecond() const;
236

237 238
    ////////////////MediaSourceEvent相关接口实现////////////////

xiongziliang committed
239
    // 设置监听者
240
    virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
241
    // 获取监听者
242
    std::weak_ptr<MediaSourceEvent> getListener(bool next = false) const;
243

244
    // 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数
xiongziliang committed
245
    virtual int readerCount() = 0;
246 247
    // 观看者个数,包括(hls/rtsp/rtmp)
    virtual int totalReaderCount();
248

249 250 251 252 253 254 255
    // 获取媒体源类型
    MediaOriginType getOriginType() const;
    // 获取媒体源url或者文件路径
    string getOriginUrl() const;
    // 获取媒体源客户端相关信息
    std::shared_ptr<SockInfo> getOriginSock() const;

xiongziliang committed
256
    // 拖动进度条
xiongziliang committed
257
    bool seekTo(uint32_t stamp);
258
    //暂停
ziyue committed
259
    bool pause(bool pause);
260 261
    //倍数播放
    bool speed(float speed);
xiongziliang committed
262 263
    // 关闭该流
    bool close(bool force);
264 265
    // 该流观看人数变化
    void onReaderChanged(int size);
266
    // 开启或关闭录制
267
    bool setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second);
268
    // 获取录制状态
269
    bool isRecording(Recorder::type type);
270
    // 开始发送ps-rtp
271
    void startSendRtp(const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb);
272
    // 停止发送ps-rtp
273
    bool stopSendRtp(const string &ssrc);
274

275 276
    ////////////////static方法,查找或生成MediaSource////////////////

xiongziliang committed
277
    // 同步查找流
278
    static Ptr find(const string &schema, const string &vhost, const string &app, const string &id);
279 280 281 282

    // 忽略类型,同步查找流,可能返回rtmp/rtsp/hls类型
    static Ptr find(const string &vhost, const string &app, const string &stream_id);

xiongziliang committed
283
    // 异步查找流
284
    static void findAsync(const MediaInfo &info, const std::shared_ptr<Session> &session, const function<void(const Ptr &src)> &cb);
xiongziliang committed
285
    // 遍历所有流
286 287 288 289 290
    static void for_each_media(const function<void(const Ptr &src)> &cb,
                               const string &schema = "",
                               const string &vhost = "",
                               const string &app = "",
                               const string &stream = "");
291
    // 从mp4文件生成MediaSource
xiongziliang committed
292
    static MediaSource::Ptr createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &file_path = "", bool check_app = true);
293

294
protected:
xiongziliang committed
295 296
    //媒体注册
    void regist();
297 298

private:
xiongziliang committed
299 300 301 302 303
    //媒体注销
    bool unregist();
    //触发媒体事件
    void emitEvent(bool regist);

304
protected:
xiongziliang committed
305
    BytesSpeed _speed[TrackMax];
306

307
private:
308 309
    time_t _create_stamp;
    Ticker _ticker;
xiongziliang committed
310 311 312 313 314
    string _schema;
    string _vhost;
    string _app;
    string _stream_id;
    std::weak_ptr<MediaSourceEvent> _listener;
315 316
    //对象个数统计
    ObjectStatistic<MediaSource> _statistic;
317 318
};

xiongziliang committed
319 320 321
///缓存刷新策略类
class FlushPolicy {
public:
322
    FlushPolicy() = default;
xiongziliang committed
323 324
    ~FlushPolicy() = default;

325
    bool isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, size_t cache_size);
326

xiongziliang committed
327
private:
xiongziliang committed
328
    uint64_t _last_stamp[2] = {0, 0};
xiongziliang committed
329 330
};

331
/// 合并写缓存模板
xiongziliang committed
332 333 334 335
/// \tparam packet 包类型
/// \tparam policy 刷新缓存策略
/// \tparam packet_list 包缓存类型
template<typename packet, typename policy = FlushPolicy, typename packet_list = List<std::shared_ptr<packet> > >
336
class PacketCache {
xiongziliang committed
337
public:
338
    PacketCache(){
xiongziliang committed
339 340 341
        _cache = std::make_shared<packet_list>();
    }

342
    virtual ~PacketCache() = default;
xiongziliang committed
343

xiongziliang committed
344 345
    void inputPacket(uint64_t stamp, bool is_video, std::shared_ptr<packet> pkt, bool key_pos) {
        if (_policy.isFlushAble(is_video, key_pos, stamp, _cache->size())) {
xiongziliang committed
346 347 348 349
            flushAll();
        }

        //追加数据到最后
350
        _cache->emplace_back(std::move(pkt));
xiongziliang committed
351 352 353 354 355
        if (key_pos) {
            _key_pos = key_pos;
        }
    }

356 357 358 359
    virtual void clearCache() {
        _cache->clear();
    }

360
    virtual void onFlush(std::shared_ptr<packet_list>, bool key_pos) = 0;
xiongziliang committed
361 362 363 364 365 366

private:
    void flushAll() {
        if (_cache->empty()) {
            return;
        }
367
        onFlush(std::move(_cache), _key_pos);
xiongziliang committed
368 369 370 371 372
        _cache = std::make_shared<packet_list>();
        _key_pos = false;
    }

private:
xiongziliang committed
373
    bool _key_pos = false;
xiongziliang committed
374 375 376 377
    policy _policy;
    std::shared_ptr<packet_list> _cache;
};

xiongziliang committed
378
} /* namespace mediakit */
379
#endif //ZLMEDIAKIT_MEDIASOURCE_H