@@ -71,6 +71,7 @@ void adaptive::AdaptiveStream::Reset()
7171 segment_read_pos_ = 0 ;
7272 currentPTSOffset_ = 0 ;
7373 absolutePTSOffset_ = 0 ;
74+ m_isWaitingForSegment = false ;
7475}
7576
7677bool adaptive::AdaptiveStream::Download (const DownloadInfo& downloadInfo,
@@ -969,15 +970,15 @@ bool adaptive::AdaptiveStream::ensureSegment()
969970 }
970971 else if (!m_tree->IsLastSegment (current_period_, current_rep_, current_rep_->current_segment_ ))
971972 {
972- if (m_segBuffers.IsEmpty () && !current_rep_-> IsWaitForSegment () )
973+ if (m_segBuffers.IsEmpty () && !m_isWaitingForSegment )
973974 {
974- current_rep_-> SetIsWaitForSegment ( true ) ;
975- LOG::LogF (LOGDEBUG , " [AS-%u] Begin WaitForSegment stream rep. id \" %s\" period id \" %s\" " ,
975+ m_isWaitingForSegment = true ;
976+ LOG::LogF (LOGDEBUG , " [AS-%u] Begin WaitForSegment (buffer is empty) stream rep. id \" %s\" period id \" %s\" " ,
976977 clsId, current_rep_->GetId ().c_str (), current_period_->GetId ().c_str ());
977978 }
978979 return false ;
979980 }
980- else if (current_rep_-> IsWaitForSegment () &&
981+ else if (m_isWaitingForSegment &&
981982 (m_tree->HasManifestUpdates () || m_tree->HasManifestUpdatesSegs ()))
982983 {
983984 return false ;
@@ -1115,7 +1116,7 @@ bool adaptive::AdaptiveStream::seek(uint64_t const pos, bool& isEos)
11151116 if (ensureSegment ())
11161117 return true ;
11171118
1118- if (!current_rep_-> IsWaitForSegment () )
1119+ if (!m_isWaitingForSegment )
11191120 isEos = true ;
11201121
11211122 return false ;
@@ -1254,21 +1255,49 @@ bool adaptive::AdaptiveStream::seek_time(double seek_seconds)
12541255 return true ;
12551256}
12561257
1257- bool adaptive::AdaptiveStream::waitingForSegment () const
1258+ bool adaptive::AdaptiveStream::OnSampleRequested ()
12581259{
1259- if (m_tree->IsLive () && thread_data_->State () == THREADDATA ::ThState::RUNNING )
1260+ if (!m_tree->IsLive () || !thread_data_)
1261+ return false ;
1262+
1263+ // ! @todo: its missing an appropriate buffering management, example:
1264+ // ! if the bandwidth is too low stuttering may occur due to constant buffering
1265+ // ! it should wait that buffer if full before to resume the playback,
1266+ // ! it should also help to manage adaptive streaming (CRepresentationChooserDefault)
1267+ // ! the mechanism to auto-increase/decrease stream quality based on the actual network conditions
1268+
1269+ const auto now = std::chrono::steady_clock::now ();
1270+ const long long elapsedMs =
1271+ std::chrono::duration_cast<std::chrono::milliseconds>(now - m_tsOnSampleRequest).count ();
1272+
1273+ // Sample are requested in a very fast way, limit the requests to a minimum interval of 500 ms
1274+ if (elapsedMs >= 500 )
12601275 {
1276+ m_tsOnSampleRequest = now;
12611277 std::lock_guard<adaptive::AdaptiveTree::TreeUpdateThread> lckUpdTree (m_tree->GetTreeUpdMutex ());
12621278
12631279 // Some manifests require segments to be generated and managed by the client
12641280 // so they are not provided by the server through periodic manifest updates
12651281 m_tree->InsertLiveSegment (current_period_, current_adp_, current_rep_);
12661282
1267- // Although IsWaitForSegment may be true, do not anticipate the wait for segments
1268- // if there are still segments in the buffer that can be read and/or downloaded
1269- return current_rep_ && current_rep_->IsWaitForSegment () && m_segBuffers.IsEmpty ();
1283+ if (m_isWaitingForSegment)
1284+ {
1285+ if (thread_data_->State () != THREADDATA ::ThState::RUNNING )
1286+ return false ;
1287+
1288+ // Unlocks buffering wait, if a segment exists and the buffer level is full
1289+ m_isWaitingForSegment = !current_rep_->GetNextSegment () ||
1290+ m_segBuffers.GetSizeDownloaded () < m_segBuffers.GetSize ();
1291+
1292+ if (!m_isWaitingForSegment)
1293+ {
1294+ LOG::LogF (LOGDEBUG , " [AS-%u] End WaitForSegment stream rep. id \" %s\" period id \" %s\" " ,
1295+ clsId, current_rep_->GetId ().c_str (), current_period_->GetId ().c_str ());
1296+ }
1297+ }
12701298 }
1271- return false ;
1299+
1300+ return m_isWaitingForSegment;
12721301}
12731302
12741303void adaptive::AdaptiveStream::FixateInitialization (bool on)
@@ -1355,6 +1384,8 @@ void adaptive::AdaptiveStream::Stop()
13551384 // otherwise if read some segments may invalidate this change
13561385 if (current_rep_)
13571386 current_rep_->SetIsEnabled (false );
1387+
1388+ m_isWaitingForSegment = false ;
13581389}
13591390
13601391void adaptive::AdaptiveStream::clear ()
0 commit comments