Skip to content

Commit

Permalink
Added Push/File publisher information to stream monitoring (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keukhan committed Nov 6, 2024
1 parent ead714f commit 7d1a985
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/projects/modules/ffmpeg/ffmpeg_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace ffmpeg
return true;
}

bool Writer::SendPacket(const std::shared_ptr<MediaPacket> &packet)
bool Writer::SendPacket(const std::shared_ptr<MediaPacket> &packet, uint64_t *sent_bytes)
{
if (!packet)
{
Expand Down Expand Up @@ -366,11 +366,17 @@ namespace ffmpeg
{
av_packet_unref(&av_packet);
SetState(WriterStateError);

return false;
}

_last_packet_sent_time = std::chrono::high_resolution_clock::now();

if (sent_bytes != nullptr)
{
*sent_bytes = av_packet.size;
}

int error = av_interleaved_write_frame(av_format.get(), &av_packet);
if (error != 0)
{
Expand Down
3 changes: 2 additions & 1 deletion src/projects/modules/ffmpeg/ffmpeg_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ namespace ffmpeg
bool Stop();

bool AddTrack(const std::shared_ptr<MediaTrack> &media_track);
bool SendPacket(const std::shared_ptr<MediaPacket> &packet);
bool SendPacket(const std::shared_ptr<MediaPacket> &packet, uint64_t *sent_bytes = nullptr);

std::chrono::high_resolution_clock::time_point GetLastPacketSentTime();

void SetTimestampMode(TimestampMode mode);
Expand Down
12 changes: 9 additions & 3 deletions src/projects/publishers/file/file_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ namespace pub
: pub::Session(session_info, application, stream),
_writer(nullptr)
{
MonitorInstance->OnSessionConnected(*stream, PublisherType::File);
}

FileSession::~FileSession()
{
logtd("FileSession(%d) has been terminated finally", GetId());
MonitorInstance->OnSessionDisconnected(*GetStream(), PublisherType::File);
}

bool FileSession::Start()
Expand Down Expand Up @@ -154,7 +156,7 @@ namespace pub

if (ffmpeg::Conv::IsSupportCodec(output_format, track->GetCodecId()) == false)
{
logtw("%s format does not support the codec(%s)", output_format.CStr(), cmn::GetCodecIdToString(track->GetCodecId()).CStr());
logtd("%s format does not support the codec(%s)", output_format.CStr(), cmn::GetCodecIdToString(track->GetCodecId()).CStr());
continue;
}

Expand Down Expand Up @@ -376,7 +378,9 @@ namespace pub

if (_writer != nullptr)
{
bool ret = _writer->SendPacket(session_packet);
uint64_t sent_bytes = 0;

bool ret = _writer->SendPacket(session_packet, &sent_bytes);

if (ret == false)
{
Expand All @@ -390,7 +394,9 @@ namespace pub
}

GetRecord()->UpdateRecordTime();
GetRecord()->IncreaseRecordBytes(session_packet->GetData()->GetLength());
GetRecord()->IncreaseRecordBytes(sent_bytes);

MonitorInstance->IncreaseBytesOut(*GetStream(), PublisherType::File, sent_bytes);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/projects/publishers/push/push_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ namespace pub
_push(push),
_writer(nullptr)
{
MonitorInstance->OnSessionConnected(*stream, PublisherType::Push);
}

PushSession::~PushSession()
{
Stop();
logtd("PushSession(%d) has been terminated finally", GetId());
MonitorInstance->OnSessionDisconnected(*GetStream(), PublisherType::Push);
}

bool PushSession::Start()
Expand Down Expand Up @@ -204,7 +206,9 @@ namespace pub
return;
}

bool ret = writer->SendPacket(session_packet);
uint64_t sent_bytes = 0;

bool ret = writer->SendPacket(session_packet, &sent_bytes);
if (ret == false)
{
logte("Failed to send packet");
Expand All @@ -218,7 +222,9 @@ namespace pub
}

GetPush()->UpdatePushTime();
GetPush()->IncreasePushBytes(session_packet->GetData()->GetLength());
GetPush()->IncreasePushBytes(sent_bytes);

MonitorInstance->IncreaseBytesOut(*GetStream(), PublisherType::Push, sent_bytes);
}

std::shared_ptr<ffmpeg::Writer> PushSession::CreateWriter()
Expand Down
3 changes: 0 additions & 3 deletions src/projects/publishers/push/push_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ namespace pub
auto stream_packet = std::make_any<std::shared_ptr<MediaPacket>>(media_packet);

BroadcastPacket(stream_packet);

// TODO(Keukhan): Because the transmission size varies for each session, it needs to be improved
MonitorInstance->IncreaseBytesOut(*pub::Stream::GetSharedPtrAs<info::Stream>(), PublisherType::Push, media_packet->GetData()->GetLength() * GetSessionCount());
}

void PushStream::SendVideoFrame(const std::shared_ptr<MediaPacket> &media_packet)
Expand Down

0 comments on commit 7d1a985

Please sign in to comment.