ZLMediaKit/src/Rtmp/RtmpMediaSource.h

217 lines
6.3 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/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.
2017-04-01 16:35:56 +08:00
*/
#ifndef SRC_RTMP_RTMPMEDIASOURCE_H_
#define SRC_RTMP_RTMPMEDIASOURCE_H_
2017-04-25 11:35:41 +08:00
#include <mutex>
#include <memory>
2017-04-01 16:35:56 +08:00
#include <string>
#include <functional>
#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
2019-06-28 17:30:13 +08:00
#include "RtmpDemuxer.h"
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
#include "Common/MediaSource.h"
2017-04-25 11:35:41 +08:00
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/RingBuffer.h"
#include "Util/TimeTicker.h"
#include "Util/ResourcePool.h"
#include "Util/NoticeCenter.h"
#include "Thread/ThreadPool.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-01 16:35:56 +08:00
2020-01-20 16:22:25 +08:00
#define RTMP_GOP_SIZE 512
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
2020-04-09 16:19:03 +08:00
typedef VideoPacketCache<RtmpPacket> RtmpVideoCache;
typedef AudioPacketCache<RtmpPacket> RtmpAudioCache;
2019-12-25 11:04:12 +08:00
/**
* rtmp媒体源的数据抽象
* rtmp有关键的三要素metadataconfig帧
* metadata是非必须的config帧(MP3)
* rtmp推流rtmp服务器就很简单了
* rtmp推拉流协议中metadataconfig帧
*/
2020-04-09 16:19:03 +08:00
class RtmpMediaSource : public MediaSource, public RingDelegate<RtmpPacket::Ptr>, public RtmpVideoCache, public RtmpAudioCache{
2017-04-01 16:35:56 +08:00
public:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<RtmpMediaSource> Ptr;
2020-04-09 16:19:03 +08:00
typedef std::shared_ptr<List<RtmpPacket::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
*/
RtmpMediaSource(const string &vhost,
const string &app,
const string &stream_id,
int ring_size = RTMP_GOP_SIZE) :
MediaSource(RTMP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {
}
virtual ~RtmpMediaSource() {}
/**
*
*/
const RingType::Ptr &getRing() const {
return _ring;
}
/**
*
* @return
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
/**
* metadata
*/
const AMFValue &getMetaData() const {
lock_guard<recursive_mutex> lock(_mtx);
return _metadata;
}
/**
* config帧
*/
template<typename FUNC>
void getConfigFrame(const FUNC &f) {
lock_guard<recursive_mutex> lock(_mtx);
for (auto &pr : _config_frame_map) {
f(pr.second);
}
}
/**
* metadata
*/
virtual void setMetaData(const AMFValue &metadata) {
lock_guard<recursive_mutex> lock(_mtx);
_metadata = metadata;
if(_ring){
regist();
}
}
/**
* rtmp包
* @param pkt rtmp包
* @param key
*/
void onWrite(const RtmpPacket::Ptr &pkt, bool key = true) override {
lock_guard<recursive_mutex> lock(_mtx);
if(pkt->typeId == MSG_VIDEO){
//有视频那么启用GOP缓存
2020-01-24 22:16:28 +08:00
_have_video = true;
2020-03-20 11:51:24 +08:00
}
if (pkt->isCfgFrame()) {
_config_frame_map[pkt->typeId] = pkt;
return;
}
2020-04-09 16:19:03 +08:00
//保存当前时间戳
_track_stamps_map[pkt->typeId] = pkt->timeStamp;
2020-03-20 11:51:24 +08:00
if (!_ring) {
weak_ptr<RtmpMediaSource> weakSelf = dynamic_pointer_cast<RtmpMediaSource>(shared_from_this());
auto lam = [weakSelf](const EventPoller::Ptr &, int size, bool) {
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return;
}
strongSelf->onReaderChanged(size);
};
//rtmp包缓存最大允许512个如果是纯视频(25fps)大概为20秒数据
//但是这个是GOP缓存的上限值真实的GOP缓存大小等于两个I帧之间的包数的两倍
//而且每次遇到I帧则会清空GOP缓存所以真实的GOP缓存远小于此值
_ring = std::make_shared<RingType>(_ring_size,std::move(lam));
onReaderChanged(0);
if(_metadata){
regist();
}
}
2020-04-09 16:19:03 +08:00
if(pkt->typeId == MSG_VIDEO){
RtmpVideoCache::inputVideo(pkt, key);
}else{
RtmpAudioCache::inputAudio(pkt);
}
2020-03-20 11:51:24 +08:00
}
/**
*
*/
uint32_t getTimeStamp(TrackType trackType) override {
lock_guard<recursive_mutex> lock(_mtx);
switch (trackType) {
case TrackVideo:
return _track_stamps_map[MSG_VIDEO];
case TrackAudio:
return _track_stamps_map[MSG_AUDIO];
default:
return MAX(_track_stamps_map[MSG_VIDEO], _track_stamps_map[MSG_AUDIO]);
}
}
2019-05-27 18:39:43 +08:00
private:
2020-04-09 16:19:03 +08:00
/**
* flush时间戳相同的视频rtmp包时触发该函数
* @param rtmp_list rtmp包列表
* @param key_pos
*/
void onFlushVideo(std::shared_ptr<List<RtmpPacket::Ptr> > &rtmp_list, bool key_pos) override {
_ring->write(rtmp_list, key_pos);
}
/**
* flush一定数量的音频rtmp包时触发该函数
* @param rtmp_list rtmp包列表
*/
void onFlushAudio(std::shared_ptr<List<RtmpPacket::Ptr> > &rtmp_list) override{
//只有音频的话就不存在gop缓存的意义
_ring->write(rtmp_list, !_have_video);
}
2020-03-20 11:51:24 +08:00
/**
*
*/
void onReaderChanged(int size) {
if (size == 0) {
2020-03-20 11:51:24 +08:00
onNoneReader();
}
}
private:
2020-03-20 11:51:24 +08:00
int _ring_size;
bool _have_video = false;
mutable recursive_mutex _mtx;
AMFValue _metadata;
2020-04-09 16:19:03 +08:00
RingType::Ptr _ring;
2020-03-20 11:51:24 +08:00
unordered_map<int, uint32_t> _track_stamps_map;
unordered_map<int, RtmpPacket::Ptr> _config_frame_map;
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_RTMP_RTMPMEDIASOURCE_H_ */