2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_RTSP_RTSPMEDIASOURCE_H_
|
|
|
|
|
#define SRC_RTSP_RTSPMEDIASOURCE_H_
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <mutex>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <functional>
|
2018-02-02 18:06:08 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/PacketCache.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/RingBuffer.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2020-04-07 13:03:53 +08:00
|
|
|
|
#define RTP_GOP_SIZE 512
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2020-04-07 13:03:53 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-05-27 11:09:02 +08:00
|
|
|
|
/**
|
2019-12-25 11:04:12 +08:00
|
|
|
|
* rtsp媒体源的数据抽象
|
|
|
|
|
* rtsp有关键的两要素,分别是sdp、rtp包
|
|
|
|
|
* 只要生成了这两要素,那么要实现rtsp推流、rtsp服务器就很简单了
|
|
|
|
|
* rtsp推拉流协议中,先传递sdp,然后再协商传输方式(tcp/udp/组播),最后一直传递rtp
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtspMediaSource : public MediaSource, public toolkit::RingDelegate<RtpPacket::Ptr>, private PacketCache<RtpPacket> {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtspMediaSource>;
|
|
|
|
|
using RingDataType = std::shared_ptr<toolkit::List<RtpPacket::Ptr> >;
|
|
|
|
|
using RingType = toolkit::RingBuffer<RingDataType>;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param vhost 虚拟主机名
|
|
|
|
|
* @param app 应用名
|
|
|
|
|
* @param stream_id 流id
|
|
|
|
|
* @param ring_size 可以设置固定的环形缓冲大小,0则自适应
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
RtspMediaSource(const std::string &vhost,
|
|
|
|
|
const std::string &app,
|
|
|
|
|
const std::string &stream_id,
|
2020-03-20 11:51:24 +08:00
|
|
|
|
int ring_size = RTP_GOP_SIZE) :
|
|
|
|
|
MediaSource(RTSP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {}
|
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
~RtspMediaSource() override { flush(); }
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
|
*/
|
|
|
|
|
const RingType::Ptr &getRing() const {
|
|
|
|
|
return _ring;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 21:05:19 +08:00
|
|
|
|
void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
|
|
|
|
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) override {
|
|
|
|
|
_ring->getInfoList(cb, on_change);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取播放器个数
|
|
|
|
|
*/
|
|
|
|
|
int readerCount() override {
|
|
|
|
|
return _ring ? _ring->readerCount() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取该源的sdp
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &getSdp() const {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
return _sdp;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 20:47:40 +08:00
|
|
|
|
virtual RtspMediaSource::Ptr Clone(const std::string& stream) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的ssrc
|
|
|
|
|
*/
|
|
|
|
|
virtual uint32_t getSsrc(TrackType trackType) {
|
2020-09-06 17:53:22 +08:00
|
|
|
|
assert(trackType >= 0 && trackType < TrackMax);
|
2020-11-01 21:33:42 +08:00
|
|
|
|
auto &track = _tracks[trackType];
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (!track) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_ssrc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的seqence
|
|
|
|
|
*/
|
|
|
|
|
virtual uint16_t getSeqence(TrackType trackType) {
|
2020-09-06 17:53:22 +08:00
|
|
|
|
assert(trackType >= 0 && trackType < TrackMax);
|
2020-11-01 21:33:42 +08:00
|
|
|
|
auto &track = _tracks[trackType];
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (!track) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_seq;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的时间戳,单位毫秒
|
|
|
|
|
*/
|
2022-11-29 11:07:13 +08:00
|
|
|
|
uint32_t getTimeStamp(TrackType trackType) override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新时间戳
|
|
|
|
|
*/
|
2022-11-29 11:07:13 +08:00
|
|
|
|
void setTimeStamp(uint32_t stamp) override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置sdp
|
|
|
|
|
*/
|
2022-11-29 11:07:13 +08:00
|
|
|
|
virtual void setSdp(const std::string &sdp);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入rtp
|
|
|
|
|
* @param rtp rtp包
|
|
|
|
|
* @param keyPos 该包是否为关键帧的第一个包
|
|
|
|
|
*/
|
2022-11-29 11:07:13 +08:00
|
|
|
|
void onWrite(RtpPacket::Ptr rtp, bool keyPos) override;
|
2020-04-07 13:03:53 +08:00
|
|
|
|
|
2020-09-12 19:09:56 +08:00
|
|
|
|
void clearCache() override{
|
|
|
|
|
PacketCache<RtpPacket>::clearCache();
|
|
|
|
|
_ring->clearCache();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 18:39:43 +08:00
|
|
|
|
private:
|
2020-04-07 13:03:53 +08:00
|
|
|
|
/**
|
2020-05-27 11:09:02 +08:00
|
|
|
|
* 批量flush rtp包时触发该函数
|
2020-04-07 13:03:53 +08:00
|
|
|
|
* @param rtp_list rtp包列表
|
2020-05-27 11:09:02 +08:00
|
|
|
|
* @param key_pos 是否包含关键帧
|
2020-04-07 13:03:53 +08:00
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onFlush(std::shared_ptr<toolkit::List<RtpPacket::Ptr> > rtp_list, bool key_pos) override {
|
2020-05-27 11:09:02 +08:00
|
|
|
|
//如果不存在视频,那么就没有存在GOP缓存的意义,所以is_key一直为true确保一直清空GOP缓存
|
2020-10-10 18:23:25 +08:00
|
|
|
|
_ring->write(std::move(rtp_list), _have_video ? key_pos : true);
|
2020-04-07 13:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 10:21:17 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
bool _have_video = false;
|
2020-09-06 17:53:22 +08:00
|
|
|
|
int _ring_size;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _sdp;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2020-09-06 17:53:22 +08:00
|
|
|
|
SdpTrack::Ptr _tracks[TrackMax];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_RTSP_RTSPMEDIASOURCE_H_ */
|