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_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"
|
2018-02-02 18:06:08 +08:00
|
|
|
|
#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"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-01-20 16:22:25 +08:00
|
|
|
|
#define RTMP_GOP_SIZE 512
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* rtmp媒体源的数据抽象
|
|
|
|
|
* rtmp有关键的三要素,分别是metadata、config帧,普通帧
|
|
|
|
|
* 其中metadata是非必须的,有些编码格式也没有config帧(比如MP3)
|
|
|
|
|
* 只要生成了这三要素,那么要实现rtmp推流、rtmp服务器就很简单了
|
|
|
|
|
* rtmp推拉流协议中,先传递metadata,然后传递config帧,然后一直传递普通帧
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtmpMediaSource : public MediaSource, public toolkit::RingDelegate<RtmpPacket::Ptr>, private PacketCache<RtmpPacket>{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtmpMediaSource>;
|
|
|
|
|
using RingDataType = std::shared_ptr<toolkit::List<RtmpPacket::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
|
|
|
|
RtmpMediaSource(const std::string &vhost,
|
|
|
|
|
const std::string &app,
|
|
|
|
|
const std::string &stream_id,
|
2020-03-20 11:51:24 +08:00
|
|
|
|
int ring_size = RTMP_GOP_SIZE) :
|
|
|
|
|
MediaSource(RTMP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:09:56 +08:00
|
|
|
|
~RtmpMediaSource() override{}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
|
*/
|
|
|
|
|
const RingType::Ptr &getRing() const {
|
|
|
|
|
return _ring;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取播放器个数
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int readerCount() override {
|
|
|
|
|
return _ring ? _ring->readerCount() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取metadata
|
|
|
|
|
*/
|
|
|
|
|
const AMFValue &getMetaData() const {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
return _metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有的config帧
|
|
|
|
|
*/
|
|
|
|
|
template<typename FUNC>
|
|
|
|
|
void getConfigFrame(const FUNC &f) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
for (auto &pr : _config_frame_map) {
|
|
|
|
|
f(pr.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置metadata
|
|
|
|
|
*/
|
|
|
|
|
virtual void setMetaData(const AMFValue &metadata) {
|
|
|
|
|
_metadata = metadata;
|
2021-10-16 16:58:03 +08:00
|
|
|
|
_metadata.set("server", kServerName);
|
2021-07-15 11:29:21 +08:00
|
|
|
|
_have_video = _metadata["videocodecid"];
|
|
|
|
|
_have_audio = _metadata["audiocodecid"];
|
2020-09-06 17:53:22 +08:00
|
|
|
|
if (_ring) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
regist();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 18:01:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* 更新metadata
|
|
|
|
|
*/
|
|
|
|
|
void updateMetaData(const AMFValue &metadata) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2020-05-28 18:01:03 +08:00
|
|
|
|
_metadata = metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 输入rtmp包
|
|
|
|
|
* @param pkt rtmp包
|
|
|
|
|
*/
|
2020-10-10 18:23:25 +08:00
|
|
|
|
void onWrite(RtmpPacket::Ptr pkt, bool = true) override {
|
2020-12-05 12:22:17 +08:00
|
|
|
|
bool is_video = pkt->type_id == MSG_VIDEO;
|
|
|
|
|
_speed[is_video ? TrackVideo : TrackAudio] += pkt->size();
|
2020-09-06 17:53:22 +08:00
|
|
|
|
//保存当前时间戳
|
|
|
|
|
switch (pkt->type_id) {
|
|
|
|
|
case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break;
|
2021-07-15 11:29:21 +08:00
|
|
|
|
case MSG_AUDIO : _track_stamps[TrackAudio] = pkt->time_stamp, _have_audio = true; break;
|
2020-09-06 17:53:22 +08:00
|
|
|
|
default : break;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-09-06 17:53:22 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (pkt->isCfgFrame()) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_config_frame_map[pkt->type_id] = pkt;
|
2021-03-21 16:50:15 +08:00
|
|
|
|
if (!_ring) {
|
|
|
|
|
//注册后收到config帧更新到各播放器
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_ring) {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::weak_ptr<RtmpMediaSource> weakSelf = std::dynamic_pointer_cast<RtmpMediaSource>(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组RTMP包,每组RTMP包时间戳相同(如果开启合并写了,那么每组为合并写时间内的RTMP包),
|
|
|
|
|
//每次遇到关键帧第一个RTMP包,则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开)
|
2020-03-20 11:51:24 +08:00
|
|
|
|
_ring = std::make_shared<RingType>(_ring_size,std::move(lam));
|
|
|
|
|
onReaderChanged(0);
|
|
|
|
|
|
|
|
|
|
if(_metadata){
|
|
|
|
|
regist();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-10 18:23:25 +08:00
|
|
|
|
bool key = pkt->isVideoKeyFrame();
|
2020-10-24 23:28:25 +08:00
|
|
|
|
auto stamp = pkt->time_stamp;
|
|
|
|
|
PacketCache<RtmpPacket>::inputPacket(stamp, is_video, std::move(pkt), key);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前时间戳
|
|
|
|
|
*/
|
|
|
|
|
uint32_t getTimeStamp(TrackType trackType) override {
|
2020-09-06 17:53:22 +08:00
|
|
|
|
assert(trackType >= TrackInvalid && trackType < TrackMax);
|
|
|
|
|
if (trackType != TrackInvalid) {
|
|
|
|
|
//获取某track的时间戳
|
|
|
|
|
return _track_stamps[trackType];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取所有track的最小时间戳
|
|
|
|
|
uint32_t ret = UINT32_MAX;
|
|
|
|
|
for (auto &stamp : _track_stamps) {
|
|
|
|
|
if (stamp > 0 && stamp < ret) {
|
|
|
|
|
ret = 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
|
|
|
|
}
|
2019-05-27 18:39:43 +08:00
|
|
|
|
|
2020-09-12 19:09:56 +08:00
|
|
|
|
void clearCache() override{
|
|
|
|
|
PacketCache<RtmpPacket>::clearCache();
|
|
|
|
|
_ring->clearCache();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 11:29:21 +08:00
|
|
|
|
bool haveVideo() const {
|
|
|
|
|
return _have_video;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool haveAudio() const {
|
|
|
|
|
return _have_audio;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 18:39:43 +08:00
|
|
|
|
private:
|
2020-04-09 16:19:03 +08:00
|
|
|
|
/**
|
2020-05-27 11:09:02 +08:00
|
|
|
|
* 批量flush rtmp包时触发该函数
|
|
|
|
|
* @param rtmp_list rtmp包列表
|
2020-04-09 16:19:03 +08:00
|
|
|
|
* @param key_pos 是否包含关键帧
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onFlush(std::shared_ptr<toolkit::List<RtmpPacket::Ptr> > rtmp_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(rtmp_list), _have_video ? key_pos : true);
|
2020-04-09 16:19:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 10:21:17 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
bool _have_video = false;
|
2021-07-15 11:29:21 +08:00
|
|
|
|
bool _have_audio = false;
|
2020-09-06 17:53:22 +08:00
|
|
|
|
int _ring_size;
|
|
|
|
|
uint32_t _track_stamps[TrackMax] = {0};
|
2020-03-20 11:51:24 +08:00
|
|
|
|
AMFValue _metadata;
|
2020-04-09 16:19:03 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2020-09-06 17:53:22 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
mutable std::recursive_mutex _mtx;
|
|
|
|
|
std::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_ */
|