ZLMediaKit/src/Rtmp/RtmpMediaSource.h

211 lines
5.8 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
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
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 RingDelegate<RtmpPacket::Ptr> {
2017-04-01 16:35:56 +08:00
public:
typedef std::shared_ptr<RtmpMediaSource> Ptr;
2017-12-04 23:55:09 +08:00
typedef RingBuffer<RtmpPacket::Ptr> RingType;
2019-12-25 11:04:12 +08:00
/**
*
* @param vhost
* @param app
* @param stream_id id
* @param ring_size 0
*/
2019-05-27 18:39:43 +08:00
RtmpMediaSource(const string &vhost,
2019-12-25 11:04:12 +08:00
const string &app,
const string &stream_id,
int ring_size = 0) :
MediaSource(RTMP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {
_metadata = TitleMeta().getMetadata();
}
2019-05-27 18:39:43 +08:00
virtual ~RtmpMediaSource() {}
2019-12-25 11:04:12 +08:00
/**
*
*/
2017-04-01 16:35:56 +08:00
const RingType::Ptr &getRing() const {
2019-12-25 11:04:12 +08:00
return _ring;
2017-04-01 16:35:56 +08:00
}
2019-12-25 11:04:12 +08:00
/**
*
* @return
*/
int readerCount() override {
2019-12-25 11:04:12 +08:00
return _ring ? _ring->readerCount() : 0;
}
2019-12-25 11:04:12 +08:00
/**
* metadata
*/
2017-04-01 16:35:56 +08:00
const AMFValue &getMetaData() const {
2019-12-25 11:04:12 +08:00
lock_guard<recursive_mutex> lock(_mtx);
2018-10-24 15:43:52 +08:00
return _metadata;
2017-04-01 16:35:56 +08:00
}
2019-12-25 11:04:12 +08:00
/**
* config帧
*/
template<typename FUNC>
void getConfigFrame(const FUNC &f) {
lock_guard<recursive_mutex> lock(_mtx);
for (auto &pr : _config_frame_map) {
2017-04-01 16:35:56 +08:00
f(pr.second);
}
}
2019-12-25 11:04:12 +08:00
/**
* metadata
*/
virtual void setMetaData(const AMFValue &metadata) {
lock_guard<recursive_mutex> lock(_mtx);
2018-10-24 15:43:52 +08:00
_metadata = metadata;
2017-04-01 16:35:56 +08:00
}
2018-10-25 16:55:48 +08:00
2019-12-25 11:04:12 +08:00
/**
* rtmp包
* @param pkt rtmp包
* @param key
2019-12-25 11:04:12 +08:00
*/
void onWrite(const RtmpPacket::Ptr &pkt, bool key = true) override {
2019-12-25 11:04:12 +08:00
lock_guard<recursive_mutex> lock(_mtx);
if (pkt->isCfgFrame()) {
2019-12-25 11:04:12 +08:00
_config_frame_map[pkt->typeId] = pkt;
return;
2017-04-01 16:35:56 +08:00
}
2019-05-27 18:39:43 +08:00
2019-12-25 11:04:12 +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);
};
_ring = std::make_shared<RingType>(_ring_size, std::move(lam));
onReaderChanged(0);
//如果输入了非config帧
//那么说明不再可能获取config帧以及metadata,
//所以我们强制其为已注册
regist();
}
2019-12-25 11:04:12 +08:00
_track_stamps_map[pkt->typeId] = pkt->timeStamp;
_ring->write(pkt, pkt->isVideoKeyFrame());
checkNoneReader();
2019-12-25 11:04:12 +08:00
}
2018-10-26 15:09:08 +08:00
2019-12-25 11:04:12 +08:00
/**
*
*/
2018-10-26 15:09:08 +08:00
uint32_t getTimeStamp(TrackType trackType) override {
2019-12-25 11:04:12 +08:00
lock_guard<recursive_mutex> lock(_mtx);
switch (trackType) {
2018-10-26 15:09:08 +08:00
case TrackVideo:
2019-12-25 11:04:12 +08:00
return _track_stamps_map[MSG_VIDEO];
2018-10-26 15:09:08 +08:00
case TrackAudio:
2019-12-25 11:04:12 +08:00
return _track_stamps_map[MSG_AUDIO];
2018-10-26 15:09:08 +08:00
default:
2019-12-25 11:04:12 +08:00
return MAX(_track_stamps_map[MSG_VIDEO], _track_stamps_map[MSG_AUDIO]);
2018-10-26 15:09:08 +08:00
}
}
2019-05-27 18:39:43 +08:00
private:
2019-12-25 11:04:12 +08:00
/**
*
*/
void onReaderChanged(int size) {
//我们记录最后一次活动时间
_reader_changed_ticker.resetTime();
2019-12-28 16:57:35 +08:00
if (size != 0 || totalReaderCount() != 0) {
2019-12-25 11:04:12 +08:00
//还有消费者正在观看该流
_async_emit_none_reader = false;
return;
}
_async_emit_none_reader = true;
}
/**
*
* onNoneReader事件
*/
void checkNoneReader() {
GET_CONFIG(int, stream_none_reader_delay, General::kStreamNoneReaderDelayMS);
if (_async_emit_none_reader && _reader_changed_ticker.elapsedTime() > stream_none_reader_delay) {
_async_emit_none_reader = false;
onNoneReader();
}
}
2017-04-01 16:35:56 +08:00
protected:
2019-12-25 11:04:12 +08:00
int _ring_size;
bool _async_emit_none_reader = false;
mutable recursive_mutex _mtx;
Ticker _reader_changed_ticker;
2018-10-24 15:43:52 +08:00
AMFValue _metadata;
2019-12-25 11:04:12 +08:00
RingBuffer<RtmpPacket::Ptr>::Ptr _ring;
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_ */