使用临时变量和std::move减少智能指针的拷贝构造

This commit is contained in:
xiongziliang 2020-10-10 18:23:25 +08:00
parent 83a24a5863
commit dfaa8b540c
12 changed files with 31 additions and 30 deletions

@ -1 +1 @@
Subproject commit 735f194e18d5fb576c0da03024226a6f38537e5a Subproject commit ac02dc10fb0cc065d72a0e950e5c1aafff1da0d6

View File

@ -328,13 +328,13 @@ public:
virtual ~PacketCache() = default; virtual ~PacketCache() = default;
void inputPacket(bool is_video, const std::shared_ptr<packet> &pkt, bool key_pos) { void inputPacket(bool is_video, std::shared_ptr<packet> pkt, bool key_pos) {
if (_policy.isFlushAble(is_video, key_pos, _policy.getStamp(pkt), _cache->size())) { if (_policy.isFlushAble(is_video, key_pos, _policy.getStamp(pkt), _cache->size())) {
flushAll(); flushAll();
} }
//追加数据到最后 //追加数据到最后
_cache->emplace_back(pkt); _cache->emplace_back(std::move(pkt));
if (key_pos) { if (key_pos) {
_key_pos = key_pos; _key_pos = key_pos;
} }
@ -344,14 +344,14 @@ public:
_cache->clear(); _cache->clear();
} }
virtual void onFlush(std::shared_ptr<packet_list> &, bool key_pos) = 0; virtual void onFlush(std::shared_ptr<packet_list>, bool key_pos) = 0;
private: private:
void flushAll() { void flushAll() {
if (_cache->empty()) { if (_cache->empty()) {
return; return;
} }
onFlush(_cache, _key_pos); onFlush(std::move(_cache), _key_pos);
_cache = std::make_shared<packet_list>(); _cache = std::make_shared<packet_list>();
_key_pos = false; _key_pos = false;
} }

View File

@ -92,7 +92,7 @@ public:
* @param packet FMP4包 * @param packet FMP4包
* @param key * @param key
*/ */
void onWrite(const FMP4Packet::Ptr &packet, bool key) override { void onWrite(FMP4Packet::Ptr packet, bool key) override {
if (!_ring) { if (!_ring) {
createRing(); createRing();
} }
@ -100,7 +100,7 @@ public:
_have_video = true; _have_video = true;
} }
_speed += packet->size(); _speed += packet->size();
PacketCache<FMP4Packet, FMP4FlushPolicy>::inputPacket(true, packet, key); PacketCache<FMP4Packet, FMP4FlushPolicy>::inputPacket(true, std::move(packet), key);
} }
/** /**
@ -132,9 +132,9 @@ private:
* @param packet_list * @param packet_list
* @param key_pos * @param key_pos
*/ */
void onFlush(std::shared_ptr<List<FMP4Packet::Ptr> > &packet_list, bool key_pos) override { void onFlush(std::shared_ptr<List<FMP4Packet::Ptr> > packet_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以确保一直清空GOP缓存 //如果不存在视频那么就没有存在GOP缓存的意义所以确保一直清空GOP缓存
_ring->write(packet_list, _have_video ? key_pos : true); _ring->write(std::move(packet_list), _have_video ? key_pos : true);
} }
private: private:

View File

@ -74,7 +74,7 @@ protected:
} }
FMP4Packet::Ptr packet = std::make_shared<FMP4Packet>(std::move(string)); FMP4Packet::Ptr packet = std::make_shared<FMP4Packet>(std::move(string));
packet->time_stamp = stamp; packet->time_stamp = stamp;
_media_src->onWrite(packet, key_frame); _media_src->onWrite(std::move(packet), key_frame);
} }
private: private:

View File

@ -118,7 +118,7 @@ public:
* rtmp包 * rtmp包
* @param pkt rtmp包 * @param pkt rtmp包
*/ */
void onWrite(const RtmpPacket::Ptr &pkt, bool = true) override { void onWrite(RtmpPacket::Ptr pkt, bool = true) override {
_speed += pkt->size(); _speed += pkt->size();
//保存当前时间戳 //保存当前时间戳
switch (pkt->type_id) { switch (pkt->type_id) {
@ -152,7 +152,8 @@ public:
regist(); regist();
} }
} }
PacketCache<RtmpPacket>::inputPacket(pkt->type_id == MSG_VIDEO, pkt, pkt->isVideoKeyFrame()); bool key = pkt->isVideoKeyFrame();
PacketCache<RtmpPacket>::inputPacket(pkt->type_id == MSG_VIDEO, std::move(pkt), key);
} }
/** /**
@ -186,9 +187,9 @@ private:
* @param rtmp_list rtmp包列表 * @param rtmp_list rtmp包列表
* @param key_pos * @param key_pos
*/ */
void onFlush(std::shared_ptr<List<RtmpPacket::Ptr> > &rtmp_list, bool key_pos) override { void onFlush(std::shared_ptr<List<RtmpPacket::Ptr> > rtmp_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存 //如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存
_ring->write(rtmp_list, _have_video ? key_pos : true); _ring->write(std::move(rtmp_list), _have_video ? key_pos : true);
} }
private: private:

View File

@ -60,12 +60,12 @@ public:
/** /**
* rtmp并解析 * rtmp并解析
*/ */
void onWrite(const RtmpPacket::Ptr &pkt, bool = true) override { void onWrite(RtmpPacket::Ptr pkt, bool = true) override {
if (!_all_track_ready || _muxer->isEnabled()) { if (!_all_track_ready || _muxer->isEnabled()) {
//未获取到所有Track后或者开启转协议那么需要解复用rtmp //未获取到所有Track后或者开启转协议那么需要解复用rtmp
_demuxer->inputRtmp(pkt); _demuxer->inputRtmp(pkt);
} }
RtmpMediaSource::onWrite(pkt); RtmpMediaSource::onWrite(std::move(pkt));
} }
/** /**

View File

@ -113,7 +113,7 @@ void PSRtpSender::onRtp(const RtpPacket::Ptr &rtp, bool) {
} }
//此函数在其他线程执行 //此函数在其他线程执行
void PSRtpSender::onFlush(shared_ptr<List<RtpPacket::Ptr>> &rtp_list, bool) { void PSRtpSender::onFlush(shared_ptr<List<RtpPacket::Ptr> > rtp_list, bool) {
if(!_is_connect){ if(!_is_connect){
//连接成功后才能发送数据 //连接成功后才能发送数据
return; return;

View File

@ -24,7 +24,7 @@ public:
RingDelegateHelper(onRtp on_rtp){ RingDelegateHelper(onRtp on_rtp){
_on_rtp = std::move(on_rtp); _on_rtp = std::move(on_rtp);
} }
void onWrite(const RtpPacket::Ptr &in, bool is_key) override{ void onWrite(RtpPacket::Ptr in, bool is_key) override{
_on_rtp(in, is_key); _on_rtp(in, is_key);
} }
@ -68,7 +68,7 @@ protected:
* @param rtp_list rtp包列表 * @param rtp_list rtp包列表
* @param key_pos * @param key_pos
*/ */
void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > &rtp_list, bool key_pos) override; void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > rtp_list, bool key_pos) override;
private: private:
//rtp打包后回调 //rtp打包后回调

View File

@ -156,7 +156,7 @@ public:
* @param rtp rtp包 * @param rtp rtp包
* @param keyPos * @param keyPos
*/ */
void onWrite(const RtpPacket::Ptr &rtp, bool keyPos) override { void onWrite(RtpPacket::Ptr rtp, bool keyPos) override {
_speed += rtp->size(); _speed += rtp->size();
assert(rtp->type >= 0 && rtp->type < TrackMax); assert(rtp->type >= 0 && rtp->type < TrackMax);
auto track = _tracks[rtp->type]; auto track = _tracks[rtp->type];
@ -182,7 +182,7 @@ public:
regist(); regist();
} }
} }
PacketCache<RtpPacket>::inputPacket(rtp->type == TrackVideo, rtp, keyPos); PacketCache<RtpPacket>::inputPacket(rtp->type == TrackVideo, std::move(rtp), keyPos);
} }
void clearCache() override{ void clearCache() override{
@ -196,9 +196,9 @@ private:
* @param rtp_list rtp包列表 * @param rtp_list rtp包列表
* @param key_pos * @param key_pos
*/ */
void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > &rtp_list, bool key_pos) override { void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > rtp_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存 //如果不存在视频那么就没有存在GOP缓存的意义所以is_key一直为true确保一直清空GOP缓存
_ring->write(rtp_list, _have_video ? key_pos : true); _ring->write(std::move(rtp_list), _have_video ? key_pos : true);
} }
private: private:

View File

@ -47,7 +47,7 @@ public:
/** /**
* rtp并解析 * rtp并解析
*/ */
void onWrite(const RtpPacket::Ptr &rtp, bool key_pos) override { void onWrite(RtpPacket::Ptr rtp, bool key_pos) override {
if (_all_track_ready && !_muxer->isEnabled()) { if (_all_track_ready && !_muxer->isEnabled()) {
//获取到所有Track后并且未开启转协议那么不需要解复用rtp //获取到所有Track后并且未开启转协议那么不需要解复用rtp
//在关闭rtp解复用后无法知道是否为关键帧这样会导致无法秒开或者开播花屏 //在关闭rtp解复用后无法知道是否为关键帧这样会导致无法秒开或者开播花屏
@ -56,7 +56,7 @@ public:
//需要解复用rtp //需要解复用rtp
key_pos = _demuxer->inputRtp(rtp); key_pos = _demuxer->inputRtp(rtp);
} }
RtspMediaSource::onWrite(rtp, key_pos); RtspMediaSource::onWrite(std::move(rtp), key_pos);
} }
/** /**

View File

@ -75,7 +75,7 @@ public:
* @param packet TS包 * @param packet TS包
* @param key * @param key
*/ */
void onWrite(const TSPacket::Ptr &packet, bool key) override { void onWrite(TSPacket::Ptr packet, bool key) override {
_speed += packet->size(); _speed += packet->size();
if (!_ring) { if (!_ring) {
createRing(); createRing();
@ -83,7 +83,7 @@ public:
if (key) { if (key) {
_have_video = true; _have_video = true;
} }
PacketCache<TSPacket, TSFlushPolicy>::inputPacket(true, packet, key); PacketCache<TSPacket, TSFlushPolicy>::inputPacket(true, std::move(packet), key);
} }
/** /**
@ -114,9 +114,9 @@ private:
* @param packet_list * @param packet_list
* @param key_pos * @param key_pos
*/ */
void onFlush(std::shared_ptr<List<TSPacket::Ptr> > &packet_list, bool key_pos) override { void onFlush(std::shared_ptr<List<TSPacket::Ptr> > packet_list, bool key_pos) override {
//如果不存在视频那么就没有存在GOP缓存的意义所以确保一直清空GOP缓存 //如果不存在视频那么就没有存在GOP缓存的意义所以确保一直清空GOP缓存
_ring->write(packet_list, _have_video ? key_pos : true); _ring->write(std::move(packet_list), _have_video ? key_pos : true);
} }
private: private:

View File

@ -70,7 +70,7 @@ protected:
TSPacket::Ptr packet = _pool.obtain(); TSPacket::Ptr packet = _pool.obtain();
packet->assign((char *) data, len); packet->assign((char *) data, len);
packet->time_stamp = timestamp; packet->time_stamp = timestamp;
_media_src->onWrite(packet, is_idr_fast_packet); _media_src->onWrite(std::move(packet), is_idr_fast_packet);
} }
private: private: