Skip to content

Commit

Permalink
Organized source codes related to RTMP Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Nov 14, 2024
1 parent 7ed8718 commit 1c0f795
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
15 changes: 15 additions & 0 deletions src/projects/base/ovlibrary/bit_reader.cpp
Original file line number Diff line number Diff line change
@@ -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<const ov::Data> &data)
: _buffer(data->GetDataAs<uint8_t>()),
_position(data->GetDataAs<uint8_t>()),
_capacity(data->GetLength())
{
}
21 changes: 5 additions & 16 deletions src/projects/base/ovlibrary/bit_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const ov::Data> &data);

template <typename T>
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;
}
Expand Down Expand Up @@ -69,14 +65,7 @@ class BitReader
return false;
}

if (big_endian == true)
{
value = ByteReader<T>::ReadBigEndian(_position);
}
else
{
value = ByteReader<T>::ReadLittleEndian(_position);
}
value = big_endian ? ByteReader<T>::ReadBigEndian(_position) : ByteReader<T>::ReadLittleEndian(_position);

_position += sizeof(value);

Expand Down
48 changes: 24 additions & 24 deletions src/projects/modules/containers/flv/flv_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#include "flv_parser.h"

#include <base/ovlibrary/ovlibrary.h>
#include <base/ovlibrary/bit_reader.h>
#include <base/ovlibrary/ovlibrary.h>

#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<const ov::Data>& 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<FlvVideoFrameTypes>(parser.ReadBits<uint8_t>(4));
video_data._codec_id = static_cast<FlvVideoCodecId>(parser.ReadBits<uint8_t>(4));
_frame_type = static_cast<FlvVideoFrameTypes>(parser.ReadBits<uint8_t>(4));
_codec_id = static_cast<FlvVideoCodecId>(parser.ReadBits<uint8_t>(4));

if(video_data._codec_id != FlvVideoCodecId::AVC)
if (_codec_id != FlvVideoCodecId::AVC)
{
logte("Unsupported codec : %d", static_cast<uint8_t>(video_data._codec_id));
logte("Unsupported codec : %d", static_cast<uint8_t>(_codec_id));
return false;
}

video_data._packet_type = static_cast<FlvAvcPacketType>(parser.ReadBytes<uint8_t>());
_packet_type = static_cast<FlvAvcPacketType>(parser.ReadBytes<uint8_t>());

int32_t composition_time = static_cast<int32_t>(parser.ReadBits<uint32_t>(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;
}
Expand Down Expand Up @@ -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<const ov::Data>& 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<FlvSoundFormat>(parser.ReadBits<uint8_t>(4));
audio_data._sample_rate = static_cast<FlvSoundRate>(parser.ReadBits<uint8_t>(2));
audio_data._sample_size = static_cast<FlvSoundSize>(parser.ReadBit());
audio_data._channel = static_cast<FlvSoundType>(parser.ReadBit());
audio_data._packet_type = static_cast<FlvAACPacketType>(parser.ReadBytes<uint8_t>());
_format = static_cast<FlvSoundFormat>(parser.ReadBits<uint8_t>(4));
_sample_rate = static_cast<FlvSoundRate>(parser.ReadBits<uint8_t>(2));
_sample_size = static_cast<FlvSoundSize>(parser.ReadBit());
_channel = static_cast<FlvSoundType>(parser.ReadBit());
_packet_type = static_cast<FlvAACPacketType>(parser.ReadBytes<uint8_t>());

audio_data._payload = parser.CurrentPosition();
audio_data._payload_length = parser.BytesRemained();
_payload = parser.CurrentPosition();
_payload_length = parser.BytesRemained();

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/projects/modules/containers/flv/flv_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const ov::Data> &data);

FlvVideoFrameTypes FrameType();
FlvVideoCodecId CodecId();
Expand Down Expand Up @@ -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<const ov::Data> &data);

FlvSoundFormat Format();
FlvSoundRate SampleRate();
Expand Down
20 changes: 10 additions & 10 deletions src/projects/providers/rtmp/rtmp_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ namespace pvd
{
// Parsing FLV
FlvVideoData flv_video;
if (FlvVideoData::Parse(message->payload->GetDataAs<uint8_t>(), 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;
Expand Down Expand Up @@ -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<uint8_t>(), 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;
Expand All @@ -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);
Expand Down

0 comments on commit 1c0f795

Please sign in to comment.