From 713164977905aac968a91fbb725f7d76114810e2 Mon Sep 17 00:00:00 2001 From: along Date: Thu, 3 Dec 2020 10:23:46 +0800 Subject: [PATCH 1/6] Update HttpTSPlayer.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 保存http status返回值,在非200或206的情况下,不再进行数据判断。 --- src/Http/HttpTSPlayer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Http/HttpTSPlayer.cpp b/src/Http/HttpTSPlayer.cpp index 706e1df3..cd6d6fb0 100644 --- a/src/Http/HttpTSPlayer.cpp +++ b/src/Http/HttpTSPlayer.cpp @@ -20,6 +20,7 @@ HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller, bool split_ts){ HttpTSPlayer::~HttpTSPlayer() {} int64_t HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &headers) { + _status = status; if (status != "200" && status != "206") { //http状态码不符合预期 shutdown(SockException(Err_other, StrPrinter << "bad http status code:" + status)); @@ -35,6 +36,9 @@ int64_t HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::H } void HttpTSPlayer::onResponseBody(const char *buf, int64_t size, int64_t recvedSize, int64_t totalSize) { + if (_status != "200" && _status != "206") { + return; + } if (recvedSize == size) { //开始接收数据 if (buf[0] == TS_SYNC_BYTE) { @@ -78,4 +82,4 @@ void HttpTSPlayer::setOnPacket(const TSSegment::onSegment &cb) { _on_segment = cb; } -}//namespace mediakit \ No newline at end of file +}//namespace mediakit From 68926b4ae7b6ef05277c48db1825b06d0da789d7 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Fri, 4 Dec 2020 16:44:53 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=94=AF=E6=8C=81bitrate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 7 +++++-- src/Common/MediaSource.h | 4 ++-- src/Extension/AAC.cpp | 2 +- src/Extension/AAC.h | 8 +++++--- src/Extension/G711.cpp | 2 +- src/Extension/G711.h | 7 +++++-- src/Extension/H264.cpp | 2 +- src/Extension/H264.h | 8 +++++--- src/Extension/H265.cpp | 2 +- src/Extension/H265.h | 8 +++++--- src/Extension/Opus.cpp | 2 +- src/Extension/Opus.h | 7 +++++-- src/Extension/Track.h | 28 ++++++++++++++++++---------- src/FMP4/FMP4MediaSource.h | 2 +- src/Record/HlsMediaSource.h | 2 +- src/Rtmp/Rtmp.cpp | 12 ++++++++---- src/Rtmp/Rtmp.h | 4 ++-- src/Rtmp/RtmpDemuxer.cpp | 24 ++++++++++++++++++------ src/Rtmp/RtmpDemuxer.h | 4 ++-- src/Rtmp/RtmpMediaSource.h | 4 ++-- src/Rtsp/RtspDemuxer.cpp | 11 +++++++++++ src/Rtsp/RtspMediaSource.h | 2 +- src/TS/TSMediaSource.h | 2 +- 23 files changed, 102 insertions(+), 52 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 51bd7efb..afe42ff7 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -68,8 +68,11 @@ const string& MediaSource::getId() const { return _stream_id; } -int MediaSource::getBytesSpeed(){ - return _speed.getSpeed(); +int MediaSource::getBytesSpeed(TrackType type){ + if(type == TrackInvalid){ + return _speed[TrackVideo].getSpeed() + _speed[TrackAudio].getSpeed(); + } + return _speed[type].getSpeed(); } uint64_t MediaSource::getCreateStamp() const { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 5df31f50..965e1e57 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -220,7 +220,7 @@ public: virtual void setTimeStamp(uint32_t stamp) {}; // 获取数据速率,单位bytes/s - int getBytesSpeed(); + int getBytesSpeed(TrackType type = TrackInvalid); // 获取流创建GMT unix时间戳,单位秒 uint64_t getCreateStamp() const; // 获取流上线时间,单位秒 @@ -286,7 +286,7 @@ private: void emitEvent(bool regist); protected: - BytesSpeed _speed; + BytesSpeed _speed[TrackMax]; private: time_t _create_stamp; diff --git a/src/Extension/AAC.cpp b/src/Extension/AAC.cpp index 1a820e42..30b4cf2f 100644 --- a/src/Extension/AAC.cpp +++ b/src/Extension/AAC.cpp @@ -185,7 +185,7 @@ Sdp::Ptr AACTrack::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getAacCfg(),getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getAacCfg(), getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit \ No newline at end of file diff --git a/src/Extension/AAC.h b/src/Extension/AAC.h index 243f919f..0dd5ecaf 100644 --- a/src/Extension/AAC.h +++ b/src/Extension/AAC.h @@ -177,10 +177,12 @@ public: AACSdp(const string &aac_cfg, int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " MPEG4-GENERIC/" << sample_rate << "/" << channels << "\r\n"; string configStr; diff --git a/src/Extension/G711.cpp b/src/Extension/G711.cpp index 15c3d74e..9943e97f 100644 --- a/src/Extension/G711.cpp +++ b/src/Extension/G711.cpp @@ -17,7 +17,7 @@ Sdp::Ptr G711Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getCodecId(), getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getCodecId(), getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/G711.h b/src/Extension/G711.h index 72273831..f7d82ee7 100644 --- a/src/Extension/G711.h +++ b/src/Extension/G711.h @@ -48,9 +48,12 @@ public: G711Sdp(CodecId codecId, int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type), _codecId(codecId){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type), _codecId(codecId){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << sample_rate << "/" << channels << "\r\n"; _printer << "a=control:trackID=" << (int)TrackAudio << "\r\n"; } diff --git a/src/Extension/H264.cpp b/src/Extension/H264.cpp index 910e79e2..84fc3e1c 100644 --- a/src/Extension/H264.cpp +++ b/src/Extension/H264.cpp @@ -123,7 +123,7 @@ Sdp::Ptr H264Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getSps(),getPps()); + return std::make_shared(getSps(), getPps(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/H264.h b/src/Extension/H264.h index 02eba813..589701cf 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -292,11 +292,13 @@ public: */ H264Sdp(const string &strSPS, const string &strPPS, - int payload_type = 96, - int bitrate = 4000) : Sdp(90000,payload_type) { + int bitrate = 4000, + int payload_type = 96) : Sdp(90000,payload_type) { //视频通道 _printer << "m=video 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " H264/" << 90000 << "\r\n"; _printer << "a=fmtp:" << payload_type << " packetization-mode=1; profile-level-id="; diff --git a/src/Extension/H265.cpp b/src/Extension/H265.cpp index f46a756d..0283f33c 100644 --- a/src/Extension/H265.cpp +++ b/src/Extension/H265.cpp @@ -55,7 +55,7 @@ Sdp::Ptr H265Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getVps(),getSps(),getPps()); + return std::make_shared(getVps(), getSps(), getPps(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/H265.h b/src/Extension/H265.h index 4fc3d82e..a4014c2f 100644 --- a/src/Extension/H265.h +++ b/src/Extension/H265.h @@ -321,11 +321,13 @@ public: H265Sdp(const string &strVPS, const string &strSPS, const string &strPPS, - int payload_type = 96, - int bitrate = 4000) : Sdp(90000,payload_type) { + int bitrate = 4000, + int payload_type = 96) : Sdp(90000,payload_type) { //视频通道 _printer << "m=video 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " H265/" << 90000 << "\r\n"; _printer << "a=fmtp:" << payload_type << " "; _printer << "sprop-vps="; diff --git a/src/Extension/Opus.cpp b/src/Extension/Opus.cpp index e9dea37d..ada06174 100644 --- a/src/Extension/Opus.cpp +++ b/src/Extension/Opus.cpp @@ -17,7 +17,7 @@ Sdp::Ptr OpusTrack::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit \ No newline at end of file diff --git a/src/Extension/Opus.h b/src/Extension/Opus.h index 144afb77..5402d4a9 100644 --- a/src/Extension/Opus.h +++ b/src/Extension/Opus.h @@ -46,9 +46,12 @@ public: */ OpusSdp(int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " opus/" << sample_rate << "/" << channels << "\r\n"; _printer << "a=control:trackID=" << (int)TrackAudio << "\r\n"; } diff --git a/src/Extension/Track.h b/src/Extension/Track.h index 788af78f..70f02ead 100644 --- a/src/Extension/Track.h +++ b/src/Extension/Track.h @@ -32,7 +32,6 @@ public: /** * 是否准备好,准备好才能获取譬如sps pps等信息 - * @return */ virtual bool ready() = 0; @@ -40,7 +39,6 @@ public: * 克隆接口,用于复制本对象用 * 在调用该接口时只会复制派生类的信息 * 环形缓存和代理关系不能拷贝,否则会关系紊乱 - * @return */ virtual Track::Ptr clone() = 0; @@ -50,12 +48,28 @@ public: */ virtual Sdp::Ptr getSdp() = 0; + /** + * 返回比特率 + * @return 比特率 + */ + virtual int getBitRate() const { return _bit_rate; } + + /** + * 设置比特率 + * @param bit_rate 比特率 + */ + virtual void setBitRate(int bit_rate) { _bit_rate = bit_rate; } + /** * 复制拷贝,只能拷贝派生类的信息, * 环形缓存和代理关系不能拷贝,否则会关系紊乱 - * @param that */ - Track(const Track &that){} + Track(const Track &that){ + _bit_rate = that._bit_rate; + } + +private: + int _bit_rate = 0; }; /** @@ -67,19 +81,16 @@ public: /** * 返回视频高度 - * @return */ virtual int getVideoHeight() const {return 0;}; /** * 返回视频宽度 - * @return */ virtual int getVideoWidth() const {return 0;}; /** * 返回视频fps - * @return */ virtual float getVideoFps() const {return 0;}; }; @@ -93,19 +104,16 @@ public: /** * 返回音频采样率 - * @return */ virtual int getAudioSampleRate() const {return 0;}; /** * 返回音频采样位数,一般为16或8 - * @return */ virtual int getAudioSampleBit() const {return 0;}; /** * 返回音频通道数 - * @return */ virtual int getAudioChannel() const {return 0;}; }; diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index ef4af868..16fb2ab2 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -88,7 +88,7 @@ public: if (key) { _have_video = true; } - _speed += packet->size(); + _speed[TrackVideo] += packet->size(); auto stamp = packet->time_stamp; PacketCache::inputPacket(stamp, true, std::move(packet), key); } diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index 32e46cc5..aa624b83 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -80,7 +80,7 @@ public: } void onSegmentSize(uint64_t bytes) { - _speed += bytes; + _speed[TrackVideo] += bytes; } private: diff --git a/src/Rtmp/Rtmp.cpp b/src/Rtmp/Rtmp.cpp index b7046163..0c80692c 100644 --- a/src/Rtmp/Rtmp.cpp +++ b/src/Rtmp/Rtmp.cpp @@ -12,7 +12,7 @@ #include "Extension/Factory.h" namespace mediakit{ -VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){ +VideoMeta::VideoMeta(const VideoTrack::Ptr &video){ if(video->getVideoWidth() > 0 ){ _metadata.set("width", video->getVideoWidth()); } @@ -22,13 +22,17 @@ VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){ if(video->getVideoFps() > 0 ){ _metadata.set("framerate", video->getVideoFps()); } - _metadata.set("videodatarate", datarate); + if (video->getBitRate()) { + _metadata.set("videodatarate", video->getBitRate() / 1024); + } _codecId = video->getCodecId(); _metadata.set("videocodecid", Factory::getAmfByCodecId(_codecId)); } -AudioMeta::AudioMeta(const AudioTrack::Ptr &audio,int datarate){ - _metadata.set("audiodatarate", datarate); +AudioMeta::AudioMeta(const AudioTrack::Ptr &audio){ + if (audio->getBitRate()) { + _metadata.set("audiodatarate", audio->getBitRate() / 1024); + } if(audio->getAudioSampleRate() > 0){ _metadata.set("audiosamplerate", audio->getAudioSampleRate()); } diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 1f8d1e3e..f9f9790a 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -261,7 +261,7 @@ class VideoMeta : public Metadata{ public: typedef std::shared_ptr Ptr; - VideoMeta(const VideoTrack::Ptr &video,int datarate = 5000); + VideoMeta(const VideoTrack::Ptr &video); virtual ~VideoMeta(){} CodecId getCodecId() const override{ @@ -275,7 +275,7 @@ class AudioMeta : public Metadata{ public: typedef std::shared_ptr Ptr; - AudioMeta(const AudioTrack::Ptr &audio,int datarate = 160); + AudioMeta(const AudioTrack::Ptr &audio); virtual ~AudioMeta(){} diff --git a/src/Rtmp/RtmpDemuxer.cpp b/src/Rtmp/RtmpDemuxer.cpp index 53ce4c87..5225ce32 100644 --- a/src/Rtmp/RtmpDemuxer.cpp +++ b/src/Rtmp/RtmpDemuxer.cpp @@ -19,6 +19,8 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){ int audiosamplerate = 0; int audiochannels = 0; int audiosamplesize = 0; + int videodatarate = 0; + int audiodatarate = 0; const AMFValue *audiocodecid = nullptr; const AMFValue *videocodecid = nullptr; val.object_for_each([&](const string &key, const AMFValue &val) { @@ -48,16 +50,24 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){ audiocodecid = &val; return; } + if (key == "audiodatarate") { + audiodatarate = val.as_integer(); + return; + } + if (key == "videodatarate") { + videodatarate = val.as_integer(); + return; + } }); if (videocodecid) { //有视频 ret = true; - makeVideoTrack(*videocodecid); + makeVideoTrack(*videocodecid, videodatarate * 1024); } if (audiocodecid) { //有音频 ret = true; - makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize); + makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize, audiodatarate * 1024); } } catch (std::exception &ex) { WarnL << ex.what(); @@ -71,7 +81,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { if (!_try_get_video_track) { _try_get_video_track = true; auto codec = AMFValue(pkt->getMediaType()); - makeVideoTrack(codec); + makeVideoTrack(codec, 0); } if (_video_rtmp_decoder) { _video_rtmp_decoder->inputRtmp(pkt); @@ -83,7 +93,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { if (!_try_get_audio_track) { _try_get_audio_track = true; auto codec = AMFValue(pkt->getMediaType()); - makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit()); + makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit(), 0); } if (_audio_rtmp_decoder) { _audio_rtmp_decoder->inputRtmp(pkt); @@ -94,10 +104,11 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { } } -void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) { +void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) { //生成Track对象 _videoTrack = dynamic_pointer_cast(Factory::getVideoTrackByAmf(videoCodec)); if (_videoTrack) { + _videoTrack->setBitRate(bit_rate); //生成rtmpCodec对象以便解码rtmp _video_rtmp_decoder = Factory::getRtmpCodecByTrack(_videoTrack, false); if (_video_rtmp_decoder) { @@ -112,10 +123,11 @@ void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) { } } -void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit) { +void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) { //生成Track对象 _audioTrack = dynamic_pointer_cast(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit)); if (_audioTrack) { + _audioTrack->setBitRate(bit_rate); //生成rtmpCodec对象以便解码rtmp _audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audioTrack, false); if (_audio_rtmp_decoder) { diff --git a/src/Rtmp/RtmpDemuxer.h b/src/Rtmp/RtmpDemuxer.h index 253cad69..f339ced3 100644 --- a/src/Rtmp/RtmpDemuxer.h +++ b/src/Rtmp/RtmpDemuxer.h @@ -39,8 +39,8 @@ public: void inputRtmp(const RtmpPacket::Ptr &pkt); private: - void makeVideoTrack(const AMFValue &val); - void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit); + void makeVideoTrack(const AMFValue &val, int bit_rate); + void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate); private: bool _try_get_video_track = false; diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 8d309654..483ff1ba 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -119,7 +119,8 @@ public: * @param pkt rtmp包 */ void onWrite(RtmpPacket::Ptr pkt, bool = true) override { - _speed += pkt->size(); + 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; @@ -153,7 +154,6 @@ public: } } bool key = pkt->isVideoKeyFrame(); - bool is_video = pkt->type_id == MSG_VIDEO; auto stamp = pkt->time_stamp; PacketCache::inputPacket(stamp, is_video, std::move(pkt), key); } diff --git a/src/Rtsp/RtspDemuxer.cpp b/src/Rtsp/RtspDemuxer.cpp index 2c406b2a..cee1aab4 100644 --- a/src/Rtsp/RtspDemuxer.cpp +++ b/src/Rtsp/RtspDemuxer.cpp @@ -63,11 +63,21 @@ bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) { } } +static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track){ + if (!sdp->_b.empty()) { + int data_rate = 0; + sscanf(sdp->_b.data(), "AS:%d", &data_rate); + if (data_rate) { + track->setBitRate(data_rate * 1024); + } + } +} void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) { //生成Track对象 _audioTrack = dynamic_pointer_cast(Factory::getTrackBySdp(audio)); if(_audioTrack){ + setBitRate(audio, _audioTrack); //生成RtpCodec对象以便解码rtp _audioRtpDecoder = Factory::getRtpDecoderByTrack(_audioTrack); if(_audioRtpDecoder){ @@ -85,6 +95,7 @@ void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) { //生成Track对象 _videoTrack = dynamic_pointer_cast(Factory::getTrackBySdp(video)); if(_videoTrack){ + setBitRate(video, _videoTrack); //生成RtpCodec对象以便解码rtp _videoRtpDecoder = Factory::getRtpDecoderByTrack(_videoTrack); if(_videoRtpDecoder){ diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index 119a3e4c..77b5d97b 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -157,7 +157,7 @@ public: * @param keyPos 该包是否为关键帧的第一个包 */ void onWrite(RtpPacket::Ptr rtp, bool keyPos) override { - _speed += rtp->size(); + _speed[rtp->type] += rtp->size(); assert(rtp->type >= 0 && rtp->type < TrackMax); auto &track = _tracks[rtp->type]; if (track) { diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index f13ad1c1..10e6fe77 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -65,7 +65,7 @@ public: * @param key 是否为关键帧第一个包 */ void onWrite(TSPacket::Ptr packet, bool key) override { - _speed += packet->size(); + _speed[TrackVideo] += packet->size(); if (!_ring) { createRing(); } From 55cf411c1e8ff4fc38dbc9b4b2f07404059c83c7 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 5 Dec 2020 12:22:17 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=94=AF=E6=8C=81bitrate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 7 +++++-- src/Common/MediaSource.h | 4 ++-- src/Extension/AAC.cpp | 2 +- src/Extension/AAC.h | 8 +++++--- src/Extension/G711.cpp | 2 +- src/Extension/G711.h | 7 +++++-- src/Extension/H264.cpp | 2 +- src/Extension/H264.h | 8 +++++--- src/Extension/H265.cpp | 2 +- src/Extension/H265.h | 8 +++++--- src/Extension/Opus.cpp | 2 +- src/Extension/Opus.h | 7 +++++-- src/Extension/Track.h | 28 ++++++++++++++++++---------- src/FMP4/FMP4MediaSource.h | 2 +- src/Record/HlsMediaSource.h | 2 +- src/Rtmp/Rtmp.cpp | 12 ++++++++---- src/Rtmp/Rtmp.h | 4 ++-- src/Rtmp/RtmpDemuxer.cpp | 24 ++++++++++++++++++------ src/Rtmp/RtmpDemuxer.h | 4 ++-- src/Rtmp/RtmpMediaSource.h | 4 ++-- src/Rtsp/RtspDemuxer.cpp | 11 +++++++++++ src/Rtsp/RtspMediaSource.h | 2 +- src/TS/TSMediaSource.h | 2 +- 23 files changed, 102 insertions(+), 52 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 51bd7efb..afe42ff7 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -68,8 +68,11 @@ const string& MediaSource::getId() const { return _stream_id; } -int MediaSource::getBytesSpeed(){ - return _speed.getSpeed(); +int MediaSource::getBytesSpeed(TrackType type){ + if(type == TrackInvalid){ + return _speed[TrackVideo].getSpeed() + _speed[TrackAudio].getSpeed(); + } + return _speed[type].getSpeed(); } uint64_t MediaSource::getCreateStamp() const { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 5df31f50..965e1e57 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -220,7 +220,7 @@ public: virtual void setTimeStamp(uint32_t stamp) {}; // 获取数据速率,单位bytes/s - int getBytesSpeed(); + int getBytesSpeed(TrackType type = TrackInvalid); // 获取流创建GMT unix时间戳,单位秒 uint64_t getCreateStamp() const; // 获取流上线时间,单位秒 @@ -286,7 +286,7 @@ private: void emitEvent(bool regist); protected: - BytesSpeed _speed; + BytesSpeed _speed[TrackMax]; private: time_t _create_stamp; diff --git a/src/Extension/AAC.cpp b/src/Extension/AAC.cpp index 1a820e42..30b4cf2f 100644 --- a/src/Extension/AAC.cpp +++ b/src/Extension/AAC.cpp @@ -185,7 +185,7 @@ Sdp::Ptr AACTrack::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getAacCfg(),getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getAacCfg(), getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit \ No newline at end of file diff --git a/src/Extension/AAC.h b/src/Extension/AAC.h index 243f919f..0dd5ecaf 100644 --- a/src/Extension/AAC.h +++ b/src/Extension/AAC.h @@ -177,10 +177,12 @@ public: AACSdp(const string &aac_cfg, int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " MPEG4-GENERIC/" << sample_rate << "/" << channels << "\r\n"; string configStr; diff --git a/src/Extension/G711.cpp b/src/Extension/G711.cpp index 15c3d74e..9943e97f 100644 --- a/src/Extension/G711.cpp +++ b/src/Extension/G711.cpp @@ -17,7 +17,7 @@ Sdp::Ptr G711Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getCodecId(), getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getCodecId(), getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/G711.h b/src/Extension/G711.h index 72273831..f7d82ee7 100644 --- a/src/Extension/G711.h +++ b/src/Extension/G711.h @@ -48,9 +48,12 @@ public: G711Sdp(CodecId codecId, int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type), _codecId(codecId){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type), _codecId(codecId){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << sample_rate << "/" << channels << "\r\n"; _printer << "a=control:trackID=" << (int)TrackAudio << "\r\n"; } diff --git a/src/Extension/H264.cpp b/src/Extension/H264.cpp index 910e79e2..84fc3e1c 100644 --- a/src/Extension/H264.cpp +++ b/src/Extension/H264.cpp @@ -123,7 +123,7 @@ Sdp::Ptr H264Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getSps(),getPps()); + return std::make_shared(getSps(), getPps(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/H264.h b/src/Extension/H264.h index 02eba813..589701cf 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -292,11 +292,13 @@ public: */ H264Sdp(const string &strSPS, const string &strPPS, - int payload_type = 96, - int bitrate = 4000) : Sdp(90000,payload_type) { + int bitrate = 4000, + int payload_type = 96) : Sdp(90000,payload_type) { //视频通道 _printer << "m=video 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " H264/" << 90000 << "\r\n"; _printer << "a=fmtp:" << payload_type << " packetization-mode=1; profile-level-id="; diff --git a/src/Extension/H265.cpp b/src/Extension/H265.cpp index f46a756d..0283f33c 100644 --- a/src/Extension/H265.cpp +++ b/src/Extension/H265.cpp @@ -55,7 +55,7 @@ Sdp::Ptr H265Track::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getVps(),getSps(),getPps()); + return std::make_shared(getVps(), getSps(), getPps(), getBitRate() / 1024); } }//namespace mediakit diff --git a/src/Extension/H265.h b/src/Extension/H265.h index 4fc3d82e..a4014c2f 100644 --- a/src/Extension/H265.h +++ b/src/Extension/H265.h @@ -321,11 +321,13 @@ public: H265Sdp(const string &strVPS, const string &strSPS, const string &strPPS, - int payload_type = 96, - int bitrate = 4000) : Sdp(90000,payload_type) { + int bitrate = 4000, + int payload_type = 96) : Sdp(90000,payload_type) { //视频通道 _printer << "m=video 0 RTP/AVP " << payload_type << "\r\n"; - _printer << "b=AS:" << bitrate << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " H265/" << 90000 << "\r\n"; _printer << "a=fmtp:" << payload_type << " "; _printer << "sprop-vps="; diff --git a/src/Extension/Opus.cpp b/src/Extension/Opus.cpp index e9dea37d..ada06174 100644 --- a/src/Extension/Opus.cpp +++ b/src/Extension/Opus.cpp @@ -17,7 +17,7 @@ Sdp::Ptr OpusTrack::getSdp() { WarnL << getCodecName() << " Track未准备好"; return nullptr; } - return std::make_shared(getAudioSampleRate(), getAudioChannel()); + return std::make_shared(getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024); } }//namespace mediakit \ No newline at end of file diff --git a/src/Extension/Opus.h b/src/Extension/Opus.h index 144afb77..5402d4a9 100644 --- a/src/Extension/Opus.h +++ b/src/Extension/Opus.h @@ -46,9 +46,12 @@ public: */ OpusSdp(int sample_rate, int channels, - int payload_type = 98, - int bitrate = 128) : Sdp(sample_rate,payload_type){ + int bitrate = 128, + int payload_type = 98) : Sdp(sample_rate,payload_type){ _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n"; + if (bitrate) { + _printer << "b=AS:" << bitrate << "\r\n"; + } _printer << "a=rtpmap:" << payload_type << " opus/" << sample_rate << "/" << channels << "\r\n"; _printer << "a=control:trackID=" << (int)TrackAudio << "\r\n"; } diff --git a/src/Extension/Track.h b/src/Extension/Track.h index 788af78f..70f02ead 100644 --- a/src/Extension/Track.h +++ b/src/Extension/Track.h @@ -32,7 +32,6 @@ public: /** * 是否准备好,准备好才能获取譬如sps pps等信息 - * @return */ virtual bool ready() = 0; @@ -40,7 +39,6 @@ public: * 克隆接口,用于复制本对象用 * 在调用该接口时只会复制派生类的信息 * 环形缓存和代理关系不能拷贝,否则会关系紊乱 - * @return */ virtual Track::Ptr clone() = 0; @@ -50,12 +48,28 @@ public: */ virtual Sdp::Ptr getSdp() = 0; + /** + * 返回比特率 + * @return 比特率 + */ + virtual int getBitRate() const { return _bit_rate; } + + /** + * 设置比特率 + * @param bit_rate 比特率 + */ + virtual void setBitRate(int bit_rate) { _bit_rate = bit_rate; } + /** * 复制拷贝,只能拷贝派生类的信息, * 环形缓存和代理关系不能拷贝,否则会关系紊乱 - * @param that */ - Track(const Track &that){} + Track(const Track &that){ + _bit_rate = that._bit_rate; + } + +private: + int _bit_rate = 0; }; /** @@ -67,19 +81,16 @@ public: /** * 返回视频高度 - * @return */ virtual int getVideoHeight() const {return 0;}; /** * 返回视频宽度 - * @return */ virtual int getVideoWidth() const {return 0;}; /** * 返回视频fps - * @return */ virtual float getVideoFps() const {return 0;}; }; @@ -93,19 +104,16 @@ public: /** * 返回音频采样率 - * @return */ virtual int getAudioSampleRate() const {return 0;}; /** * 返回音频采样位数,一般为16或8 - * @return */ virtual int getAudioSampleBit() const {return 0;}; /** * 返回音频通道数 - * @return */ virtual int getAudioChannel() const {return 0;}; }; diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index ef4af868..16fb2ab2 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -88,7 +88,7 @@ public: if (key) { _have_video = true; } - _speed += packet->size(); + _speed[TrackVideo] += packet->size(); auto stamp = packet->time_stamp; PacketCache::inputPacket(stamp, true, std::move(packet), key); } diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index 32e46cc5..aa624b83 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -80,7 +80,7 @@ public: } void onSegmentSize(uint64_t bytes) { - _speed += bytes; + _speed[TrackVideo] += bytes; } private: diff --git a/src/Rtmp/Rtmp.cpp b/src/Rtmp/Rtmp.cpp index b7046163..0c80692c 100644 --- a/src/Rtmp/Rtmp.cpp +++ b/src/Rtmp/Rtmp.cpp @@ -12,7 +12,7 @@ #include "Extension/Factory.h" namespace mediakit{ -VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){ +VideoMeta::VideoMeta(const VideoTrack::Ptr &video){ if(video->getVideoWidth() > 0 ){ _metadata.set("width", video->getVideoWidth()); } @@ -22,13 +22,17 @@ VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){ if(video->getVideoFps() > 0 ){ _metadata.set("framerate", video->getVideoFps()); } - _metadata.set("videodatarate", datarate); + if (video->getBitRate()) { + _metadata.set("videodatarate", video->getBitRate() / 1024); + } _codecId = video->getCodecId(); _metadata.set("videocodecid", Factory::getAmfByCodecId(_codecId)); } -AudioMeta::AudioMeta(const AudioTrack::Ptr &audio,int datarate){ - _metadata.set("audiodatarate", datarate); +AudioMeta::AudioMeta(const AudioTrack::Ptr &audio){ + if (audio->getBitRate()) { + _metadata.set("audiodatarate", audio->getBitRate() / 1024); + } if(audio->getAudioSampleRate() > 0){ _metadata.set("audiosamplerate", audio->getAudioSampleRate()); } diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 1f8d1e3e..f9f9790a 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -261,7 +261,7 @@ class VideoMeta : public Metadata{ public: typedef std::shared_ptr Ptr; - VideoMeta(const VideoTrack::Ptr &video,int datarate = 5000); + VideoMeta(const VideoTrack::Ptr &video); virtual ~VideoMeta(){} CodecId getCodecId() const override{ @@ -275,7 +275,7 @@ class AudioMeta : public Metadata{ public: typedef std::shared_ptr Ptr; - AudioMeta(const AudioTrack::Ptr &audio,int datarate = 160); + AudioMeta(const AudioTrack::Ptr &audio); virtual ~AudioMeta(){} diff --git a/src/Rtmp/RtmpDemuxer.cpp b/src/Rtmp/RtmpDemuxer.cpp index 53ce4c87..5225ce32 100644 --- a/src/Rtmp/RtmpDemuxer.cpp +++ b/src/Rtmp/RtmpDemuxer.cpp @@ -19,6 +19,8 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){ int audiosamplerate = 0; int audiochannels = 0; int audiosamplesize = 0; + int videodatarate = 0; + int audiodatarate = 0; const AMFValue *audiocodecid = nullptr; const AMFValue *videocodecid = nullptr; val.object_for_each([&](const string &key, const AMFValue &val) { @@ -48,16 +50,24 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){ audiocodecid = &val; return; } + if (key == "audiodatarate") { + audiodatarate = val.as_integer(); + return; + } + if (key == "videodatarate") { + videodatarate = val.as_integer(); + return; + } }); if (videocodecid) { //有视频 ret = true; - makeVideoTrack(*videocodecid); + makeVideoTrack(*videocodecid, videodatarate * 1024); } if (audiocodecid) { //有音频 ret = true; - makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize); + makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize, audiodatarate * 1024); } } catch (std::exception &ex) { WarnL << ex.what(); @@ -71,7 +81,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { if (!_try_get_video_track) { _try_get_video_track = true; auto codec = AMFValue(pkt->getMediaType()); - makeVideoTrack(codec); + makeVideoTrack(codec, 0); } if (_video_rtmp_decoder) { _video_rtmp_decoder->inputRtmp(pkt); @@ -83,7 +93,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { if (!_try_get_audio_track) { _try_get_audio_track = true; auto codec = AMFValue(pkt->getMediaType()); - makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit()); + makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit(), 0); } if (_audio_rtmp_decoder) { _audio_rtmp_decoder->inputRtmp(pkt); @@ -94,10 +104,11 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { } } -void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) { +void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) { //生成Track对象 _videoTrack = dynamic_pointer_cast(Factory::getVideoTrackByAmf(videoCodec)); if (_videoTrack) { + _videoTrack->setBitRate(bit_rate); //生成rtmpCodec对象以便解码rtmp _video_rtmp_decoder = Factory::getRtmpCodecByTrack(_videoTrack, false); if (_video_rtmp_decoder) { @@ -112,10 +123,11 @@ void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) { } } -void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit) { +void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) { //生成Track对象 _audioTrack = dynamic_pointer_cast(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit)); if (_audioTrack) { + _audioTrack->setBitRate(bit_rate); //生成rtmpCodec对象以便解码rtmp _audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audioTrack, false); if (_audio_rtmp_decoder) { diff --git a/src/Rtmp/RtmpDemuxer.h b/src/Rtmp/RtmpDemuxer.h index 253cad69..f339ced3 100644 --- a/src/Rtmp/RtmpDemuxer.h +++ b/src/Rtmp/RtmpDemuxer.h @@ -39,8 +39,8 @@ public: void inputRtmp(const RtmpPacket::Ptr &pkt); private: - void makeVideoTrack(const AMFValue &val); - void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit); + void makeVideoTrack(const AMFValue &val, int bit_rate); + void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate); private: bool _try_get_video_track = false; diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 8d309654..483ff1ba 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -119,7 +119,8 @@ public: * @param pkt rtmp包 */ void onWrite(RtmpPacket::Ptr pkt, bool = true) override { - _speed += pkt->size(); + 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; @@ -153,7 +154,6 @@ public: } } bool key = pkt->isVideoKeyFrame(); - bool is_video = pkt->type_id == MSG_VIDEO; auto stamp = pkt->time_stamp; PacketCache::inputPacket(stamp, is_video, std::move(pkt), key); } diff --git a/src/Rtsp/RtspDemuxer.cpp b/src/Rtsp/RtspDemuxer.cpp index 2c406b2a..cee1aab4 100644 --- a/src/Rtsp/RtspDemuxer.cpp +++ b/src/Rtsp/RtspDemuxer.cpp @@ -63,11 +63,21 @@ bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) { } } +static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track){ + if (!sdp->_b.empty()) { + int data_rate = 0; + sscanf(sdp->_b.data(), "AS:%d", &data_rate); + if (data_rate) { + track->setBitRate(data_rate * 1024); + } + } +} void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) { //生成Track对象 _audioTrack = dynamic_pointer_cast(Factory::getTrackBySdp(audio)); if(_audioTrack){ + setBitRate(audio, _audioTrack); //生成RtpCodec对象以便解码rtp _audioRtpDecoder = Factory::getRtpDecoderByTrack(_audioTrack); if(_audioRtpDecoder){ @@ -85,6 +95,7 @@ void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) { //生成Track对象 _videoTrack = dynamic_pointer_cast(Factory::getTrackBySdp(video)); if(_videoTrack){ + setBitRate(video, _videoTrack); //生成RtpCodec对象以便解码rtp _videoRtpDecoder = Factory::getRtpDecoderByTrack(_videoTrack); if(_videoRtpDecoder){ diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index 119a3e4c..77b5d97b 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -157,7 +157,7 @@ public: * @param keyPos 该包是否为关键帧的第一个包 */ void onWrite(RtpPacket::Ptr rtp, bool keyPos) override { - _speed += rtp->size(); + _speed[rtp->type] += rtp->size(); assert(rtp->type >= 0 && rtp->type < TrackMax); auto &track = _tracks[rtp->type]; if (track) { diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index f13ad1c1..10e6fe77 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -65,7 +65,7 @@ public: * @param key 是否为关键帧第一个包 */ void onWrite(TSPacket::Ptr packet, bool key) override { - _speed += packet->size(); + _speed[TrackVideo] += packet->size(); if (!_ring) { createRing(); } From 3f37ef618dad01710f1ce0720c2481b92a01257a Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 5 Dec 2020 12:25:52 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=AA=E5=B0=8F=E7=9A=84=E5=BD=95=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Recorder.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index b9922b17..431ed8ca 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -11,6 +11,7 @@ #ifdef ENABLE_MP4 #include #include +#include "Util/File.h" #include "Common/config.h" #include "MP4Recorder.h" #include "Thread/WorkThreadPool.h" @@ -77,12 +78,19 @@ void MP4Recorder::asyncClose() { const_cast(info).time_len = ::time(NULL) - info.start_time; //关闭mp4非常耗时,所以要放在后台线程执行 muxer->closeMP4(); - //临时文件名改成正式文件名,防止mp4未完成时被访问 - rename(strFileTmp.data(),strFile.data()); + //获取文件大小 struct stat fileData; - stat(strFile.data(), &fileData); - const_cast(info).file_size = fileData.st_size; + stat(strFileTmp.data(), &fileData); + const_cast(info).file_size = fileData.st_size; + if (fileData.st_size < 1024) { + //录像文件太小,删除之 + File::delete_file(strFileTmp.data()); + return; + } + //临时文件名改成正式文件名,防止mp4未完成时被访问 + rename(strFileTmp.data(),strFile.data()); + /////record 业务逻辑////// NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info); }); From b90ebf6511fe20ba4d990aa7d7a689e00b4018c0 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 5 Dec 2020 12:31:28 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=90=88=E5=B9=B6pr:#607?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpTSPlayer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/HttpTSPlayer.h b/src/Http/HttpTSPlayer.h index 6b12a453..7dc985f0 100644 --- a/src/Http/HttpTSPlayer.h +++ b/src/Http/HttpTSPlayer.h @@ -48,6 +48,7 @@ private: bool _is_first_packet_ts = false; //是否判断是否是ts并split bool _split_ts; + string _status; TSSegment _segment; onShutdown _on_disconnect; TSSegment::onSegment _on_segment; From dc3701e6ccbca7845cee40e7802c05a9c18b395c Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sun, 6 Dec 2020 21:08:16 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtp=E5=AD=98=E5=9C=A8ext?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=B3=95=E8=A7=A3=E6=9E=90=E7=9A=84bug:#585?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtsp/RtpReceiver.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Rtsp/RtpReceiver.cpp b/src/Rtsp/RtpReceiver.cpp index 3ae03e01..75d5b45e 100644 --- a/src/Rtsp/RtpReceiver.cpp +++ b/src/Rtsp/RtpReceiver.cpp @@ -101,16 +101,18 @@ bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate, //ssrc匹配正确,不匹配计数清零 _ssrc_err_count[track_index] = 0; - //获取rtp中媒体数据偏移量 - rtp.offset = 12 + 4; + //rtp 12个固定字节头 + rtp.offset = 12; + //rtp有csrc rtp.offset += 4 * csrc; - if (ext && rtp_raw_len >= rtp.offset) { - /* calculate the header extension length (stored as number of 32-bit words) */ - ext = (AV_RB16(rtp_raw_ptr + rtp.offset - 2) + 1) << 2; - rtp.offset += ext; + if (ext) { + //rtp有ext + uint16_t reserved = AV_RB16(rtp_raw_ptr + rtp.offset); + uint16_t extlen = AV_RB16(rtp_raw_ptr + rtp.offset + 2) << 2; + rtp.offset += extlen + 4; } - if (rtp_raw_len + 4 <= rtp.offset) { + if (rtp_raw_len <= rtp.offset) { WarnL << "无有效负载的rtp包:" << rtp_raw_len << " <= " << (int) rtp.offset; return false; } @@ -128,9 +130,10 @@ bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate, payload_ptr[1] = rtp.interleaved; payload_ptr[2] = rtp_raw_len >> 8; payload_ptr[3] = (rtp_raw_len & 0x00FF); + //添加rtp over tcp前4个字节的偏移量 + rtp.offset += 4; //拷贝rtp负载 memcpy(payload_ptr + 4, rtp_raw_ptr, rtp_raw_len); - //排序rtp auto seq = rtp_ptr->sequence; _rtp_sortor[track_index].sortPacket(seq, std::move(rtp_ptr));