ZLMediaKit/src/Rtmp/RtmpMediaSource.h

163 lines
4.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_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"
#include "Common/MediaSource.h"
#include "Common/PacketCache.h"
2017-04-25 11:35:41 +08:00
#include "Util/RingBuffer.h"
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
2019-12-25 11:04:12 +08:00
/**
* rtmp媒体源的数据抽象
* rtmp有关键的三要素metadataconfig帧
* metadata是非必须的config帧(MP3)
* rtmp推流rtmp服务器就很简单了
* rtmp推拉流协议中metadataconfig帧
*/
class RtmpMediaSource : public MediaSource, public toolkit::RingDelegate<RtmpPacket::Ptr>, private PacketCache<RtmpPacket>{
2017-04-01 16:35:56 +08:00
public:
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
*/
2023-05-25 16:23:24 +08:00
RtmpMediaSource(const MediaTuple& tuple, int ring_size = RTMP_GOP_SIZE): MediaSource(RTMP_SCHEMA, tuple), _ring_size(ring_size) {}
2020-03-20 11:51:24 +08:00
2022-10-16 19:49:56 +08:00
~RtmpMediaSource() override { flush(); }
2020-03-20 11:51:24 +08:00
/**
*
*/
const RingType::Ptr &getRing() const {
return _ring;
}
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
/**
*
* @return
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
/**
* metadata
*/
const AMFValue &getMetaData() const {
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) {
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;
2023-04-21 22:25:06 +08:00
_metadata.set("title", std::string("Streamed by ") + kServerName);
_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();
}
}
/**
* metadata
*/
void updateMetaData(const AMFValue &metadata) {
std::lock_guard<std::recursive_mutex> lock(_mtx);
_metadata = metadata;
}
2020-03-20 11:51:24 +08:00
/**
* rtmp包
* @param pkt rtmp包
*/
void onWrite(RtmpPacket::Ptr pkt, bool = true) override;
2020-03-20 11:51:24 +08:00
/**
*
*/
uint32_t getTimeStamp(TrackType trackType) override;
2019-05-27 18:39:43 +08:00
2020-09-12 19:09:56 +08:00
void clearCache() override{
PacketCache<RtmpPacket>::clearCache();
_ring->clearCache();
}
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
/**
* flush rtmp包时触发该函数
* @param rtmp_list rtmp包列表
2020-04-09 16:19:03 +08:00
* @param key_pos
*/
void onFlush(std::shared_ptr<toolkit::List<RtmpPacket::Ptr> > rtmp_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存
_ring->write(std::move(rtmp_list), _have_video ? key_pos : true);
2020-04-09 16:19:03 +08:00
}
private:
2020-03-20 11:51:24 +08:00
bool _have_video = false;
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
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_ */