From 754073918a5b99c11df99fbcce9a7adfd6429e2a Mon Sep 17 00:00:00 2001 From: mtdxc Date: Tue, 29 Nov 2022 11:07:13 +0800 Subject: [PATCH] Header refactor (#2115) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 优化MultiMediaSourceMuxer头文件包含 * 将MediaSinkDelegate和Demux移到MediaSink中 * MediaSource头文件重构, 独立出PacketCache.h 精简Frame和Track的头文件 * Rtmp头文件重构 * Rtsp头文件重构 * webrtc头文件重构 * 规范.h头文件包含,并将其移到.cpp中: - 尽量不包含Common\config.h - Util\File.h - Rtsp/RtspPlayer.h - Rtmp/RtmpPlayer.h * 删除多余的Stamp.h和Base64包含 --- api/source/mk_common.cpp | 2 + player/test_player.cpp | 1 + server/FFmpegSource.h | 1 - server/WebApi.cpp | 1 + src/Codec/Transcode.cpp | 2 +- src/Common/Device.h | 1 - src/Common/MediaSink.cpp | 83 ++++++++++++++ src/Common/MediaSink.h | 38 ++++++ src/Common/MediaSource.cpp | 5 +- src/Common/MediaSource.h | 84 -------------- src/Common/MultiMediaSourceMuxer.cpp | 7 ++ src/Common/MultiMediaSourceMuxer.h | 29 ++--- src/Common/PacketCache.h | 87 ++++++++++++++ src/Extension/Factory.cpp | 2 + src/Extension/Frame.cpp | 9 ++ src/Extension/Frame.h | 14 +-- src/Extension/H264.cpp | 2 + src/Extension/H264.h | 1 - src/Extension/H264Rtmp.h | 1 - src/Extension/H264Rtp.cpp | 1 + src/Extension/H264Rtp.h | 2 +- src/Extension/H265.cpp | 1 + src/Extension/H265.h | 1 - src/Extension/H265Rtmp.h | 1 - src/Extension/H265Rtp.h | 2 +- src/Extension/Track.h | 1 - src/FMP4/FMP4MediaSource.h | 2 + src/Http/HlsPlayer.cpp | 2 +- src/Http/HlsPlayer.h | 1 - src/Http/HttpFileManager.cpp | 1 + src/Http/HttpSession.h | 1 - src/Http/TsPlayer.cpp | 2 +- src/Http/TsplayerImp.cpp | 1 + src/Player/MediaPlayer.cpp | 2 - src/Player/MediaPlayer.h | 3 - src/Player/PlayerBase.cpp | 83 -------------- src/Player/PlayerBase.h | 39 ------- src/Player/PlayerProxy.cpp | 4 + src/Player/PlayerProxy.h | 5 +- src/Pusher/MediaPusher.h | 1 - src/Record/HlsMaker.cpp | 1 + src/Record/HlsMaker.h | 6 +- src/Record/HlsMakerImp.cpp | 2 + src/Record/HlsMediaSource.cpp | 37 ++++++ src/Record/HlsMediaSource.h | 35 +----- src/Record/HlsRecorder.h | 1 + src/Record/MP4Muxer.cpp | 5 +- src/Record/MP4Muxer.h | 4 - src/Record/MP4Reader.cpp | 1 + src/Record/MP4Recorder.cpp | 1 + src/Record/MP4Recorder.h | 11 +- src/Record/MPEG.h | 4 +- src/Record/Recorder.cpp | 2 + src/Rtcp/RtcpFCI.h | 1 - src/Rtmp/FlvMuxer.h | 3 +- src/Rtmp/Rtmp.cpp | 112 ++++++++++++++++++ src/Rtmp/Rtmp.h | 108 ++--------------- src/Rtmp/RtmpCodec.h | 2 +- src/Rtmp/RtmpDemuxer.cpp | 2 +- src/Rtmp/RtmpDemuxer.h | 10 +- src/Rtmp/RtmpMediaSource.h | 67 +---------- src/Rtmp/RtmpMediaSourceImp.cpp | 166 +++++++++++++++++++++++++++ src/Rtmp/RtmpMediaSourceImp.h | 94 ++------------- src/Rtmp/RtmpPlayer.cpp | 59 +++++++++- src/Rtmp/RtmpPlayer.h | 2 - src/Rtmp/RtmpPlayerImp.h | 48 ++------ src/Rtmp/RtmpProtocol.cpp | 1 + src/Rtmp/RtmpProtocol.h | 5 +- src/Rtmp/RtmpPusher.cpp | 10 +- src/Rtmp/RtmpSession.h | 3 - src/Rtmp/amf.cpp | 2 +- src/Rtmp/amf.h | 7 +- src/Rtp/GB28181Process.cpp | 3 + src/Rtp/GB28181Process.h | 6 +- src/Rtp/PSEncoder.cpp | 2 + src/Rtp/PSEncoder.h | 4 +- src/Rtp/RawEncoder.h | 8 +- src/Rtp/RtpCache.h | 2 +- src/Rtp/RtpProcess.cpp | 2 + src/Rtp/RtpSender.cpp | 1 + src/Rtp/RtpSender.h | 2 + src/Rtp/RtpServer.cpp | 1 + src/Rtp/RtpSession.cpp | 1 + src/Rtsp/RtpCodec.h | 5 +- src/Rtsp/RtpMultiCaster.cpp | 1 + src/Rtsp/RtpMultiCaster.h | 2 - src/Rtsp/RtpReceiver.h | 7 +- src/Rtsp/Rtsp.cpp | 37 ++++++ src/Rtsp/Rtsp.h | 32 +----- src/Rtsp/RtspDemuxer.cpp | 1 + src/Rtsp/RtspDemuxer.h | 10 +- src/Rtsp/RtspMediaSource.h | 79 +------------ src/Rtsp/RtspMediaSourceImp.cpp | 131 +++++++++++++++++++++ src/Rtsp/RtspMediaSourceImp.h | 54 ++------- src/Rtsp/RtspPlayer.cpp | 50 ++++++++ src/Rtsp/RtspPlayer.h | 12 +- src/Rtsp/RtspPlayerImp.h | 51 ++------ src/Rtsp/RtspPusher.cpp | 2 + src/Rtsp/RtspPusher.h | 7 +- src/Rtsp/RtspSession.cpp | 2 + src/Rtsp/RtspSession.h | 38 +----- src/Rtsp/UDPServer.h | 3 - src/Shell/ShellSession.h | 1 - src/TS/TSMediaSource.h | 2 + srt/SrtTransport.hpp | 2 +- srt/SrtTransportImp.cpp | 3 +- tests/test_bench_pull.cpp | 1 + tests/test_bench_push.cpp | 2 + tests/test_httpApi.cpp | 1 - tests/test_httpClient.cpp | 1 - tests/test_pusherMp4.cpp | 1 + tests/test_server.cpp | 1 - webrtc/Nack.h | 4 + webrtc/SctpAssociation.cpp | 1 + webrtc/Sdp.cpp | 1 + webrtc/Sdp.h | 2 + webrtc/StunPacket.cpp | 1 + webrtc/TwccContext.h | 2 +- webrtc/Utils.hpp | 16 --- webrtc/WebRtcPlayer.cpp | 1 + webrtc/WebRtcPlayer.h | 1 + webrtc/WebRtcPusher.cpp | 1 + webrtc/WebRtcPusher.h | 1 + webrtc/WebRtcSession.cpp | 3 + webrtc/WebRtcSession.h | 4 +- webrtc/WebRtcTransport.cpp | 3 +- webrtc/WebRtcTransport.h | 10 +- 127 files changed, 1043 insertions(+), 917 deletions(-) create mode 100644 src/Common/PacketCache.h create mode 100644 src/Rtmp/RtmpMediaSourceImp.cpp create mode 100644 src/Rtsp/RtspMediaSourceImp.cpp diff --git a/api/source/mk_common.cpp b/api/source/mk_common.cpp index 59bbfde7..1c62eb02 100644 --- a/api/source/mk_common.cpp +++ b/api/source/mk_common.cpp @@ -13,6 +13,7 @@ #include #include "Util/logger.h" #include "Util/SSLBox.h" +#include "Util/File.h" #include "Network/TcpServer.h" #include "Network/UdpServer.h" #include "Thread/WorkThreadPool.h" @@ -37,6 +38,7 @@ static std::shared_ptr rtpServer; #ifdef ENABLE_WEBRTC #include "../webrtc/WebRtcSession.h" +#include "../webrtc/WebRtcTransport.h" static std::shared_ptr rtcServer_udp; static std::shared_ptr rtcServer_tcp; #endif diff --git a/player/test_player.cpp b/player/test_player.cpp index 5bbedf23..126280a0 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -11,6 +11,7 @@ #include #include "Util/logger.h" #include +#include "Common/config.h" #include "Rtsp/UDPServer.h" #include "Player/MediaPlayer.h" #include "Util/onceToken.h" diff --git a/server/FFmpegSource.h b/server/FFmpegSource.h index 3f28f228..e85df3ec 100644 --- a/server/FFmpegSource.h +++ b/server/FFmpegSource.h @@ -16,7 +16,6 @@ #include #include "Process.h" #include "Util/TimeTicker.h" -#include "Network/Socket.h" #include "Common/MediaSource.h" namespace FFmpeg { diff --git a/server/WebApi.cpp b/server/WebApi.cpp index cb3b69b0..319a222d 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -17,6 +17,7 @@ #include "Util/logger.h" #include "Util/onceToken.h" #include "Util/NoticeCenter.h" +#include "Util/File.h" #ifdef ENABLE_MYSQL #include "Util/SqlPool.h" #endif //ENABLE_MYSQL diff --git a/src/Codec/Transcode.cpp b/src/Codec/Transcode.cpp index b89fc951..da57389a 100644 --- a/src/Codec/Transcode.cpp +++ b/src/Codec/Transcode.cpp @@ -16,7 +16,7 @@ #include "Util/uv_errno.h" #include "Transcode.h" #include "Extension/AAC.h" - +#include "Common/config.h" #define MAX_DELAY_SECOND 3 using namespace std; diff --git a/src/Common/Device.h b/src/Common/Device.h index b39a24dd..8c78da46 100644 --- a/src/Common/Device.h +++ b/src/Common/Device.h @@ -14,7 +14,6 @@ #include #include #include -#include "Util/util.h" #include "Util/TimeTicker.h" #include "Common/MultiMediaSourceMuxer.h" diff --git a/src/Common/MediaSink.cpp b/src/Common/MediaSink.cpp index 2cd3749a..5f3bf2be 100644 --- a/src/Common/MediaSink.cpp +++ b/src/Common/MediaSink.cpp @@ -10,6 +10,7 @@ #include "MediaSink.h" #include "Extension/AAC.h" +#include "Common/config.h" using namespace std; @@ -271,4 +272,86 @@ void MediaSink::enableMuteAudio(bool flag) { _add_mute_audio = flag; } +///////////////////////////DemuxerSink////////////////////////////// + +void MediaSinkDelegate::setTrackListener(TrackListener *listener) { + _listener = listener; +} + +bool MediaSinkDelegate::onTrackReady(const Track::Ptr &track) { + if (_listener) { + _listener->addTrack(track); + } + return true; +} + +void MediaSinkDelegate::onAllTrackReady() { + if (_listener) { + _listener->addTrackCompleted(); + } +} + +void MediaSinkDelegate::resetTracks() { + MediaSink::resetTracks(); + if (_listener) { + _listener->resetTracks(); + } +} + +///////////////////////////Demuxer////////////////////////////// + +void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) { + if (wait_track_ready) { + auto sink = std::make_shared(); + sink->setTrackListener(listener); + _sink = std::move(sink); + } + _listener = listener; +} + +bool Demuxer::addTrack(const Track::Ptr &track) { + if (!_sink) { + _origin_track.emplace_back(track); + return _listener ? _listener->addTrack(track) : false; + } + + if (_sink->addTrack(track)) { + track->addDelegate([this](const Frame::Ptr &frame) { + return _sink->inputFrame(frame); + }); + return true; + } + return false; +} + +void Demuxer::addTrackCompleted() { + if (_sink) { + _sink->addTrackCompleted(); + } else if (_listener) { + _listener->addTrackCompleted(); + } +} + +void Demuxer::resetTracks() { + if (_sink) { + _sink->resetTracks(); + } else if (_listener) { + _listener->resetTracks(); + } +} + +vector Demuxer::getTracks(bool ready) const { + if (_sink) { + return _sink->getTracks(ready); + } + + vector ret; + for (auto &track : _origin_track) { + if (ready && !track->ready()) { + continue; + } + ret.emplace_back(track); + } + return ret; +} }//namespace mediakit diff --git a/src/Common/MediaSink.h b/src/Common/MediaSink.h index 0e41821b..ffe0a5c8 100644 --- a/src/Common/MediaSink.h +++ b/src/Common/MediaSink.h @@ -168,6 +168,44 @@ private: }; +class MediaSinkDelegate : public MediaSink { +public: + MediaSinkDelegate() = default; + ~MediaSinkDelegate() override = default; + + /** + * 设置track监听器 + */ + void setTrackListener(TrackListener *listener); + +protected: + void resetTracks() override; + bool onTrackReady(const Track::Ptr & track) override; + void onAllTrackReady() override; + +private: + TrackListener *_listener = nullptr; +}; + +class Demuxer : protected TrackListener, public TrackSource { +public: + Demuxer() = default; + ~Demuxer() override = default; + + void setTrackListener(TrackListener *listener, bool wait_track_ready = false); + std::vector getTracks(bool trackReady = true) const override; + +protected: + bool addTrack(const Track::Ptr &track) override; + void addTrackCompleted() override; + void resetTracks() override; + +private: + MediaSink::Ptr _sink; + TrackListener *_listener = nullptr; + std::vector _origin_track; +}; + }//namespace mediakit #endif //ZLMEDIAKIT_MEDIASINK_H diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index fa37c508..5e378124 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -7,15 +7,16 @@ * 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. */ - +#include #include "Util/util.h" #include "Util/NoticeCenter.h" #include "Network/sockutil.h" #include "Network/Session.h" #include "MediaSource.h" #include "Common/config.h" +#include "Common/Parser.h" #include "Record/MP4Reader.h" - +#include "PacketCache.h" using namespace std; using namespace toolkit; diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index e80f1e03..84961d2c 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -11,15 +11,10 @@ #ifndef ZLMEDIAKIT_MEDIASOURCE_H #define ZLMEDIAKIT_MEDIASOURCE_H -#include #include #include #include #include -#include -#include "Common/config.h" -#include "Common/Parser.h" -#include "Util/List.h" #include "Network/Socket.h" #include "Extension/Track.h" #include "Record/Recorder.h" @@ -410,84 +405,5 @@ private: toolkit::ObjectStatistic _statistic; }; -/// 缓存刷新策略类 -class FlushPolicy { -public: - FlushPolicy() = default; - ~FlushPolicy() = default; - - bool isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, size_t cache_size); - -private: - // 音视频的最后时间戳 - uint64_t _last_stamp[2] = { 0, 0 }; -}; - -/// 合并写缓存模板 -/// \tparam packet 包类型 -/// \tparam policy 刷新缓存策略 -/// \tparam packet_list 包缓存类型 -template > > -class PacketCache { -public: - PacketCache() { _cache = std::make_shared(); } - - virtual ~PacketCache() = default; - - void inputPacket(uint64_t stamp, bool is_video, std::shared_ptr pkt, bool key_pos) { - bool flag = flushImmediatelyWhenCloseMerge(); - if (!flag && _policy.isFlushAble(is_video, key_pos, stamp, _cache->size())) { - flush(); - } - - //追加数据到最后 - _cache->emplace_back(std::move(pkt)); - if (key_pos) { - _key_pos = key_pos; - } - - if (flag) { - flush(); - } - } - - void flush() { - if (_cache->empty()) { - return; - } - onFlush(std::move(_cache), _key_pos); - _cache = std::make_shared(); - _key_pos = false; - } - - virtual void clearCache() { - _cache->clear(); - } - - virtual void onFlush(std::shared_ptr, bool key_pos) = 0; - -private: - bool flushImmediatelyWhenCloseMerge() { - // 一般的协议关闭合并写时,立即刷新缓存,这样可以减少一帧的延时,但是rtp例外 - // 因为rtp的包很小,一个RtpPacket包中也不是完整的一帧图像,所以在关闭合并写时, - // 还是有必要缓冲一帧的rtp(也就是时间戳相同的rtp)再输出,这样虽然会增加一帧的延时 - // 但是却对性能提升很大,这样做还是比较划算的 - - GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); - - GET_CONFIG(int, rtspLowLatency, Rtsp::kLowLatency); - if (std::is_same::value && rtspLowLatency) { - return true; - } - - return std::is_same::value ? false : (mergeWriteMS <= 0); - } - -private: - bool _key_pos = false; - policy _policy; - std::shared_ptr _cache; -}; - } /* namespace mediakit */ #endif //ZLMEDIAKIT_MEDIASOURCE_H diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index 7d01f382..7dd3027b 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -11,6 +11,13 @@ #include #include "Common/config.h" #include "MultiMediaSourceMuxer.h" +#include "Rtp/RtpSender.h" +#include "Record/HlsRecorder.h" +#include "Record/HlsMediaSource.h" +#include "Rtsp/RtspMediaSourceMuxer.h" +#include "Rtmp/RtmpMediaSourceMuxer.h" +#include "TS/TSMediaSourceMuxer.h" +#include "FMP4/FMP4MediaSourceMuxer.h" using namespace std; using namespace toolkit; diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index 5414ee9b..76ac0e8c 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -12,16 +12,17 @@ #define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H #include "Common/Stamp.h" -#include "Rtp/RtpSender.h" +#include "Common/MediaSource.h" +#include "Common/MediaSink.h" #include "Record/Recorder.h" -#include "Record/HlsRecorder.h" -#include "Record/HlsMediaSource.h" -#include "Rtsp/RtspMediaSourceMuxer.h" -#include "Rtmp/RtmpMediaSourceMuxer.h" -#include "TS/TSMediaSourceMuxer.h" -#include "FMP4/FMP4MediaSourceMuxer.h" - namespace mediakit { +class HlsRecorder; +class RtspMediaSourceMuxer; +class RtmpMediaSourceMuxer; +class TSMediaSourceMuxer; +class FMP4MediaSourceMuxer; +class RtpSender; + class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSink, public std::enable_shared_from_this{ public: @@ -160,17 +161,17 @@ private: Stamp _stamp[2]; std::weak_ptr _track_listener; #if defined(ENABLE_RTPPROXY) - std::unordered_map _rtp_sender; + std::unordered_map> _rtp_sender; #endif //ENABLE_RTPPROXY #if defined(ENABLE_MP4) - FMP4MediaSourceMuxer::Ptr _fmp4; + std::shared_ptr _fmp4; #endif - RtmpMediaSourceMuxer::Ptr _rtmp; - RtspMediaSourceMuxer::Ptr _rtsp; - TSMediaSourceMuxer::Ptr _ts; + std::shared_ptr _rtmp; + std::shared_ptr _rtsp; + std::shared_ptr _ts; MediaSinkInterface::Ptr _mp4; - HlsRecorder::Ptr _hls; + std::shared_ptr _hls; toolkit::EventPoller::Ptr _poller; //对象个数统计 diff --git a/src/Common/PacketCache.h b/src/Common/PacketCache.h new file mode 100644 index 00000000..c13273ba --- /dev/null +++ b/src/Common/PacketCache.h @@ -0,0 +1,87 @@ +#ifndef _SRC_PACKET_CACHE_H_ +#define _SRC_PACKET_CACHE_H_ +#include "Common/config.h" +#include "Util/List.h" + +#pragma once +namespace mediakit { +/// 缓存刷新策略类 +class FlushPolicy { +public: + FlushPolicy() = default; + ~FlushPolicy() = default; + + bool isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, size_t cache_size); + +private: + // 音视频的最后时间戳 + uint64_t _last_stamp[2] = { 0, 0 }; +}; + +/// 合并写缓存模板 +/// \tparam packet 包类型 +/// \tparam policy 刷新缓存策略 +/// \tparam packet_list 包缓存类型 +template > > +class PacketCache { +public: + PacketCache() { _cache = std::make_shared(); } + + virtual ~PacketCache() = default; + + void inputPacket(uint64_t stamp, bool is_video, std::shared_ptr pkt, bool key_pos) { + bool flag = flushImmediatelyWhenCloseMerge(); + if (!flag && _policy.isFlushAble(is_video, key_pos, stamp, _cache->size())) { + flush(); + } + + //追加数据到最后 + _cache->emplace_back(std::move(pkt)); + if (key_pos) { + _key_pos = key_pos; + } + + if (flag) { + flush(); + } + } + + void flush() { + if (_cache->empty()) { + return; + } + onFlush(std::move(_cache), _key_pos); + _cache = std::make_shared(); + _key_pos = false; + } + + virtual void clearCache() { + _cache->clear(); + } + + virtual void onFlush(std::shared_ptr, bool key_pos) = 0; + +private: + bool flushImmediatelyWhenCloseMerge() { + // 一般的协议关闭合并写时,立即刷新缓存,这样可以减少一帧的延时,但是rtp例外 + // 因为rtp的包很小,一个RtpPacket包中也不是完整的一帧图像,所以在关闭合并写时, + // 还是有必要缓冲一帧的rtp(也就是时间戳相同的rtp)再输出,这样虽然会增加一帧的延时 + // 但是却对性能提升很大,这样做还是比较划算的 + + GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); + + GET_CONFIG(int, rtspLowLatency, Rtsp::kLowLatency); + if (std::is_same::value && rtspLowLatency) { + return true; + } + + return std::is_same::value ? false : (mergeWriteMS <= 0); + } + +private: + bool _key_pos = false; + policy _policy; + std::shared_ptr _cache; +}; +} +#endif diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index a2cdcd80..aab578f1 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -22,7 +22,9 @@ #include "Opus.h" #include "G711.h" #include "L16.h" +#include "Util/base64.h" #include "Common/Parser.h" +#include "Common/config.h" using namespace std; diff --git a/src/Extension/Frame.cpp b/src/Extension/Frame.cpp index 8ff0bdf9..7cc3cb41 100644 --- a/src/Extension/Frame.cpp +++ b/src/Extension/Frame.cpp @@ -12,6 +12,8 @@ #include "H264.h" #include "H265.h" #include "Common/Parser.h" +#include "Common/Stamp.h" + using namespace std; using namespace toolkit; @@ -29,6 +31,13 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){ return std::make_shared(frame); } +FrameStamp::FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp) +{ + _frame = std::move(frame); + //覆盖时间戳 + stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp); +} + TrackType getTrackType(CodecId codecId) { switch (codecId) { #define XX(name, type, value, str, mpeg_id) case name : return type; diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index c298ed11..842eaad9 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -11,14 +11,14 @@ #ifndef ZLMEDIAKIT_FRAME_H #define ZLMEDIAKIT_FRAME_H +#include #include #include -#include "Util/RingBuffer.h" -#include "Network/Socket.h" -#include "Common/Stamp.h" +#include "Util/List.h" +#include "Network/Buffer.h" namespace mediakit { - +class Stamp; typedef enum { TrackInvalid = -1, TrackVideo = 0, @@ -441,11 +441,7 @@ private: class FrameStamp : public Frame { public: using Ptr = std::shared_ptr; - FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp) { - _frame = std::move(frame); - //覆盖时间戳 - stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp); - } + FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp); ~FrameStamp() override {} uint64_t dts() const override { return (uint64_t)_dts; } diff --git a/src/Extension/H264.cpp b/src/Extension/H264.cpp index 00217df8..c9852547 100644 --- a/src/Extension/H264.cpp +++ b/src/Extension/H264.cpp @@ -11,6 +11,8 @@ #include "H264.h" #include "SPSParser.h" #include "Util/logger.h" +#include "Util/base64.h" + using namespace toolkit; using namespace std; diff --git a/src/Extension/H264.h b/src/Extension/H264.h index 4f7a3e90..edf08f5f 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -13,7 +13,6 @@ #include "Frame.h" #include "Track.h" -#include "Util/base64.h" #define H264_TYPE(v) ((uint8_t)(v) & 0x1F) diff --git a/src/Extension/H264Rtmp.h b/src/Extension/H264Rtmp.h index b27309dc..c222047c 100644 --- a/src/Extension/H264Rtmp.h +++ b/src/Extension/H264Rtmp.h @@ -13,7 +13,6 @@ #include "Rtmp/RtmpCodec.h" #include "Extension/Track.h" -#include "Util/ResourcePool.h" #include "Extension/H264.h" namespace mediakit{ diff --git a/src/Extension/H264Rtp.cpp b/src/Extension/H264Rtp.cpp index 1130102f..f12218a4 100644 --- a/src/Extension/H264Rtp.cpp +++ b/src/Extension/H264Rtp.cpp @@ -9,6 +9,7 @@ */ #include "H264Rtp.h" +#include "Common/config.h" namespace mediakit{ diff --git a/src/Extension/H264Rtp.h b/src/Extension/H264Rtp.h index 4736f512..53e993d6 100644 --- a/src/Extension/H264Rtp.h +++ b/src/Extension/H264Rtp.h @@ -12,8 +12,8 @@ #define ZLMEDIAKIT_H264RTPCODEC_H #include "Rtsp/RtpCodec.h" -#include "Util/ResourcePool.h" #include "Extension/H264.h" +// for DtsGenerator #include "Common/Stamp.h" namespace mediakit{ diff --git a/src/Extension/H265.cpp b/src/Extension/H265.cpp index 8877371d..9985a9cb 100644 --- a/src/Extension/H265.cpp +++ b/src/Extension/H265.cpp @@ -10,6 +10,7 @@ #include "H265.h" #include "SPSParser.h" +#include "Util/base64.h" using namespace std; using namespace toolkit; diff --git a/src/Extension/H265.h b/src/Extension/H265.h index 59913c81..521663f4 100644 --- a/src/Extension/H265.h +++ b/src/Extension/H265.h @@ -13,7 +13,6 @@ #include "Frame.h" #include "Track.h" -#include "Util/base64.h" #include "H264.h" #define H265_TYPE(v) (((uint8_t)(v) >> 1) & 0x3f) diff --git a/src/Extension/H265Rtmp.h b/src/Extension/H265Rtmp.h index 3c062cfc..68973da3 100644 --- a/src/Extension/H265Rtmp.h +++ b/src/Extension/H265Rtmp.h @@ -13,7 +13,6 @@ #include "Rtmp/RtmpCodec.h" #include "Extension/Track.h" -#include "Util/ResourcePool.h" #include "Extension/H265.h" namespace mediakit{ diff --git a/src/Extension/H265Rtp.h b/src/Extension/H265Rtp.h index b1982bfd..1cecfee0 100644 --- a/src/Extension/H265Rtp.h +++ b/src/Extension/H265Rtp.h @@ -12,8 +12,8 @@ #define ZLMEDIAKIT_H265RTPCODEC_H #include "Rtsp/RtpCodec.h" -#include "Util/ResourcePool.h" #include "Extension/H265.h" +// for DtsGenerator #include "Common/Stamp.h" namespace mediakit{ diff --git a/src/Extension/Track.h b/src/Extension/Track.h index 0bada492..855b0f91 100644 --- a/src/Extension/Track.h +++ b/src/Extension/Track.h @@ -14,7 +14,6 @@ #include #include #include "Frame.h" -#include "Util/RingBuffer.h" #include "Rtsp/Rtsp.h" namespace mediakit{ diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index 52174027..e004dc5e 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -12,6 +12,8 @@ #define ZLMEDIAKIT_FMP4MEDIASOURCE_H #include "Common/MediaSource.h" +#include "Common/PacketCache.h" +#include "Util/RingBuffer.h" #define FMP4_GOP_SIZE 512 diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index f22ca7c4..ad2cadef 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -9,7 +9,7 @@ */ #include "HlsPlayer.h" - +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Http/HlsPlayer.h b/src/Http/HlsPlayer.h index 7db6316b..55c7d062 100644 --- a/src/Http/HlsPlayer.h +++ b/src/Http/HlsPlayer.h @@ -11,7 +11,6 @@ #ifndef HTTP_HLSPLAYER_H #define HTTP_HLSPLAYER_H -#include "Common/Stamp.h" #include "Player/PlayerBase.h" #include "HttpTSPlayer.h" #include "HlsParser.h" diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 50014825..6a36cfe2 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -19,6 +19,7 @@ #include "HttpSession.h" #include "Record/HlsMediaSource.h" #include "Common/Parser.h" +#include "Common/config.h" #include "strCoding.h" using namespace std; diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index dadb5e52..42eaad44 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -13,7 +13,6 @@ #include #include "Network/Session.h" -#include "Rtmp/RtmpMediaSource.h" #include "Rtmp/FlvMuxer.h" #include "HttpRequestSplitter.h" #include "WebSocketSplitter.h" diff --git a/src/Http/TsPlayer.cpp b/src/Http/TsPlayer.cpp index eefb6e53..70383899 100644 --- a/src/Http/TsPlayer.cpp +++ b/src/Http/TsPlayer.cpp @@ -9,7 +9,7 @@ */ #include "TsPlayer.h" - +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Http/TsplayerImp.cpp b/src/Http/TsplayerImp.cpp index 05db8a61..c2e13eef 100644 --- a/src/Http/TsplayerImp.cpp +++ b/src/Http/TsplayerImp.cpp @@ -10,6 +10,7 @@ #include "TsPlayerImp.h" #include "HlsPlayer.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Player/MediaPlayer.cpp b/src/Player/MediaPlayer.cpp index 1e0fd572..1eb5f449 100644 --- a/src/Player/MediaPlayer.cpp +++ b/src/Player/MediaPlayer.cpp @@ -10,8 +10,6 @@ #include #include "MediaPlayer.h" -#include "Rtmp/RtmpPlayerImp.h" -#include "Rtsp/RtspPlayerImp.h" using namespace std; using namespace toolkit; diff --git a/src/Player/MediaPlayer.h b/src/Player/MediaPlayer.h index 3f983d3d..a425f956 100644 --- a/src/Player/MediaPlayer.h +++ b/src/Player/MediaPlayer.h @@ -14,9 +14,6 @@ #include #include #include "PlayerBase.h" -#include "Rtsp/RtspPlayer.h" -#include "Rtmp/RtmpPlayer.h" -#include "Thread/TaskExecutor.h" namespace mediakit { diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 68dc6159..31d8e051 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -68,87 +68,4 @@ PlayerBase::PlayerBase() { this->mINI::operator[](Client::kWaitTrackReady) = true; } -///////////////////////////DemuxerSink////////////////////////////// - -void MediaSinkDelegate::setTrackListener(TrackListener *listener) { - _listener = listener; -} - -bool MediaSinkDelegate::onTrackReady(const Track::Ptr &track) { - if (_listener) { - _listener->addTrack(track); - } - return true; -} - -void MediaSinkDelegate::onAllTrackReady() { - if (_listener) { - _listener->addTrackCompleted(); - } -} - -void MediaSinkDelegate::resetTracks() { - MediaSink::resetTracks(); - if (_listener) { - _listener->resetTracks(); - } -} - -///////////////////////////Demuxer////////////////////////////// - -void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) { - if (wait_track_ready) { - auto sink = std::make_shared(); - sink->setTrackListener(listener); - _sink = std::move(sink); - } - _listener = listener; -} - -bool Demuxer::addTrack(const Track::Ptr &track) { - if (!_sink) { - _origin_track.emplace_back(track); - return _listener ? _listener->addTrack(track) : false; - } - - if (_sink->addTrack(track)) { - track->addDelegate([this](const Frame::Ptr &frame) { - return _sink->inputFrame(frame); - }); - return true; - } - return false; -} - -void Demuxer::addTrackCompleted() { - if (_sink) { - _sink->addTrackCompleted(); - } else if (_listener) { - _listener->addTrackCompleted(); - } -} - -void Demuxer::resetTracks() { - if (_sink) { - _sink->resetTracks(); - } else if (_listener) { - _listener->resetTracks(); - } -} - -vector Demuxer::getTracks(bool ready) const { - if (_sink) { - return _sink->getTracks(ready); - } - - vector ret; - for (auto &track : _origin_track) { - if (ready && !track->ready()) { - continue; - } - ret.emplace_back(track); - } - return ret; -} - } /* namespace mediakit */ diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index d0eca682..73c9d3ee 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -17,7 +17,6 @@ #include #include "Network/Socket.h" #include "Util/mini.h" -#include "Util/RingBuffer.h" #include "Common/MediaSource.h" #include "Common/MediaSink.h" #include "Extension/Frame.h" @@ -236,44 +235,6 @@ protected: std::shared_ptr _delegate; }; -class MediaSinkDelegate : public MediaSink { -public: - MediaSinkDelegate() = default; - ~MediaSinkDelegate() override = default; - - /** - * 设置track监听器 - */ - void setTrackListener(TrackListener *listener); - -protected: - void resetTracks() override; - bool onTrackReady(const Track::Ptr & track) override; - void onAllTrackReady() override; - -private: - TrackListener *_listener = nullptr; -}; - -class Demuxer : protected TrackListener, public TrackSource { -public: - Demuxer() = default; - ~Demuxer() override = default; - - void setTrackListener(TrackListener *listener, bool wait_track_ready = false); - std::vector getTracks(bool trackReady = true) const override; - -protected: - bool addTrack(const Track::Ptr &track) override; - void addTrackCompleted() override; - void resetTracks() override; - -private: - MediaSink::Ptr _sink; - TrackListener *_listener = nullptr; - std::vector _origin_track; -}; - } /* namespace mediakit */ #endif /* SRC_PLAYER_PLAYERBASE_H_ */ diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 477b83f0..5927d441 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -14,6 +14,10 @@ #include "Util/MD5.h" #include "Util/logger.h" #include "Extension/AAC.h" +#include "Rtmp/RtmpMediaSource.h" +#include "Rtsp/RtspMediaSource.h" +#include "Rtmp/RtmpPlayer.h" +#include "Rtsp/RtspPlayer.h" using namespace toolkit; using namespace std; diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 970c620a..5f142180 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -12,9 +12,8 @@ #define SRC_DEVICE_PLAYERPROXY_H_ #include -#include "Common/Device.h" +#include "Common/MultiMediaSourceMuxer.h" #include "Player/MediaPlayer.h" -#include "Util/TimeTicker.h" namespace mediakit { @@ -72,7 +71,7 @@ private: std::string _app; std::string _stream_id; std::string _pull_url; - toolkit::Timer::Ptr _timer; + std::shared_ptr _timer; std::function _on_close; std::function _on_play; MultiMediaSourceMuxer::Ptr _muxer; diff --git a/src/Pusher/MediaPusher.h b/src/Pusher/MediaPusher.h index 123d6c2c..7ddd7d9a 100644 --- a/src/Pusher/MediaPusher.h +++ b/src/Pusher/MediaPusher.h @@ -14,7 +14,6 @@ #include #include #include "PusherBase.h" -#include "Thread/TaskExecutor.h" namespace mediakit { diff --git a/src/Record/HlsMaker.cpp b/src/Record/HlsMaker.cpp index 7faed8e2..df4cb8de 100644 --- a/src/Record/HlsMaker.cpp +++ b/src/Record/HlsMaker.cpp @@ -9,6 +9,7 @@ */ #include "HlsMaker.h" +#include "Common/config.h" using namespace std; diff --git a/src/Record/HlsMaker.h b/src/Record/HlsMaker.h index 78dbcff5..57a5308a 100644 --- a/src/Record/HlsMaker.h +++ b/src/Record/HlsMaker.h @@ -11,13 +11,9 @@ #ifndef HLSMAKER_H #define HLSMAKER_H +#include #include #include -#include "Common/config.h" -#include "Util/TimeTicker.h" -#include "Util/File.h" -#include "Util/util.h" -#include "Util/logger.h" namespace mediakit { diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index cb36ca7e..d4061a60 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -13,6 +13,8 @@ #include "HlsMakerImp.h" #include "Util/util.h" #include "Util/uv_errno.h" +#include "Util/File.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index adcc909a..406a6c6a 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -9,6 +9,7 @@ */ #include "HlsMediaSource.h" +#include "Common/config.h" using namespace toolkit; @@ -64,4 +65,40 @@ HlsMediaSource::Ptr HlsCookieData::getMediaSource() const { return _src.lock(); } +void HlsMediaSource::setIndexFile(std::string index_file) +{ + if (!_ring) { + std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + auto lam = [weakSelf](int size) { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return; + } + strongSelf->onReaderChanged(size); + }; + _ring = std::make_shared(0, std::move(lam)); + regist(); + } + + //赋值m3u8索引文件内容 + std::lock_guard lck(_mtx_index); + _index_file = std::move(index_file); + + if (!_index_file.empty()) { + _list_cb.for_each([&](const std::function& cb) { cb(_index_file); }); + _list_cb.clear(); + } +} + +void HlsMediaSource::getIndexFile(std::function cb) +{ + std::lock_guard lck(_mtx_index); + if (!_index_file.empty()) { + cb(_index_file); + return; + } + //等待生成m3u8文件 + _list_cb.emplace_back(std::move(cb)); +} + } // namespace mediakit diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index 1e1029fe..4211eae4 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -13,6 +13,7 @@ #include "Common/MediaSource.h" #include "Util/TimeTicker.h" +#include "Util/RingBuffer.h" #include namespace mediakit { @@ -41,42 +42,12 @@ public: /** * 设置或清空m3u8索引文件内容 */ - void setIndexFile(std::string index_file) { - if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); - auto lam = [weakSelf](int size) { - auto strongSelf = weakSelf.lock(); - if (!strongSelf) { - return; - } - strongSelf->onReaderChanged(size); - }; - _ring = std::make_shared(0, std::move(lam)); - regist(); - } - - //赋值m3u8索引文件内容 - std::lock_guard lck(_mtx_index); - _index_file = std::move(index_file); - - if (!_index_file.empty()) { - _list_cb.for_each([&](const std::function &cb) { cb(_index_file); }); - _list_cb.clear(); - } - } + void setIndexFile(std::string index_file); /** * 异步获取m3u8文件 */ - void getIndexFile(std::function cb) { - std::lock_guard lck(_mtx_index); - if (!_index_file.empty()) { - cb(_index_file); - return; - } - //等待生成m3u8文件 - _list_cb.emplace_back(std::move(cb)); - } + void getIndexFile(std::function cb); /** * 同步获取m3u8文件 diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index 14e97e30..12ff668e 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -13,6 +13,7 @@ #include "HlsMakerImp.h" #include "MPEG.h" +#include "Common/config.h" namespace mediakit { diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index 02957968..1dfa3a95 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -11,8 +11,11 @@ #ifdef ENABLE_MP4 #include "MP4Muxer.h" -#include "Util/File.h" +#include "Extension/AAC.h" +#include "Extension/G711.h" #include "Extension/H264.h" +#include "Extension/H265.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Record/MP4Muxer.h b/src/Record/MP4Muxer.h index 64947d82..dcf10d5d 100644 --- a/src/Record/MP4Muxer.h +++ b/src/Record/MP4Muxer.h @@ -14,10 +14,6 @@ #ifdef ENABLE_MP4 #include "Common/MediaSink.h" -#include "Extension/AAC.h" -#include "Extension/G711.h" -#include "Extension/H264.h" -#include "Extension/H265.h" #include "Common/Stamp.h" #include "MP4.h" diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index fbc6d8d0..35e28ac8 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -13,6 +13,7 @@ #include "MP4Reader.h" #include "Common/config.h" #include "Thread/WorkThreadPool.h" +#include "Util/File.h" using namespace std; using namespace toolkit; diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index 88d2d30c..fd7c8955 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -15,6 +15,7 @@ #include "Common/config.h" #include "MP4Recorder.h" #include "Thread/WorkThreadPool.h" +#include "MP4Muxer.h" using namespace std; using namespace toolkit; diff --git a/src/Record/MP4Recorder.h b/src/Record/MP4Recorder.h index 6ef35a8c..d40da7d2 100644 --- a/src/Record/MP4Recorder.h +++ b/src/Record/MP4Recorder.h @@ -13,17 +13,14 @@ #include #include -#include "Player/PlayerBase.h" -#include "Util/util.h" -#include "Util/logger.h" -#include "Util/TimeTicker.h" -#include "Util/TimeTicker.h" #include "Common/MediaSink.h" -#include "MP4Muxer.h" +#include "Record/Recorder.h" namespace mediakit { #ifdef ENABLE_MP4 +class MP4Muxer; + class MP4Recorder final : public MediaSinkInterface { public: using Ptr = std::shared_ptr; @@ -63,7 +60,7 @@ private: std::string _full_path; std::string _full_path_tmp; RecordInfo _info; - MP4Muxer::Ptr _muxer; + std::shared_ptr _muxer; std::list _tracks; uint64_t _last_dts = 0; }; diff --git a/src/Record/MPEG.h b/src/Record/MPEG.h index 9aabccdd..86a5b93f 100644 --- a/src/Record/MPEG.h +++ b/src/Record/MPEG.h @@ -18,10 +18,8 @@ #include #include "Extension/Frame.h" #include "Extension/Track.h" -#include "Util/File.h" #include "Common/MediaSink.h" -#include "Common/Stamp.h" - +#include "Util/ResourcePool.h" namespace mediakit { //该类用于产生MPEG-TS/MPEG-PS diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index 546780d8..f3e91db5 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -10,9 +10,11 @@ #include "Recorder.h" #include "Common/config.h" +#include "Util/File.h" #include "Common/MediaSource.h" #include "MP4Recorder.h" #include "HlsRecorder.h" +#include "Util/File.h" using namespace std; using namespace toolkit; diff --git a/src/Rtcp/RtcpFCI.h b/src/Rtcp/RtcpFCI.h index 8a14e5f3..745dc16a 100644 --- a/src/Rtcp/RtcpFCI.h +++ b/src/Rtcp/RtcpFCI.h @@ -11,7 +11,6 @@ #ifndef ZLMEDIAKIT_RTCPFCI_H #define ZLMEDIAKIT_RTCPFCI_H -#include "Common/config.h" #include "Rtcp.h" namespace mediakit { diff --git a/src/Rtmp/FlvMuxer.h b/src/Rtmp/FlvMuxer.h index eabbf318..4adc68d9 100644 --- a/src/Rtmp/FlvMuxer.h +++ b/src/Rtmp/FlvMuxer.h @@ -13,8 +13,7 @@ #include "Rtmp/Rtmp.h" #include "Rtmp/RtmpMediaSource.h" -#include "Network/Socket.h" -#include "Common/Stamp.h" +#include "Poller/EventPoller.h" namespace mediakit { diff --git a/src/Rtmp/Rtmp.cpp b/src/Rtmp/Rtmp.cpp index 85c61643..c00f2208 100644 --- a/src/Rtmp/Rtmp.cpp +++ b/src/Rtmp/Rtmp.cpp @@ -12,6 +12,16 @@ #include "Extension/Factory.h" namespace mediakit{ +TitleMeta::TitleMeta(float dur_sec, size_t fileSize, const std::map &header) +{ + _metadata.set("duration", dur_sec); + _metadata.set("fileSize", (int)fileSize); + _metadata.set("server", kServerName); + for (auto &pr : header) { + _metadata.set(pr.first, pr.second); + } +} + VideoMeta::VideoMeta(const VideoTrack::Ptr &video){ if(video->getVideoWidth() > 0 ){ _metadata.set("width", video->getVideoWidth()); @@ -146,6 +156,108 @@ RtmpPacket::Ptr RtmpPacket::create(){ #endif } +void RtmpPacket::clear() +{ + is_abs_stamp = false; + time_stamp = 0; + ts_field = 0; + body_size = 0; + buffer.clear(); +} + +bool RtmpPacket::isVideoKeyFrame() const +{ + return type_id == MSG_VIDEO && (uint8_t)buffer[0] >> 4 == FLV_KEY_FRAME && (uint8_t)buffer[1] == 1; +} + +bool RtmpPacket::isCfgFrame() const +{ + switch (type_id) { + case MSG_VIDEO: return buffer[1] == 0; + case MSG_AUDIO: { + switch (getMediaType()) { + case FLV_CODEC_AAC: return buffer[1] == 0; + default: return false; + } + } + default: return false; + } +} + +int RtmpPacket::getMediaType() const +{ + switch (type_id) { + case MSG_VIDEO: return (uint8_t)buffer[0] & 0x0F; + case MSG_AUDIO: return (uint8_t)buffer[0] >> 4; + default: return 0; + } +} + +int RtmpPacket::getAudioSampleRate() const +{ + if (type_id != MSG_AUDIO) { + return 0; + } + int flvSampleRate = ((uint8_t)buffer[0] & 0x0C) >> 2; + const static int sampleRate[] = { 5512, 11025, 22050, 44100 }; + return sampleRate[flvSampleRate]; +} + +int RtmpPacket::getAudioSampleBit() const +{ + if (type_id != MSG_AUDIO) { + return 0; + } + int flvSampleBit = ((uint8_t)buffer[0] & 0x02) >> 1; + const static int sampleBit[] = { 8, 16 }; + return sampleBit[flvSampleBit]; +} + +int RtmpPacket::getAudioChannel() const +{ + if (type_id != MSG_AUDIO) { + return 0; + } + int flvStereoOrMono = (uint8_t)buffer[0] & 0x01; + const static int channel[] = { 1, 2 }; + return channel[flvStereoOrMono]; +} + +RtmpPacket & RtmpPacket::operator=(const RtmpPacket &that) +{ + is_abs_stamp = that.is_abs_stamp; + stream_index = that.stream_index; + body_size = that.body_size; + type_id = that.type_id; + ts_field = that.ts_field; + time_stamp = that.time_stamp; + return *this; +} + +RtmpHandshake::RtmpHandshake(uint32_t _time, uint8_t *_random /*= nullptr*/) +{ + _time = htonl(_time); + memcpy(time_stamp, &_time, 4); + if (!_random) { + random_generate((char *)random, sizeof(random)); + } + else { + memcpy(random, _random, sizeof(random)); + } +} + +void RtmpHandshake::random_generate(char *bytes, int size) +{ + static char cdata[] = { 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x2d, 0x72, + 0x74, 0x6d, 0x70, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, + 0x2d, 0x77, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x2d, 0x77, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, + 0x40, 0x31, 0x32, 0x36, 0x2e, 0x63, 0x6f, 0x6d }; + for (int i = 0; i < size; i++) { + bytes[i] = cdata[rand() % (sizeof(cdata) - 1)]; + } +} + }//namespace mediakit namespace toolkit { diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index e8f58401..efbb2dea 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -14,11 +14,8 @@ #include #include #include -#include "Util/util.h" -#include "Util/logger.h" -#include "Network/Buffer.h" -#include "Network/sockutil.h" #include "amf.h" +#include "Network/Buffer.h" #include "Extension/Track.h" #if !defined(_WIN32) @@ -86,30 +83,13 @@ namespace mediakit { class RtmpHandshake { public: - RtmpHandshake(uint32_t _time, uint8_t *_random = nullptr) { - _time = htonl(_time); - memcpy(time_stamp, &_time, 4); - if (!_random) { - random_generate((char *) random, sizeof(random)); - } else { - memcpy(random, _random, sizeof(random)); - } - } + RtmpHandshake(uint32_t _time, uint8_t *_random = nullptr); uint8_t time_stamp[4]; uint8_t zero[4] = {0}; uint8_t random[RANDOM_LEN]; - void random_generate(char *bytes, int size) { - static char cdata[] = {0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x2d, 0x72, - 0x74, 0x6d, 0x70, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, - 0x2d, 0x77, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x2d, 0x77, 0x69, - 0x6e, 0x74, 0x65, 0x72, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, - 0x40, 0x31, 0x32, 0x36, 0x2e, 0x63, 0x6f, 0x6d}; - for (int i = 0; i < size; i++) { - bytes[i] = cdata[rand() % (sizeof(cdata) - 1)]; - } - } + void random_generate(char *bytes, int size); void create_complex_c0c1(); @@ -196,65 +176,16 @@ public: return buffer.size(); } - void clear(){ - is_abs_stamp = false; - time_stamp = 0; - ts_field = 0; - body_size = 0; - buffer.clear(); - } + void clear(); - bool isVideoKeyFrame() const { - return type_id == MSG_VIDEO && (uint8_t) buffer[0] >> 4 == FLV_KEY_FRAME && (uint8_t) buffer[1] == 1; - } + bool isVideoKeyFrame() const; + bool isCfgFrame() const; - bool isCfgFrame() const { - switch (type_id){ - case MSG_VIDEO : return buffer[1] == 0; - case MSG_AUDIO : { - switch (getMediaType()){ - case FLV_CODEC_AAC : return buffer[1] == 0; - default : return false; - } - } - default : return false; - } - } + int getMediaType() const; - int getMediaType() const { - switch (type_id) { - case MSG_VIDEO : return (uint8_t) buffer[0] & 0x0F; - case MSG_AUDIO : return (uint8_t) buffer[0] >> 4; - default : return 0; - } - } - - int getAudioSampleRate() const { - if (type_id != MSG_AUDIO) { - return 0; - } - int flvSampleRate = ((uint8_t) buffer[0] & 0x0C) >> 2; - const static int sampleRate[] = { 5512, 11025, 22050, 44100 }; - return sampleRate[flvSampleRate]; - } - - int getAudioSampleBit() const { - if (type_id != MSG_AUDIO) { - return 0; - } - int flvSampleBit = ((uint8_t) buffer[0] & 0x02) >> 1; - const static int sampleBit[] = { 8, 16 }; - return sampleBit[flvSampleBit]; - } - - int getAudioChannel() const { - if (type_id != MSG_AUDIO) { - return 0; - } - int flvStereoOrMono = (uint8_t) buffer[0] & 0x01; - const static int channel[] = { 1, 2 }; - return channel[flvStereoOrMono]; - } + int getAudioSampleRate() const; + int getAudioSampleBit() const; + int getAudioChannel() const; private: friend class toolkit::ResourcePool_l; @@ -262,15 +193,7 @@ private: clear(); } - RtmpPacket &operator=(const RtmpPacket &that) { - is_abs_stamp = that.is_abs_stamp; - stream_index = that.stream_index; - body_size = that.body_size; - type_id = that.type_id; - ts_field = that.ts_field; - time_stamp = that.time_stamp; - return *this; - } + RtmpPacket &operator=(const RtmpPacket &that); private: //对象个数统计 @@ -304,14 +227,7 @@ public: TitleMeta(float dur_sec = 0, size_t fileSize = 0, - const std::map &header = std::map()){ - _metadata.set("duration", dur_sec); - _metadata.set("fileSize", (int)fileSize); - _metadata.set("server",kServerName); - for (auto &pr : header){ - _metadata.set(pr.first, pr.second); - } - } + const std::map &header = std::map()); CodecId getCodecId() const override{ return CodecInvalid; diff --git a/src/Rtmp/RtmpCodec.h b/src/Rtmp/RtmpCodec.h index 476d0a39..620f830d 100644 --- a/src/Rtmp/RtmpCodec.h +++ b/src/Rtmp/RtmpCodec.h @@ -53,7 +53,7 @@ protected: RingType::Ptr _ring; }; -class RtmpCodec : public RtmpRing, public FrameDispatcher , public CodecInfo{ +class RtmpCodec : public RtmpRing, public FrameDispatcher, public CodecInfo { public: typedef std::shared_ptr Ptr; RtmpCodec() = default; diff --git a/src/Rtmp/RtmpDemuxer.cpp b/src/Rtmp/RtmpDemuxer.cpp index f9076f0b..b91dd503 100644 --- a/src/Rtmp/RtmpDemuxer.cpp +++ b/src/Rtmp/RtmpDemuxer.cpp @@ -7,7 +7,7 @@ * 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. */ - +#include "RtmpCodec.h" #include "RtmpDemuxer.h" #include "Extension/Factory.h" diff --git a/src/Rtmp/RtmpDemuxer.h b/src/Rtmp/RtmpDemuxer.h index f9e76a87..3b5df964 100644 --- a/src/Rtmp/RtmpDemuxer.h +++ b/src/Rtmp/RtmpDemuxer.h @@ -15,12 +15,10 @@ #include #include "Rtmp/amf.h" #include "Rtmp/Rtmp.h" -#include "Player/PlayerBase.h" -#include "Util/TimeTicker.h" -#include "RtmpCodec.h" +#include "Common/MediaSink.h" namespace mediakit { - +class RtmpCodec; class RtmpDemuxer : public Demuxer { public: using Ptr = std::shared_ptr; @@ -54,8 +52,8 @@ private: float _duration = 0; AudioTrack::Ptr _audio_track; VideoTrack::Ptr _video_track; - RtmpCodec::Ptr _audio_rtmp_decoder; - RtmpCodec::Ptr _video_rtmp_decoder; + std::shared_ptr _audio_rtmp_decoder; + std::shared_ptr _video_rtmp_decoder; }; } /* namespace mediakit */ diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 5bd8ea10..b80bc238 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -18,16 +18,9 @@ #include #include "amf.h" #include "Rtmp.h" -#include "RtmpDemuxer.h" -#include "Common/config.h" #include "Common/MediaSource.h" -#include "Util/util.h" -#include "Util/logger.h" +#include "Common/PacketCache.h" #include "Util/RingBuffer.h" -#include "Util/TimeTicker.h" -#include "Util/ResourcePool.h" -#include "Util/NoticeCenter.h" -#include "Thread/ThreadPool.h" #define RTMP_GOP_SIZE 512 @@ -126,66 +119,12 @@ public: * 输入rtmp包 * @param pkt rtmp包 */ - void onWrite(RtmpPacket::Ptr pkt, bool = true) override { - bool is_video = pkt->type_id == MSG_VIDEO; - _speed[is_video ? TrackVideo : TrackAudio] += pkt->size(); - //保存当前时间戳 - switch (pkt->type_id) { - case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break; - case MSG_AUDIO : _track_stamps[TrackAudio] = pkt->time_stamp, _have_audio = true; break; - default : break; - } - - if (pkt->isCfgFrame()) { - std::lock_guard lock(_mtx); - _config_frame_map[pkt->type_id] = pkt; - if (!_ring) { - //注册后收到config帧更新到各播放器 - return; - } - } - - if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); - auto lam = [weakSelf](int size) { - auto strongSelf = weakSelf.lock(); - if (!strongSelf) { - return; - } - strongSelf->onReaderChanged(size); - }; - - //GOP默认缓冲512组RTMP包,每组RTMP包时间戳相同(如果开启合并写了,那么每组为合并写时间内的RTMP包), - //每次遇到关键帧第一个RTMP包,则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开) - _ring = std::make_shared(_ring_size,std::move(lam)); - if(_metadata){ - regist(); - } - } - bool key = pkt->isVideoKeyFrame(); - auto stamp = pkt->time_stamp; - PacketCache::inputPacket(stamp, is_video, std::move(pkt), key); - } + void onWrite(RtmpPacket::Ptr pkt, bool = true) override; /** * 获取当前时间戳 */ - uint32_t getTimeStamp(TrackType trackType) override { - 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; - } - } - return ret; - } + uint32_t getTimeStamp(TrackType trackType) override; void clearCache() override{ PacketCache::clearCache(); diff --git a/src/Rtmp/RtmpMediaSourceImp.cpp b/src/Rtmp/RtmpMediaSourceImp.cpp new file mode 100644 index 00000000..8e49e534 --- /dev/null +++ b/src/Rtmp/RtmpMediaSourceImp.cpp @@ -0,0 +1,166 @@ +#include "RtmpDemuxer.h" +#include "RtmpMediaSourceImp.h" + +namespace mediakit { +uint32_t RtmpMediaSource::getTimeStamp(TrackType trackType) +{ + 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; + } + } + return ret; +} + +void RtmpMediaSource::onWrite(RtmpPacket::Ptr pkt, bool /*= true*/) +{ + bool is_video = pkt->type_id == MSG_VIDEO; + _speed[is_video ? TrackVideo : TrackAudio] += pkt->size(); + //保存当前时间戳 + switch (pkt->type_id) { + case MSG_VIDEO: _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break; + case MSG_AUDIO: _track_stamps[TrackAudio] = pkt->time_stamp, _have_audio = true; break; + default: break; + } + + if (pkt->isCfgFrame()) { + std::lock_guard lock(_mtx); + _config_frame_map[pkt->type_id] = pkt; + if (!_ring) { + //注册后收到config帧更新到各播放器 + return; + } + } + + if (!_ring) { + std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + auto lam = [weakSelf](int size) { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return; + } + strongSelf->onReaderChanged(size); + }; + + //GOP默认缓冲512组RTMP包,每组RTMP包时间戳相同(如果开启合并写了,那么每组为合并写时间内的RTMP包), + //每次遇到关键帧第一个RTMP包,则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开) + _ring = std::make_shared(_ring_size, std::move(lam)); + if (_metadata) { + regist(); + } + } + bool key = pkt->isVideoKeyFrame(); + auto stamp = pkt->time_stamp; + PacketCache::inputPacket(stamp, is_video, std::move(pkt), key); +} + + +RtmpMediaSourceImp::RtmpMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize) : RtmpMediaSource(vhost, app, id, ringSize) +{ + _demuxer = std::make_shared(); + _demuxer->setTrackListener(this); +} + +void RtmpMediaSourceImp::setMetaData(const AMFValue &metadata) +{ + if (!_demuxer->loadMetaData(metadata)) { + //该metadata无效,需要重新生成 + _metadata = metadata; + _recreate_metadata = true; + } + RtmpMediaSource::setMetaData(metadata); +} + +void RtmpMediaSourceImp::onWrite(RtmpPacket::Ptr pkt, bool /*= true*/) +{ + if (!_all_track_ready || _muxer->isEnabled()) { + //未获取到所有Track后,或者开启转协议,那么需要解复用rtmp + _demuxer->inputRtmp(pkt); + } + RtmpMediaSource::onWrite(std::move(pkt)); +} + +int RtmpMediaSourceImp::totalReaderCount() +{ + return readerCount() + (_muxer ? _muxer->totalReaderCount() : 0); +} + +void RtmpMediaSourceImp::setProtocolOption(const ProtocolOption &option) +{ + //不重复生成rtmp + _option = option; + //不重复生成rtmp协议 + _option.enable_rtmp = false; + _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); + _muxer->setMediaListener(getListener()); + _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); + //让_muxer对象拦截一部分事件(比如说录像相关事件) + MediaSource::setListener(_muxer); + + for (auto &track : _demuxer->getTracks(false)) { + _muxer->addTrack(track); + track->addDelegate(_muxer); + } +} + +bool RtmpMediaSourceImp::addTrack(const Track::Ptr &track) +{ + if (_muxer) { + if (_muxer->addTrack(track)) { + track->addDelegate(_muxer); + return true; + } + } + return false; +} + +void RtmpMediaSourceImp::addTrackCompleted() +{ + if (_muxer) { + _muxer->addTrackCompleted(); + } +} + +void RtmpMediaSourceImp::resetTracks() +{ + if (_muxer) { + _muxer->resetTracks(); + } +} + +void RtmpMediaSourceImp::onAllTrackReady() +{ + _all_track_ready = true; + + if (_recreate_metadata) { + //更新metadata + for (auto &track : _muxer->getTracks()) { + Metadata::addTrack(_metadata, track); + } + RtmpMediaSource::updateMetaData(_metadata); + } +} + +void RtmpMediaSourceImp::setListener(const std::weak_ptr &listener) +{ + if (_muxer) { + //_muxer对象不能处理的事件再给listener处理 + _muxer->setMediaListener(listener); + } + else { + //未创建_muxer对象,事件全部给listener处理 + MediaSource::setListener(listener); + } +} + +} + + diff --git a/src/Rtmp/RtmpMediaSourceImp.h b/src/Rtmp/RtmpMediaSourceImp.h index 90258198..7bb1363f 100644 --- a/src/Rtmp/RtmpMediaSourceImp.h +++ b/src/Rtmp/RtmpMediaSourceImp.h @@ -16,16 +16,13 @@ #include #include #include -#include "Util/util.h" -#include "Util/logger.h" #include "amf.h" #include "Rtmp.h" #include "RtmpMediaSource.h" -#include "RtmpDemuxer.h" #include "Common/MultiMediaSourceMuxer.h" namespace mediakit { - +class RtmpDemuxer; class RtmpMediaSourceImp final : public RtmpMediaSource, private TrackListener, public MultiMediaSourceMuxer::Listener { public: using Ptr = std::shared_ptr; @@ -37,62 +34,29 @@ public: * @param id 流id * @param ringSize 环形缓存大小 */ - RtmpMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTMP_GOP_SIZE) : RtmpMediaSource(vhost, app, id, ringSize) { - _demuxer = std::make_shared(); - _demuxer->setTrackListener(this); - } + RtmpMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTMP_GOP_SIZE); ~RtmpMediaSourceImp() override = default; /** * 设置metadata */ - void setMetaData(const AMFValue &metadata) override{ - if(!_demuxer->loadMetaData(metadata)){ - //该metadata无效,需要重新生成 - _metadata = metadata; - _recreate_metadata = true; - } - RtmpMediaSource::setMetaData(metadata); - } + void setMetaData(const AMFValue &metadata) override; /** * 输入rtmp并解析 */ - void onWrite(RtmpPacket::Ptr pkt, bool = true) override { - if (!_all_track_ready || _muxer->isEnabled()) { - //未获取到所有Track后,或者开启转协议,那么需要解复用rtmp - _demuxer->inputRtmp(pkt); - } - RtmpMediaSource::onWrite(std::move(pkt)); - } + void onWrite(RtmpPacket::Ptr pkt, bool = true) override; /** * 获取观看总人数,包括(hls/rtsp/rtmp) */ - int totalReaderCount() override{ - return readerCount() + (_muxer ? _muxer->totalReaderCount() : 0); - } + int totalReaderCount() override; /** * 设置协议转换 */ - void setProtocolOption(const ProtocolOption &option) { - //不重复生成rtmp - _option = option; - //不重复生成rtmp协议 - _option.enable_rtmp = false; - _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); - _muxer->setMediaListener(getListener()); - _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); - //让_muxer对象拦截一部分事件(比如说录像相关事件) - MediaSource::setListener(_muxer); - - for (auto &track : _demuxer->getTracks(false)) { - _muxer->addTrack(track); - track->addDelegate(_muxer); - } - } + void setProtocolOption(const ProtocolOption &option); const ProtocolOption &getProtocolOption() const { return _option; @@ -101,66 +65,32 @@ public: /** * _demuxer触发的添加Track事件 */ - bool addTrack(const Track::Ptr &track) override { - if (_muxer) { - if (_muxer->addTrack(track)) { - track->addDelegate(_muxer); - return true; - } - } - return false; - } + bool addTrack(const Track::Ptr &track) override; /** * _demuxer触发的Track添加完毕事件 */ - void addTrackCompleted() override { - if (_muxer) { - _muxer->addTrackCompleted(); - } - } + void addTrackCompleted() override; - void resetTracks() override { - if (_muxer) { - _muxer->resetTracks(); - } - } + void resetTracks() override; /** * _muxer触发的所有Track就绪的事件 */ - void onAllTrackReady() override{ - _all_track_ready = true; - - if (_recreate_metadata) { - //更新metadata - for (auto &track : _muxer->getTracks()) { - Metadata::addTrack(_metadata, track); - } - RtmpMediaSource::updateMetaData(_metadata); - } - } + void onAllTrackReady() override; /** * 设置事件监听器 * @param listener 监听器 */ - void setListener(const std::weak_ptr &listener) override{ - if (_muxer) { - //_muxer对象不能处理的事件再给listener处理 - _muxer->setMediaListener(listener); - } else { - //未创建_muxer对象,事件全部给listener处理 - MediaSource::setListener(listener); - } - } + void setListener(const std::weak_ptr &listener) override; private: bool _all_track_ready = false; bool _recreate_metadata = false; ProtocolOption _option; AMFValue _metadata; - RtmpDemuxer::Ptr _demuxer; + std::shared_ptr _demuxer; MultiMediaSourceMuxer::Ptr _muxer; }; diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index e0185ba3..3630afe8 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -13,6 +13,12 @@ #include "Util/util.h" #include "Util/onceToken.h" #include "Thread/ThreadPool.h" +#include "Common/config.h" +#include "Common/Parser.h" + +#include "RtmpDemuxer.h" +#include "RtmpPlayerImp.h" + using namespace toolkit; using namespace std; @@ -171,7 +177,7 @@ void RtmpPlayer::speed(float speed) { //todo } -inline void RtmpPlayer::send_connect() { +void RtmpPlayer::send_connect() { AMFValue obj(AMF_OBJECT); obj.set("app", _app); obj.set("tcUrl", _tc_url); @@ -199,7 +205,7 @@ inline void RtmpPlayer::send_connect() { }); } -inline void RtmpPlayer::send_createStream() { +void RtmpPlayer::send_createStream() { AMFValue obj(AMF_NULL); sendInvoke("createStream", obj); addOnResultCB([this](AMFDecoder &dec) { @@ -210,7 +216,7 @@ inline void RtmpPlayer::send_createStream() { }); } -inline void RtmpPlayer::send_play() { +void RtmpPlayer::send_play() { AMFEncoder enc; enc << "play" << ++_send_req_id << nullptr << _stream_id << -2000; sendRequest(MSG_CMD, enc.data()); @@ -226,7 +232,7 @@ inline void RtmpPlayer::send_play() { addOnStatusCB(fun); } -inline void RtmpPlayer::send_pause(bool pause) { +void RtmpPlayer::send_pause(bool pause) { AMFEncoder enc; enc << "pause" << ++_send_req_id << nullptr << pause; sendRequest(MSG_CMD, enc.data()); @@ -414,4 +420,49 @@ void RtmpPlayer::seekToMilliSecond(uint32_t seekMS){ }); } +//////////////////////////////////////////// +float RtmpPlayerImp::getDuration() const +{ + return _demuxer ? _demuxer->getDuration() : 0; +} + +std::vector RtmpPlayerImp::getTracks(bool ready /*= true*/) const +{ + return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); +} + +bool RtmpPlayerImp::onCheckMeta(const AMFValue &val) +{ + //无metadata或metadata中无track信息时,需要从数据包中获取track + _wait_track_ready = (*this)[Client::kWaitTrackReady].as() || RtmpDemuxer::trackCount(val) == 0; + onCheckMeta_l(val); + return true; +} + +void RtmpPlayerImp::onMediaData(RtmpPacket::Ptr chunkData) +{ + if (!_demuxer) { + //有些rtmp流没metadata + onCheckMeta_l(TitleMeta().getMetadata()); + } + _demuxer->inputRtmp(chunkData); + if (_rtmp_src) { + _rtmp_src->onWrite(std::move(chunkData)); + } +} + +void RtmpPlayerImp::onCheckMeta_l(const AMFValue &val) +{ + _rtmp_src = std::dynamic_pointer_cast(_media_src); + if (_rtmp_src) { + _rtmp_src->setMetaData(val); + } + if (_demuxer) { + return; + } + _demuxer = std::make_shared(); + //TraceL<<" _wait_track_ready "<<_wait_track_ready; + _demuxer->setTrackListener(this, _wait_track_ready); + _demuxer->loadMetaData(val); +} } /* namespace mediakit */ diff --git a/src/Rtmp/RtmpPlayer.h b/src/Rtmp/RtmpPlayer.h index 52b27288..bedbf4c1 100644 --- a/src/Rtmp/RtmpPlayer.h +++ b/src/Rtmp/RtmpPlayer.h @@ -18,8 +18,6 @@ #include "Rtmp.h" #include "RtmpProtocol.h" #include "Player/PlayerBase.h" -#include "Util/util.h" -#include "Util/logger.h" #include "Util/TimeTicker.h" #include "Network/Socket.h" #include "Network/TcpClient.h" diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index 64dd4c1d..21c23110 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -13,15 +13,11 @@ #include #include -#include "Common/config.h" #include "RtmpPlayer.h" #include "RtmpMediaSource.h" -#include "RtmpDemuxer.h" -#include "Poller/Timer.h" -#include "Util/TimeTicker.h" namespace mediakit { - +class RtmpDemuxer; class RtmpPlayerImp: public PlayerImp, private TrackListener { public: using Ptr = std::shared_ptr; @@ -50,33 +46,15 @@ public: seekToMilliSecond(pos); } - float getDuration() const override { - return _demuxer ? _demuxer->getDuration() : 0; - } + float getDuration() const override; - std::vector getTracks(bool ready = true) const override { - return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); - } + std::vector getTracks(bool ready = true) const override; private: //派生类回调函数 - bool onCheckMeta(const AMFValue &val) override { - //无metadata或metadata中无track信息时,需要从数据包中获取track - _wait_track_ready = (*this)[Client::kWaitTrackReady].as() || RtmpDemuxer::trackCount(val) == 0; - onCheckMeta_l(val); - return true; - } + bool onCheckMeta(const AMFValue &val) override; - void onMediaData(RtmpPacket::Ptr chunkData) override { - if (!_demuxer) { - //有些rtmp流没metadata - onCheckMeta_l(TitleMeta().getMetadata()); - } - _demuxer->inputRtmp(chunkData); - if (_rtmp_src) { - _rtmp_src->onWrite(std::move(chunkData)); - } - } + void onMediaData(RtmpPacket::Ptr chunkData) override; void onPlayResult(const toolkit::SockException &ex) override { if (!_wait_track_ready || ex) { @@ -94,23 +72,11 @@ private: } private: - void onCheckMeta_l(const AMFValue &val) { - _rtmp_src = std::dynamic_pointer_cast(_media_src); - if (_rtmp_src) { - _rtmp_src->setMetaData(val); - } - if(_demuxer){ - return; - } - _demuxer = std::make_shared(); - //TraceL<<" _wait_track_ready "<<_wait_track_ready; - _demuxer->setTrackListener(this, _wait_track_ready); - _demuxer->loadMetaData(val); - } + void onCheckMeta_l(const AMFValue &val); private: bool _wait_track_ready = true; - RtmpDemuxer::Ptr _demuxer; + std::shared_ptr _demuxer; RtmpMediaSource::Ptr _rtmp_src; }; diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index dc536c03..a4899d66 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -12,6 +12,7 @@ #include "RtmpProtocol.h" #include "Rtmp/utils.h" #include "RtmpMediaSource.h" +#include "Util/util.h" using namespace std; using namespace toolkit; diff --git a/src/Rtmp/RtmpProtocol.h b/src/Rtmp/RtmpProtocol.h index ee05cd33..c509641f 100644 --- a/src/Rtmp/RtmpProtocol.h +++ b/src/Rtmp/RtmpProtocol.h @@ -14,12 +14,9 @@ #include #include #include +#include #include "amf.h" #include "Rtmp.h" -#include "Util/util.h" -#include "Util/logger.h" -#include "Util/TimeTicker.h" -#include "Network/Socket.h" #include "Util/ResourcePool.h" #include "Http/HttpRequestSplitter.h" diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 8725b088..109696c5 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -13,6 +13,8 @@ #include "Util/util.h" #include "Util/onceToken.h" #include "Thread/ThreadPool.h" +#include "Common/Parser.h" +#include "Common/config.h" using namespace std; using namespace toolkit; @@ -127,7 +129,7 @@ void RtmpPusher::onRecv(const Buffer::Ptr &buf){ } } -inline void RtmpPusher::send_connect() { +void RtmpPusher::send_connect() { AMFValue obj(AMF_OBJECT); obj.set("app", _app); obj.set("type", "nonprivate"); @@ -147,7 +149,7 @@ inline void RtmpPusher::send_connect() { }); } -inline void RtmpPusher::send_createStream() { +void RtmpPusher::send_createStream() { AMFValue obj(AMF_NULL); sendInvoke("createStream", obj); addOnResultCB([this](AMFDecoder &dec) { @@ -159,7 +161,7 @@ inline void RtmpPusher::send_createStream() { } #define RTMP_STREAM_LIVE "live" -inline void RtmpPusher::send_publish() { +void RtmpPusher::send_publish() { AMFEncoder enc; enc << "publish" << ++_send_req_id << nullptr << _stream_id << RTMP_STREAM_LIVE; sendRequest(MSG_CMD, enc.data()); @@ -175,7 +177,7 @@ inline void RtmpPusher::send_publish() { }); } -inline void RtmpPusher::send_metaData(){ +void RtmpPusher::send_metaData(){ auto src = _publish_src.lock(); if (!src) { throw std::runtime_error("the media source was released"); diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index d124d858..243ac749 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -15,13 +15,10 @@ #include "amf.h" #include "Rtmp.h" #include "utils.h" -#include "Common/config.h" #include "RtmpProtocol.h" #include "RtmpMediaSourceImp.h" -#include "Util/util.h" #include "Util/TimeTicker.h" #include "Network/Session.h" -#include "Common/Stamp.h" namespace mediakit { diff --git a/src/Rtmp/amf.cpp b/src/Rtmp/amf.cpp index cb32ae59..8ffcf5d5 100644 --- a/src/Rtmp/amf.cpp +++ b/src/Rtmp/amf.cpp @@ -15,7 +15,7 @@ #include "Util/util.h" #include "Util/logger.h" #include "Network/sockutil.h" -#include "Util/util.h" +#include "Network/Buffer.h" using namespace std; using namespace toolkit; diff --git a/src/Rtmp/amf.h b/src/Rtmp/amf.h index c6daee70..283fbcc7 100644 --- a/src/Rtmp/amf.h +++ b/src/Rtmp/amf.h @@ -14,12 +14,11 @@ #include #include #include -#include #include -#include #include -#include "Network/Buffer.h" - +namespace toolkit { + class BufferLikeString; +} enum AMFType { AMF_NUMBER, AMF_INTEGER, diff --git a/src/Rtp/GB28181Process.cpp b/src/Rtp/GB28181Process.cpp index 485fed6a..d7bc0142 100644 --- a/src/Rtp/GB28181Process.cpp +++ b/src/Rtp/GB28181Process.cpp @@ -18,6 +18,9 @@ #include "Extension/Opus.h" #include "Http/HttpTSPlayer.h" #include "Util/File.h" +#include "Common/config.h" +#include "Rtsp/RtpCodec.h" +#include "Rtsp/RtpReceiver.h" using namespace std; using namespace toolkit; diff --git a/src/Rtp/GB28181Process.h b/src/Rtp/GB28181Process.h index 4afba479..a8e4f4d9 100644 --- a/src/Rtp/GB28181Process.h +++ b/src/Rtp/GB28181Process.h @@ -15,12 +15,12 @@ #include "Decoder.h" #include "ProcessInterface.h" -#include "Rtsp/RtpCodec.h" -#include "Rtsp/RtpReceiver.h" #include "Http/HttpRequestSplitter.h" +// for MediaInfo +#include "Common/MediaSource.h" namespace mediakit{ - +class RtpCodec; class RtpReceiverImp; class GB28181Process : public ProcessInterface { public: diff --git a/src/Rtp/PSEncoder.cpp b/src/Rtp/PSEncoder.cpp index a3ca8086..78cedb34 100644 --- a/src/Rtp/PSEncoder.cpp +++ b/src/Rtp/PSEncoder.cpp @@ -11,7 +11,9 @@ #if defined(ENABLE_RTPPROXY) #include "PSEncoder.h" +#include "Common/config.h" #include "Extension/H264.h" +#include "Extension/CommonRtp.h" #include "Rtsp/RtspMuxer.h" using namespace toolkit; diff --git a/src/Rtp/PSEncoder.h b/src/Rtp/PSEncoder.h index 1e3c13d4..12a6e639 100644 --- a/src/Rtp/PSEncoder.h +++ b/src/Rtp/PSEncoder.h @@ -15,11 +15,9 @@ #include "Record/MPEG.h" #include "Common/MediaSink.h" -#include "Common/Stamp.h" -#include "Extension/CommonRtp.h" namespace mediakit{ - +class CommonRtpEncoder; class PSEncoderImp : public MpegMuxer{ public: PSEncoderImp(uint32_t ssrc, uint8_t payload_type = 96); diff --git a/src/Rtp/RawEncoder.h b/src/Rtp/RawEncoder.h index d6c9620d..ca5e1841 100644 --- a/src/Rtp/RawEncoder.h +++ b/src/Rtp/RawEncoder.h @@ -14,11 +14,9 @@ #if defined(ENABLE_RTPPROXY) #include "Common/MediaSink.h" -#include "Common/Stamp.h" -#include "Extension/CommonRtp.h" namespace mediakit { - +class RtpCodec; class RawEncoderImp : public MediaSinkInterface { public: RawEncoderImp(uint32_t ssrc, uint8_t payload_type = 96, bool send_audio = true); @@ -44,13 +42,13 @@ protected: virtual void onRTP(toolkit::Buffer::Ptr rtp, bool is_key = false) = 0; private: - RtpCodec::Ptr createRtpEncoder(const Track::Ptr &track); + std::shared_ptr createRtpEncoder(const Track::Ptr &track); private: bool _send_audio; uint8_t _payload_type; uint32_t _ssrc; - RtpCodec::Ptr _rtp_encoder; + std::shared_ptr _rtp_encoder; }; } // namespace mediakit diff --git a/src/Rtp/RtpCache.h b/src/Rtp/RtpCache.h index 1121f384..c03329cd 100644 --- a/src/Rtp/RtpCache.h +++ b/src/Rtp/RtpCache.h @@ -15,7 +15,7 @@ #include "PSEncoder.h" #include "RawEncoder.h" -#include "Extension/CommonRtp.h" +#include "Common/PacketCache.h" namespace mediakit{ diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 4f1f0ce5..868e27e9 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -12,6 +12,8 @@ #include "GB28181Process.h" #include "RtpProcess.h" #include "Http/HttpTSPlayer.h" +#include "Util/File.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Rtp/RtpSender.cpp b/src/Rtp/RtpSender.cpp index ea7910ba..930b2835 100644 --- a/src/Rtp/RtpSender.cpp +++ b/src/Rtp/RtpSender.cpp @@ -14,6 +14,7 @@ #include "Thread/WorkThreadPool.h" #include "Util/uv_errno.h" #include "RtpCache.h" +#include "Rtcp/RtcpContext.h" using namespace std; using namespace toolkit; diff --git a/src/Rtp/RtpSender.h b/src/Rtp/RtpSender.h index 61bdfc3c..d59052e1 100644 --- a/src/Rtp/RtpSender.h +++ b/src/Rtp/RtpSender.h @@ -14,6 +14,8 @@ #include "PSEncoder.h" #include "Extension/CommonRtp.h" #include "Rtcp/RtcpContext.h" +#include "Common/MediaSource.h" +#include "Common/MediaSink.h" namespace mediakit{ diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index ccf8590b..f355ed14 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -13,6 +13,7 @@ #include "RtpServer.h" #include "RtpSelector.h" #include "Rtcp/RtcpContext.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index db72f822..52eb67b3 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -13,6 +13,7 @@ #include "RtpSelector.h" #include "Network/TcpServer.h" #include "Rtsp/RtpReceiver.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Rtsp/RtpCodec.h b/src/Rtsp/RtpCodec.h index 46988008..141f34da 100644 --- a/src/Rtsp/RtpCodec.h +++ b/src/Rtsp/RtpCodec.h @@ -12,8 +12,9 @@ #define ZLMEDIAKIT_RTPCODEC_H #include +#include "Extension/Frame.h" #include "Util/RingBuffer.h" -#include "Player/PlayerBase.h" +#include "Rtsp/Rtsp.h" namespace mediakit { @@ -58,7 +59,7 @@ protected: RingType::Ptr _ring; }; -class RtpInfo{ +class RtpInfo { public: using Ptr = std::shared_ptr; diff --git a/src/Rtsp/RtpMultiCaster.cpp b/src/Rtsp/RtpMultiCaster.cpp index 3c24aa68..8504b83a 100644 --- a/src/Rtsp/RtpMultiCaster.cpp +++ b/src/Rtsp/RtpMultiCaster.cpp @@ -14,6 +14,7 @@ #include "Util/util.h" #include "Network/sockutil.h" #include "RtspSession.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Rtsp/RtpMultiCaster.h b/src/Rtsp/RtpMultiCaster.h index f55189e6..1fe8e380 100644 --- a/src/Rtsp/RtpMultiCaster.h +++ b/src/Rtsp/RtpMultiCaster.h @@ -15,9 +15,7 @@ #include #include #include -#include "Common/config.h" #include "RtspMediaSource.h" -#include "Util/mini.h" #include "Network/Socket.h" namespace mediakit{ diff --git a/src/Rtsp/RtpReceiver.h b/src/Rtsp/RtpReceiver.h index f855d604..392df104 100644 --- a/src/Rtsp/RtpReceiver.h +++ b/src/Rtsp/RtpReceiver.h @@ -14,8 +14,9 @@ #include #include #include -#include "RtpCodec.h" -#include "RtspMediaSource.h" +#include "Rtsp/Rtsp.h" +#include "Extension/Frame.h" +// for NtpStamp #include "Common/Stamp.h" namespace mediakit { @@ -166,7 +167,7 @@ private: std::function _cb; }; -class RtpTrack : private PacketSortor{ +class RtpTrack : private PacketSortor { public: class BadRtpException : public std::invalid_argument { public: diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index e6c64f3b..a2db7796 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -12,6 +12,8 @@ #include #include "Rtsp.h" #include "Common/Parser.h" +#include "Common/config.h" +#include "Network/Socket.h" using namespace std; using namespace toolkit; @@ -589,6 +591,41 @@ RtpPacket::Ptr RtpPacket::create() { #endif } + +/** +* 构造title类型sdp +* @param dur_sec rtsp点播时长,0代表直播,单位秒 +* @param header 自定义sdp描述 +* @param version sdp版本 +*/ + +TitleSdp::TitleSdp(float dur_sec, const std::map& header, int version) : Sdp(0, 0) { + _printer << "v=" << version << "\r\n"; + + if (!header.empty()) { + for (auto &pr : header) { + _printer << pr.first << "=" << pr.second << "\r\n"; + } + } + else { + _printer << "o=- 0 0 IN IP4 0.0.0.0\r\n"; + _printer << "s=Streamed by " << kServerName << "\r\n"; + _printer << "c=IN IP4 0.0.0.0\r\n"; + _printer << "t=0 0\r\n"; + } + + if (dur_sec <= 0) { + //直播 + _printer << "a=range:npt=now-\r\n"; + } + else { + //点播 + _dur_sec = dur_sec; + _printer << "a=range:npt=0-" << dur_sec << "\r\n"; + } + _printer << "a=control:*\r\n"; +} + }//namespace mediakit namespace toolkit { diff --git a/src/Rtsp/Rtsp.h b/src/Rtsp/Rtsp.h index 839c67dc..e50e09a2 100644 --- a/src/Rtsp/Rtsp.h +++ b/src/Rtsp/Rtsp.h @@ -15,10 +15,11 @@ #include #include #include -#include "Util/util.h" -#include "Common/config.h" #include "Common/macros.h" #include "Extension/Frame.h" +namespace toolkit { + class Socket; +} namespace mediakit { @@ -312,30 +313,7 @@ public: */ TitleSdp(float dur_sec = 0, const std::map &header = std::map(), - int version = 0) : Sdp(0, 0) { - _printer << "v=" << version << "\r\n"; - - if (!header.empty()) { - for (auto &pr : header) { - _printer << pr.first << "=" << pr.second << "\r\n"; - } - } else { - _printer << "o=- 0 0 IN IP4 0.0.0.0\r\n"; - _printer << "s=Streamed by " << kServerName << "\r\n"; - _printer << "c=IN IP4 0.0.0.0\r\n"; - _printer << "t=0 0\r\n"; - } - - if (dur_sec <= 0) { - //直播 - _printer << "a=range:npt=now-\r\n"; - } else { - //点播 - _dur_sec = dur_sec; - _printer << "a=range:npt=0-" << dur_sec << "\r\n"; - } - _printer << "a=control:*\r\n"; - } + int version = 0); std::string getSdp() const override { return _printer; @@ -357,7 +335,7 @@ private: //创建rtp over tcp4个字节的头 toolkit::Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved); //创建rtp-rtcp端口对 -void makeSockPair(std::pair &pair, const std::string &local_ip, bool re_use_port = false, bool is_udp = true); +void makeSockPair(std::pair, std::shared_ptr> &pair, const std::string &local_ip, bool re_use_port = false, bool is_udp = true); //十六进制方式打印ssrc std::string printSSRC(uint32_t ui32Ssrc); diff --git a/src/Rtsp/RtspDemuxer.cpp b/src/Rtsp/RtspDemuxer.cpp index c380252e..10524f90 100644 --- a/src/Rtsp/RtspDemuxer.cpp +++ b/src/Rtsp/RtspDemuxer.cpp @@ -10,6 +10,7 @@ #include #include +#include "RtpCodec.h" #include "RtspDemuxer.h" #include "Util/base64.h" #include "Extension/Factory.h" diff --git a/src/Rtsp/RtspDemuxer.h b/src/Rtsp/RtspDemuxer.h index 7941d919..de9dd310 100644 --- a/src/Rtsp/RtspDemuxer.h +++ b/src/Rtsp/RtspDemuxer.h @@ -12,12 +12,10 @@ #define SRC_RTP_RTSPDEMUXER_H_ #include -#include "Player/PlayerBase.h" -#include "Util/TimeTicker.h" -#include "RtpCodec.h" +#include "Common/MediaSink.h" namespace mediakit { - +class RtpCodec; class RtspDemuxer : public Demuxer { public: typedef std::shared_ptr Ptr; @@ -51,8 +49,8 @@ private: float _duration = 0; AudioTrack::Ptr _audio_track; VideoTrack::Ptr _video_track; - RtpCodec::Ptr _audio_rtp_decoder; - RtpCodec::Ptr _video_rtp_decoder; + std::shared_ptr _audio_rtp_decoder; + std::shared_ptr _video_rtp_decoder; }; } /* namespace mediakit */ diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index c9bdc0df..4296f131 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -15,16 +15,9 @@ #include #include #include -#include -#include "Common/config.h" #include "Common/MediaSource.h" -#include "RtpCodec.h" -#include "Util/logger.h" +#include "Common/PacketCache.h" #include "Util/RingBuffer.h" -#include "Util/TimeTicker.h" -#include "Util/ResourcePool.h" -#include "Util/NoticeCenter.h" -#include "Thread/ThreadPool.h" #define RTP_GOP_SIZE 512 @@ -38,7 +31,6 @@ namespace mediakit { */ class RtspMediaSource : public MediaSource, public toolkit::RingDelegate, private PacketCache { public: - using PoolType = toolkit::ResourcePool; using Ptr = std::shared_ptr; using RingDataType = std::shared_ptr >; using RingType = toolkit::RingBuffer; @@ -111,85 +103,24 @@ public: /** * 获取相应轨道的时间戳,单位毫秒 */ - uint32_t getTimeStamp(TrackType trackType) override { - assert(trackType >= TrackInvalid && trackType < TrackMax); - if (trackType != TrackInvalid) { - //获取某track的时间戳 - auto &track = _tracks[trackType]; - if (track) { - return track->_time_stamp; - } - } - - //获取所有track的最小时间戳 - uint32_t ret = UINT32_MAX; - for (auto &track : _tracks) { - if (track && track->_time_stamp < ret) { - ret = track->_time_stamp; - } - } - return ret; - } + uint32_t getTimeStamp(TrackType trackType) override; /** * 更新时间戳 */ - void setTimeStamp(uint32_t stamp) override { - for (auto &track : _tracks) { - if (track) { - track->_time_stamp = stamp; - } - } - } + void setTimeStamp(uint32_t stamp) override; /** * 设置sdp */ - virtual void setSdp(const std::string &sdp) { - SdpParser sdp_parser(sdp); - _tracks[TrackVideo] = sdp_parser.getTrack(TrackVideo); - _tracks[TrackAudio] = sdp_parser.getTrack(TrackAudio); - _have_video = (bool) _tracks[TrackVideo]; - _sdp = sdp_parser.toString(); - if (_ring) { - regist(); - } - } + virtual void setSdp(const std::string &sdp); /** * 输入rtp * @param rtp rtp包 * @param keyPos 该包是否为关键帧的第一个包 */ - void onWrite(RtpPacket::Ptr rtp, bool keyPos) override { - _speed[rtp->type] += rtp->size(); - assert(rtp->type >= 0 && rtp->type < TrackMax); - auto &track = _tracks[rtp->type]; - auto stamp = rtp->getStampMS(); - if (track) { - track->_seq = rtp->getSeq(); - track->_time_stamp = rtp->getStamp() * uint64_t(1000) / rtp->sample_rate; - track->_ssrc = rtp->getSSRC(); - } - if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); - auto lam = [weakSelf](int size) { - auto strongSelf = weakSelf.lock(); - if (!strongSelf) { - return; - } - strongSelf->onReaderChanged(size); - }; - //GOP默认缓冲512组RTP包,每组RTP包时间戳相同(如果开启合并写了,那么每组为合并写时间内的RTP包), - //每次遇到关键帧第一个RTP包,则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开) - _ring = std::make_shared(_ring_size, std::move(lam)); - if (!_sdp.empty()) { - regist(); - } - } - bool is_video = rtp->type == TrackVideo; - PacketCache::inputPacket(stamp, is_video, std::move(rtp), keyPos); - } + void onWrite(RtpPacket::Ptr rtp, bool keyPos) override; void clearCache() override{ PacketCache::clearCache(); diff --git a/src/Rtsp/RtspMediaSourceImp.cpp b/src/Rtsp/RtspMediaSourceImp.cpp new file mode 100644 index 00000000..a262fad5 --- /dev/null +++ b/src/Rtsp/RtspMediaSourceImp.cpp @@ -0,0 +1,131 @@ +#include "RtspMediaSourceImp.h" +#include "RtspDemuxer.h" +#include "Common/config.h" +namespace mediakit { +void RtspMediaSource::setSdp(const std::string &sdp) { + SdpParser sdp_parser(sdp); + _tracks[TrackVideo] = sdp_parser.getTrack(TrackVideo); + _tracks[TrackAudio] = sdp_parser.getTrack(TrackAudio); + _have_video = (bool)_tracks[TrackVideo]; + _sdp = sdp_parser.toString(); + if (_ring) { + regist(); + } +} + +uint32_t RtspMediaSource::getTimeStamp(TrackType trackType) { + assert(trackType >= TrackInvalid && trackType < TrackMax); + if (trackType != TrackInvalid) { + //获取某track的时间戳 + auto &track = _tracks[trackType]; + if (track) { + return track->_time_stamp; + } + } + + //获取所有track的最小时间戳 + uint32_t ret = UINT32_MAX; + for (auto &track : _tracks) { + if (track && track->_time_stamp < ret) { + ret = track->_time_stamp; + } + } + return ret; +} + +/** + * 更新时间戳 + */ +void RtspMediaSource::setTimeStamp(uint32_t stamp) { + for (auto &track : _tracks) { + if (track) { + track->_time_stamp = stamp; + } + } +} + +void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) { + _speed[rtp->type] += rtp->size(); + assert(rtp->type >= 0 && rtp->type < TrackMax); + auto &track = _tracks[rtp->type]; + auto stamp = rtp->getStampMS(); + if (track) { + track->_seq = rtp->getSeq(); + track->_time_stamp = rtp->getStamp() * uint64_t(1000) / rtp->sample_rate; + track->_ssrc = rtp->getSSRC(); + } + if (!_ring) { + std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + auto lam = [weakSelf](int size) { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return; + } + strongSelf->onReaderChanged(size); + }; + //GOP默认缓冲512组RTP包,每组RTP包时间戳相同(如果开启合并写了,那么每组为合并写时间内的RTP包), + //每次遇到关键帧第一个RTP包,则会清空GOP缓存(因为有新的关键帧了,同样可以实现秒开) + _ring = std::make_shared(_ring_size, std::move(lam)); + if (!_sdp.empty()) { + regist(); + } + } + bool is_video = rtp->type == TrackVideo; + PacketCache::inputPacket(stamp, is_video, std::move(rtp), keyPos); +} + +RtspMediaSourceImp::RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize) + : RtspMediaSource(vhost, app, id, ringSize) +{ + _demuxer = std::make_shared(); + _demuxer->setTrackListener(this); +} + +void RtspMediaSourceImp::setSdp(const std::string &strSdp) +{ + if (!getSdp().empty()) { + return; + } + _demuxer->loadSdp(strSdp); + RtspMediaSource::setSdp(strSdp); +} + +void RtspMediaSourceImp::onWrite(RtpPacket::Ptr rtp, bool key_pos) +{ + if (_all_track_ready && !_muxer->isEnabled()) { + //获取到所有Track后,并且未开启转协议,那么不需要解复用rtp + //在关闭rtp解复用后,无法知道是否为关键帧,这样会导致无法秒开,或者开播花屏 + key_pos = rtp->type == TrackVideo; + } else { + //需要解复用rtp + key_pos = _demuxer->inputRtp(rtp); + } + GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy); + if (directProxy) { + //直接代理模式才直接使用原始rtp + RtspMediaSource::onWrite(std::move(rtp), key_pos); + } +} + +void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option) +{ + GET_CONFIG(bool, direct_proxy, Rtsp::kDirectProxy); + //开启直接代理模式时,rtsp直接代理,不重复产生;但是有些rtsp推流端,由于sdp中已有sps pps,rtp中就不再包括sps pps, + //导致rtc无法播放,所以在rtsp推流rtc播放时,建议关闭直接代理模式 + _option = option; + _option.enable_rtsp = !direct_proxy; + _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); + _muxer->setMediaListener(getListener()); + _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); + //让_muxer对象拦截一部分事件(比如说录像相关事件) + MediaSource::setListener(_muxer); + + for (auto &track : _demuxer->getTracks(false)) { + _muxer->addTrack(track); + track->addDelegate(_muxer); + } +} + + +} + diff --git a/src/Rtsp/RtspMediaSourceImp.h b/src/Rtsp/RtspMediaSourceImp.h index 975491bd..3bd9af02 100644 --- a/src/Rtsp/RtspMediaSourceImp.h +++ b/src/Rtsp/RtspMediaSourceImp.h @@ -11,12 +11,11 @@ #ifndef SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_ #define SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_ -#include "Rtmp/amf.h" #include "RtspMediaSource.h" -#include "RtspDemuxer.h" #include "Common/MultiMediaSourceMuxer.h" namespace mediakit { +class RtspDemuxer; class RtspMediaSourceImp final : public RtspMediaSource, private TrackListener, public MultiMediaSourceMuxer::Listener { public: using Ptr = std::shared_ptr; @@ -28,70 +27,31 @@ public: * @param id 流id * @param ringSize 环形缓存大小 */ - RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTP_GOP_SIZE) : RtspMediaSource(vhost, app, id,ringSize) { - _demuxer = std::make_shared(); - _demuxer->setTrackListener(this); - } + RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTP_GOP_SIZE); ~RtspMediaSourceImp() override = default; /** * 设置sdp */ - void setSdp(const std::string &strSdp) override { - if (!getSdp().empty()) { - return; - } - _demuxer->loadSdp(strSdp); - RtspMediaSource::setSdp(strSdp); - } + void setSdp(const std::string &strSdp) override; /** * 输入rtp并解析 */ - void onWrite(RtpPacket::Ptr rtp, bool key_pos) override { - if (_all_track_ready && !_muxer->isEnabled()) { - //获取到所有Track后,并且未开启转协议,那么不需要解复用rtp - //在关闭rtp解复用后,无法知道是否为关键帧,这样会导致无法秒开,或者开播花屏 - key_pos = rtp->type == TrackVideo; - } else { - //需要解复用rtp - key_pos = _demuxer->inputRtp(rtp); - } - GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy); - if (directProxy) { - //直接代理模式才直接使用原始rtp - RtspMediaSource::onWrite(std::move(rtp), key_pos); - } - } + void onWrite(RtpPacket::Ptr rtp, bool key_pos) override; /** * 获取观看总人数,包括(hls/rtsp/rtmp) */ - int totalReaderCount() override{ + int totalReaderCount() override { return readerCount() + (_muxer ? _muxer->totalReaderCount() : 0); } /** * 设置协议转换选项 */ - void setProtocolOption(const ProtocolOption &option) { - GET_CONFIG(bool, direct_proxy, Rtsp::kDirectProxy); - //开启直接代理模式时,rtsp直接代理,不重复产生;但是有些rtsp推流端,由于sdp中已有sps pps,rtp中就不再包括sps pps, - //导致rtc无法播放,所以在rtsp推流rtc播放时,建议关闭直接代理模式 - _option = option; - _option.enable_rtsp = !direct_proxy; - _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); - _muxer->setMediaListener(getListener()); - _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); - //让_muxer对象拦截一部分事件(比如说录像相关事件) - MediaSource::setListener(_muxer); - - for (auto &track : _demuxer->getTracks(false)) { - _muxer->addTrack(track); - track->addDelegate(_muxer); - } - } + void setProtocolOption(const ProtocolOption &option); const ProtocolOption &getProtocolOption() const { return _option; @@ -149,7 +109,7 @@ public: private: bool _all_track_ready = false; ProtocolOption _option; - RtspDemuxer::Ptr _demuxer; + std::shared_ptr _demuxer; MultiMediaSourceMuxer::Ptr _muxer; }; } /* namespace mediakit */ diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index ec63a08e..e99e2591 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -17,6 +17,11 @@ #include "Util/MD5.h" #include "Util/base64.h" #include "Rtcp/Rtcp.h" +#include "Rtcp/RtcpContext.h" +#include "RtspMediaSource.h" +#include "RtspDemuxer.h" +#include "RtspPlayerImp.h" + using namespace toolkit; using namespace std; @@ -745,4 +750,49 @@ int RtspPlayer::getTrackIndexByTrackType(TrackType track_type) const { throw SockException(Err_shutdown, StrPrinter << "no such track with type:" << getTrackString(track_type)); } +/////////////////////////////////////////////////// +// RtspPlayerImp +float RtspPlayerImp::getDuration() const +{ + return _demuxer ? _demuxer->getDuration() : 0; +} + +void RtspPlayerImp::onPlayResult(const toolkit::SockException &ex) { + if (!(*this)[Client::kWaitTrackReady].as() || ex) { + Super::onPlayResult(ex); + return; + } +} + +void RtspPlayerImp::addTrackCompleted() { + if ((*this)[Client::kWaitTrackReady].as()) { + Super::onPlayResult(toolkit::SockException(toolkit::Err_success, "play success")); + } +} + +std::vector RtspPlayerImp::getTracks(bool ready /*= true*/) const +{ + return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); +} + +bool RtspPlayerImp::onCheckSDP(const std::string &sdp) +{ + _rtsp_media_src = std::dynamic_pointer_cast(_media_src); + if (_rtsp_media_src) { + _rtsp_media_src->setSdp(sdp); + } + _demuxer = std::make_shared(); + _demuxer->setTrackListener(this, (*this)[Client::kWaitTrackReady].as()); + _demuxer->loadSdp(sdp); + return true; +} + +void RtspPlayerImp::onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) { + //rtp解复用时可以判断是否为关键帧起始位置 + auto key_pos = _demuxer->inputRtp(rtp); + if (_rtsp_media_src) { + _rtsp_media_src->onWrite(std::move(rtp), key_pos); + } +} + } /* namespace mediakit */ diff --git a/src/Rtsp/RtspPlayer.h b/src/Rtsp/RtspPlayer.h index 83eb5bfa..a0d328da 100644 --- a/src/Rtsp/RtspPlayer.h +++ b/src/Rtsp/RtspPlayer.h @@ -13,22 +13,16 @@ #include #include -#include "RtspSession.h" -#include "RtspMediaSource.h" -#include "Player/PlayerBase.h" -#include "Util/util.h" -#include "Util/logger.h" #include "Util/TimeTicker.h" #include "Poller/Timer.h" #include "Network/Socket.h" +#include "Player/PlayerBase.h" #include "Network/TcpClient.h" #include "RtspSplitter.h" #include "RtpReceiver.h" -#include "Common/Stamp.h" -#include "Rtcp/RtcpContext.h" namespace mediakit { - +class RtcpContext; //实现了rtsp播放器协议部分的功能,及数据接收功能 class RtspPlayer : public PlayerBase, public toolkit::TcpClient, public RtspSplitter, public RtpReceiver { public: @@ -149,7 +143,7 @@ private: //rtcp发送时间,trackid idx 为数组下标 toolkit::Ticker _rtcp_send_ticker[2]; //统计rtp并发送rtcp - std::vector _rtcp_context; + std::vector> _rtcp_context; }; } /* namespace mediakit */ diff --git a/src/Rtsp/RtspPlayerImp.h b/src/Rtsp/RtspPlayerImp.h index e2cdf6d4..db0a1cc2 100644 --- a/src/Rtsp/RtspPlayerImp.h +++ b/src/Rtsp/RtspPlayerImp.h @@ -14,14 +14,11 @@ #include #include #include -#include "Common/config.h" #include "RtspPlayer.h" -#include "RtspDemuxer.h" -#include "Poller/Timer.h" -#include "Util/TimeTicker.h" namespace mediakit { - +class RtspDemuxer; +class RtspMediaSource; class RtspPlayerImp : public PlayerImp ,private TrackListener { public: using Ptr = std::shared_ptr; @@ -57,53 +54,25 @@ public: seekToMilliSecond(pos); } - float getDuration() const override { - return _demuxer ? _demuxer->getDuration() : 0; - } + float getDuration() const override; - std::vector getTracks(bool ready = true) const override { - return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); - } + std::vector getTracks(bool ready = true) const override; private: //派生类回调函数 - bool onCheckSDP(const std::string &sdp) override { - _rtsp_media_src = std::dynamic_pointer_cast(_media_src); - if (_rtsp_media_src) { - _rtsp_media_src->setSdp(sdp); - } - _demuxer = std::make_shared(); - _demuxer->setTrackListener(this, (*this)[Client::kWaitTrackReady].as()); - _demuxer->loadSdp(sdp); - return true; - } + bool onCheckSDP(const std::string &sdp) override; - void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) override { - //rtp解复用时可以判断是否为关键帧起始位置 - auto key_pos = _demuxer->inputRtp(rtp); - if (_rtsp_media_src) { - _rtsp_media_src->onWrite(std::move(rtp), key_pos); - } - } + void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) override; - void onPlayResult(const toolkit::SockException &ex) override { - if (!(*this)[Client::kWaitTrackReady].as() || ex) { - Super::onPlayResult(ex); - return; - } - } + void onPlayResult(const toolkit::SockException &ex) override; bool addTrack(const Track::Ptr &track) override { return true; } - void addTrackCompleted() override { - if ((*this)[Client::kWaitTrackReady].as()) { - Super::onPlayResult(toolkit::SockException(toolkit::Err_success, "play success")); - } - } + void addTrackCompleted() override; private: - RtspDemuxer::Ptr _demuxer; - RtspMediaSource::Ptr _rtsp_media_src; + std::shared_ptr _demuxer; + std::shared_ptr _rtsp_media_src; }; } /* namespace mediakit */ diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index ab783e10..04d0d1a1 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -12,6 +12,8 @@ #include "Util/base64.h" #include "RtspPusher.h" #include "RtspSession.h" +#include "Rtcp/RtcpContext.h" +#include "Common/config.h" using namespace std; using namespace toolkit; diff --git a/src/Rtsp/RtspPusher.h b/src/Rtsp/RtspPusher.h index ec0087a0..48a59d95 100644 --- a/src/Rtsp/RtspPusher.h +++ b/src/Rtsp/RtspPusher.h @@ -14,17 +14,14 @@ #include #include #include "RtspMediaSource.h" -#include "Util/util.h" -#include "Util/logger.h" #include "Poller/Timer.h" #include "Network/Socket.h" #include "Network/TcpClient.h" #include "RtspSplitter.h" #include "Pusher/PusherBase.h" -#include "Rtcp/RtcpContext.h" namespace mediakit { - +class RtcpContext; class RtspPusher : public toolkit::TcpClient, public RtspSplitter, public PusherBase { public: typedef std::shared_ptr Ptr; @@ -96,7 +93,7 @@ private: //rtcp发送时间,trackid idx 为数组下标 toolkit::Ticker _rtcp_send_ticker[2]; //统计rtp并发送rtcp - std::vector _rtcp_context; + std::vector> _rtcp_context; }; using RtspPusherImp = PusherImp; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 2a67a18e..d3b40804 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -15,6 +15,8 @@ #include "RtspSession.h" #include "Util/MD5.h" #include "Util/base64.h" +#include "RtpMultiCaster.h" +#include "Rtcp/RtcpContext.h" using namespace std; using namespace toolkit; diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index fb8f080b..6d11c203 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -14,43 +14,17 @@ #include #include #include -#include -#include "Util/util.h" -#include "Util/logger.h" -#include "Common/config.h" #include "Network/Session.h" -#include "Player/PlayerBase.h" -#include "RtpMultiCaster.h" -#include "RtspMediaSource.h" #include "RtspSplitter.h" #include "RtpReceiver.h" +#include "RtspMediaSource.h" #include "RtspMediaSourceImp.h" -#include "Common/Stamp.h" -#include "Rtcp/RtcpContext.h" namespace mediakit { - +class RtpMultiCaster; class RtspSession; - -class BufferRtp : public toolkit::Buffer{ -public: - using Ptr = std::shared_ptr; - - BufferRtp(Buffer::Ptr pkt, size_t offset = 0) : _offset(offset), _rtp(std::move(pkt)) {} - ~BufferRtp() override = default; - - char *data() const override { - return (char *)_rtp->data() + _offset; - } - - size_t size() const override { - return _rtp->size() - _offset; - } - -private: - size_t _offset; - Buffer::Ptr _rtp; -}; +class RtcpContext; +using BufferRtp = toolkit::BufferOffset; class RtspSession : public toolkit::Session, public RtspSplitter, public RtpReceiver, public MediaSourceEvent { public: @@ -209,7 +183,7 @@ private: std::unordered_set _udp_connected_flags; ////////RTP over udp_multicast//////// //共享的rtp组播对象 - RtpMultiCaster::Ptr _multicaster; + std::shared_ptr _multicaster; ////////RTSP over HTTP //////// //quicktime 请求rtsp会产生两次tcp连接, //一次发送 get 一次发送post,需要通过x-sessioncookie关联起来 @@ -219,7 +193,7 @@ private: //rtcp发送时间,trackid idx 为数组下标 toolkit::Ticker _rtcp_send_tickers[2]; //统计rtp并发送rtcp - std::vector _rtcp_context; + std::vector> _rtcp_context; }; /** diff --git a/src/Rtsp/UDPServer.h b/src/Rtsp/UDPServer.h index a46cef85..9ee337db 100644 --- a/src/Rtsp/UDPServer.h +++ b/src/Rtsp/UDPServer.h @@ -15,9 +15,6 @@ #include #include #include -#include -#include "Util/util.h" -#include "Util/logger.h" #include "Network/Socket.h" namespace mediakit { diff --git a/src/Shell/ShellSession.h b/src/Shell/ShellSession.h index 6f1a1b7b..46113a27 100644 --- a/src/Shell/ShellSession.h +++ b/src/Shell/ShellSession.h @@ -12,7 +12,6 @@ #define SRC_SHELL_SHELLSESSION_H_ #include -#include "Common/config.h" #include "Util/TimeTicker.h" #include "Network/Session.h" diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index 253e1652..1f4503a4 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -12,6 +12,8 @@ #define ZLMEDIAKIT_TSMEDIASOURCE_H #include "Common/MediaSource.h" +#include "Common/PacketCache.h" +#include "Util/RingBuffer.h" #define TS_GOP_SIZE 512 diff --git a/srt/SrtTransport.hpp b/srt/SrtTransport.hpp index 094ba1b3..fe3dfe69 100644 --- a/srt/SrtTransport.hpp +++ b/srt/SrtTransport.hpp @@ -9,7 +9,7 @@ #include "Network/Session.h" #include "Poller/EventPoller.h" #include "Poller/Timer.h" - +#include "Common/Stamp.h" #include "Common.hpp" #include "NackContext.hpp" #include "Packet.hpp" diff --git a/srt/SrtTransportImp.cpp b/srt/SrtTransportImp.cpp index 8f0694b2..f5457e5f 100644 --- a/srt/SrtTransportImp.cpp +++ b/srt/SrtTransportImp.cpp @@ -1,6 +1,7 @@ #include "Util/util.h" #include - +#include "Common/Parser.h" +#include "Common/config.h" #include "SrtTransportImp.hpp" namespace SRT { diff --git a/tests/test_bench_pull.cpp b/tests/test_bench_pull.cpp index e5e79eae..321ae542 100644 --- a/tests/test_bench_pull.cpp +++ b/tests/test_bench_pull.cpp @@ -14,6 +14,7 @@ #include "Util/logger.h" #include "Util/onceToken.h" #include "Util/CMD.h" +#include "Common/config.h" #include "Rtsp/UDPServer.h" #include "Thread/WorkThreadPool.h" #include "Player/PlayerProxy.h" diff --git a/tests/test_bench_push.cpp b/tests/test_bench_push.cpp index e5496f66..d0929a8e 100644 --- a/tests/test_bench_push.cpp +++ b/tests/test_bench_push.cpp @@ -14,6 +14,8 @@ #include "Util/logger.h" #include "Util/onceToken.h" #include "Util/CMD.h" +#include "Common/config.h" +#include "Common/Parser.h" #include "Rtsp/Rtsp.h" #include "Thread/WorkThreadPool.h" #include "Pusher/MediaPusher.h" diff --git a/tests/test_httpApi.cpp b/tests/test_httpApi.cpp index 29ef7b23..c03531c6 100644 --- a/tests/test_httpApi.cpp +++ b/tests/test_httpApi.cpp @@ -11,7 +11,6 @@ #include #include #include -#include "Util/File.h" #include "Util/SSLBox.h" #include "Util/logger.h" #include "Util/onceToken.h" diff --git a/tests/test_httpClient.cpp b/tests/test_httpClient.cpp index 567b17a1..f218318b 100644 --- a/tests/test_httpClient.cpp +++ b/tests/test_httpClient.cpp @@ -12,7 +12,6 @@ #include #include #include "Util/MD5.h" -#include "Util/File.h" #include "Util/logger.h" #include "Util/onceToken.h" #include "Poller/EventPoller.h" diff --git a/tests/test_pusherMp4.cpp b/tests/test_pusherMp4.cpp index a29d74f3..ef338344 100644 --- a/tests/test_pusherMp4.cpp +++ b/tests/test_pusherMp4.cpp @@ -16,6 +16,7 @@ #include "Player/PlayerProxy.h" #include "Rtmp/RtmpPusher.h" #include "Common/config.h" +#include "Common/Parser.h" #include "Pusher/MediaPusher.h" #include "Record/MP4Reader.h" diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 7d073b5e..c13b8595 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -13,7 +13,6 @@ #include #include "Util/MD5.h" -#include "Util/File.h" #include "Util/logger.h" #include "Util/SSLBox.h" #include "Util/onceToken.h" diff --git a/webrtc/Nack.h b/webrtc/Nack.h index 2a02f311..2b38e6a3 100644 --- a/webrtc/Nack.h +++ b/webrtc/Nack.h @@ -11,6 +11,10 @@ #ifndef ZLMEDIAKIT_NACK_H #define ZLMEDIAKIT_NACK_H +#include +#include +#include +#include #include "Rtsp/Rtsp.h" #include "Rtcp/RtcpFCI.h" diff --git a/webrtc/SctpAssociation.cpp b/webrtc/SctpAssociation.cpp index 26148ddc..0aec443b 100644 --- a/webrtc/SctpAssociation.cpp +++ b/webrtc/SctpAssociation.cpp @@ -5,6 +5,7 @@ #include "SctpAssociation.hpp" #include "logger.h" +#include #include // std::malloc(), std::free() #include // std::memset(), std::memcpy() #include diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index 3a30ba67..7d9b99d5 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -10,6 +10,7 @@ #include "Sdp.h" #include "Rtsp/Rtsp.h" +#include "Common/config.h" #include using namespace std; diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index 564362a4..a96774c1 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -11,6 +11,8 @@ #ifndef ZLMEDIAKIT_SDP_H #define ZLMEDIAKIT_SDP_H +#include +#include #include #include #include "RtpExt.h" diff --git a/webrtc/StunPacket.cpp b/webrtc/StunPacket.cpp index 50120a70..c7a403fb 100644 --- a/webrtc/StunPacket.cpp +++ b/webrtc/StunPacket.cpp @@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "StunPacket.hpp" #include // std::snprintf() #include // std::memcmp(), std::memcpy() +#include namespace RTC { diff --git a/webrtc/TwccContext.h b/webrtc/TwccContext.h index 597d6d61..db81edc6 100644 --- a/webrtc/TwccContext.h +++ b/webrtc/TwccContext.h @@ -14,7 +14,7 @@ #include #include #include -#include "Util/TimeTicker.h" +#include namespace mediakit { diff --git a/webrtc/Utils.hpp b/webrtc/Utils.hpp index 1692ff50..d1386504 100644 --- a/webrtc/Utils.hpp +++ b/webrtc/Utils.hpp @@ -22,30 +22,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #if defined(_WIN32) #include #include -#include #pragma comment (lib, "Ws2_32.lib") -#pragma comment(lib,"Iphlpapi.lib") #else -#include #include -#include -#include -#include -#include -#include #endif // defined(_WIN32) -#include // std::transform(), std::find(), std::min(), std::max() #include // PRIu64, etc -#include #include // size_t #include // uint8_t, etc -#include // std::memcmp(), std::memcpy() -#include -#include -#include -#include -#include namespace Utils { diff --git a/webrtc/WebRtcPlayer.cpp b/webrtc/WebRtcPlayer.cpp index 0ef9cbfe..7829c387 100644 --- a/webrtc/WebRtcPlayer.cpp +++ b/webrtc/WebRtcPlayer.cpp @@ -9,6 +9,7 @@ */ #include "WebRtcPlayer.h" +#include "Common/config.h" using namespace std; diff --git a/webrtc/WebRtcPlayer.h b/webrtc/WebRtcPlayer.h index 538c4151..194ad4cb 100644 --- a/webrtc/WebRtcPlayer.h +++ b/webrtc/WebRtcPlayer.h @@ -12,6 +12,7 @@ #define ZLMEDIAKIT_WEBRTCPLAYER_H #include "WebRtcTransport.h" +#include "Rtsp/RtspMediaSource.h" namespace mediakit { diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index dbf83c7e..4a8b6096 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -9,6 +9,7 @@ */ #include "WebRtcPusher.h" +#include "Common/config.h" using namespace std; diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index 7f64b1f5..45e70d32 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.h @@ -12,6 +12,7 @@ #define ZLMEDIAKIT_WEBRTCPUSHER_H #include "WebRtcTransport.h" +#include "Rtsp/RtspMediaSourceImp.h" namespace mediakit { diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 382e4c3d..c797ddb0 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -11,6 +11,9 @@ #include "WebRtcSession.h" #include "Util/util.h" #include "Network/TcpServer.h" +#include "Common/config.h" +#include "IceServer.hpp" +#include "WebRtcTransport.h" using namespace std; diff --git a/webrtc/WebRtcSession.h b/webrtc/WebRtcSession.h index 9d48e814..c4303b85 100644 --- a/webrtc/WebRtcSession.h +++ b/webrtc/WebRtcSession.h @@ -13,8 +13,6 @@ #define ZLMEDIAKIT_WEBRTCSESSION_H #include "Network/Session.h" -#include "IceServer.hpp" -#include "WebRtcTransport.h" #include "Http/HttpRequestSplitter.h" namespace toolkit { @@ -22,6 +20,8 @@ namespace toolkit { } namespace mediakit { +class WebRtcTransportImp; +using namespace toolkit; class WebRtcSession : public Session, public HttpRequestSplitter { public: diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index fc0612a3..e55e6417 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -10,10 +10,11 @@ #include #include - +#include "Common/config.h" #include "RtpExt.h" #include "Rtcp/Rtcp.h" #include "Rtcp/RtcpFCI.h" +#include "Rtcp/RtcpContext.h" #include "Rtsp/RtpReceiver.h" #include "WebRtcTransport.h" diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index c34ad017..6503d2a5 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -17,18 +17,16 @@ #include "SrtpSession.hpp" #include "StunPacket.hpp" #include "Sdp.h" +#include "Util/mini.h" #include "Poller/EventPoller.h" #include "Network/Socket.h" -#include "Rtsp/RtspMediaSourceImp.h" -#include "Rtcp/RtcpContext.h" -#include "Rtcp/RtcpFCI.h" -#include "Nack.h" #include "Network/Session.h" +#include "Nack.h" #include "TwccContext.h" #include "SctpAssociation.hpp" namespace mediakit { - +class RtcpContext; //RTC配置项目 namespace Rtc { extern const std::string kPort; @@ -203,7 +201,7 @@ public: //for send rtp NackList nack_list; - RtcpContext::Ptr rtcp_context_send; + std::shared_ptr rtcp_context_send; //for recv rtp std::unordered_map > rtp_channel;