ZLMediaKit/src/Rtsp/RtspMediaSource.h

217 lines
6.5 KiB
C++
Raw Normal View History

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
*
* 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>
2017-04-01 16:35:56 +08:00
#include <unordered_map>
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
#include "Common/MediaSource.h"
2019-06-28 17:30:13 +08:00
#include "RtpCodec.h"
2017-04-01 16:35:56 +08:00
#include "Util/logger.h"
2017-04-25 11:35:41 +08:00
#include "Util/RingBuffer.h"
2017-04-01 16:35:56 +08:00
#include "Util/TimeTicker.h"
2017-04-25 11:35:41 +08:00
#include "Util/ResourcePool.h"
#include "Util/NoticeCenter.h"
#include "Thread/ThreadPool.h"
2017-04-01 16:35:56 +08:00
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2020-04-07 13:03:53 +08:00
#define RTP_GOP_SIZE 512
namespace mediakit {
2017-04-01 16:35:56 +08:00
/**
2019-12-25 11:04:12 +08:00
* rtsp媒体源的数据抽象
* rtsp有关键的两要素sdprtp包
* rtsp推流rtsp服务器就很简单了
* rtsp推拉流协议中sdp(tcp/udp/)rtp
*/
class RtspMediaSource : public MediaSource, public RingDelegate<RtpPacket::Ptr>, public PacketCache<RtpPacket> {
2017-04-01 16:35:56 +08:00
public:
2020-03-20 11:51:24 +08:00
typedef ResourcePool<RtpPacket> PoolType;
typedef std::shared_ptr<RtspMediaSource> Ptr;
2020-04-07 13:03:53 +08:00
typedef std::shared_ptr<List<RtpPacket::Ptr> > RingDataType;
typedef RingBuffer<RingDataType> RingType;
2020-03-20 11:51:24 +08:00
/**
*
* @param vhost
* @param app
* @param stream_id id
* @param ring_size 0
*/
RtspMediaSource(const string &vhost,
const string &app,
const string &stream_id,
int ring_size = RTP_GOP_SIZE) :
MediaSource(RTSP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {}
2020-09-12 19:09:56 +08:00
~RtspMediaSource() override{}
2020-03-20 11:51:24 +08:00
/**
*
*/
const RingType::Ptr &getRing() const {
return _ring;
}
/**
*
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
/**
* sdp
*/
const string &getSdp() const {
return _sdp;
}
/**
* 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;
}
/**
*
*/
uint32_t getTimeStamp(TrackType trackType) override {
2020-09-06 17:53:22 +08:00
assert(trackType >= TrackInvalid && trackType < TrackMax);
if (trackType != TrackInvalid) {
//获取某track的时间戳
2020-11-01 21:33:42 +08:00
auto &track = _tracks[trackType];
2020-09-06 17:53:22 +08:00
if (track) {
return track->_time_stamp;
}
2020-03-20 11:51:24 +08:00
}
2020-09-06 17:53:22 +08:00
//获取所有track的最小时间戳
uint32_t ret = UINT32_MAX;
for (auto &track : _tracks) {
if (track && track->_time_stamp < ret) {
ret = track->_time_stamp;
}
2020-03-20 11:51:24 +08:00
}
2020-09-06 17:53:22 +08:00
return ret;
2020-03-20 11:51:24 +08:00
}
/**
*
*/
2020-09-06 17:53:22 +08:00
void setTimeStamp(uint32_t stamp) override {
for (auto &track : _tracks) {
if (track) {
track->_time_stamp = stamp;
}
2020-03-20 11:51:24 +08:00
}
}
/**
* sdp
*/
virtual void setSdp(const string &sdp) {
2020-09-06 17:53:22 +08:00
SdpParser sdp_parser(sdp);
_tracks[TrackVideo] = sdp_parser.getTrack(TrackVideo);
_tracks[TrackAudio] = sdp_parser.getTrack(TrackAudio);
_have_video = (bool) _tracks[TrackVideo];
_sdp = sdp_parser.toString();
2020-03-20 11:51:24 +08:00
if (_ring) {
regist();
}
}
/**
* rtp
* @param rtp rtp包
* @param keyPos
*/
void onWrite(RtpPacket::Ptr rtp, bool keyPos) override {
2020-12-05 12:22:17 +08:00
_speed[rtp->type] += rtp->size();
2020-09-06 17:53:22 +08:00
assert(rtp->type >= 0 && rtp->type < TrackMax);
2020-11-01 21:33:42 +08:00
auto &track = _tracks[rtp->type];
2021-01-31 19:19:24 +08:00
auto stamp = rtp->getStampMS();
2020-03-20 11:51:24 +08:00
if (track) {
2021-01-31 19:19:24 +08:00
track->_seq = rtp->getSeq();
track->_time_stamp = rtp->getStamp() * uint64_t(1000) / rtp->sample_rate;
2021-01-31 19:19:24 +08:00
track->_ssrc = rtp->getSSRC();
2020-03-20 11:51:24 +08:00
}
if (!_ring) {
weak_ptr<RtspMediaSource> weakSelf = dynamic_pointer_cast<RtspMediaSource>(shared_from_this());
2020-09-12 19:09:56 +08:00
auto lam = [weakSelf](int size) {
2020-03-20 11:51:24 +08:00
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return;
}
strongSelf->onReaderChanged(size);
};
2020-09-06 17:53:22 +08:00
//GOP默认缓冲512组RTP包每组RTP包时间戳相同(如果开启合并写了那么每组为合并写时间内的RTP包),
//每次遇到关键帧第一个RTP包则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开)
2020-03-20 11:51:24 +08:00
_ring = std::make_shared<RingType>(_ring_size, std::move(lam));
onReaderChanged(0);
if (!_sdp.empty()) {
regist();
}
}
bool is_video = rtp->type == TrackVideo;
2020-10-24 23:28:25 +08:00
PacketCache<RtpPacket>::inputPacket(stamp, is_video, std::move(rtp), keyPos);
2020-03-20 11:51:24 +08:00
}
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
/**
* flush rtp包时触发该函数
2020-04-07 13:03:53 +08:00
* @param rtp_list rtp包列表
* @param key_pos
2020-04-07 13:03:53 +08:00
*/
void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > rtp_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存
_ring->write(std::move(rtp_list), _have_video ? key_pos : true);
2020-04-07 13:03:53 +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;
2020-03-20 11:51:24 +08:00
string _sdp;
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_ */