Skip to content

Commit 4b7e8b2

Browse files
committed
Fixed an intermittent issue where (LL-)HLS requests could be missed
1 parent 236bc15 commit 4b7e8b2

File tree

5 files changed

+80
-22
lines changed

5 files changed

+80
-22
lines changed

src/projects/base/publisher/stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ namespace pub
465465
return nullptr;
466466
}
467467

468-
return _sessions[id];
468+
return _sessions.at(id);
469469
}
470470

471471
const std::map<session_id_t, std::shared_ptr<Session>> Stream::GetAllSessions()

src/projects/publishers/hls/hls_stream.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,48 @@ ov::String HlsStream::GetStreamId() const
8989
return ov::String::FormatString("hlsv3/%s", GetUri().CStr());
9090
}
9191

92+
bool HlsStream::CreateOriginSessionPool()
93+
{
94+
if (_origin_mode == false)
95+
{
96+
return false;
97+
}
98+
99+
size_t max_pool_size = _worker_count == 0 ? 1 : _worker_count;
100+
101+
// Create sessions up to _worker_count
102+
for (size_t i = 0; i < max_pool_size; i++)
103+
{
104+
auto session = HlsSession::Create(static_cast<session_id_t>(i),
105+
_origin_mode,
106+
"",
107+
GetApplication(),
108+
pub::Stream::GetSharedPtr(),
109+
0);
110+
logtd("LLHlsStream(%s/%s) - Pre-created origin mode session in pool, session id: %zu", GetApplication()->GetVHostAppName().CStr(), GetName().CStr(), i);
111+
AddSession(session);
112+
}
113+
114+
return true;
115+
}
116+
92117
std::shared_ptr<HlsSession> HlsStream::GetSessionFromPool()
93118
{
94119
// Max session pool size if _worker_count
95-
size_t max_pool_size = _worker_count;
120+
size_t max_pool_size = _worker_count == 0 ? 1 : _worker_count;
96121

97122
// Get random index
98123
size_t index = ov::Random::GenerateUInt32() % max_pool_size;
99124

100125
auto session = GetSession(static_cast<session_id_t>(index));
101126
if (session == nullptr)
102127
{
103-
// create
104-
session = HlsSession::Create(static_cast<session_id_t>(index),
105-
true,
106-
"",
107-
GetApplication(),
108-
pub::Stream::GetSharedPtr(),
109-
0);
110-
logtd("HlsStream(%s/%s) - Created origin mode session from pool, session id: %zu", GetApplication()->GetVHostAppName().CStr(), GetName().CStr(), index);
111-
AddSession(session);
128+
return nullptr;
112129
}
113130

114131
return std::static_pointer_cast<HlsSession>(session);
115132
}
116133

117-
118134
bool HlsStream::Start()
119135
{
120136
if (GetState() != State::CREATED)
@@ -127,6 +143,15 @@ bool HlsStream::Start()
127143
return false;
128144
}
129145

146+
if (_origin_mode == true)
147+
{
148+
if (CreateOriginSessionPool() == false)
149+
{
150+
logte("HLS Stream(%s/%s) - Failed to create origin session pool", GetApplication()->GetVHostAppName().CStr(), GetName().CStr());
151+
return false;
152+
}
153+
}
154+
130155
auto config = GetApplication()->GetConfig();
131156
_ts_config = config.GetPublishers().GetHlsPublisher();
132157

src/projects/publishers/hls/hls_stream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@ class HlsStream final : public pub::Stream, public mpegts::PackagerSink
149149
std::shared_mutex _dumps_lock;
150150

151151
bool _ready_to_play = false; // true if the stream is ready to play, all playlists are ready and have enough segments
152+
153+
154+
bool CreateOriginSessionPool();
155+
bool _origin_mode = true;
152156
};

src/projects/publishers/llhls/llhls_stream.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,49 @@ ov::String LLHlsStream::GetStreamId() const
4444
return ov::String::FormatString("llhls/%s", GetUri().CStr());
4545
}
4646

47+
bool LLHlsStream::CreateOriginSessionPool()
48+
{
49+
if (_origin_mode == false)
50+
{
51+
return false;
52+
}
53+
54+
size_t max_pool_size = _worker_count == 0 ? 1 : _worker_count;
55+
56+
// Create sessions up to _worker_count
57+
for (size_t i = 0; i < max_pool_size; i++)
58+
{
59+
auto session = LLHlsSession::Create(static_cast<session_id_t>(i),
60+
_origin_mode,
61+
"",
62+
GetApplication(),
63+
pub::Stream::GetSharedPtr(),
64+
0);
65+
logtd("LLHlsStream(%s/%s) - Pre-created origin mode session in pool, session id: %zu", GetApplication()->GetVHostAppName().CStr(), GetName().CStr(), i);
66+
AddSession(session);
67+
}
68+
69+
return true;
70+
}
71+
4772
std::shared_ptr<LLHlsSession> LLHlsStream::GetSessionFromPool()
4873
{
4974
// Max session pool size if _worker_count
50-
size_t max_pool_size = _worker_count;
75+
size_t max_pool_size = _worker_count == 0 ? 1 : _worker_count;
5176

5277
// Get random index
5378
size_t index = ov::Random::GenerateUInt32() % max_pool_size;
5479

5580
auto session = GetSession(static_cast<session_id_t>(index));
5681
if (session == nullptr)
5782
{
58-
// create
59-
session = LLHlsSession::Create(static_cast<session_id_t>(index),
60-
_origin_mode,
61-
"",
62-
GetApplication(),
63-
pub::Stream::GetSharedPtr(),
64-
0);
65-
logtd("LLHlsStream(%s/%s) - Created origin mode session from pool, session id: %zu", GetApplication()->GetVHostAppName().CStr(), GetName().CStr(), index);
66-
AddSession(session);
83+
return nullptr;
6784
}
6885

6986
return std::static_pointer_cast<LLHlsSession>(session);
7087
}
7188

89+
7290
std::shared_ptr<const pub::Stream::DefaultPlaylistInfo> LLHlsStream::GetDefaultPlaylistInfo() const
7391
{
7492
static auto info = []() -> std::shared_ptr<const pub::Stream::DefaultPlaylistInfo> {
@@ -96,6 +114,15 @@ bool LLHlsStream::Start()
96114
return false;
97115
}
98116

117+
if (_origin_mode == true)
118+
{
119+
if (CreateOriginSessionPool() == false)
120+
{
121+
logte("LLHlsStream(%s/%s) - Failed to create origin session pool", GetApplication()->GetVHostAppName().CStr(), GetName().CStr());
122+
return false;
123+
}
124+
}
125+
99126
auto config = GetApplication()->GetConfig();
100127

101128
auto llhls_config = config.GetPublishers().GetLLHlsPublisher();

src/projects/publishers/llhls/llhls_stream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,6 @@ class LLHlsStream final : public pub::Stream, public bmff::FMp4StorageObserver
224224

225225
std::map<int32_t, std::shared_ptr<webvtt::Packager>> _vtt_packagers;
226226
mutable std::shared_mutex _vtt_packagers_lock;
227+
228+
bool CreateOriginSessionPool();
227229
};

0 commit comments

Comments
 (0)