Skip to content

Commit

Permalink
Organized source codes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Oct 14, 2024
1 parent 061d02d commit 99cbf68
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 93 deletions.
4 changes: 2 additions & 2 deletions src/projects/providers/rtmp/chunk/rtmp_export_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ std::shared_ptr<ExportStream> RtmpExportChunk::GetStream(uint32_t chunk_stream_i
// - Type3는 확장 헤더 확인 못함 - 이전 정보를 기반으로 이후에 확인
// - 압축 헤더 : 프레임이 정상 적으로 않나오는 상황에서 헤더 압축 사용하면 타임스템프 전달이 정상적으로 않됨
//====================================================================================================
std::shared_ptr<RtmpChunkHeader> RtmpExportChunk::GetChunkHeader(std::shared_ptr<ExportStream> &stream, const std::shared_ptr<RtmpMuxMessageHeader> &message_header)
std::shared_ptr<RtmpChunkHeader> RtmpExportChunk::GetChunkHeader(std::shared_ptr<ExportStream> &stream, const std::shared_ptr<const RtmpMuxMessageHeader> &message_header)
{
auto chunk_header = std::make_shared<RtmpChunkHeader>();

Expand Down Expand Up @@ -119,7 +119,7 @@ std::shared_ptr<RtmpChunkHeader> RtmpExportChunk::GetChunkHeader(std::shared_ptr
//====================================================================================================
// Export 스트림
//====================================================================================================
std::shared_ptr<std::vector<uint8_t>> RtmpExportChunk::ExportStreamData(std::shared_ptr<RtmpMuxMessageHeader> &message_header, const uint8_t *chunk_data, size_t chunk_size)
std::shared_ptr<std::vector<uint8_t>> RtmpExportChunk::ExportStreamData(const std::shared_ptr<const RtmpMuxMessageHeader> &message_header, const uint8_t *chunk_data, size_t chunk_size)
{
int buffer_size = 0;
int export_size = 0;
Expand Down
10 changes: 3 additions & 7 deletions src/projects/providers/rtmp/chunk/rtmp_export_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public :
timestamp_delta = 0;
}
public :
std::shared_ptr<RtmpMuxMessageHeader> message_header;
std::shared_ptr<const RtmpMuxMessageHeader> message_header;
uint32_t timestamp_delta;
};

Expand All @@ -39,19 +39,15 @@ class RtmpExportChunk : public RtmpMuxUtil
~RtmpExportChunk() override;

public:
std::shared_ptr<std::vector<uint8_t>> ExportStreamData(std::shared_ptr<RtmpMuxMessageHeader> &message_header, const uint8_t *chunk_data, size_t chunk_size);
std::shared_ptr<std::vector<uint8_t>> ExportStreamData(const std::shared_ptr<const RtmpMuxMessageHeader> &message_header, const uint8_t *chunk_data, size_t chunk_size);

private:
void Destroy();
std::shared_ptr<ExportStream> GetStream(uint32_t chunk_stream_id);
std::shared_ptr<RtmpChunkHeader> GetChunkHeader(std::shared_ptr<ExportStream> &stream, const std::shared_ptr<RtmpMuxMessageHeader> &message_header);
std::shared_ptr<RtmpChunkHeader> GetChunkHeader(std::shared_ptr<ExportStream> &stream, const std::shared_ptr<const RtmpMuxMessageHeader> &message_header);

private:
std::map<uint32_t, std::shared_ptr<ExportStream>> _stream_map;
bool _compress_header;
int _chunk_size;
};




53 changes: 21 additions & 32 deletions src/projects/providers/rtmp/chunk/rtmp_mux_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,44 @@
// Copyright (c) 2018 AirenSoft. All rights reserved.
//
//==============================================================================

#pragma once

#include "rtmp_datastructure.h"
#pragma pack(1)
//====================================================================================================
// Mux 메시지 헤더
//====================================================================================================

struct RtmpMuxMessageHeader
{
public:
RtmpMuxMessageHeader()
: chunk_stream_id(0),
timestamp(0),
type_id(RtmpMessageTypeID::UNKNOWN),
stream_id(0),
body_size(0)
RtmpMuxMessageHeader() = default;
RtmpMuxMessageHeader(uint32_t chunk_stream_id, uint32_t timestamp, RtmpMessageTypeID type_id, uint32_t stream_id, uint32_t body_size)
: chunk_stream_id(chunk_stream_id),
timestamp(timestamp),
type_id(type_id),
stream_id(stream_id),
body_size(body_size)
{
}

RtmpMuxMessageHeader(uint32_t chunk_stream_id_,
uint32_t timestamp_,
RtmpMessageTypeID type_id_,
uint32_t stream_id_,
uint32_t body_size_)
static std::shared_ptr<RtmpMuxMessageHeader> Create(
uint32_t chunk_stream_id,
RtmpMessageTypeID type_id = RtmpMessageTypeID::AMF0_COMMAND,
uint32_t stream_id = 0,
uint32_t body_size = 0)
{
this->chunk_stream_id = chunk_stream_id_;
this->timestamp = timestamp_;
this->type_id = type_id_;
this->stream_id = stream_id_;
this->body_size = body_size_;
return std::make_shared<RtmpMuxMessageHeader>(chunk_stream_id, 0, type_id, stream_id, body_size);
}

public:
uint32_t chunk_stream_id;
uint32_t timestamp;
RtmpMessageTypeID type_id;
uint32_t stream_id;
uint32_t body_size;
uint32_t chunk_stream_id = 0;
uint32_t timestamp = 0;
RtmpMessageTypeID type_id = RtmpMessageTypeID::UNKNOWN;
uint32_t stream_id = 0;
uint32_t body_size = 0;
};
#pragma pack()

class RtmpMuxUtil
{
public:
RtmpMuxUtil() = default;
virtual ~RtmpMuxUtil() = default;

public:
static uint8_t ReadInt8(const void *data);
static uint16_t ReadInt16(const void *data);
static uint32_t ReadInt24(const void *data);
Expand All @@ -71,8 +60,8 @@ class RtmpMuxUtil
static int GetBasicHeaderSizeByChunkStreamID(uint32_t chunk_stream_id) noexcept;

static int GetChunkHeaderSize(RtmpMessageHeaderType chunk_type, uint32_t chunk_stream_id, int basic_header_size, void *raw_data, int raw_data_size); // ret:길이, ret<=0:실패
static std::shared_ptr<RtmpChunkHeader> GetChunkHeader(void *raw_data, int raw_data_size, int &chunk_header_size, bool &extend_type); // ret<=0:실패, ret>0:처리길이
static int GetChunkData(int chunk_size, void *raw_data, int raw_data_size, int chunk_data_size, void *chunk_data, bool extend_type); // ret=> 0:실패, 1:처리길이
static std::shared_ptr<RtmpChunkHeader> GetChunkHeader(void *raw_data, int raw_data_size, int &chunk_header_size, bool &extend_type); // ret<=0:실패, ret>0:처리길이
static int GetChunkData(int chunk_size, void *raw_data, int raw_data_size, int chunk_data_size, void *chunk_data, bool extend_type); // ret=> 0:실패, 1:처리길이

static int GetChunkBasicHeaderRaw(RtmpMessageHeaderType chunk_type, uint32_t chunk_stream_id, void *raw_data);
static int GetChunkHeaderRaw(std::shared_ptr<RtmpChunkHeader> &chunk_header, void *raw_data, bool extend_type);
Expand Down
73 changes: 21 additions & 52 deletions src/projects/providers/rtmp/rtmp_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,12 +1135,7 @@ namespace pvd
// ping response == event type (16 bits) + timestamp (32 bits)
auto body = std::make_shared<std::vector<uint8_t>>(2 + 4);
auto write_buffer = body->data();
auto message_header = std::make_shared<RtmpMuxMessageHeader>(
chunk_stream_id,
0,
RtmpMessageTypeID::USER_CONTROL,
message_stream_id,
6);
auto message_header = RtmpMuxMessageHeader::Create(chunk_stream_id, RtmpMessageTypeID::USER_CONTROL, message_stream_id, 6);

*(reinterpret_cast<uint16_t *>(write_buffer)) = ov::HostToBE16(RTMP_UCMID_PINGRESPONSE);
write_buffer += sizeof(uint16_t);
Expand Down Expand Up @@ -2061,16 +2056,14 @@ namespace pvd
bool RtmpStream::SendHandshake(const std::shared_ptr<const ov::Data> &data)
{
uint8_t s0 = 0;
uint8_t s1[RTMP_HANDSHAKE_PACKET_SIZE] = {
0,
};
uint8_t s2[RTMP_HANDSHAKE_PACKET_SIZE] = {
0,
};
uint8_t s1[RTMP_HANDSHAKE_PACKET_SIZE]{};
uint8_t s2[RTMP_HANDSHAKE_PACKET_SIZE]{};

s0 = RTMP_HANDSHAKE_VERSION;

RtmpHandshake::MakeS1(s1);
RtmpHandshake::MakeS2(data->GetDataAs<uint8_t>() + sizeof(uint8_t), s2);

_handshake_state = RtmpHandshakeState::C1;

// OM-1629 - Elemental Encoder
Expand All @@ -2093,11 +2086,8 @@ namespace pvd

bool RtmpStream::SendUserControlMessage(uint16_t message, std::shared_ptr<std::vector<uint8_t>> &data)
{
auto message_header = std::make_shared<RtmpMuxMessageHeader>(RTMP_CHUNK_STREAM_ID_URGENT,
0,
RtmpMessageTypeID::USER_CONTROL,
0,
data->size() + 2);
auto message_header = RtmpMuxMessageHeader::Create(
RTMP_CHUNK_STREAM_ID_URGENT, RtmpMessageTypeID::USER_CONTROL, 0, data->size() + 2);

data->insert(data->begin(), 0);
data->insert(data->begin(), 0);
Expand All @@ -2109,11 +2099,8 @@ namespace pvd
bool RtmpStream::SendWindowAcknowledgementSize(uint32_t size)
{
auto body = std::make_shared<std::vector<uint8_t>>(sizeof(int));
auto message_header = std::make_shared<RtmpMuxMessageHeader>(RTMP_CHUNK_STREAM_ID_URGENT,
0,
RtmpMessageTypeID::WINDOW_ACKNOWLEDGEMENT_SIZE,
_rtmp_stream_id,
body->size());
auto message_header = RtmpMuxMessageHeader::Create(
RTMP_CHUNK_STREAM_ID_URGENT, RtmpMessageTypeID::WINDOW_ACKNOWLEDGEMENT_SIZE, _rtmp_stream_id, body->size());

RtmpMuxUtil::WriteInt32(body->data(), size);

Expand All @@ -2123,11 +2110,8 @@ namespace pvd
bool RtmpStream::SendAcknowledgementSize(uint32_t acknowledgement_traffic)
{
auto body = std::make_shared<std::vector<uint8_t>>(sizeof(int));
auto message_header = std::make_shared<RtmpMuxMessageHeader>(RTMP_CHUNK_STREAM_ID_URGENT,
0,
RtmpMessageTypeID::ACKNOWLEDGEMENT,
0,
body->size());
auto message_header = RtmpMuxMessageHeader::Create(
RTMP_CHUNK_STREAM_ID_URGENT, RtmpMessageTypeID::ACKNOWLEDGEMENT, 0, body->size());

RtmpMuxUtil::WriteInt32(body->data(), acknowledgement_traffic);

Expand All @@ -2137,11 +2121,8 @@ namespace pvd
bool RtmpStream::SendSetPeerBandwidth(uint32_t bandwidth)
{
auto body = std::make_shared<std::vector<uint8_t>>(5);
auto message_header = std::make_shared<RtmpMuxMessageHeader>(RTMP_CHUNK_STREAM_ID_URGENT,
0,
RtmpMessageTypeID::SET_PEER_BANDWIDTH,
_rtmp_stream_id,
body->size());
auto message_header = RtmpMuxMessageHeader::Create(
RTMP_CHUNK_STREAM_ID_URGENT, RtmpMessageTypeID::SET_PEER_BANDWIDTH, _rtmp_stream_id, body->size());

RtmpMuxUtil::WriteInt32(body->data(), bandwidth);
RtmpMuxUtil::WriteInt8(body->data() + 4, 2);
Expand Down Expand Up @@ -2188,11 +2169,8 @@ namespace pvd

bool RtmpStream::SendAmfConnectResult(uint32_t chunk_stream_id, double transaction_id, double object_encoding)
{
auto message_header = std::make_shared<RtmpMuxMessageHeader>(chunk_stream_id,
0,
RtmpMessageTypeID::AMF0_COMMAND,
0,
0);
auto message_header = RtmpMuxMessageHeader::Create(chunk_stream_id);

AmfDocument document;

// _result
Expand Down Expand Up @@ -2229,11 +2207,9 @@ namespace pvd

bool RtmpStream::SendAmfOnFCPublish(uint32_t chunk_stream_id, uint32_t stream_id, double client_id)
{
auto message_header = std::make_shared<RtmpMuxMessageHeader>(chunk_stream_id,
0,
RtmpMessageTypeID::AMF0_COMMAND,
_rtmp_stream_id,
0);
auto message_header = RtmpMuxMessageHeader::Create(
chunk_stream_id, RtmpMessageTypeID::AMF0_COMMAND, _rtmp_stream_id);

AmfDocument document;

document.AppendProperty(RTMP_CMD_NAME_ONFCPUBLISH);
Expand All @@ -2254,11 +2230,8 @@ namespace pvd

bool RtmpStream::SendAmfCreateStreamResult(uint32_t chunk_stream_id, double transaction_id)
{
auto message_header = std::make_shared<RtmpMuxMessageHeader>(chunk_stream_id,
0,
RtmpMessageTypeID::AMF0_COMMAND,
0,
0);
auto message_header = RtmpMuxMessageHeader::Create(chunk_stream_id);

AmfDocument document;

// 스트림ID 정하기
Expand All @@ -2279,11 +2252,7 @@ namespace pvd
const char *description,
double client_id)
{
auto message_header = std::make_shared<RtmpMuxMessageHeader>(chunk_stream_id,
0,
RtmpMessageTypeID::AMF0_COMMAND,
stream_id,
0);
auto message_header = RtmpMuxMessageHeader::Create(chunk_stream_id, RtmpMessageTypeID::AMF0_COMMAND, stream_id);
AmfDocument document;

document.AppendProperty(RTMP_CMD_NAME_ONSTATUS);
Expand Down

0 comments on commit 99cbf68

Please sign in to comment.