From 1c0f795aff8416461073154b65b5bf20eb99b0e0 Mon Sep 17 00:00:00 2001 From: Dimiden Date: Thu, 14 Nov 2024 21:17:24 +0900 Subject: [PATCH] Organized source codes related to RTMP Provider --- src/projects/base/ovlibrary/bit_reader.cpp | 15 ++++++ src/projects/base/ovlibrary/bit_reader.h | 21 ++------ .../modules/containers/flv/flv_parser.cpp | 48 +++++++++---------- .../modules/containers/flv/flv_parser.h | 4 +- src/projects/providers/rtmp/rtmp_stream.cpp | 20 ++++---- 5 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 src/projects/base/ovlibrary/bit_reader.cpp diff --git a/src/projects/base/ovlibrary/bit_reader.cpp b/src/projects/base/ovlibrary/bit_reader.cpp new file mode 100644 index 000000000..cd3be5711 --- /dev/null +++ b/src/projects/base/ovlibrary/bit_reader.cpp @@ -0,0 +1,15 @@ +#include "bit_reader.h" + +BitReader::BitReader(const uint8_t *buffer, size_t capacity) + : _buffer(buffer), + _position(buffer), + _capacity(capacity) +{ +} + +BitReader::BitReader(const std::shared_ptr &data) + : _buffer(data->GetDataAs()), + _position(data->GetDataAs()), + _capacity(data->GetLength()) +{ +} diff --git a/src/projects/base/ovlibrary/bit_reader.h b/src/projects/base/ovlibrary/bit_reader.h index 382d73d02..651b8696d 100644 --- a/src/projects/base/ovlibrary/bit_reader.h +++ b/src/projects/base/ovlibrary/bit_reader.h @@ -12,19 +12,15 @@ class BitReader { public: - BitReader(const uint8_t *buffer, size_t capacity) - : _buffer(buffer), - _position(buffer), - _capacity(capacity) - { - } + BitReader(const uint8_t *buffer, size_t capacity); + BitReader(const std::shared_ptr &data); template T ReadBytes(bool big_endian = true) { T value; - bool result = ReadBytes(value, big_endian); - if (result == false) + + if (ReadBytes(value, big_endian) == false) { return 0; } @@ -69,14 +65,7 @@ class BitReader return false; } - if (big_endian == true) - { - value = ByteReader::ReadBigEndian(_position); - } - else - { - value = ByteReader::ReadLittleEndian(_position); - } + value = big_endian ? ByteReader::ReadBigEndian(_position) : ByteReader::ReadLittleEndian(_position); _position += sizeof(value); diff --git a/src/projects/modules/containers/flv/flv_parser.cpp b/src/projects/modules/containers/flv/flv_parser.cpp index a0cbb845d..05f244994 100644 --- a/src/projects/modules/containers/flv/flv_parser.cpp +++ b/src/projects/modules/containers/flv/flv_parser.cpp @@ -1,37 +1,37 @@ #include "flv_parser.h" -#include #include +#include #define OV_LOG_TAG "FlvParser" -bool FlvVideoData::Parse(const uint8_t *data, size_t data_length, FlvVideoData &video_data) +bool FlvVideoData::Parse(const std::shared_ptr& data) { - if(data_length < MIN_FLV_VIDEO_DATA_LENGTH) + if (data->GetLength() < MIN_FLV_VIDEO_DATA_LENGTH) { - logte("The data inputted is too small for parsing (%d must be bigger than %d)", data_length, MIN_FLV_VIDEO_DATA_LENGTH); + logte("The data inputted is too small for parsing (%zu must be bigger than %d)", data->GetLength(), MIN_FLV_VIDEO_DATA_LENGTH); return false; } - BitReader parser(data, data_length); + BitReader parser(data); - video_data._frame_type = static_cast(parser.ReadBits(4)); - video_data._codec_id = static_cast(parser.ReadBits(4)); + _frame_type = static_cast(parser.ReadBits(4)); + _codec_id = static_cast(parser.ReadBits(4)); - if(video_data._codec_id != FlvVideoCodecId::AVC) + if (_codec_id != FlvVideoCodecId::AVC) { - logte("Unsupported codec : %d", static_cast(video_data._codec_id)); + logte("Unsupported codec : %d", static_cast(_codec_id)); return false; } - video_data._packet_type = static_cast(parser.ReadBytes()); + _packet_type = static_cast(parser.ReadBytes()); int32_t composition_time = static_cast(parser.ReadBits(24)); // Need to convert UI24 to SI24 - video_data._composition_time = OV_CHECK_FLAG(composition_time, 0x800000) ? composition_time |= 0xFF000000 : composition_time; - video_data._payload = parser.CurrentPosition(); - video_data._payload_length = parser.BytesRemained(); + _composition_time = OV_CHECK_FLAG(composition_time, 0x800000) ? composition_time |= 0xFF000000 : composition_time; + _payload = parser.CurrentPosition(); + _payload_length = parser.BytesRemained(); return true; } @@ -65,24 +65,24 @@ size_t FlvVideoData::PayloadLength() return _payload_length; } -bool FlvAudioData::Parse(const uint8_t *data, size_t data_length, FlvAudioData &audio_data) +bool FlvAudioData::Parse(const std::shared_ptr& data) { - if(data_length < MIN_FLV_AUDIO_DATA_LENGTH) + if (data->GetLength() < MIN_FLV_AUDIO_DATA_LENGTH) { - logte("The data inputted is too small for parsing (%d must be bigger than %d)", data_length, MIN_FLV_AUDIO_DATA_LENGTH); + logte("The data inputted is too small for parsing (%zu must be bigger than %d)", data->GetLength(), MIN_FLV_AUDIO_DATA_LENGTH); return false; } - BitReader parser(data, data_length); + BitReader parser(data); - audio_data._format = static_cast(parser.ReadBits(4)); - audio_data._sample_rate = static_cast(parser.ReadBits(2)); - audio_data._sample_size = static_cast(parser.ReadBit()); - audio_data._channel = static_cast(parser.ReadBit()); - audio_data._packet_type = static_cast(parser.ReadBytes()); + _format = static_cast(parser.ReadBits(4)); + _sample_rate = static_cast(parser.ReadBits(2)); + _sample_size = static_cast(parser.ReadBit()); + _channel = static_cast(parser.ReadBit()); + _packet_type = static_cast(parser.ReadBytes()); - audio_data._payload = parser.CurrentPosition(); - audio_data._payload_length = parser.BytesRemained(); + _payload = parser.CurrentPosition(); + _payload_length = parser.BytesRemained(); return true; } diff --git a/src/projects/modules/containers/flv/flv_parser.h b/src/projects/modules/containers/flv/flv_parser.h index 139ca791c..a9d2966c9 100644 --- a/src/projects/modules/containers/flv/flv_parser.h +++ b/src/projects/modules/containers/flv/flv_parser.h @@ -80,7 +80,7 @@ enum class FlvAACPacketType : uint8_t class FlvVideoData { public: - static bool Parse(const uint8_t *data, size_t data_length, FlvVideoData &video_data); + bool Parse(const std::shared_ptr &data); FlvVideoFrameTypes FrameType(); FlvVideoCodecId CodecId(); @@ -112,7 +112,7 @@ class FlvVideoData class FlvAudioData { public: - static bool Parse(const uint8_t *data, size_t data_length, FlvAudioData &audio_data); + bool Parse(const std::shared_ptr &data); FlvSoundFormat Format(); FlvSoundRate SampleRate(); diff --git a/src/projects/providers/rtmp/rtmp_stream.cpp b/src/projects/providers/rtmp/rtmp_stream.cpp index ad26138c3..2dab5b2a1 100644 --- a/src/projects/providers/rtmp/rtmp_stream.cpp +++ b/src/projects/providers/rtmp/rtmp_stream.cpp @@ -1593,7 +1593,7 @@ namespace pvd { // Parsing FLV FlvVideoData flv_video; - if (FlvVideoData::Parse(message->payload->GetDataAs(), message->payload->GetLength(), flv_video) == false) + if (flv_video.Parse(message->payload) == false) { logte("Could not parse flv video (%s/%s)", _vhost_app_name.CStr(), GetName().CStr()); return false; @@ -1791,9 +1791,17 @@ namespace pvd // audio stream callback if (_media_info->audio_stream_coming) { + // Get audio track info + auto audio_track = GetTrack(RTMP_AUDIO_TRACK_ID); + if (audio_track == nullptr) + { + logte("Cannot get audio track (%s/%s)", _vhost_app_name.CStr(), GetName().CStr()); + return false; + } + // Parsing FLV FlvAudioData flv_audio; - if (FlvAudioData::Parse(message->payload->GetDataAs(), message->payload->GetLength(), flv_audio) == false) + if (flv_audio.Parse(message->payload) == false) { logte("Could not parse flv audio (%s/%s)", _vhost_app_name.CStr(), GetName().CStr()); return false; @@ -1820,14 +1828,6 @@ namespace pvd pts += ADJUST_PTS; } - // Get audio track info - auto audio_track = GetTrack(RTMP_AUDIO_TRACK_ID); - if (audio_track == nullptr) - { - logte("Cannot get video track (%s/%s)", _vhost_app_name.CStr(), GetName().CStr()); - return false; - } - if (_is_incoming_timestamp_used == false) { AdjustTimestamp(pts, dts);